fixed detection + profile issues

This commit is contained in:
Louis Heredero 2024-03-18 18:56:11 +01:00
parent 575bdc4968
commit f36bb0577a

26
main.py
View File

@ -54,7 +54,7 @@ if getPublicIp() == "153.109.1.93":
warningLogger.log("You are already in the HEI network. You cannot use the VPN")
exit()
IS_SNAP = b"snap" in subprocess.run(["which", "firefox"], stdout=subprocess.PIPE).stdout
IS_SNAP = os.path.isfile("/snap/bin/firefox") and os.access("/snap/bin/firefox", os.X_OK)
DRIVER_PATH = "/snap/bin/geckodriver" if IS_SNAP else "/usr/local/bin/geckodriver"
if IS_SNAP:
@ -97,21 +97,24 @@ def startBrowser(headless: bool = True) -> tuple[Firefox, FirefoxProfile]:
if d.endswith(".default-release"):
break
else:
warningLogger.log("Default firefox profile not found. Please create one manually")
exit()
if profilePath == PERSISTENT_PROFILE_PATH:
warningLogger.log("Default firefox profile not found. Please create one manually")
exit()
profile = FirefoxProfile(profilePath)
infoLogger.log(f"Using firefox profile in {profilePath}")
options = FirefoxOptions()
profile = FirefoxProfile(profilePath)
options.profile = profile
#options.set_preference("profile", profilePath)
#options.add_argument(f"-profile {profilePath}")
distinguishkey = "-distinguishkey" + str(random.randint(111111, 999999))
options.add_argument(distinguishkey)
if headless:
options.add_argument("--headless")
service = FirefoxService(executable_path=DRIVER_PATH)
service = FirefoxService(executable_path=DRIVER_PATH, log_output="/dev/null")
driver = Firefox(options, service)
return driver, distinguishkey
@ -134,12 +137,20 @@ def closeBrowser(driver: Firefox, distinguishkey: str) -> None:
infoLogger.log(f"Profile is stored in {realProfilePath}")
psutil.Process(pid).kill() # kill firefox (nicely) and unlock profile lock
psutil.Process(pid).kill() # kill firefox (nicely) and unlock profile lock
if os.path.isdir(PERSISTENT_PROFILE_PATH):
shutil.rmtree(PERSISTENT_PROFILE_PATH)
# Copy profile
subprocess.run(["sudo", "cp", "-r", realProfilePath, PERSISTENT_PROFILE_PATH])
r = subprocess.run(["sudo", "cp", "-r", realProfilePath, PERSISTENT_PROFILE_PATH])
if r.returncode:
errorLogger.log("Could not copy temporary profile to persistent directory")
errorLogger.log(f"Tried: sudo cp -r {realProfilePath} {PERSISTENT_PROFILE_PATH}")
exit()
# Remove lock
subprocess.run(["sudo", "rm", os.path.join(PERSISTENT_PROFILE_PATH, "lock")])
# Grant user access to directory
subprocess.run(["sudo", "chown", "-R", f"{os.getuid()}:{os.getgid()}", PERSISTENT_PROFILE_PATH])
@ -221,6 +232,7 @@ if re.match(r"[a-f0-9]{32}", DSID):
infoLogger.log("VPN is up and running")
except KeyboardInterrupt:
print()
proc.send_signal(signal.SIGINT)
proc.wait()