Best Python code snippet using tempest_python
stun_udp_data.py
Source:stun_udp_data.py
1# coding=utf-82"""3Prepare data for sending udp messages to stun, YSSTUN4C_DISTINGUISH_STUNv1 = 0x815C_STUN_QUERY_TYPE_REQ = 16C_STUN_QUERY_TYPE_RSP = 27C_STUN_NAT_UPDATE_REQ = 38C_STUN_NAT_UPDATE_RSP = 49C_STUN_NAT_QUIT = 510__author__ = 'zsw'11"""12import socket13import struct14import time15import binascii16from lib.decorator.trace import *17@print_trace18def query_type_req_data(step, udp_check):19 """20 sdk => stun21 :param step: 1byte, 1 or 2, ç®ç端å£ä¸º9000, å为1, ç®ç端å£ä¸º9002, å为222 :param udp_check: æ¯å¦å
å«checksum23 :return:24 """25 step = str(step).rjust(2, '0')26 data_combine = "8101{0}".format(step).ljust(6, '0')27 if udp_check:28 checksum = udp_checksum(data_combine)29 send_data = data_combine + checksum30 else:31 send_data = data_combine32 return send_data33@print_trace34def query_type_req_data_indefinite(step, udp_check):35 """36 sdk => stun æ¥æé¿åº¦ä¸å®37 :param step: 1byte, 1 or 2, ç®ç端å£ä¸º9000, å为1, ç®ç端å£ä¸º9002, å为238 :param udp_check: æ¯å¦å
å«checksum39 :return:40 """41 step = str(step)42 if len(step) % 2 != 0:43 step += '0'44 data_combine = "8101{0}".format(step)45 if udp_check:46 checksum = udp_checksum(data_combine)47 send_data = data_combine + checksum48 else:49 send_data = data_combine50 return send_data51@print_trace52def query_type_rsp_data(pub_ip, pub_port, step, udp_check):53 """54 stun => sdk55 :param pub_ip: 4bytes, sdkçå
¬ç½å°å56 :param pub_port: 2bytes, sdkçå
¬ç½port57 :param step: 1byte58 :param udp_check: æ¯å¦å
å«checksum59 :return:60 """61 step = str(step).rjust(2, '0')62 pub_ip_split = pub_ip.split(".")63 pub_ip_hex = ""64 for i in pub_ip_split:65 i_hex = str(hex(int(i))[2:]).rjust(2, '0')66 pub_ip_hex += i_hex67 pub_port_hex = str(hex(int(pub_port)))[2:].rjust(4, '0')68 data_combine = "8102{0}{1}{2}".format(step, pub_ip_hex, pub_port_hex).ljust(18, '0')69 if udp_check:70 checksum = udp_checksum(data_combine)71 send_data = data_combine + checksum72 else:73 send_data = data_combine74 return send_data75@print_trace76def nat_update_req_data(peer_id, nat_type, pri_ip, pri_port, udp_check):77 """78 sdk => stun79 :param peer_id: 16bytes, sdkçpeer_id80 :param nat_type: 1byte, sdkèªå·±æ¢æµåºæ¥çNATç±»å81 :param pri_ip: 4bytes, sdkçç§ç½IP82 :param pri_port: 2bytes, sdkçç§ç½port83 :param udp_check: æ¯å¦å
å«checksum84 :return:85 """86 peer_id = str(peer_id.lower()).rjust(32, '0')87 nat_type = str(nat_type).rjust(2, '0')88 pri_ip_split = pri_ip.split(".")89 pri_ip_hex = ""90 for i in pri_ip_split:91 i_hex = str(hex(int(i))[2:]).rjust(2, '0')92 pri_ip_hex += i_hex93 pri_port_hex = str(hex(int(pri_port)))[2:].rjust(4, '0')94 data_combine = "8103{0}{1}{2}{3}".format(peer_id, nat_type, pri_ip_hex, pri_port_hex).ljust(50, '0')95 if udp_check:96 checksum = udp_checksum(data_combine)97 send_data = data_combine + checksum98 else:99 send_data = data_combine100 return send_data101@print_trace102def nat_update_req_data_indefinite(peer_id, nat_type, pri_ip, pri_port, udp_check):103 """104 sdk => stun æ¥æé¿åº¦ä¸å®105 :param peer_id: 16bytes, sdkçpeer_id106 :param nat_type: 4bytes, sdkèªå·±æ¢æµåºæ¥çNATç±»å107 :param pri_ip: 4bytes, sdkçç§ç½IP108 :param pri_port: 2bytes, sdkçç§ç½port109 :param udp_check: æ¯å¦å
å«checksum110 :return:111 """112 peer_id = str(peer_id.lower())113 nat_type = str(nat_type).rjust(2, '0')114 pri_ip_split = pri_ip.split(".")115 pri_ip_hex = ""116 for i in pri_ip_split:117 i_hex = str(hex(int(i))[2:]).rjust(2, '0')118 pri_ip_hex += i_hex119 pri_port_hex = str(hex(int(pri_port)))[2:]120 data_combine = "8103{0}{1}{2}{3}".format(peer_id, nat_type, pri_ip_hex, pri_port_hex)121 if len(data_combine) % 2 != 0:122 data_combine += '0'123 if udp_check:124 checksum = udp_checksum(data_combine)125 send_data = data_combine + checksum126 else:127 send_data = data_combine128 return send_data129@print_trace130def nat_update_rsp_data(rsp_code, udp_check):131 """132 stun => sdk133 :param rsp_code: 1byte, ç¶æç ; timestamp, 8bytes134 :param udp_check: æ¯å¦å
å«checksum135 :return:136 """137 rsp_code = str(rsp_code).rjust(2, "0")138 timestamp = int(time.time()*1000)139 timestamp_hex = str(hex(timestamp))[2:-1].rjust(16, '0')140 data_combine = "8104{0}{1}".format(rsp_code, timestamp_hex)141 if udp_check:142 checksum = udp_checksum(data_combine)143 send_data = data_combine + checksum144 else:145 send_data = data_combine146 return send_data147@print_trace148def nat_quit_data(peer_id, udp_check):149 """150 sdk => stun151 :param peer_id: 16bytes, sdkçpeer_id152 :param udp_check: æ¯å¦å
å«checksum153 :return:154 """155 peer_id = str(peer_id.lower()).rjust(32, '0')156 data_combine = "8105{0}".format(peer_id)157 if udp_check:158 checksum = udp_checksum(data_combine)159 send_data = data_combine + checksum160 else:161 send_data = data_combine162 return send_data163@print_trace164def nat_quit_data_indefinite(peer_id, udp_check):165 """166 sdk => stun æ¥æé¿åº¦ä¸å®167 :param peer_id: 16bytes, sdkçpeer_id168 :param udp_check: æ¯å¦å
å«checksum169 :return:170 """171 peer_id = str(peer_id.lower())172 data_combine = "8105{0}".format(peer_id)173 if len(data_combine) % 2 != 0:174 data_combine += '0'175 if udp_check:176 checksum = udp_checksum(data_combine)177 send_data = data_combine + checksum178 else:179 send_data = data_combine180 return send_data181@print_trace182def rrpc_ack_data(uuid, peer_id, result, udp_check):183 """184 :param uuid: 16bytes, æ¥æå¯ä¸æ è¯185 :param peer_id: 16bytes, sdkçpeer_id186 :param result:187 :param udp_check: æ¯å¦å
å«checksum188 :return:189 """190 distinguish = '91'191 token = str(uuid).rjust(32, '0')192 reserved = '0' * 64193 rrpc_id = '0' * 4194 peer_id = str(peer_id.lower()).rjust(32, '0')195 result = str(result).rjust(2, '0')196 data_combine = distinguish + token + reserved + rrpc_id + peer_id + result197 if udp_check:198 checksum = udp_checksum(data_combine)199 send_data = data_combine + checksum200 else:201 send_data = data_combine202 return send_data203@print_trace204def rrpc_join_leifeng_data(uuid, peer_id, file_id, file_url, piece_size, ppc, cppc, udp_check):205 """206 :param uuid: 16bytes, æ¥æå¯ä¸æ è¯207 :param peer_id: 16bytes, sdkçpeer_id208 :param file_id: 16bytes, æ件æ è¯209 :param file_url: 256bytes210 :param piece_size: 2bytes, æ件piece大å°211 :param ppc: 2bytes212 :param cppc: 2bytes213 :param live_push_server: 64bytes, é¢ç214 :param udp_check: æ¯å¦å
å«checksum215 :return:216 """217 distinguish = '91'218 token = str(uuid).rjust(32, '0')219 reserved = '0' * 64220 rrpc_id = '0001'221 peer_id = str(peer_id.lower()).rjust(32, '0')222 file_id = str(file_id.lower()).rjust(32, '0')223 file_url = str(binascii.b2a_hex(file_url)).rjust(512, '0')224 piece_size = str(hex(piece_size)[2:-1]).rjust(4, '0')225 ppc = str(hex(ppc)[2:-1]).rjust(4, '0')226 cppc = str(hex(cppc)[2:-1]).rjust(4, '0')227 live_push_server = '0' * 128228 data_combine = distinguish + token + reserved + rrpc_id + peer_id + file_id + file_url + piece_size + ppc + cppc + live_push_server229 if udp_check:230 checksum = udp_checksum(data_combine)231 send_data = data_combine + checksum232 else:233 send_data = data_combine234 return send_data235@print_trace236def rrpc_leave_leifeng_data(uuid, peer_id, file_id, udp_check):237 """238 :param uuid: 16bytes, æ¥æå¯ä¸æ è¯239 :param peer_id: 16bytes, sdkçpeer_id240 :param file_id: 16bytes, æ件æ è¯241 :param udp_check: æ¯å¦å
å«checksum242 :return:243 """244 distinguish = '91'245 token = str(uuid).rjust(32, '0')246 reserved = '0' * 64247 rrpc_id = '0002'248 peer_id = str(peer_id.lower()).rjust(32, '0')249 file_id = str(file_id.lower()).rjust(32, '0')250 # checksum251 data_combine = distinguish + token + reserved + rrpc_id + peer_id + file_id252 if udp_check:253 checksum = udp_checksum(data_combine)254 send_data = data_combine + checksum255 else:256 send_data = data_combine257 return send_data258@print_trace259def rrpc_p2p_setting(uuid, peer_id, setting, udp_check):260 """261 :param uuid: 16bytes, æ¥æå¯ä¸æ è¯262 :param peer_id: 16bytes, sdkçpeer_id263 :param setting: æ¯å¦å¼å¯èç¹p2påè½, 为'01'åå¯ç¨ï¼ä¸º'00'åç¦ç¨264 :param udp_check: æ¯å¦å
å«checksum265 :return:266 """267 distinguish = '91'268 token = str(uuid).rjust(32, '0')269 reserved = '0' * 64270 rrpc_id = '0003'271 peer_id = str(peer_id.lower()).rjust(32, '0')272 # checksum273 data_combine = distinguish + token + reserved + rrpc_id + peer_id + setting274 if udp_check:275 checksum = udp_checksum(data_combine)276 send_data = data_combine + checksum277 else:278 send_data = data_combine279 return send_data280@print_trace281def rrpc_rsm_report_data(uuid, peer_id, udp_check):282 """283 2016.09.30 ææªä½¿ç¨284 :param uuid: 16bytes, æ¥æå¯ä¸æ è¯285 :param peer_id: 16bytes, sdkçpeer_id286 :param udp_check: æ¯å¦å
å«checksum287 :return:288 """289 distinguish = '91'290 token = str(uuid).rjust(32, '0')291 reserved = '0' * 64292 rrpc_id = '0003'293 peer_id = str(peer_id.lower()).rjust(32, '0')294 data_combine = distinguish + token + reserved + rrpc_id + peer_id295 if udp_check:296 checksum = udp_checksum(data_combine)297 send_data = data_combine + checksum298 else:299 send_data = data_combine300 return send_data301@print_trace302def udp_checksum(udp_data):303 """304 æ ¹æ®udpæ°æ®çæchecksum305 :param udp_data: "810101"306 :return:307 """308 data_binascii = binascii.a2b_hex(udp_data)309 data = bytearray(data_binascii)310 length = len(data)311 t = length >> 3312 rest = length & 7313 # print t, rest314 sum = socket.htons(0x7973)315 for i in range(0, t):316 p = struct.unpack_from("Q", data, i * 8)[0]317 sum = sum ^ p318 if rest:319 temp = bytearray(8)320 for i in range(0, rest):321 temp[i] = data[t * 8 + i]322 last = struct.unpack_from("Q", temp, 0)[0]323 sum = sum ^ last324 sum = ((sum >> 32) ^ sum) & 0xffffffff325 cs = (((sum >> 16) ^ sum) & 0xffff)326 checksum = socket.ntohs(cs)327 checksum_hex = str(hex(checksum)).replace("0x", "")328 return checksum_hex329@print_trace330def pene_query_peer_req(peer_id, udp_check):331 """332 sdk => stun ç©¿éåè®®333 :param peer_id:334 :param udp_check335 :return:336 """337 peer_id = str(peer_id.lower()).rjust(32, '0')338 data_combine = "a101{0}".format(peer_id).ljust(36, '0')339 if udp_check:340 checksum = udp_checksum(data_combine)341 send_data = data_combine + checksum342 else:343 send_data = data_combine344 return send_data345@print_trace346def pene_penetrate_req(dst_peer_id, src_public_ip, src_public_port, udp_check):347 dst_peer_id = str(dst_peer_id.lower()).rjust(32, '0')348 src_public_ip_split = src_public_ip.split(".")349 src_public_ip = ""350 for i in src_public_ip_split:351 i_hex = str(hex(int(i))[2:]).rjust(2, '0')352 src_public_ip += i_hex353 src_public_port = str(hex(int(src_public_port)))[2:]354 data_combine = "a103{0}01{1}{2}".format(dst_peer_id, src_public_ip, src_public_port).ljust(50, '0')355 if udp_check:356 checksum = udp_checksum(data_combine)357 send_data = data_combine + checksum358 else:359 send_data = data_combine360 return send_data361@print_trace362def pene_reversing_req(dst_peer_id, src_peer_id, src_public_ip, src_public_port, src_stun_ip, src_stun_port, udp_check):363 dst_peer_id = str(dst_peer_id.lower()).rjust(32, '0')364 src_peer_id = str(src_peer_id.lower()).rjust(32, '0')365 src_public_ip_split = src_public_ip.split(".")366 src_public_ip = ""367 for i in src_public_ip_split:368 i_hex = str(hex(int(i))[2:]).rjust(2, '0')369 src_public_ip += i_hex370 src_public_port = str(hex(int(src_public_port)))[2:]371 src_stun_ip_split = src_stun_ip.split(".")372 src_stun_ip = ""373 for i in src_stun_ip_split:374 i_hex = str(hex(int(i))[2:]).rjust(2, '0')375 src_stun_ip += i_hex376 src_stun_port = str(hex(int(src_stun_port)))[2:]377 data_combine = "a106{0}{1}{2}{3}{4}{5}".format(dst_peer_id, src_peer_id, src_public_ip, src_public_port,378 src_stun_ip, src_stun_port).ljust(92, '0')379 if udp_check:380 checksum = udp_checksum(data_combine)381 send_data = data_combine + checksum382 else:383 send_data = data_combine384 return send_data385if __name__ == '__main__':386 test_data = "810101"387 c = udp_checksum(test_data)388 print c389 a = query_type_req_data(1)...
stun_udp_req.py
Source:stun_udp_req.py
1# coding=utf-82"""3stun related api test keyword for udp, YSSTUN, YSRRPC4__author__ = 'zsw'5"""6from lib.interface.stun_udp_data import *7from lib.constant.stun_udp import *8from lib.request.udp_request import *9from lib.decorator.trace import *10@print_trace11def sdk_query_type_req(stun_udp_host, stun_udp_port, step=1, listening_port=60000, udp_check=False):12 """13 模æsdkåstun-srvåéquery_type_req14 :param stun_udp_host: udpå
çæ¥æ¶æ¹å°å15 :param stun_udp_port: udpå
çæ¥æ¶æ¹ç«¯å£16 :param step: STUNV1åè®®çstepï¼ç®ç端å£ä¸º9000, å为1, ç®ç端å£ä¸º9002, å为217 :param listening_port:æ¬å°åéudpå
ççå¬ç«¯å£18 :param udp_check: æ¯å¦å
å«checksum19 :return:20 """21 send_data = query_type_req_data(step, udp_check)22 response = send_udp_request(23 '[SdkQueryTypeReq]',24 stun_udp_host,25 stun_udp_port,26 send_data,27 listening_port,28 SOCKET_TIME_OUT29 )30 return response31@print_trace32def stun_query_type_rsp(stun_udp_host, stun_udp_port, pub_ip, pub_port, step=1, listening_port=60000, udp_check=False):33 """34 åstun-srvåéquery_type_rsp35 :param stun_udp_host:36 :param stun_udp_port:37 :param pub_ip: sdkçå
¬ç½å°å38 :param pub_port: sdkçå
¬ç½port39 :param step: sdkåstunç9000端å£åéC_STUN_QUERY_TYPE_REQæ¶, å¯è·å¾stunä»9000端å£ä»¥å9001端å£çåå¤, æ¥èª40 9000端å£çåå¤step为1, æ¥èª9001çåå¤step为3, sdkåstunç9002端å£åéC_STUN_QUERY_TYPE_REQæ¶ï¼å¯è·å¾stun41 ä»9002端å£ä»¥å9001端å£çåå¤, æ¥èª9002端å£çåå¤step为2, æ¥èª9001çåå¤step为442 :param listening_port: æ¬å°åéudpå
ççå¬ç«¯å£43 :param udp_check: æ¯å¦å
å«checksum44 :return:45 """46 send_data = query_type_rsp_data(pub_ip, pub_port, step, udp_check)47 response = send_udp_request(48 '[SdkQueryTypeRsp]',49 stun_udp_host,50 stun_udp_port,51 send_data,52 listening_port,53 SOCKET_TIME_OUT54 )55 return response56@print_trace57def sdk_nat_update_req(stun_udp_host, stun_udp_port, peer_id, nat_type, pri_ip, pri_port, listening_port=60000, udp_check=False):58 """59 åstun-srvåénat_update_req60 :param stun_udp_host:61 :param stun_udp_port:62 :param peer_id: sdkçpeer_id63 :param nat_type: sdkèªå·±æ¢æµåºæ¥çNATç±»å64 :param pri_ip: sdkçç§ç½IP65 :param pri_port: sdkçç§ç½port66 :param listening_port:67 :param udp_check: æ¯å¦å
å«checksum68 :return:69 """70 send_data = nat_update_req_data(peer_id, nat_type, pri_ip, pri_port, udp_check)71 response = send_udp_request(72 '[SdkNatUpdateReq]',73 stun_udp_host,74 stun_udp_port,75 send_data,76 listening_port,77 SOCKET_TIME_OUT78 )79 return response80@print_trace81def stun_nat_update_rsp(stun_udp_host, stun_udp_port, rsp_code=0, listening_port=60000, udp_check=False):82 """83 åstun-srvåénat_update_rsp84 :param stun_udp_host:85 :param stun_udp_port:86 :param rsp_code: ç¶æç 87 :param listening_port:88 :param udp_check: æ¯å¦å
å«checksum89 :return:90 """91 send_data = nat_update_rsp_data(rsp_code, udp_check)92 response = send_udp_request(93 '[SdkNatUpdateRsp]',94 stun_udp_host,95 stun_udp_port,96 send_data,97 listening_port,98 SOCKET_TIME_OUT99 )100 return response101@print_trace102def sdk_nat_quit(stun_udp_host, stun_udp_port, peer_id, listening_port=60000, udp_check=False):103 """104 åstun-srvåénat_quit105 :param stun_udp_host:106 :param stun_udp_port:107 :param peer_id:108 :param listening_port:109 :param udp_check: æ¯å¦å
å«checksum110 :return:111 """112 send_data = nat_quit_data(peer_id, udp_check)113 response = send_udp_request(114 '[SdkNatQuit]',115 stun_udp_host,116 stun_udp_port,117 send_data,118 listening_port,119 1120 )121 return response122@print_trace123def sdk_rrpc_ack(stun_udp_host, stun_udp_port, uuid, peer_id, result, listening_port=60000, udp_check=False):124 """125 åstun-srvåérrpc_ack126 :param uuid: æ¥æå¯ä¸æ è¯127 :param peer_id:128 :param result:129 :param listening_port:130 :param stun_udp_host:131 :param stun_udp_port:132 :param udp_check:133 :return:134 """135 send_data = rrpc_ack_data(uuid, peer_id, result, udp_check)136 response = send_udp_request(137 '[SdkRrpcAck]',138 stun_udp_host,139 stun_udp_port,140 send_data,141 listening_port,142 SOCKET_TIME_OUT143 )144 return response145@print_trace146def stun_rrpc_join_lf(stun_udp_host, stun_udp_port, uuid, peer_id, file_id, file_url, piece_size, ppc, cppc, listening_port=60000, udp_check=False):147 """148 åstun-srvåérrpc_join_lf149 :param uuid:150 :param peer_id:151 :param file_id:152 :param file_url:153 :param piece_size:154 :param ppc:155 :param cppc:156 :param listening_port:157 :param stun_udp_host:158 :param stun_udp_port:159 :param udp_check:160 :return:161 """162 send_data = rrpc_join_leifeng_data(uuid, peer_id, file_id, file_url, piece_size, ppc, cppc, udp_check)163 response = send_udp_request(164 '[SdkRrpcJoinLf]',165 stun_udp_host,166 stun_udp_port,167 send_data,168 listening_port,169 SOCKET_TIME_OUT170 )171 return response172@print_trace173def stun_rrpc_leave_lf(stun_udp_host, stun_udp_port, uuid, peer_id, file_id, listening_port=60000, udp_check=False):174 """175 åstun-srvåérrpc_leave_lf176 :param uuid:177 :param peer_id:178 :param file_id:179 :param listening_port:180 :param stun_udp_host:181 :param stun_udp_port:182 :param udp_check:183 :return:184 """185 send_data = rrpc_leave_leifeng_data(uuid, peer_id, file_id, udp_check)186 response = send_udp_request(187 '[SdkRrpcLeaveLf]',188 stun_udp_host,189 stun_udp_port,190 send_data,191 listening_port,192 SOCKET_TIME_OUT193 )194 return response195@print_trace196def stun_rrpc_p2p_setting(stun_udp_host, stun_udp_port, uuid, peer_id, setting, listening_port=60000, udp_check=False):197 """198 åstun-srvåép2p_setting199 :param uuid:200 :param peer_id:201 :param setting: æ¯å¦å¼å¯èç¹p2påè½, 为'01'åå¯ç¨ï¼ä¸º'00'åç¦ç¨202 :param listening_port:203 :param stun_udp_host:204 :param stun_udp_port:205 :param udp_check:206 :return:207 """208 send_data = rrpc_p2p_setting(uuid, peer_id, setting, udp_check)209 response = send_udp_request(210 '[SdkRrpcP2pSetting]',211 stun_udp_host,212 stun_udp_port,213 send_data,214 listening_port,215 SOCKET_TIME_OUT216 )217 return response218@print_trace219def sdk_pene_query_peer_req(stun_udp_host, stun_udp_port, peer_id, listening_port=60000, udp_check=False):220 """221 åstunåéç©¿éæ¥è¯¢åè®®222 :param stun_udp_host:223 :param stun_udp_port:224 :param peer_id:225 :param listening_port:226 :param udp_check:227 :return:228 """229 send_data = pene_query_peer_req(peer_id, udp_check)230 response = send_udp_request(231 '[PeneQueryPeer]',232 stun_udp_host,233 stun_udp_port,234 send_data,235 listening_port,236 SOCKET_TIME_OUT237 )238 return response239@print_trace240def sdk_pene_penetrate_req(stun_udp_host, stun_udp_port, dst_peer_id, src_public_ip, src_public_port, 241 listening_port=60000, udp_check=False):242 print src_public_port243 send_data = pene_penetrate_req(dst_peer_id, src_public_ip, src_public_port, udp_check)244 response = send_udp_request(245 '[SdkPenePenetrateReq]',246 stun_udp_host,247 stun_udp_port,248 send_data,249 listening_port,250 1251 )252 return response253@print_trace254def sdk_pene_reserving_req(stun_udp_host, stun_udp_port, dst_peer_id, src_peer_id, src_public_ip, src_public_port, 255 src_stun_ip, src_stun_port,listening_port=60000, udp_check=False):256 send_data = pene_reversing_req(dst_peer_id, src_peer_id, src_public_ip, src_public_port, src_stun_ip, src_stun_port, udp_check)257 response = send_udp_request(258 '[SdkPeneReservingReq]',259 stun_udp_host,260 stun_udp_port,261 send_data,262 listening_port,263 1264 )265 return response266@print_trace267def sdk_query_type_req_indefinite(stun_udp_host, stun_udp_port, step=1, listening_port=60000, udp_check=False):268 """269 模æsdkåstun-srvåéquery_type_req270 :param stun_udp_host: udpå
çæ¥æ¶æ¹å°å271 :param stun_udp_port: udpå
çæ¥æ¶æ¹ç«¯å£272 :param step: STUNV1åè®®çstepï¼ç®ç端å£ä¸º9000, å为1, ç®ç端å£ä¸º9002, å为2273 :param listening_port:æ¬å°åéudpå
ççå¬ç«¯å£274 :param udp_check:275 :return:276 """277 print stun_udp_host, stun_udp_port, step278 send_data = query_type_req_data_indefinite(step, udp_check)279 response = send_udp_request(280 '[SdkQueryTypeReq]',281 stun_udp_host,282 stun_udp_port,283 send_data,284 listening_port,285 SOCKET_TIME_OUT286 )287 return response288@print_trace289def sdk_nat_update_req_indefinite(stun_udp_host, stun_udp_port, peer_id, nat_type, pri_ip, pri_port, listening_port=60000, udp_check=False):290 """291 åstun-srvåénat_update_req292 :param stun_udp_host:293 :param stun_udp_port:294 :param peer_id: sdkçpeer_id295 :param nat_type: sdkèªå·±æ¢æµåºæ¥çNATç±»å296 :param pri_ip: sdkçç§ç½IP297 :param pri_port: sdkçç§ç½port298 :param listening_port:299 :param udp_check:300 :return:301 """302 send_data = nat_update_req_data_indefinite(peer_id, nat_type, pri_ip, pri_port, udp_check)303 response = send_udp_request(304 '[SdkNatUpdateReq]',305 stun_udp_host,306 stun_udp_port,307 send_data,308 listening_port,309 SOCKET_TIME_OUT310 )311 return response312@print_trace313def sdk_nat_quit_indefinite(stun_udp_host, stun_udp_port, peer_id, listening_port=60000, udp_check=False):314 """315 åstun-srvåénat_quit316 :param stun_udp_host:317 :param stun_udp_port:318 :param peer_id:319 :param listening_port:320 :param udp_check:321 :return:322 """323 send_data = nat_quit_data_indefinite(peer_id, udp_check)324 response = send_udp_request(325 '[SdkNatQuit]',326 stun_udp_host,327 stun_udp_port,328 send_data,329 listening_port,330 SOCKET_TIME_OUT331 )...
nat.py
Source:nat.py
1import select, socket, sys, queue2import struct3ETH_P_ALL = 0x00034ETH_LENGTH = 145ETH0_NETWORK = '10.0.0.'6ETH1_NETWORK = '10.0.1.'7ETH0_IP_ADDR = ETH0_NETWORK + '11'8ETH1_IP_ADDR = ETH1_NETWORK + '10'9ETH0_HOSTS = {10 '10.0.0.10': {11 'mac': b'\x00\x00\x00\xaa\x00\x00',12 'private_port': None,13 'public_port': 600114 },15 '10.0.0.12': {16 'mac': b'\x00\x00\x00\xaa\x00\x04',17 'private_port': None,18 'public_port': 600219 }20}21def bytes_to_mac(bytesmac):22 return ':'.join('{:02x}'.format(x) for x in bytesmac)23def checksum(msg):24 s = 025 msg = (msg + b'\x00') if len(msg)%2 else msg26 for i in range(0, len(msg), 2):27 w = msg[i] + (msg[i+1] << 8)28 s = s + w29 s = (s & 0xffff) + (s >> 16)30 s = ~s & 0xffff31 return socket.ntohs(s)32def create_socket(interface_name):33 s = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.ntohs(ETH_P_ALL))34 s.bind((interface_name, 0))35 mac_addr = s.getsockname()[-1]36 print(interface_name + ' MAC address:' + bytes_to_mac(mac_addr))37 return (s, mac_addr)38def is_dest_ip_private(packet):39 iph = struct.unpack('!BBHHHBBH4s4s', packet[ETH_LENGTH:20+ETH_LENGTH])40 return socket.inet_ntoa(iph[-1]).startswith(ETH0_NETWORK)41def pack_ip_header(packet, s_ip_addr, d_ip_addr):42 iph = struct.unpack('!BBHHHBBH4s4s', packet[ETH_LENGTH:20+ETH_LENGTH])43 # Header IP44 ip_ver = 445 ip_ihl = 546 ip_tos = iph[1]47 ip_tot_len = iph[2]48 ip_id = iph[3]49 ip_frag_off = iph[4]50 ip_ttl = iph[5]51 ip_proto = iph[6]52 ip_check = 053 ip_saddr = socket.inet_aton(s_ip_addr) if s_ip_addr is not None else iph[-2]54 ip_daddr = socket.inet_aton(d_ip_addr) if d_ip_addr is not None else iph[-1]55 ip_ihl_ver = (ip_ver << 4) + ip_ihl56 ip_header = struct.pack('!BBHHHBBH4s4s', ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl,57 ip_proto, ip_check, ip_saddr, ip_daddr)58 ip_check = checksum(ip_header)59 ip_header = struct.pack('!BBHHHBBH4s4s', ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl,60 ip_proto, ip_check, ip_saddr, ip_daddr)61 return (ip_header, ip_proto, ip_saddr, ip_daddr)62def udp(packet, ip_saddr, ip_daddr):63 udph = struct.unpack('!HHHH', packet[20+ETH_LENGTH:28+ETH_LENGTH])64 data = packet[28+ETH_LENGTH:]65 udp_sport = udph[0]66 udp_dport = udph[1]67 udp_len = udph[2]68 udp_check = 069 udp_header = struct.pack('!HHHH', udp_sport, udp_dport, udp_len, udp_check)70 udp_pseudo_header = struct.pack('!4s4sBBH', ip_saddr, ip_daddr, udp_check, socket.IPPROTO_UDP, udp_len)71 udp_check = checksum(udp_pseudo_header + udp_header + data)72 udp_header = struct.pack('!HHHH', udp_sport, udp_dport, udp_len, udp_check)73 return udp_header + data74def tcp(packet, ip_saddr, ip_daddr):75 tcph = struct.unpack('!HHLLBBHHH', packet[20+ETH_LENGTH:40+ETH_LENGTH])76 data = packet[40+ETH_LENGTH:]77 tcp_sport = tcph[0]78 tcp_dport = tcph[1]79 tcp_seq = tcph[2]80 tcp_ack = tcph[3]81 tcp_hl_r = tcph[4]82 tcp_flags = tcph[5]83 tcp_wsize = tcph[6]84 tcp_check = 085 tcp_urgptr = tcph[8]86 tcp_header = struct.pack("!HHLLBBHHH", tcp_sport, tcp_dport, tcp_seq, tcp_ack, tcp_hl_r, tcp_flags,87 tcp_wsize, tcp_check, tcp_urgptr)88 tcp_pseudo_header = struct.pack("!4s4sBBH", ip_saddr, ip_daddr, tcp_check, socket.IPPROTO_TCP, len(tcp_header + data))89 tcp_check = checksum(tcp_pseudo_header + tcp_header + data)90 tcp_header = struct.pack("!HHLLBBHHH", tcp_sport, tcp_dport, tcp_seq, tcp_ack, tcp_hl_r, tcp_flags,91 tcp_wsize, tcp_check, tcp_urgptr)92 return tcp_header + data93def process_packet(packet, ip_proto, ip_saddr, ip_daddr):94 if ip_proto == socket.IPPROTO_TCP:95 return tcp(packet, ip_saddr, ip_daddr)96 elif ip_proto == socket.IPPROTO_UDP:97 return udp(packet, ip_saddr, ip_daddr)98 return packet[20+ETH_LENGTH:]99try:100 (s0, eth0_mac_addr) = create_socket('eth0')101 (s1, eth1_mac_addr) = create_socket('eth1')102except OSError as msg:103 print('Error' + str(msg))104 sys.exit(1)105print('Sockets created!')106inputs = [s0, s1]107outputs = []108message_queues = {}109while inputs:110 readable, writable, exceptional = select.select(inputs, outputs, inputs)111 for s in readable:112 (packet, addr) = s.recvfrom(65536)113 eth_header = packet[:ETH_LENGTH]114 eth = struct.unpack('!6s6sH', eth_header)115 protocol = eth[2]116 interface = 'eth0' if s is s0 else 'eth1'117 print('Received from ' + interface)118 print('MAC Dst: ' + bytes_to_mac(eth[0]))119 print('MAC Src: ' + bytes_to_mac(eth[1]))120 print('Type: ' + hex(protocol))121 print('{0}'.format(protocol))122 nexthdr = packet[ETH_LENGTH:]123 if protocol == 2048:124 if s is s0:125 dest_mac = b'\x00\x00\x00\xaa\x00\x03'126 source_mac = eth1_mac_addr127 eth_hdr = struct.pack('!6s6sH', dest_mac, source_mac, protocol)128 (ip_header, ip_proto, ip_saddr, ip_daddr) = pack_ip_header(packet, ETH1_IP_ADDR, None)129 s1.send(eth_hdr + ip_header + process_packet(packet, ip_proto, ip_saddr, ip_daddr))130 elif s is s1 and not is_dest_ip_private(packet):131 host_n1 = ETH0_NETWORK + '10'132 dest_mac = ETH0_HOSTS[host_n1]['mac']133 source_mac = eth0_mac_addr134 eth_hdr = struct.pack('!6s6sH', dest_mac, source_mac, protocol)135 (ip_header, ip_proto, ip_saddr, ip_daddr) = pack_ip_header(packet, None, host_n1)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!