-
Notifications
You must be signed in to change notification settings - Fork 4
/
chisel.py
77 lines (65 loc) · 2.56 KB
/
chisel.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
import subprocess
import time
import signal
import atexit
#Replace x.x.x.x with the public IP of your Chisel server and akash:akash with a unique username and password.
# Global settings 🌍
server_ip, server_port, local_ip, auth = "x.x.x.x", "8000", "localhost", "akash:akash"
fixed_ports = [80, 443, 1317, 26656, 26657, 8443, 8444]
commands = []
# Terminate all subprocesses 🛑
def terminate_processes():
print("🔌 Disconnecting all ports...")
for cmd in commands:
subprocess.run(["pkill", "-f", " ".join(cmd)])
print("🎉 All ports disconnected! 🎉")
# Register termination function 📝
atexit.register(terminate_processes)
# Handle termination signals 🚨
def signal_handler(signum, frame):
print(f"🚨 Received signal {signum}. Terminating...")
terminate_processes()
exit(1)
# Register signal handlers 🛡️
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Connect a range of ports 🌈
def connect_ports(port_list, protocol=""):
base_command = ["chisel", "client", "--keepalive", "1m", "--auth", auth, f"{server_ip}:{server_port}"]
port_commands = [f"R:{server_ip}:{port}:{local_ip}:{port}{protocol}" for port in port_list]
command = base_command + port_commands
commands.append(command)
print(f"🔌 Connecting ports: {port_list} with protocol {protocol} 🚀")
subprocess.Popen(command)
time.sleep(10)
# Main function 🚀
def main():
print("🌟 Starting the chisel client setup... 🌟")
# Connect fixed ports first with TCP 🛠️
connect_ports(fixed_ports, "/tcp")
print(f"🎯 Fixed ports {fixed_ports} connected with TCP! 🎉")
# Connect range of ports 🌈
for i in range(30000, 32768, 500):
connect_ports(range(i, min(i + 500, 32768)), "/tcp")
time.sleep(5)
connect_ports(range(i, min(i + 500, 32768)), "/udp")
time.sleep(5)
print("✨ All ports successfully connected! ✨")
# Monitor and restart subprocesses 👀
print("👀 Starting to monitor subprocesses... 👀")
while True:
reconnected = False
for cmd in commands:
if subprocess.run(["pgrep", "-f", " ".join(cmd)]).returncode != 0:
subprocess.Popen(cmd)
reconnected = True
if reconnected:
print("🔄 Successfully reconnected disconnected clients! 🚀")
time.sleep(60)
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"😱 An exception occurred: {e} 😱")
terminate_processes()
exit(1)