From 0777aac061795d4b0380bb00eba615a81990e510 Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Sun, 10 Feb 2019 00:37:12 -0500 Subject: [PATCH] Add new request type for HOLE_PUNCH_ALL. In the setup tool when querying all servers from the master server, we also need to hole punch all servers which need it. Rather than doing this with many individual hole punch requests to the master server, add a single new message type to hole punch all servers. This could have been done as part of the normal query request handling, automatically hole punching all servers unrequested by the client. However, clients need to hole punch again when connecting and older clients do not know how to do this. Separating out the functionality means that hole punching will only be performed for newer clients which know about it. --- chocolate-master | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/chocolate-master b/chocolate-master index 930407f..0b627e9 100755 --- a/chocolate-master +++ b/chocolate-master @@ -55,6 +55,7 @@ NET_MASTER_PACKET_TYPE_SIGN_START_RESPONSE = 7 NET_MASTER_PACKET_TYPE_SIGN_END = 8 NET_MASTER_PACKET_TYPE_SIGN_END_RESPONSE = 9 NET_MASTER_PACKET_TYPE_NAT_HOLE_PUNCH = 10 +NET_MASTER_PACKET_TYPE_NAT_HOLE_PUNCH_ALL = 11 def bind_socket_to(sock, config): """ Bind the specified socket to the address/port configuration from @@ -288,14 +289,12 @@ class MasterServer: # Generate a list of strings representing servers. Only include # verified servers. - verified_servers = filter(lambda s: s.verified, self.servers.values()) - strings = [ str(server) for server in verified_servers] + verified_servers = [s for s in self.servers.values() if s.verified] + strings = [str(server) for server in verified_servers] # Send response packets. - for packet in self.strings_to_packets(strings): - self.send_message(addr, - NET_MASTER_PACKET_TYPE_QUERY_RESPONSE, + self.send_message(addr, NET_MASTER_PACKET_TYPE_QUERY_RESPONSE, packet) def process_metadata_request(self, addr): @@ -393,6 +392,16 @@ class MasterServer: # Forward hole punch request to the server: self.send_hole_punch(self.servers[server_addr], addr) + def process_hole_punch_all(self, addr): + """Process a hole punch request for all servers.""" + # For NET_MASTER_PACKET_TYPE_NAT_HOLE_PUNCH_ALL, we send hole punch + # requests on behalf of the client to all servers we have flagged as + # requiring hole punch assistance to contact. + self.log_output(addr, "Mass hole punch request") + for server in self.servers.values(): + if server.needs_hole_punch: + self.send_hole_punch(server, addr) + def process_packet(self, data, addr): """ Process a packet received from a server. """ @@ -410,6 +419,8 @@ class MasterServer: self.sign_end_message(data[2:], addr) elif packet_type == NET_MASTER_PACKET_TYPE_NAT_HOLE_PUNCH: self.process_hole_punch(data[2:], addr) + elif packet_type == NET_MASTER_PACKET_TYPE_NAT_HOLE_PUNCH_ALL: + self.process_hole_punch_all(addr) def is_blocked(self, addr): addr_str = "%s:%i" % addr