def method_eotu_ask_user_start(self, ask_user=None): logging.debug("###############EOTU ASK USER server[%s:%d]" % (self.coturn_server[0], self.coturn_server[1])) #初始化报头 msg_buf = bytearray(MESSAGE_MAX_LENGTH) msg_buf_header = stun_init_header(STUN_METHOD_EOTU_ASK_USER, self.my_user) msg_buf = set_buf_to_msg_buf(msg_buf, 0, msg_buf_header, STUN_HEADER_LENGTH) if ask_user == None: ask_user = self.ask_user if ask_user != None: msg = set_data_to_msg_buf(msg_buf, STUN_HEADER_LENGTH, STUN_ATTRIBUTE_ASK_USERID_INFO, ask_user) else: logging.error("ask user is None ,exiting ....") sys.exit() #设置msg_buf长度值 msg_to_send = set_msg_buf_size_final(msg) return msg_to_send
def ask_user_handle(self, reg_json, addr): #必须包含的字段! register_fields = ("ask_uid", "channel") try: if False in map(lambda x: True if x in reg_json else False, register_fields): raise Exception("reg_json field ERROR") res = None ask_uid = reg_json["ask_uid"] if ask_uid in self.clients: res = self.clients[ask_uid] res["ask_uid"] = ask_uid res["method"] = "ask_user_response" res = json.dumps(res) #res = json.dumps(self.clients[ask_uid]) else: res = "error! user[%d] not found in server" % (ask_uid) logging.debug(res) self.transport_write_back(res, addr) except Exception as e: logging.error("PYTHON_SERVER ERROR:" + str(e)) self.transport_write_back(str(e), addr)
def ask_user_all(self): logging.info("TEST ask user_all") regs = {"method": "ask_user_all"} bytes_regs = bytes(json.dumps(regs), encoding="utf-8") bytes_send = eotudata().bytedata(1, bytes_regs) self.socket_fd.send(bytes_send) while True: data, server = self.socket_fd.recvfrom(4096) logging.debug('RECV: {!r}'.format(data)) return data
def register(self, auth_value=0): logging.info("TEST register user [%d]" % self.user_me[0]) regs = { "method": "register", "uid": self.user_me[0], "channel": self.user_me[1], "auth_value": auth_value } bytes_regs = bytes(json.dumps(regs), encoding="utf-8") bytes_send = eotudata().bytedata(1, bytes_regs) self.socket_fd.send(bytes_send) while True: data, server = self.socket_fd.recvfrom(4096) logging.debug('RECV: {!r}'.format(data)) return data
def turn_data_to_uid(self, turndata, to_uid): logging.info("TEST turn_data_to_uid") json_regs = { "method": "turn_data", "to_uid": to_uid, "from_uid": self.user_me } bytes_json_regs = bytes(json.dumps(json_regs), encoding="utf-8") bytes_stream_data = bytes(turndata, encoding="utf-8") bytes_send = eotudata().bytedata(2, bytes_json_regs, bytes_stream_data) self.socket_fd.send(bytes_send) while True: data, server = self.socket_fd.recvfrom(4096) logging.debug('RECV: {!r}'.format(data)) return data
def datagramReceived(self, data, addr): logging.info("CLIENT[%s:%d] visit now! ----------- " % (addr[0], addr[1])) logging.debug("SERVER RECIEVED: %s" % data) protocol, pdata = eotudata().get_content( data) # return (protocol, data) if protocol == 1: content = pdata recv_json = json.loads(content) self.prtone_method_handle(recv_json, addr) elif protocol == 2: try: json_data = json.loads(pdata[0].decode(encoding="utf-8")) logging.debug("turn data is %s" % pdata[1]) self.prttwo_method_handle(json_data, pdata[1], addr, data) except Exception as e: logging.error(e) self.reactor.stop()
def ask_user(self, user_op, try_times=10, pwd=0): logging.info("TEST ask user [%d]" % user_op[0]) regs = { "method": "ask_user", "ask_uid": user_op[0], "channel": user_op[1], "pwd": pwd } bytes_regs = bytes(json.dumps(regs), encoding="utf-8") bytes_send = eotudata().bytedata(1, bytes_regs) self.socket_fd.send(bytes_send) while True: data, server = self.socket_fd.recvfrom(4096) if b"error" in data: try_times = try_times - 1 sleep(3) self.ask_user(user_op) else: logging.debug('RECV: {!r}'.format(data)) return 1, data if try_times == 0: return -1, "not found"
def allocation_msg(self, allocation_times=0): logging.debug("################ALLOCATION START ! server[%s:%d]" % (self.coturn_server[0], self.coturn_server[1])) #初始化报头 msg_buf = bytearray(MESSAGE_MAX_LENGTH) if allocation_times == 0: msg_buf_header = stun_init_header(STUN_METHOD_ALLOCATE, self.my_user, set_random=0xffffffff) else: msg_buf_header = stun_init_header(STUN_METHOD_ALLOCATE, self.my_user) msg_buf = set_buf_to_msg_buf(msg_buf, 0, msg_buf_header, STUN_HEADER_LENGTH) #添加本地地址 if self.local_addr != None: msg = set_data_to_msg_buf(msg_buf, STUN_HEADER_LENGTH, STUN_ATTRIBUTE_EOTU_LOCAL_ADDR, self.local_addr) #设置REQUESTED_TRANSPORT 默认UDP , 值为17 msg = set_data_to_msg_buf(msg_buf, msg[1], STUN_ATTRIBUTE_REQUESTED_TRANSPORT, 0x11000000) msg = set_data_to_msg_buf(msg_buf, msg[1], STUN_ATTRIBUTE_REQUESTED_ADDRESS_FAMILY, 0x01000000) #设置msg_buf长度值 msg_to_send = set_msg_buf_size_final(msg) logging.debug("ALLOCATION DATA [%s]" % msg_to_send.hex()) return msg_to_send
def transport_write_back(self, data, addr): res = b"UNEXCEPT for transport_write_back" if isinstance(data, str): res = bytes(data, encoding="utf-8") elif isinstance(data, dict): tmp = json.dumps(self.clients) res = bytes(tmp, encoding="utf-8") logging.debug("dict debug ---:%s", res) elif isinstance(data, bytes): res = data elif isinstance(data, int): res = bytes(data, encoding="utf-8") else: res = b"SERVER INTER ERROR! What data type to send!" logging.error(res) logging.debug("SERVER RESPONSE:%s", res) self.transport.write(res, addr) logging.debug("SERVER RESPONSE SEND END \n\n")
def loop(self): while True: data, server = self.socket_fd.recvfrom(4096) logging.debug('RECV: {!r}'.format(data))
def allocation_recv_handle(data): logging.debug("ready to format recieved data") msg = dismessage(data) return debug_recv(msg)