From 3a715346af241707c952ff446734f8c7bfccd21f Mon Sep 17 00:00:00 2001 From: Miguel Jacq Date: Mon, 25 Oct 2021 10:44:38 +1100 Subject: [PATCH] Add cleanup method for the Meek class to kill any meek-client subprocesses once done. Hide stderr from the CLI printed output --- cli/onionshare_cli/__init__.py | 6 +++-- cli/onionshare_cli/meek.py | 37 +++++++++++++++++++++++++++ desktop/src/onionshare/moat_dialog.py | 2 ++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/cli/onionshare_cli/__init__.py b/cli/onionshare_cli/__init__.py index 99992b25..4e34a508 100644 --- a/cli/onionshare_cli/__init__.py +++ b/cli/onionshare_cli/__init__.py @@ -286,8 +286,8 @@ def main(cwd=None): web = Web(common, False, mode_settings, mode) # Create the Meek object and start the meek client - meek = Meek(common) - meek.start() + # meek = Meek(common) + # meek.start() # Create the CensorshipCircumvention object to make # API calls to Tor over Meek @@ -296,6 +296,8 @@ def main(cwd=None): # domain fronting. # censorship_recommended_settings = censorship.request_settings(country="cn") # print(censorship_recommended_settings) + # Clean up the meek subprocess once we're done working with the censorship circumvention API + # meek.cleanup() # Start the Onion object try: diff --git a/cli/onionshare_cli/meek.py b/cli/onionshare_cli/meek.py index 482deedd..675402d3 100644 --- a/cli/onionshare_cli/meek.py +++ b/cli/onionshare_cli/meek.py @@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . """ import subprocess +import time from queue import Queue, Empty from threading import Thread @@ -86,6 +87,7 @@ class Meek(object): self.meek_front, ], stdout=subprocess.PIPE, + stderr=subprocess.PIPE, startupinfo=startupinfo, bufsize=1, env=self.meek_env, @@ -101,6 +103,7 @@ class Meek(object): self.meek_front, ], stdout=subprocess.PIPE, + stderr=subprocess.PIPE, bufsize=1, env=self.meek_env, text=True, @@ -136,6 +139,40 @@ class Meek(object): self.common.log("Meek", "start", "Could not obtain the meek port") raise MeekNotRunning() + def cleanup(self): + """ + Kill any meek subprocesses. + """ + self.common.log("Meek", "cleanup") + + if self.meek_proc: + self.meek_proc.terminate() + time.sleep(0.2) + if self.meek_proc.poll() is None: + self.common.log( + "Meek", + "cleanup", + "Tried to terminate meek-client process but it's still running", + ) + try: + self.meek_proc.kill() + time.sleep(0.2) + if self.meek_proc.poll() is None: + self.common.log( + "Meek", + "cleanup", + "Tried to kill meek-client process but it's still running", + ) + except Exception: + self.common.log( + "Meek", "cleanup", "Exception while killing meek-client process" + ) + self.meek_proc = None + + # Reset other Meek settings + self.meek_proxies = {} + self.meek_port = None + class MeekNotRunning(Exception): """ diff --git a/desktop/src/onionshare/moat_dialog.py b/desktop/src/onionshare/moat_dialog.py index af7b70b6..9046c989 100644 --- a/desktop/src/onionshare/moat_dialog.py +++ b/desktop/src/onionshare/moat_dialog.py @@ -264,6 +264,7 @@ class MoatThread(QtCore.QThread): ] }, ) + self.meek.cleanup() if r.status_code != 200: self.common.log("MoatThread", "run", f"status_code={r.status_code}") self.bridgedb_error.emit() @@ -316,6 +317,7 @@ class MoatThread(QtCore.QThread): ] }, ) + self.meek.cleanup() if r.status_code != 200: self.common.log("MoatThread", "run", f"status_code={r.status_code}") self.bridgedb_error.emit()