def proc_udp_request(self, req): self.client_associate = (req.dstaddr, req.dstport) self.last_clientaddr = self.client_associate self.client2local_udpsock = bind_local_udp(self.socksconn) self.local2remote_udpsock = bind_local_udp(self.remoteconn) if not self.client2local_udpsock or not self.local2remote_udpsock: request_fail(self.socksconn, req, GENERAL_SOCKS_SERVER_FAILURE) return False self.track_sock(self.client2local_udpsock) self.track_sock(self.local2remote_udpsock) send_request(self.remoteconn, UDP_ASSOCIATE, *sock_addr_info(self.local2remote_udpsock)) reply = read_reply(self.remoteconn) if reply.rep != SUCCEEDED: return False self.remote_associate = (reply.bndaddr, reply.bndport) request_success(self.socksconn, *sock_addr_info(self.client2local_udpsock)) return True
def forward_socks5_udp(self, url, localhandler, firstdata, firstaddr): remoteconn = socket.create_connection((url.hostname, url.port), self.timeout) remoteconn.settimeout(self.timeout) handler = SocksForwardSession(self.socksconn, remoteconn) # copy already-exist states from local handler handler.client_associate = localhandler.client_associate handler.last_clientaddr = localhandler.last_clientaddr handler.client2local_udpsock = localhandler.client2local_udpsock handler.track_sock(handler.client2local_udpsock) self.handler = handler # handshake, then request-reply, then send first packet, finally start to pipe if self.forward_socks5_handshake(handler.remoteconn): handler.local2remote_udpsock = utils.bind_local_udp(handler.remoteconn) handler.track_sock(handler.local2remote_udpsock) utils.send_request(handler.remoteconn, msg.UDP_ASSOCIATE, *utils.sock_addr_info(handler.local2remote_udpsock)) reply = utils.read_reply(handler.remoteconn) if reply.rep != msg.SUCCEEDED: return False handler.remote_associate = (reply.bndaddr, reply.bndport) handler.last_clientaddr = firstaddr handler.local2remote_udpsock.sendto(firstdata, handler.remote_associate) handler.relay_udp() return True