def sb_00respgen(self, fields): #keysheader r = '' r += byteencode.ipaddr(self.host) r += byteencode.uint16(self.port) r += byteencode.uint8(len(fields)) for field in fields: r += '\x00' + field + '\x00' r += '\x00' #list of hosts for key, host in self.server.hosts.items(): if host.lastheard + 360 < time.time( ): #deleting old servers here so we dont need a timer for that del self.server.hosts[key] #print "deleting old gamehost" else: flags = 0 flags_buffer = '' #setting flags - this part has potential to be improved if len(host.data) != 0: flags |= UNSOLICITED_UDP_FLAG flags |= HAS_KEYS_FLAG if "natneg" in host.data: flags |= CONNECT_NEGOTIATE_FLAG flags |= NONSTANDARD_PORT_FLAG flags |= PRIVATE_IP_FLAG #? flags |= NONSTANDARD_PRIVATE_PORT_FLAG #? flags_buffer += byteencode.ipaddr(host.ip) flags_buffer += byteencode.uint16(host.port) #adding 1 of local ip's :localport #for now server sends a random localip from all supplied lips = [] for key1, value1 in host.data.items(): if key1.startswith('localip') == True: lips.append(value1) for x1 in lips[random.randrange(0, len(lips))].split( '.'): #value.data['localip0'].split('.'): flags_buffer += chr(int(x1)) flags_buffer += byteencode.uint16( host.data.get('localport', 6500)) r += byteencode.uint8(flags) r += flags_buffer #adding fields if len(host.data) != 0: for field in fields: r += '\xff' + str(host.data.get(field, '0')) + '\x00' #ending symbols r += '\x00' r += '\xff' * 4 return r
def sb_00respgen(self, fields): #keysheader r = '' r += byteencode.ipaddr(self.host) r += byteencode.uint16(self.port) r += byteencode.uint8(len(fields)) for field in fields: r += '\x00' + field + '\x00' r += '\x00' #list of hosts for key, host in self.server.hosts.items(): if host.lastheard + 360 < time.time(): #deleting old servers here so we dont need a timer for that del self.server.hosts[key] else: flags = 0 flags_buffer = '' #setting flags - this part has potential to be improved if len(host.data) != 0: flags |= UNSOLICITED_UDP_FLAG flags |= HAS_KEYS_FLAG if "natneg" in host.data: flags |= CONNECT_NEGOTIATE_FLAG flags |= NONSTANDARD_PORT_FLAG flags |= PRIVATE_IP_FLAG #? flags |= NONSTANDARD_PRIVATE_PORT_FLAG #? flags_buffer += byteencode.ipaddr(host.ip) flags_buffer += byteencode.uint16(host.port) #adding 1 of local ip's :localport #for now server sends a random localip from all supplied lips = [] for key1, value1 in host.data.items(): if key1.startswith('localip') == True: lips.append(value1) if not lips: lips.append('127.0.0.1') for x1 in lips[random.randrange(0, len(lips))].split('.'): #value.data['localip0'].split('.'): flags_buffer += chr(int(x1)) flags_buffer += byteencode.uint16(host.data.get('localport', 6500)) r += byteencode.uint8(flags) r += flags_buffer #adding fields if len(host.data) != 0: for field in fields: r += '\xff' + str(host.data.get(field, '0')) + '\x00' #ending symbols r += '\x00' r += '\xff' * 4 return r
def sb_sendpush02(self, host): msg = '\x02' flags = 0 flags_buffer = '' if len(host.data) != 0: flags |= UNSOLICITED_UDP_FLAG flags |= HAS_KEYS_FLAG if "natneg" in host.data: flags |= CONNECT_NEGOTIATE_FLAG flags |= NONSTANDARD_PORT_FLAG flags |= PRIVATE_IP_FLAG #? flags |= NONSTANDARD_PRIVATE_PORT_FLAG #? msg += byteencode.uint8(flags) flags_buffer += byteencode.ipaddr(host.ip) flags_buffer += byteencode.uint16(host.port) localips = [] for key1, value1 in host.data.items(): if key1.startswith('localip'): localips.append(value1) if len(localips) == 1: localip = localips[0] logging.info('sb_sendpush02, single localip: %s', localip) elif not localips: logging.warning('sb_sendpush02: Missing localips, using fake') localip = '127.0.0.1' else: localip = random.choice(localips) logging.info( 'sb_sendpush02: Multiple localips: %s, using random one: %s', localips, localip) flags_buffer += byteencode.ipaddr(localip) flags_buffer += byteencode.uint16(host.data.get('localport', 6500)) msg += flags_buffer for field in defaultfields: msg += host.data[field] + '\x00' msg += '\x01' l = byteencode.uint16(len(msg) + 2) msg = l + msg # iterate through SBClients and make a message for each logging.info('Sending info about host %s to %d clients', host, len(self._clients_by_socket)) for client in self._clients_by_socket.values(): client.write(msg)
def sb_sendpush02(self, host): msg = '\x02' flags = 0 flags_buffer = '' if len(host.data) != 0: flags |= UNSOLICITED_UDP_FLAG flags |= HAS_KEYS_FLAG if "natneg" in host.data: flags |= CONNECT_NEGOTIATE_FLAG flags |= NONSTANDARD_PORT_FLAG flags |= PRIVATE_IP_FLAG #? flags |= NONSTANDARD_PRIVATE_PORT_FLAG #? msg += byteencode.uint8(flags) flags_buffer += byteencode.ipaddr(host.ip) flags_buffer += byteencode.uint16(host.port) localips = [] for key1, value1 in host.data.items(): if key1.startswith('localip'): localips.append(value1) if len(localips) == 1: localip = localips[0] logging.info('sb_sendpush02, single localip: %s', localip) elif not localips: logging.warning('sb_sendpush02: Missing localips, using fake') localip = '127.0.0.1' else: localip = random.choice(localips) logging.info('sb_sendpush02: Multiple localips: %s, using random one: %s', localips, localip) flags_buffer += byteencode.ipaddr(localip) flags_buffer += byteencode.uint16(host.data.get('localport', 6500)) msg += flags_buffer for field in defaultfields: msg += host.data[field] + '\x00' msg += '\x01' l = byteencode.uint16(len(msg) + 2) msg = l + msg # iterate through SBClients and make a message for each logging.info('Sending info about host %s to %d clients', host, len(self._clients_by_socket)) for client in self._clients_by_socket.values(): client.write(msg)
def sb_senddel04(self, address): msg = '\x00\x09\x04' msg += byteencode.ipaddr(address[0]) msg += byteencode.uint16(address[1]) for key in self.clients: self.clients[key].write(msg)