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 run(self): global PUBLIC_IP_UPDATE_LAST self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind(('', REFLECTION_SERVER_PORT)) debug("Reflection Server listening on %d"%REFLECTION_SERVER_PORT) refreshPublicIP() PUBLIC_IP_UPDATE_LAST = time.time() while True: r, w, e = select([self.sock], [self.sock], [], 0) if r: data, addr = self.sock.recvfrom(1024) ip = addr[0] port = addr[1] if ip == SERVER_GATEWAY_IP: ip = SERVER_PUBLIC_IP out = ByteStream() out.writeString(ip) out.writeInt(port) self.sock.sendto(out.toString(), addr) now = time.time() if now - PUBLIC_IP_UPDATE_LAST > PUBLIC_IP_UPDATE_INTERVAL: PUBLIC_IP_UPDATE_LAST = now threading.Thread(target=refreshPublicIP).start() time.sleep(0.01) self.sock.close()
def send(self, data): if isinstance(data, ByteStream): data = data.toString() packet = ByteStream() packet.writeByte(P_DATA) packet.writeString(data) data = packet.toString() self._send(data) self.lastPacketSent = time.time()
def closeInternal(self, reason): with self.closedLock: if self.closed: return if reason == ERR_CLOSED_BY_SELF: packet = ByteStream() packet.writeByte(P_CLOSE) self._send(packet.toString()) self.threadStopped.set() self.node.connectionDied(self) self.closed = True self.closedReason = reason if not self.loopback and not self.relay: self.sock.close()
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 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 updateNotConnected(self): if self.state == CS_TUNNELING: debug("Tunneling") self.tunnelTicks += 1 # 0.2 x 20 = 4s for tunnel establishment if self.tunnelTicks == 21: # Switch to relay for now :( debug("Fallback to relay.") self.state = CS_CONNECTED self.relay = True if not self.outbound: self.pushToPort() return mySyn = ByteStream() mySyn.writeByte(P_TUNNEL_SYN) mySyn.writeString(self.node.id) mySyn.writeString(self.targetPort) mySyn = mySyn.toString() myAck = ByteStream() myAck.writeByte(P_TUNNEL_ACK) myAck.writeString(self.node.id) myAck.writeString(self.targetPort) myAck = myAck.toString() theirSyn = ByteStream() theirSyn.writeByte(P_TUNNEL_SYN) theirSyn.writeString(self.targetId) theirSyn.writeString(self.targetPort) theirSyn = theirSyn.toString() theirAck = ByteStream() theirAck.writeByte(P_TUNNEL_ACK) theirAck.writeString(self.targetId) theirAck.writeString(self.targetPort) theirAck = theirAck.toString() while True: r, w, e = select([self.sock], [self.sock], [], 0) if r: try: data, addr = self.sock.recvfrom(4096) except socket.error as e: if e.errno == 10054: # UDP returns a ECONNRESET for IMCP failures, ignore them data = None if data == theirSyn and not self.tunnelGotSyn: self.tunnelGotSyn = True debug("Got syn for tunnel.") elif data == theirAck and self.tunnelGotSyn: self.state = CS_CONNECTED # Lock in the address self.addr = addr self.sock.connect(addr) if not self.outbound: self.pushToPort() debug("Got ack. Tunnel established.") break else: break packetToSend = mySyn if self.tunnelGotSyn: self.sock.sendto(myAck, self.tunnelPrivAddr) self.sock.sendto(myAck, self.tunnelPubAddr) debug("Sending ack...") self.sock.sendto(mySyn, self.tunnelPrivAddr) self.sock.sendto(mySyn, self.tunnelPubAddr) debug("Sending syn...") if self.state != CS_CONNECTED and time.time() - self.acceptTime > 5: debug("Connect timed out. current=%d, accept=%d"%(time.time(), self.acceptTime)) self.state = CS_FAILED_TIMED_OUT self.threadStopped.set() self.node.connectionDied(self)
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 updateNotConnected(self): if self.state == CS_TUNNELING: debug("Tunneling") self.tunnelTicks += 1 # 0.2 x 20 = 4s for tunnel establishment if self.tunnelTicks == 21: # Switch to relay for now :( debug("Fallback to relay.") self.state = CS_CONNECTED self.relay = True if not self.outbound: self.pushToPort() return mySyn = ByteStream() mySyn.writeByte(P_TUNNEL_SYN) mySyn.writeString(self.node.id) mySyn.writeString(self.targetPort) mySyn = mySyn.toString() myAck = ByteStream() myAck.writeByte(P_TUNNEL_ACK) myAck.writeString(self.node.id) myAck.writeString(self.targetPort) myAck = myAck.toString() theirSyn = ByteStream() theirSyn.writeByte(P_TUNNEL_SYN) theirSyn.writeString(self.targetId) theirSyn.writeString(self.targetPort) theirSyn = theirSyn.toString() theirAck = ByteStream() theirAck.writeByte(P_TUNNEL_ACK) theirAck.writeString(self.targetId) theirAck.writeString(self.targetPort) theirAck = theirAck.toString() while True: r, w, e = select([self.sock], [self.sock], [], 0) if r: try: data, addr = self.sock.recvfrom(4096) except socket.error as e: if e.errno == 10054: # UDP returns a ECONNRESET for IMCP failures, ignore them data = None if data == theirSyn and not self.tunnelGotSyn: self.tunnelGotSyn = True debug("Got syn for tunnel.") elif data == theirAck and self.tunnelGotSyn: self.state = CS_CONNECTED # Lock in the address self.addr = addr self.sock.connect(addr) if not self.outbound: self.pushToPort() debug("Got ack. Tunnel established.") break else: break packetToSend = mySyn if self.tunnelGotSyn: self.sock.sendto(myAck, self.tunnelPrivAddr) self.sock.sendto(myAck, self.tunnelPubAddr) debug("Sending ack...") self.sock.sendto(mySyn, self.tunnelPrivAddr) self.sock.sendto(mySyn, self.tunnelPubAddr) debug("Sending syn...") if self.state != CS_CONNECTED and time.time() - self.acceptTime > 5: debug("Connect timed out. current=%d, accept=%d" % (time.time(), self.acceptTime)) self.state = CS_FAILED_TIMED_OUT self.threadStopped.set() self.node.connectionDied(self)
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)