Kill stale tor process

This commit is contained in:
Micah Lee 2020-11-17 18:06:36 -08:00
parent c94f6eea26
commit 46647feffd

View file

@ -28,6 +28,7 @@ import tempfile
import subprocess import subprocess
import time import time
import shlex import shlex
import getpass
import psutil import psutil
from distutils.version import LooseVersion as Version from distutils.version import LooseVersion as Version
@ -115,13 +116,6 @@ class TorTooOld(Exception):
pass pass
class BundledTorNotSupported(Exception):
"""
This exception is raised if onionshare is set to use the bundled Tor binary,
but it's not supported on that platform, or in dev mode.
"""
class BundledTorTimeout(Exception): class BundledTorTimeout(Exception):
""" """
This exception is raised if onionshare is set to use the bundled Tor binary, This exception is raised if onionshare is set to use the bundled Tor binary,
@ -242,6 +236,23 @@ class Onion(object):
raise OSError("OnionShare port not available") raise OSError("OnionShare port not available")
self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc") self.tor_torrc = os.path.join(self.tor_data_directory_name, "torrc")
# If there is an existing OnionShare tor process, kill it
for proc in psutil.process_iter(["pid", "name", "username"]):
if proc.username() == getpass.getuser():
cmdline = proc.cmdline()
if (
cmdline[0] == self.tor_path
and cmdline[1] == "-f"
and cmdline[2] == self.torrc
):
self.common.log(
"Onion",
"connect",
"found a stale tor process, killing it",
)
proc.terminate()
proc.wait()
if self.common.platform == "Windows" or self.common.platform == "Darwin": if self.common.platform == "Windows" or self.common.platform == "Darwin":
# Windows doesn't support unix sockets, so it must use a network port. # Windows doesn't support unix sockets, so it must use a network port.
# macOS can't use unix sockets either because socket filenames are limited to # macOS can't use unix sockets either because socket filenames are limited to