Add cleanup method for the Meek class to kill any meek-client subprocesses once done. Hide stderr from the CLI printed output

This commit is contained in:
Miguel Jacq 2021-10-25 10:44:38 +11:00
parent c81862130b
commit 3a715346af
No known key found for this signature in database
GPG key ID: EEA4341C6D97A0B6
3 changed files with 43 additions and 2 deletions

View file

@ -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:

View file

@ -18,6 +18,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
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):
"""

View file

@ -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()