-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
119 lines (78 loc) · 2.61 KB
/
client.py
File metadata and controls
119 lines (78 loc) · 2.61 KB
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
import time
import socket
from threading import Thread
punched = False
def formatIpForUsage(address):
# Assumes that string in the format looking like
# "192.168.10.4:1234" is supplied
result = address.split(":")
if len(result) == 2:
result[1] = int(result[1])
return tuple(result)
return f in chat
def holdPortOpenToSTUN(addr, socket):
while not punched:
socket.sendto(b'pulse', addr)
print("STUN SERVER-CONNECTION: maintaining the port open")
time.sleep(5)
def punchThrough(address, sock):
print("PUNCH-TARGET: attempting to punch through to: {}".format(address))
while not punched:
sock.sendto(b'hi mark', address)
try:
time.sleep(1)
data, address = sock.recvfrom(1024)
print("PUNCH-TARGET: I punched through to {} who said: {}".format(address[1], data.decode("utf-8")))
except socket.timeout:
print("PUNCH-TARGET: timed out - {} did not send anything to me".format(address))
def waitForPunch(sock):
# What port is this?
data = ""
address = ""
punchThroughTo = ""
punched = False
while not punched:
try:
time.sleep(1)
# expect a package from host corresponding to
# "192.168.0.1:1234"
data, address = sock.recvfrom(1024)
print("PUNCH-RECEIVER: {} wants to talk to us".format(data.decode("utf-8")))
punchThroughTo = formatIpForUsage(data.decode("utf-8"))
punched = True
except socket.timeout:
print("PUNCH-RECEIVER: Waiting for a punch attempt")
punched = False
while not punched:
try:
time.sleep(1)
sock.sendto(b'hi mark', punchThroughTo)
data, address = sock.recvfrom(1024)
print(data.decode("utf-8"))
except socket.timeout:
print('REQUEST TIMED OUT')
print("We punched through")
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.settimeout(1.0)
punchServerAddr = ("104.248.28.213", 12000)
mode = ""
t = Thread(target=holdPortOpenToSTUN, args=(punchServerAddr, client_socket))
t.start()
if mode == "punch":
request = b'188.148.26.151'
print("STUN SERVER-CONNECTION: Talking to:{}".format(punchServerAddr[0]))
client_socket.sendto(request, punchServerAddr)
try:
request = request.decode("utf-8")
data, server = client_socket.recvfrom(1024)
messageString = data.decode("utf-8")
if messageString == "404":
print("STUN SERVER-CONNECTION: Host could not resolve this")
else:
print("STUN SERVER-CONNECTION: Host told me to contact via: {}:{}".format(request, messageString))
punchThroughTo=(request, int(messageString))
punchThrough(punchThroughTo, client_socket)
except socket.timeout:
print('STUN SERVER-CONNECTION: REQUEST TIMED OUT')
else:
waitForPunch(client_socket)