diff --git a/bridges/downloadbridges.py b/bridges/downloadbridges.py index e74cd38..5dde0fe 100644 --- a/bridges/downloadbridges.py +++ b/bridges/downloadbridges.py @@ -1,31 +1,27 @@ -from .meek import Meek -import requests -from logging_msg import log_msg -import os import base64 import io -import matplotlib.pyplot as plt -from PIL import Image import json +import os +import requests +from PIL import Image +from logging_msg import log_msg +from .meek import Meek class DownloadBridges: def __init__(self): + pass + + def connectMeek(self): self.meek_path = os.path.join(os.path.dirname(__file__), "meek-client") log_msg("DownloadBridges", "__init__", "Meek path: " + self.meek_path) self.meek = Meek(self.meek_path) self.meek_proxies = self.meek.start() log_msg("DownloadBridges", "__init__", f"Meek Proxie: {self.meek_proxies}") - - # cleanup meek when the program exits - import atexit - atexit.register(self.meek.cleanup) - - - def getCaptcha(self): + self.connectMeek() try: log_msg("DownloadBridges", "getBridge", "Getting bridge") captcha = requests.post( @@ -50,22 +46,13 @@ def getCaptcha(self): log_msg("DownloadBridges", "getBridge", f"Error: {e}") def display_image(self): - - # Handle window closing event - def on_close(event): - plt.close() - - img_data = self.image - # Convert Base64 data to image - img_bytes = base64.b64decode(img_data) - img = Image.open(io.BytesIO(img_bytes)) - fig, ax = plt.subplots() - ax.imshow(img) - fig.canvas.mpl_connect('close_event', on_close) - # Show the Matplotlib figure without blocking - plt.show(block=False) - - + try: + base64_image_data = self.image + image_data = base64.b64decode(base64_image_data) + image = Image.open(io.BytesIO(image_data)) + image.show() + except Exception as e: + log_msg("DownloadBridges", "display_image", f"Error: {e}") def checkCaptcha(self): """ @@ -91,8 +78,7 @@ def checkCaptcha(self): ] }, ) - log_msg("display_image","on_close", "Closing the window") - plt.close() + log_msg("display_image", "on_close", "Closing the window") except Exception as e: log_msg("DownloadBridges", "checkCaptcha", "Error: " + str(e)) return False @@ -105,7 +91,7 @@ def checkCaptcha(self): except Exception as e: log_msg("DownloadBridges", "checkCaptcha", "Error: " + str(e)) return False - log_msg("DownloadBridges","checkCaptcha", "Captcha is correct") + log_msg("DownloadBridges", "checkCaptcha", "Captcha is correct") log_msg("Bridges", data) return True @@ -114,8 +100,8 @@ def getBridges(self): Return the bridges """ bridges = [] - for item in self.bridge.json()['data']: - bridges.extend(item['bridges']) + for item in self.bridge.json()["data"]: + bridges.extend(item["bridges"]) return bridges def cleanup(self): @@ -130,7 +116,7 @@ def saveBridges(self): """ log_msg("DownloadBridges", "saveBridges", "Saving bridges to bridges.json") bridge_lst = self.getBridges() - with open('bridges.json', 'w') as f: + with open("bridges.json", "w") as f: json.dump(bridge_lst, f) def readBridges(self): @@ -138,6 +124,6 @@ def readBridges(self): Read the bridges from a file """ log_msg("DownloadBridges", "readBridges", "Reading bridges from bridges.json") - with open('bridges.json', 'r') as f: + with open("bridges.json", "r") as f: my_list = json.load(f) return my_list diff --git a/client_start.py b/client_start.py index 62e21b0..a2d83ce 100644 --- a/client_start.py +++ b/client_start.py @@ -1,44 +1,47 @@ -from client.client import Client import os + from stem.process import launch_tor_with_config -from logging_msg import log_msg +from bridges.downloadbridges import DownloadBridges +from client.client import Client +from logging_msg import log_msg verbose = True # path to the tor binary -tor_dir = os.getcwd() + '/tor/tor' -obsf4 = os.getcwd() + '/tor/obfs4proxy' -geo_ip_file = os.getcwd() + '/tor/geoip' -geo_ipv6_file = os.getcwd() + '/tor/geoip6' -# create a new Tor configuration -# 'Bridge': 'obfs4 [2a0c:4d80:42:702::1]:27015 C5B7CD6946FF10C5B3E89691A7D3F2C122D2117C cert=TD7PbUO0/0k6xYHMPW3vJxICfkMZNdkRrb63Zhl5j9dW3iRGiCx0A7mPhe5T2EDzQ35+Zw iat-mode=0', -# 'ClientTransportPlugin': f'obfs4 exec {obsf4}', -tor_cfg = { - 'SocksPort': '9050', - 'ControlPort': '9051', - 'CookieAuthentication': '1', - 'AvoidDiskWrites': '1', - 'Log': 'notice stdout', - 'GeoIPFile': f'{geo_ip_file}', - 'GeoIPv6File': f'{geo_ipv6_file}', -} -class ClientServer(): +tor_dir = os.getcwd() + "/tor/tor" +obsf4 = os.getcwd() + "/tor/obfs4proxy" +geo_ip_file = os.getcwd() + "/tor/geoip" +geo_ipv6_file = os.getcwd() + "/tor/geoip6" + + +class ClientServer: def __init__(self): - pass + self.tor_cfg = { + "SocksPort": "9050", + "ControlPort": "9051", + "CookieAuthentication": "1", + "AvoidDiskWrites": "1", + "Log": "notice stdout", + "GeoIPFile": f"{geo_ip_file}", + "GeoIPv6File": f"{geo_ipv6_file}", + } + choice = input("Use bridges? (y/n) ") + if choice == "y" or choice == "Y": + self.use_bridges() def start(self): # start Tor with the new configuration if tor is not running log_msg("Client_Start", "start", f"starting {tor_dir}") try: self.tor_bin = launch_tor_with_config( - config=tor_cfg, + config=self.tor_cfg, tor_cmd=tor_dir, # path to your tor binary - timeout=60 + timeout=60, ) except Exception as e: # tor is already running - log_msg("ClientServer","start" ,f"Error: {e}") + log_msg("ClientServer", "start", f"Error: {e}") try: client = Client() client.start() @@ -51,12 +54,40 @@ def start(self): def kill_tor(self): try: self.tor_bin.kill() - log_msg("Client_Start","kill_tor", "killed tor subprocess") + log_msg("Client_Start", "kill_tor", "killed tor subprocess") except Exception as e: print(e) + def use_bridges(self): + # If bridges.json does not exist, download bridges + if not os.path.exists("bridges.json"): + db = DownloadBridges() + db.getCaptcha() + db.display_image() + if not db.checkCaptcha(): + print("Captcha is incorrect") + self.use_bridges() + db.saveBridges() + db.cleanup() + obsf4Bridges = db.readBridges() + else: + db = DownloadBridges() + obsf4Bridges = db.readBridges() + self.tor_cfg = { + "SocksPort": "9050", + "ControlPort": "9051", + "CookieAuthentication": "1", + "AvoidDiskWrites": "1", + "Log": "notice stdout", + "GeoIPFile": f"{geo_ip_file}", + "GeoIPv6File": f"{geo_ipv6_file}", + # use bridges + "ClientTransportPlugin": f"obfs4 exec {obsf4}", + "UseBridges": "1", + "Bridge": obsf4Bridges, + } -if __name__ == '__main__': +if __name__ == "__main__": client = ClientServer() - client.start() \ No newline at end of file + client.start() diff --git a/server_start.py b/server_start.py index 15997f3..cb4da7a 100644 --- a/server_start.py +++ b/server_start.py @@ -1,38 +1,43 @@ -from server.server import Server import os import subprocess + from stem.process import launch_tor_with_config + +from bridges.downloadbridges import DownloadBridges from logging_msg import log_msg +from server.server import Server # path to the tor binary -tor_dir = os.getcwd() + '/tor/tor' -obsf4 = os.getcwd() + '/tor/obfs4proxy' -geo_ip_file = os.getcwd() + '/tor/geoip' -geo_ipv6_file = os.getcwd() + '/tor/geoip6' -# create a new Tor configuration - # 'ClientTransportPlugin': f'obfs4 exec {obsf4}', - # 'Bridge': 'obfs4 [2a0c:4d80:42:702::1]:27015 C5B7CD6946FF10C5B3E89691A7D3F2C122D2117C cert=TD7PbUO0/0k6xYHMPW3vJxICfkMZNdkRrb63Zhl5j9dW3iRGiCx0A7mPhe5T2EDzQ35+Zw iat-mode=0', -tor_cfg = { - 'SocksPort': '9050', - 'ControlPort': '9051', - 'CookieAuthentication': '1', - 'AvoidDiskWrites': '1', - 'Log': 'notice stdout', - 'GeoIPFile': f'{geo_ip_file}', - 'GeoIPv6File': f'{geo_ipv6_file}', -} -log_msg("StartServer", "SocksPort", f"{tor_cfg['SocksPort']}") -log_msg("StartServer", "ControlPort", f"{tor_cfg['ControlPort']}") -class StartServer(): +tor_dir = os.getcwd() + "/tor/tor" +obsf4 = os.getcwd() + "/tor/obfs4proxy" +geo_ip_file = os.getcwd() + "/tor/geoip" +geo_ipv6_file = os.getcwd() + "/tor/geoip6" + + +class StartServer: def __init__(self): - pass + self.tor_cfg = { + "SocksPort": "9050", + "ControlPort": "9051", + "CookieAuthentication": "1", + "AvoidDiskWrites": "1", + "Log": "notice stdout", + "GeoIPFile": f"{geo_ip_file}", + "GeoIPv6File": f"{geo_ipv6_file}", + } + + choice = input("Use bridges? (y/n) ") + if choice == "y" or choice == "Y": + self.use_bridges() + log_msg("StartServer", "SocksPort", f"{self.tor_cfg['SocksPort']}") + log_msg("StartServer", "ControlPort", f"{self.tor_cfg['ControlPort']}") def start(self): # start Tor with the new configuration if tor is not running log_msg("Onion", "connect", f"starting tor bin {tor_dir}") try: self.tor_bin = launch_tor_with_config( - config=tor_cfg, + config=self.tor_cfg, tor_cmd=tor_dir, # path to your tor binary ) except Exception as e: @@ -70,6 +75,36 @@ def force_kill_tor(self): -if __name__ == '__main__': + def use_bridges(self): + # If bridges.json does not exist, download bridges + if not os.path.exists("bridges.json"): + db = DownloadBridges() + db.getCaptcha() + db.display_image() + if not db.checkCaptcha(): + print("Captcha is incorrect") + self.use_bridges() + db.saveBridges() + db.cleanup() + obsf4Bridges = db.readBridges() + else: + db = DownloadBridges() + obsf4Bridges = db.readBridges() + self.tor_cfg = { + "SocksPort": "9050", + "ControlPort": "9051", + "CookieAuthentication": "1", + "AvoidDiskWrites": "1", + "Log": "notice stdout", + "GeoIPFile": f"{geo_ip_file}", + "GeoIPv6File": f"{geo_ipv6_file}", + # use bridges + "ClientTransportPlugin": f"obfs4 exec {obsf4}", + "UseBridges": "1", + "Bridge": obsf4Bridges, + } + + +if __name__ == "__main__": server = StartServer() - server.start() \ No newline at end of file + server.start() diff --git a/test.py b/test.py index 730aa3c..b45144f 100644 --- a/test.py +++ b/test.py @@ -1,5 +1,5 @@ from bridges.downloadbridges import DownloadBridges -import json + def main(): db = DownloadBridges() @@ -10,8 +10,9 @@ def main(): main() db.saveBridges() db.cleanup() - - print(db.readBridges()) + obsf4Bridges = db.readBridges() + print(obsf4Bridges) + if __name__ == "__main__": - main() \ No newline at end of file + main()