-
Notifications
You must be signed in to change notification settings - Fork 1
/
zt.py
216 lines (190 loc) · 6.97 KB
/
zt.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""Example low-level socket usage"""
import time
import sys
import socket
import libzt
import threading
def print_usage():
"""print help"""
print(
"\nUsage: <server|client> <storage_path> <net_id> <remote_ip> <remote_port>\n"
)
print("Ex: python3 example.py server . 0123456789abcdef 8080 22")
print("Ex: python3 example.py client . 0123456789abcdef 192.168.22.1 8080 1234\n")
sys.exit(0)
#
# (Optional) Event handler
#
def on_zerotier_event(event_code, id):
if event_code == libzt.ZTS_EVENT_NODE_ONLINE:
print("ZTS_EVENT_NODE_ONLINE (" + str(event_code) + ") : " + hex(id))
if event_code == libzt.ZTS_EVENT_NODE_OFFLINE:
print("ZTS_EVENT_NODE_OFFLINE (" + str(event_code) + ") : " + hex(id))
if event_code == libzt.ZTS_EVENT_NETWORK_READY_IP4:
print("ZTS_EVENT_NETWORK_READY_IP4 (" + str(event_code) + ") : " + hex(id))
if event_code == libzt.ZTS_EVENT_NETWORK_READY_IP6:
print("ZTS_EVENT_NETWORK_READY_IP6 (" + str(event_code) + ") : " + hex(id))
if event_code == libzt.ZTS_EVENT_PEER_DIRECT:
print("ZTS_EVENT_PEER_DIRECT (" + str(event_code) + ") : " + hex(id))
if event_code == libzt.ZTS_EVENT_PEER_RELAY:
print("ZTS_EVENT_PEER_RELAY (" + str(event_code) + ") : " + hex(id))
def forwarding(src, dst):
while True:
try:
buffer = src.recv(0x400)
recvbyte = len(buffer)
except:
print("recv error")
break
if recvbyte == 0:
print("[-] No data received! Breaking...")
break
try:
sentbyte = dst.send(buffer)
except:
print("send error")
break
print("recv: ", recvbyte)
print("sent: ", sentbyte)
def ztnet(net_id, storage_path):
print("Starting ZeroTier...")
n = libzt.ZeroTierNode()
n.init_set_event_handler(on_zerotier_event) # Optional
n.init_from_storage(storage_path) # Optional
n.init_set_port(9994) # Optional
n.node_start()
print("Waiting for node to come online...")
while not n.node_is_online():
time.sleep(1)
print("Joining network:", hex(net_id))
n.net_join(net_id)
while not n.net_transport_is_ready(net_id):
time.sleep(1)
print("Joined network")
print("addr = ", n.addr_get_ipv4(net_id))
return n
#
# Main
#
def main():
mode = None # client|server
storage_path = "." # Where identity files are stored
net_id = 0 # Network to join
remote_ip = None # ZeroTier IP of remote node
remote_port = 8080 # ZeroTier port your app logic may use
if sys.argv[1] == "server" and len(sys.argv) == 6:
mode = sys.argv[1]
storage_path = sys.argv[2]
net_id = int(sys.argv[3], 16)
remote_port = int(sys.argv[4])
local_port = int(sys.argv[5])
if sys.argv[1] == "client" and len(sys.argv) == 7:
mode = sys.argv[1]
storage_path = sys.argv[2]
net_id = int(sys.argv[3], 16)
remote_ip = sys.argv[4]
remote_port = int(sys.argv[5])
local_port = int(sys.argv[6])
if mode is None:
print_usage()
print("mode = ", mode)
print("storage_path = ", storage_path)
print("net_id = ", net_id)
print("remote_ip = ", remote_ip)
print("remote_port = ", remote_port)
#
# Example server
#
if mode == "server":
n = ztnet(net_id=net_id, storage_path=storage_path)
while 1:
try:
serv = libzt.socket(libzt.ZTS_AF_INET, libzt.ZTS_SOCK_STREAM, 0)
serv.setsockopt(libzt.ZTS_SOL_SOCKET, libzt.ZTS_SO_REUSEADDR, 1)
serv.bind(("0.0.0.0", remote_port))
serv.listen(5)
print("Waiting for connection")
conn, addr = serv.accept()
print("Accepted connection from: ", addr)
except Exception as ex:
print("Conection failed:", ex)
print("Cannot start server")
continue
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client.connect(("127.0.0.1", local_port))
except Exception as ex:
print("Conection failed:", ex)
print("Cannot start client")
continue
s = threading.Thread(target=forwarding, args=(
conn, client))
r = threading.Thread(target=forwarding, args=(
client, conn))
s.start()
r.start()
while 1:
if not s.is_alive():
print("Forwarding down")
break
if not r.is_alive():
print("Backwording down")
break
try:
serv.shutdown(libzt.ZTS_SHUT_RDWR)
serv.close()
except Exception as ex:
print("Close serv error", ex)
try:
client.shutdown(socket.SHUT_RDWR)
client.close()
except Exception as ex:
print("Close client error", ex)
#
# Example client
#
if mode == "client":
n = ztnet(net_id=net_id, storage_path=storage_path)
while 1:
try:
client = libzt.socket(libzt.ZTS_AF_INET, libzt.ZTS_SOCK_STREAM, 0)
client.setsockopt(libzt.ZTS_SOL_SOCKET, libzt.ZTS_SO_REUSEADDR, 1)
client.connect((remote_ip, remote_port))
except Exception as ex:
print("Conection failed:", ex)
continue
try:
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
serv.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
serv.bind(("127.0.0.1", local_port))
serv.listen(5)
conn, addr = serv.accept()
except Exception as ex:
print("Conection failed:", ex)
continue
s = threading.Thread(target=forwarding, args=(
conn, client))
r = threading.Thread(target=forwarding, args=(
client, conn))
s.start()
r.start()
while 1:
if not s.is_alive():
print("Forwarding down")
break
if not r.is_alive():
print("Backwording down")
break
try:
client.shutdown(libzt.ZTS_SHUT_RDWR)
client.close()
except Exception as ex:
print("Close serv error", ex)
try:
serv.shutdown(socket.SHUT_RDWR)
serv.close()
except Exception as ex:
print("Close client error", ex)
if __name__ == "__main__":
main()