Skip to content

Commit

Permalink
Merge pull request #19 from Baccount/client/server_bridges
Browse files Browse the repository at this point in the history
Client/server auto fetch and use tor bridges
  • Loading branch information
Baccount authored Mar 1, 2023
2 parents 455fdf4 + a7167a0 commit 3b8802b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 90 deletions.
58 changes: 22 additions & 36 deletions bridges/downloadbridges.py
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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):
"""
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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):
Expand All @@ -130,14 +116,14 @@ 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):
"""
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
83 changes: 57 additions & 26 deletions client_start.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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()
client.start()
83 changes: 59 additions & 24 deletions server_start.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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()
server.start()
9 changes: 5 additions & 4 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bridges.downloadbridges import DownloadBridges
import json


def main():
db = DownloadBridges()
Expand All @@ -10,8 +10,9 @@ def main():
main()
db.saveBridges()
db.cleanup()

print(db.readBridges())
obsf4Bridges = db.readBridges()
print(obsf4Bridges)


if __name__ == "__main__":
main()
main()

0 comments on commit 3b8802b

Please sign in to comment.