Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import threading
|
| 4 |
+
import time
|
| 5 |
+
from stem.process import launch_tor_with_config
|
| 6 |
+
from websockify import WebSocketProxy
|
| 7 |
+
|
| 8 |
+
# Try to import the default handler to subclass it.
|
| 9 |
+
# Depending on the websockify version, it might be in different spots.
|
| 10 |
+
try:
|
| 11 |
+
from websockify.websocketproxy import ProxyRequestHandler
|
| 12 |
+
except ImportError:
|
| 13 |
+
# Fallback: if we can't import it directly, we will grab it
|
| 14 |
+
# dynamically from the proxy instance later.
|
| 15 |
+
ProxyRequestHandler = None
|
| 16 |
+
|
| 17 |
+
# Configuration
|
| 18 |
+
TOR_SOCKS_PORT = 9050
|
| 19 |
+
WEBSOCKET_PORT = 7860
|
| 20 |
+
WEBSOCKET_LISTEN_HOST = '0.0.0.0'
|
| 21 |
+
TOR_DATA_DIR = os.path.join(os.getcwd(), "tor_data")
|
| 22 |
+
|
| 23 |
+
def print_status(msg):
|
| 24 |
+
print(f"[*] {msg}")
|
| 25 |
+
|
| 26 |
+
def start_tor():
|
| 27 |
+
"""
|
| 28 |
+
Starts the Tor process.
|
| 29 |
+
"""
|
| 30 |
+
print_status("Starting Tor process...")
|
| 31 |
+
if not os.path.exists(TOR_DATA_DIR):
|
| 32 |
+
os.makedirs(TOR_DATA_DIR)
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
config = {
|
| 36 |
+
'SocksPort': str(TOR_SOCKS_PORT),
|
| 37 |
+
'DataDirectory': TOR_DATA_DIR,
|
| 38 |
+
}
|
| 39 |
+
tor_process = launch_tor_with_config(
|
| 40 |
+
config=config,
|
| 41 |
+
init_msg_handler=lambda line: print(f" [Tor] {line}") if "Bootstrapped" in line else None,
|
| 42 |
+
take_ownership=True,
|
| 43 |
+
)
|
| 44 |
+
print_status(f"Tor is running! SOCKS proxy at 127.0.0.1:{TOR_SOCKS_PORT}")
|
| 45 |
+
return tor_process
|
| 46 |
+
except Exception as e:
|
| 47 |
+
print_status(f"Error starting Tor: {e}")
|
| 48 |
+
sys.exit(1)
|
| 49 |
+
|
| 50 |
+
def create_custom_handler(base_handler_class):
|
| 51 |
+
"""
|
| 52 |
+
Creates a custom handler class inheriting from the provided base.
|
| 53 |
+
This allows us to serve "Hello World" on HTTP GET while keeping
|
| 54 |
+
WebSocket functionality for the proxy.
|
| 55 |
+
"""
|
| 56 |
+
class HelloHandler(base_handler_class):
|
| 57 |
+
def do_GET(self):
|
| 58 |
+
# Check if this is a WebSocket upgrade request
|
| 59 |
+
if self.headers.get('Upgrade') and self.headers.get('Upgrade').lower() == 'websocket':
|
| 60 |
+
# Pass it to the standard websockify handler
|
| 61 |
+
super().do_GET()
|
| 62 |
+
else:
|
| 63 |
+
# It's a normal HTTP request (e.g., from a browser)
|
| 64 |
+
self.send_response(200)
|
| 65 |
+
self.send_header("Content-type", "text/plain")
|
| 66 |
+
self.end_headers()
|
| 67 |
+
self.wfile.write(b"Hello World")
|
| 68 |
+
|
| 69 |
+
return HelloHandler
|
| 70 |
+
|
| 71 |
+
def start_websockify():
|
| 72 |
+
"""
|
| 73 |
+
Starts the Websockify proxy with a Custom Handler.
|
| 74 |
+
"""
|
| 75 |
+
print_status(f"Starting Websockify on {WEBSOCKET_LISTEN_HOST}:{WEBSOCKET_PORT}...")
|
| 76 |
+
|
| 77 |
+
# Initialize the proxy
|
| 78 |
+
proxy = WebSocketProxy(
|
| 79 |
+
listen_host=WEBSOCKET_LISTEN_HOST,
|
| 80 |
+
listen_port=WEBSOCKET_PORT,
|
| 81 |
+
target_host='127.0.0.1',
|
| 82 |
+
target_port=TOR_SOCKS_PORT
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# --- MAGIC HAPPENS HERE ---
|
| 86 |
+
# We get the default handler class the proxy was going to use
|
| 87 |
+
default_handler = proxy.RequestHandlerClass
|
| 88 |
+
|
| 89 |
+
# We create our custom handler inheriting from the default one
|
| 90 |
+
custom_handler = create_custom_handler(default_handler)
|
| 91 |
+
|
| 92 |
+
# We tell the proxy to use our new handler instead
|
| 93 |
+
proxy.RequestHandlerClass = custom_handler
|
| 94 |
+
# --------------------------
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
proxy.start_server()
|
| 98 |
+
except OSError as e:
|
| 99 |
+
print_status(f"Error starting Websockify: {e}")
|
| 100 |
+
os._exit(1)
|
| 101 |
+
|
| 102 |
+
def main():
|
| 103 |
+
tor_process = start_tor()
|
| 104 |
+
|
| 105 |
+
ws_thread = threading.Thread(target=start_websockify)
|
| 106 |
+
ws_thread.daemon = True
|
| 107 |
+
ws_thread.start()
|
| 108 |
+
|
| 109 |
+
print_status("Setup complete.")
|
| 110 |
+
print_status(f"1. Proxy (WS): ws://<IP>:{WEBSOCKET_PORT}/")
|
| 111 |
+
print_status(f"2. Check (HTTP): http://<IP>:{WEBSOCKET_PORT}/ -> Should say 'Hello World'")
|
| 112 |
+
print_status("Press Ctrl+C to stop.")
|
| 113 |
+
|
| 114 |
+
try:
|
| 115 |
+
while True:
|
| 116 |
+
time.sleep(1)
|
| 117 |
+
except KeyboardInterrupt:
|
| 118 |
+
print_status("Stopping...")
|
| 119 |
+
tor_process.terminate()
|
| 120 |
+
sys.exit(0)
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
main()
|