def thread(): global currentKey, raidServer, raidClient net.node.waitForNS() # Connect to server... sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(5) try: sock.connect(PARSER_SERVER_ADDR) except: prnt("Failed to connect:") prnt(traceback.format_exc()) wx.CallAfter(failureFunc, "connect_failed") return # Write data stream = ByteStream() stream.writeByte(pkt.JOIN_RAID) stream.writeByte(VERSION_INT) stream.writeString(key) stream.writeString(net.node.getId()) sock.send(stream.toString()) # Read data data = sock.recv(1024) stream = ByteStream(data) # Process data success = stream.readBoolean() if success: currentKey = key isHost = stream.readBoolean() serverNode = net.node.getId() if isHost: prnt("Raid: Joined raid, became host") raidServer = RaidServer(sock) raidServer.start() else: prnt("Raid: Joined raid, didn't become host") serverNode = stream.readString() sock.close() raidClient = RaidClient(serverNode, failureFunc, successFunc) raidClient.start() else: reason = stream.readString() wx.CallAfter(failureFunc, reason) sock.close()
def thread(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect(PARSER_SERVER_ADDR) except: prnt("Failed to connect:") prnt(traceback.format_exc()) wx.CallAfter(failureFunc) return stream = ByteStream() stream.writeByte(pkt.GET_KEY) stream.writeString(vanityKey) sock.send(stream.toString()) data = sock.recv(1024) sock.close() stream = ByteStream(data) success = stream.readByte() == 1 if success: key = stream.readString() wx.CallAfter(successFunc, key) else: wx.CallAfter(failureFunc)
def reflectAddress(self): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto('', self.reflectionServer) privIp, privPort = sock.getsockname() data = sock.recv(128) sock.close() response = ByteStream(data) pubIp = response.readString() pubPort = response.readInt() return (privPort, pubIp, pubPort)
def reflectAddress(self): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto("", self.reflectionServer) privIp, privPort = sock.getsockname() data = sock.recv(128) sock.close() response = ByteStream(data) pubIp = response.readString() pubPort = response.readInt() return (privPort, pubIp, pubPort)
def getNewServerNode(): global currentKey, raidServer # Connect to server... sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.connect(PARSER_SERVER_ADDR) except: prnt("Failed to connect:") prnt(traceback.format_exc()) return # Write data stream = ByteStream() stream.writeByte(pkt.JOIN_RAID) stream.writeByte(VERSION_INT) stream.writeString(currentKey) stream.writeString(net.node.getId()) sock.send(stream.toString()) # Read data data = sock.recv(1024) stream = ByteStream(data) # Process data success = stream.readBoolean() if success: isHost = stream.readBoolean() serverNode = net.node.getId() if isHost: prnt("Raid: Became host") raidServer = RaidServer(sock) raidServer.start() else: prnt("Raid: Didn't become host") serverNode = stream.readString() sock.close() return serverNode return None
def run(self): self.acceptTime = time.time() if self.state != CS_CONNECTED: if not self.loopback: # Get tunnel info privIp = socket.gethostbyname(socket.gethostname()) while True: (privPort, pubIp, pubPort) = self.node.reflectAddress() self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: self.sock.bind(('', privPort)) except: debug("Fuzion: Port %d taken, selecting new port."%(privPort)) continue break debug("Bound at", privPort) self.node.sendTunnelInfo(self.targetId, self.targetPort, privIp, privPort, pubIp, pubPort) else: self.state = CS_CONNECTED if not self.outbound: self.pushToPort() while not self.threadStopped.isSet(): now = time.time() if self.state != CS_CONNECTED: self.updateNotConnected() time.sleep(0.2) continue if self.relay: r = len(self.relayedRead) > 0 w = 1 e = 0 else: r, w, e = select([self.sock], [self.sock], [], 0) if r: try: data = ByteStream(self._recv()) except socket.error as e: if e.errno == 10054: # UDP returns a ECONNRESET for IMCP failures, ignore them pass else: debug("Connection errno=%d"%e.errno) else: if data: packetType = data.readByte() if packetType == P_DATA: self.pendingRecv.append(data.readString()) elif packetType == P_CLOSE: self.closeInternal(ERR_CLOSED_BY_REMOTE) break elif packetType == P_KEEP_ALIVE: pass self.lastPacketReceived = now if not data and data is not None: debug("Empty, but not None packet?") if now - self.lastPacketSent > 5: packet = ByteStream() packet.writeByte(P_KEEP_ALIVE) self._send(packet.toString()) self.lastPacketSent = now if self.lastPacketReceived > 0 and now - self.lastPacketReceived > 20: debug("Timeout, now=%d, lastPacket=%d, diff=%d"%(now, self.lastPacketReceived, now - self.lastPacketReceived)) self.closeInternal(ERR_TIMED_OUT) break time.sleep(0.01)
def run(self): self.acceptTime = time.time() if self.state != CS_CONNECTED: if not self.loopback: # Get tunnel info privIp = socket.gethostbyname(socket.gethostname()) while True: (privPort, pubIp, pubPort) = self.node.reflectAddress() self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: self.sock.bind(("", privPort)) except: debug("Fuzion: Port %d taken, selecting new port." % (privPort)) continue break debug("Bound at", privPort) self.node.sendTunnelInfo(self.targetId, self.targetPort, privIp, privPort, pubIp, pubPort) else: self.state = CS_CONNECTED if not self.outbound: self.pushToPort() while not self.threadStopped.isSet(): now = time.time() if self.state != CS_CONNECTED: self.updateNotConnected() time.sleep(0.2) continue if self.relay: r = len(self.relayedRead) > 0 w = 1 e = 0 else: r, w, e = select([self.sock], [self.sock], [], 0) if r: try: data = ByteStream(self._recv()) except socket.error as e: if e.errno == 10054: # UDP returns a ECONNRESET for IMCP failures, ignore them pass else: debug("Connection errno=%d" % e.errno) else: if data: packetType = data.readByte() if packetType == P_DATA: self.pendingRecv.append(data.readString()) elif packetType == P_CLOSE: self.closeInternal(ERR_CLOSED_BY_REMOTE) break elif packetType == P_KEEP_ALIVE: pass self.lastPacketReceived = now if not data and data is not None: debug("Empty, but not None packet?") if now - self.lastPacketSent > 5: packet = ByteStream() packet.writeByte(P_KEEP_ALIVE) self._send(packet.toString()) self.lastPacketSent = now if self.lastPacketReceived > 0 and now - self.lastPacketReceived > 20: debug( "Timeout, now=%d, lastPacket=%d, diff=%d" % (now, self.lastPacketReceived, now - self.lastPacketReceived) ) self.closeInternal(ERR_TIMED_OUT) break time.sleep(0.01)