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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
import sys import json import uuid import time import socket import struct import threading import stun
CLIENT_HELLO_MESSAGE = 'ClientHelloMessage' SERVER_HELLO_MESSAGE = 'ServerHelloMessage' SERVER_REFUSE_MESSAGE = 'ServerRefuseMessage' PEER_INFO_MESSAGE = 'PeerInfoMessage' PEER_HELLO_MESSAGE = 'PeerHelloMessage' PEER_NOT_FOUND_MESSAGE = 'PeerNotFoundMessage' PEER_PORT_MESSAGE = 'PeerPortMessage'
ACTION_LISTEN = 'Listen' ACTION_CONNECT = 'Connect' ACTION_TRY_AND_LISTEN = 'TryAndListen' ACTION_DELAY_CONNECT = 'DelayConnect' ACTION_TRY_AND_REPORT = 'TryAndReport' ACTION_STANDBY = 'Standby'
def send_msg(sock, msg): msg = struct.pack('>I', len(msg)) + msg.encode('utf-8') sock.sendall(msg)
def recv_by_bytes(sock, n): data = b'' while len(data) < n: packet = sock.recv(n - len(data)) if not packet: return None data += packet return data
def recv_msg(sock): raw_msglen = recv_by_bytes(sock, 4) if not raw_msglen: return None msglen = struct.unpack('>I', raw_msglen)[0] return recv_by_bytes(sock, msglen).decode('utf-8')
def start_p2p_listen(client_id, private_ip, private_port): private_ip = '' with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) s.bind((private_ip, private_port)) s.listen(1) s.settimeout(30) while True: try: print('Client({client_id}) try to listening at {private_ip}:{private_port}'.format(client_id=client_id, private_ip=private_ip, private_port=private_port)) conn, addr = s.accept() except socket.timeout: print('Client({client_id}) listened timeout at {private_ip}:{private_port}'.format(client_id=client_id, private_ip=private_ip, private_port=private_port)) continue else: print('Client({client_id}) listened from peer({addr}).'.format(client_id=client_id, addr=str(addr))) break
while True: msg_str = recv_msg(conn) if msg_str is None: continue message = json.loads(msg_str) print('Received: {message}'.format(message=str(message)))
number = message['number'] number += 1 peer_hello_message = { 'type': PEER_HELLO_MESSAGE, 'content': '[{n}] Hello, this is Client({client_id}).'.format(n=number, client_id=client_id), 'number': number } send_msg(conn, json.dumps(peer_hello_message)) print('Send: {message}'.format(message=str(peer_hello_message)))
def start_p2p_connect(client_id, peer_ip, peer_port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
while True: try: print('Client({client_id}) try to connecting at {peer_ip}:{peer_port}'.format(client_id=client_id, peer_ip=peer_ip, peer_port=peer_port)) s.connect((peer_ip, peer_port)) except socket.error as e: print('Client({client_id}) connect failed at {peer_ip}:{peer_port}'.format(client_id=client_id, peer_ip=peer_ip, peer_port=peer_port)) print(str(e)) else: print('Client({client_id}) connected success to Peer({peer_ip}:{peer_port})'.format(client_id=client_id, peer_ip=peer_ip, peer_port=peer_port)) break
number = 0 while True: number += 1 peer_hello_message = { 'type': PEER_HELLO_MESSAGE, 'content': '[{n}] Hello, this is Client({client_id}).'.format(n=number, client_id=client_id), 'number': number } send_msg(s, json.dumps(peer_hello_message)) print('Send: {message}'.format(message=str(peer_hello_message))) message = json.loads(recv_msg(s)) print('Received: {message}'.format(message=str(message))) number = message['number'] time.sleep(2)
def try_p2p_connect(client_id, peer_ip, peer_port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) s.settimeout(3) try: print('Client({client_id}) try to connecting at {peer_ip}:{peer_port}'.format(client_id=client_id, peer_ip=peer_ip, peer_port=peer_port)) print('This try must be failed, just for ready to receive from peer next time.') s.connect((peer_ip, peer_port)) except socket.error: return else: return s.getsockname()
def p2p_connect_actions(p1, p2): """ 根据两端不通的NAT类型组合,决定双方建立p2p连接的步骤 :return: """ pair = (p1['nat_type'], p2['nat_type']) if pair == (stun.SymmetricNAT, stun.SymmetricNAT): return None, None if pair == (stun.SymmetricNAT, stun.RestricPortNAT) or pair == (stun.RestricPortNAT, stun.SymmetricNAT): return None, None
if pair == (stun.RestricPortNAT, stun.RestricPortNAT): return ACTION_TRY_AND_REPORT, ACTION_STANDBY
if stun.FullCone in pair: a, b = ACTION_LISTEN, ACTION_CONNECT return (a, b) if p1['nat_type'] == stun.FullCone else (b, a)
if stun.RestricNAT in pair or stun.RestricPortNAT in pair: a, b = ACTION_TRY_AND_LISTEN, ACTION_DELAY_CONNECT return (a, b) if p1['nat_type'] == stun.RestricNAT else (b, a)
def start_server(host, port): clients = {} with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) s.listen(1) s.settimeout(30) print('Server starting listen at {host}:{port}'.format(host=host, port=port))
while True: try: conn, addr = s.accept() print('Accepted connection from Client({client_ip}:{client_port})'.format(client_ip=addr[0], client_port=addr[1])) except socket.timeout: continue
message = json.loads(recv_msg(conn))
if message['type'] == CLIENT_HELLO_MESSAGE: print('-----Received Client Hello Message-----') print('Client ID: {client_id}'.format(client_id=message['client_id'])) print('Client NAT: {nat_type}'.format(nat_type=message['nat_type'])) print('Client Private Addr: {private_ip}:{private_port}'.format(private_ip=message['private_ip'], private_port=message['private_port'])) print('Peer ID: {peer_id}'.format(peer_id=message['peer_id']))
if message['client_id'] in clients.keys(): print('Refuse hello from Client({client_id}), Client ID is already in used.'.format(client_id=message['client_id'])) refuse_message = { 'type': SERVER_REFUSE_MESSAGE, 'client_id': message['client_id'], 'content': 'Client ID is already in used.' } send_msg(conn, json.dumps(refuse_message)) conn.close() continue else: clients[message['client_id']] = { 'client_id': message['client_id'], 'nat_type': message['nat_type'], 'conn': conn, 'addr': addr, 'private_addr': (message['private_ip'], message['private_port']), 'peer_id': message['peer_id'] } print('Client({client_id}) info saved.'.format(client_id=message['client_id']))
hello_message = { 'type': SERVER_HELLO_MESSAGE, 'client_id': message['client_id'], 'public_ip': addr[0], 'public_port': addr[1] } send_msg(conn, json.dumps(hello_message))
if message['peer_id'] is None: continue
client = clients.get(message['client_id']) peer = clients.get(message['peer_id']) if not peer: print('Client({client_id}) specified Peer({peer_id}) not found.'.format(client_id=message['client_id'], peer_id=message['peer_id'])) peer_not_found_message = { 'type': PEER_NOT_FOUND_MESSAGE, 'peer_id': message['peer_id'] } send_msg(client['conn'], json.dumps(peer_not_found_message)) del clients[message['client_id']] print('Client({client_id}) info popped.'.format(client_id=message['client_id'])) continue
clients[message['peer_id']]['peer_id'] = message['client_id']
client_action, peer_action = p2p_connect_actions(client, peer)
peer_info_message = { 'type': PEER_INFO_MESSAGE, 'action': client_action, 'peer': { 'client_id': peer['client_id'], 'nat_type': peer['nat_type'], 'public_ip': peer['addr'][0], 'public_port': peer['addr'][1], } } send_msg(client['conn'], json.dumps(peer_info_message))
client_info_message = { 'type': PEER_INFO_MESSAGE, 'action': peer_action, 'peer': { 'client_id': client['client_id'], 'nat_type': client['nat_type'], 'public_ip': client['addr'][0], 'public_port': client['addr'][1], } } send_msg(peer['conn'], json.dumps(client_info_message))
if client_action not in (ACTION_TRY_AND_REPORT, ACTION_STANDBY): del clients[message['peer_id']] del clients[message['client_id']] print('Client({client_id}) info popped.'.format(client_id=message['peer_id'])) print('Client({client_id}) info popped.'.format(client_id=message['client_id'])) elif message['type'] == PEER_PORT_MESSAGE: print('-----Received Peer Port Message-----') print('Peer ID: {client_id}'.format(client_id=message['client_id'])) print('Peer Port: {port}'.format(port=message['port']))
client_id = message['client_id'] peer_id = clients[client_id]['peer_id'] peer = clients[peer_id] send_msg(peer['conn'], json.dumps(message))
del clients[client_id] del clients[peer_id] print('Client({client_id}) info popped.'.format(client_id=client_id)) print('Client({client_id}) info popped.'.format(client_id=peer_id))
def start_client(server_host='Server端的PublicIP', server_port=15005, peer_id=None): client_id = str(uuid.uuid4()).split('-')[0] print('Client ID: {client_id}'.format(client_id=client_id)) print('Checking client NAT type...') nat_type = 'unknown' try: nat_type, _, _ = stun.get_ip_info() except: print('Checking NAT Type failed, skip this step.') else: print('Client NAT: {nat_type}'.format(nat_type=nat_type))
print('Ready to connect server at {server_addr}...'.format(server_addr=str((server_host, server_port)))) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.connect((server_host, server_port)) private_addr = s.getsockname() print('Server connected. Client private_addr is {private_addr}'.format(private_addr=str(private_addr)))
hello_message = { 'type': CLIENT_HELLO_MESSAGE, 'client_id': client_id, 'nat_type': nat_type, 'private_ip': private_addr[0], 'private_port': private_addr[1], 'peer_id': peer_id } send_msg(s, json.dumps(hello_message))
peer_info = {}
while True: message = json.loads(recv_msg(s)) if message['type'] == SERVER_HELLO_MESSAGE: print('-----Received Server Hello Message-----') print('Client ID: {client_id}'.format(client_id=message['client_id'])) print('Client Public Addr: {public_ip}:{public_port}'.format(public_ip=message['public_ip'], public_port=message['public_port'])) elif message['type'] == SERVER_REFUSE_MESSAGE: print('-----Received Server Refuse Message-----') print('Client ID: {client_id}'.format(client_id=message['client_id'])) print('Message Content: {content}'.format(content=message['content'])) return elif message['type'] == PEER_NOT_FOUND_MESSAGE: print('-----Received Peer Not Found Message-----') print('Peer ID: {client_id}'.format(client_id=message['peer_id'])) return elif message['type'] == PEER_INFO_MESSAGE: print('-----Received Peer Info Message-----') print('Peer ID: {client_id}'.format(client_id=message['peer']['client_id'])) print('Peer NAT: {nat_type}'.format(nat_type=message['peer']['nat_type'])) print('Peer Addr: {public_ip}:{public_port}'.format(public_ip=message['peer']['public_ip'], public_port=message['peer']['public_port'])) print('Build p2p Action: {action}'.format(action=str(message['action']))) peer_info['client_id'] = message['peer']['client_id'] peer_info['nat_type'] = message['peer']['nat_type'] peer_info['public_ip'] = message['peer']['public_ip'] peer_info['public_port'] = message['peer']['public_port']
if not message['action']: print('Not Support build p2p connection between {t1} and {t2}.'.format(t1=nat_type, t2=message['peer']['nat_type'])) return
if message['action'] == ACTION_TRY_AND_LISTEN: try_p2p_connect(client_id, peer_info['public_ip'], peer_info['public_port'])
thread = threading.Thread(target=start_p2p_listen, args=(client_id, private_addr[0], private_addr[1])) thread.start() thread.join() elif message['action'] == ACTION_TRY_AND_REPORT: private_addr_for_peer = try_p2p_connect(client_id, peer_info['public_ip'], peer_info['public_port']) peer_port_message = { 'type': PEER_PORT_MESSAGE, 'client_id': client_id, 'port': private_addr_for_peer[1] } print('Report new port({port}) to Server.'.format(port=private_addr_for_peer[1])) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.connect((server_host, server_port)) send_msg(s, json.dumps(peer_port_message))
thread = threading.Thread(target=start_p2p_listen, args=(client_id, private_addr[0], peer_port_message[1])) thread.start() thread.join()
elif message['action'] == ACTION_STANDBY: continue elif message['action'] == ACTION_DELAY_CONNECT: time.sleep(3) thread = threading.Thread(target=start_p2p_connect, args=(client_id, peer_info['public_ip'], peer_info['public_port'])) thread.start() thread.join() elif message['action'] == ACTION_CONNECT: thread = threading.Thread(target=start_p2p_connect, args=(client_id, peer_info['public_ip'], peer_info['public_port'])) thread.start() thread.join() elif message['action'] == ACTION_LISTEN: thread = threading.Thread(target=start_p2p_listen, args=(client_id, private_addr[0], private_addr[1])) thread.start() thread.join() else: print('Unknown action') elif message['type'] == PEER_PORT_MESSAGE: print('-----Received Peer Port Message-----') print('Peer ID: {client_id}'.format(client_id=message['client_id'])) print('Peer Port: {port}'.format(port=message['port'])) peer_info['public_port'] = message['port'] thread = threading.Thread(target=start_p2p_connect, args=(client_id, peer_info['public_ip'], peer_info['public_port'])) thread.start() thread.join() else: print('Received Unknown type message from Server. Message: {message}'.format(message=str(message)))
def main(): argv = sys.argv if len(argv) < 2: print('Invalid arguments: mode must be one of (server, client).') return
if argv[1] == 'server': if len(argv[2:]) == 1: port = int(argv[2]) start_server('0.0.0.0', port) else: print('Invalid arguments: eg. server <port>') elif argv[1] == 'client': if len(argv[2:]) == 0: start_client() elif len(argv[2:]) == 1: start_client(peer_id=argv[2]) elif len(argv[2:]) == 2: host, port = argv[2], int(argv[3]) start_client(server_host=host, server_port=port) elif len(argv[2:]) == 3: host, port, peer_id = argv[2], int(argv[3]), argv[4] start_client(server_host=host, server_port=port, peer_id=peer_id) else: print('Invalid arguments: eg. client <ip> <port> [peer_id]') return else: print('mode must be one of (server, client}') return
if __name__ == '__main__': main()
|