def connect(self, targetId, targetPort): self.waitForNS() # Prevent connecting if there is already a outbound connection. if self.getConnection(targetId, targetPort, True): debug("Duplicate connection to %s:%s found." % (targetId, targetPort)) # Yes, this may throw an error in the app, but they shouldn't # be creating duplicate connections anyway, so it doesn't matter. return None debug("Connecting to %s" % targetId) out = ByteStream() out.writeByte(P_CONNECT_REQUEST) out.writeString(targetId) out.writeString(targetPort) self.send(out) conn = NodeConnection(self, targetId, targetPort, True) conn.requestTime = time.time() self.connections.append(conn) while True: # Wait till connected. if conn.state == CS_CONNECTED or conn.state >= CS_FAILED_BASE: if conn.state == CS_FAILED_NO_NODE: debug("Failed to connect: no such node") elif conn.state == CS_FAILED_REJECTED: debug("Failed to connect: rejected") break time.sleep(0.1) return conn
def sendConnectRequest(self, sourceId, targetPort): """ Sends the connection request from sourceId to this node. """ out = ByteStream() out.writeByte(P_CONNECT_REQUEST) out.writeString(sourceId) out.writeString(targetPort) self.send(out)
def connectToNodeServer(self): debug("Connecting to node server @ %s" % repr(self.nodeServer)) while True: self.updateNodeState(NS_CONNECTING) if self.sock != None: self.sock.close() self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sockQueue = net_helpers.SocketQueue(self.sock) self.sockPacker = net_helpers.Packer() try: self.sock.connect(self.nodeServer) break except: debug("Connect failed, trying again in 5s...") self.updateNodeState(NS_FAILED) time.sleep(5) self.updateNodeState(NS_CONNECTED) debug("Connected.") # Reset last packet counter so we don't time out. self.lastPacketReceived = time.time() # send register packet debug("Registering with node server...") b = ByteStream() b.writeByte(P_REGISTER) self.send(b)
def connectToNodeServer(self): debug("Connecting to node server @ %s"%repr(self.nodeServer)) while True: self.updateNodeState(NS_CONNECTING) if self.sock != None: self.sock.close() self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sockQueue = net_helpers.SocketQueue(self.sock) self.sockPacker = net_helpers.Packer() try: self.sock.connect(self.nodeServer) break except: debug("Connect failed, trying again in 5s...") self.updateNodeState(NS_FAILED) time.sleep(5) self.updateNodeState(NS_CONNECTED) debug("Connected.") # Reset last packet counter so we don't time out. self.lastPacketReceived = time.time() # send register packet debug("Registering with node server...") b = ByteStream() b.writeByte(P_REGISTER) self.send(b)
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 connect(self, targetId, targetPort): self.waitForNS() # Prevent connecting if there is already a outbound connection. if self.getConnection(targetId, targetPort, True): debug("Duplicate connection to %s:%s found."%(targetId, targetPort)) # Yes, this may throw an error in the app, but they shouldn't # be creating duplicate connections anyway, so it doesn't matter. return None debug("Connecting to %s"%targetId) out = ByteStream() out.writeByte(P_CONNECT_REQUEST) out.writeString(targetId) out.writeString(targetPort) self.send(out) conn = NodeConnection(self, targetId, targetPort, True) conn.requestTime = time.time() self.connections.append(conn) while True: # Wait till connected. if conn.state == CS_CONNECTED or conn.state >= CS_FAILED_BASE: if conn.state == CS_FAILED_NO_NODE: debug("Failed to connect: no such node") elif conn.state == CS_FAILED_REJECTED: debug("Failed to connect: rejected") break time.sleep(0.1) return conn
def sendConnectSuccess(self, targetId, targetPort): """ Sends the connection success packet back to node. """ out = ByteStream() out.writeByte(P_CONNECT_RESPONSE) out.writeString(targetId) out.writeString(targetPort) out.writeByte(ERR_NO_ERROR) self.send(out)
def sendRelayPacket(self, targetId, targetPort, data): """ Sends tunnel info to a node. """ out = ByteStream() out.writeByte(P_RELAY_PACKET) out.writeString(targetId) out.writeString(targetPort) out.writeString(data) self.send(out)
def sendConnectFailed(self, targetId, targetPort, errorCode): """ Sends the connection failed packet back to node. """ out = ByteStream() out.writeByte(P_CONNECT_RESPONSE) out.writeString(targetId) out.writeString(targetPort) out.writeByte(errorCode) self.send(out)
def sendTunnelInfo(self, targetId, targetPort, privIp, privPort, pubIp, pubPort): out = ByteStream() out.writeByte(P_TUNNEL_INFO) out.writeString(targetId) out.writeString(targetPort) out.writeString(privIp) out.writeInt(privPort) out.writeString(pubIp) out.writeInt(pubPort) self.send(out)
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 sendRelay(self, targetId, targetPort, data, fromOutbound): # Loopback if self.id == targetId: # Send it to the opposite connection. conn = self.getConnection(targetId, targetPort, not fromOutbound) if conn: conn.injectRelayRead(data) else: debug("sendRelay: Loopback endpoint not found.") return if self.nodeState != NS_REGISTERED: debug("sendRelay: Node not registered. State is %d" % self.nodeState) return out = ByteStream() out.writeByte(P_RELAY_PACKET) out.writeString(targetId) out.writeString(targetPort) out.writeString(data) self.send(out)
def sendRelay(self, targetId, targetPort, data, fromOutbound): # Loopback if self.id == targetId: # Send it to the opposite connection. conn = self.getConnection(targetId, targetPort, not fromOutbound) if conn: conn.injectRelayRead(data) else: debug("sendRelay: Loopback endpoint not found.") return if self.nodeState != NS_REGISTERED: debug("sendRelay: Node not registered. State is %d"%self.nodeState) return out = ByteStream() out.writeByte(P_RELAY_PACKET) out.writeString(targetId) out.writeString(targetPort) out.writeString(data) self.send(out)
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 handlePacket(self, data): packet = data.readByte() if packet == P_REGISTER: self.id = self.ns.pushNode(self) out = ByteStream() out.writeByte(P_REGISTER) out.writeString(self.id) self.send(out) elif packet == P_CONNECT_REQUEST: # from source node targetId = data.readString() targetPort = data.readString() debug("%s: requesting connect to %s"%(self.id, targetId)) self.ns.sendConnectRequest(self, targetId, targetPort) elif packet == P_CONNECT_RESPONSE: # from target nodef targetId = data.readString() targetPort = data.readString() accepted = data.readBoolean() debug("%s: response for %s=%s"%(self.id, targetId, repr(accepted))) if accepted: self.ns.sendConnectSuccess(self, targetId, targetPort) else: self.ns.sendConnectRejected(self, targetId, targetPort) elif packet == P_TUNNEL_INFO: targetId = data.readString() targetPort = data.readString() privIp = data.readString() privPort = data.readInt() pubIp = data.readString() pubPort = data.readInt() debug("%s: sending tunnel info to %s"%(self.id, targetId)) self.ns.sendTunnelInfo(self, targetId, targetPort, privIp, privPort, pubIp, pubPort) elif packet == P_RELAY_PACKET: targetId = data.readString() targetPort = data.readString() data = data.readString() self.ns.sendRelayPacket(self, targetId, targetPort, data)
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 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 gotDataFromNodeServer(self, b): p = b.readByte() if p == P_REGISTER: with self.stateLock: self.id = b.readString() debug("Registered with node server, id is", self.id) self.updateNodeState(NS_REGISTERED) elif p == P_CONNECT_REQUEST: targetId = b.readString() targetPort = b.readString() port = self.getPort(targetPort) if port == None: debug("Rejecting unsilicited connection request from %s at port %s"%(targetId, targetPort)) out = ByteStream() out.writeByte(P_CONNECT_RESPONSE) out.writeString(targetId) out.writeString(targetPort) out.writeBoolean(False) self.send(out) else: debug("Connection request from %s at port %s"%(targetId, targetPort)) out = ByteStream() out.writeByte(P_CONNECT_RESPONSE) out.writeString(targetId) out.writeString(targetPort) out.writeBoolean(True) self.send(out) conn = NodeConnection(self, targetId, targetPort, False) conn.state = CS_ACCEPTED conn.start() self.connections.append(conn) elif p == P_CONNECT_RESPONSE: targetId = b.readString() targetPort = b.readString() errorCode = b.readByte() state = CS_ACCEPTED if errorCode == ERR_NO_NODE: state = CS_FAILED_NO_NODE elif errorCode == ERR_REJECTED: state = CS_FAILED_REJECTED self.updateConnectionState(targetId, targetPort, state, True) if state == CS_ACCEPTED: self.startConnection(targetId, targetPort, True) else: self.removeConnection(targetId, targetPort, True) elif p == P_TUNNEL_INFO: targetId = b.readString() targetPort = b.readString() privIp = b.readString() privPort = b.readInt() pubIp = b.readString() pubPort = b.readInt() conn = self.getConnection(targetId, targetPort) if conn != None: conn.gotTunnelInfo(privIp, privPort, pubIp, pubPort) else: debug("Got connection info for non existant connection to %s:%s"%(targetId, targetPort)) elif p == P_RELAY_PACKET: targetId = b.readString() targetPort = b.readString() data = b.readString() conn = self.getConnection(targetId, targetPort) if conn: conn.injectRelayRead(data)
def gotDataFromNodeServer(self, b): p = b.readByte() if p == P_REGISTER: with self.stateLock: self.id = b.readString() debug("Registered with node server, id is", self.id) self.updateNodeState(NS_REGISTERED) elif p == P_CONNECT_REQUEST: targetId = b.readString() targetPort = b.readString() port = self.getPort(targetPort) if port == None: debug("Rejecting unsilicited connection request from %s at port %s" % (targetId, targetPort)) out = ByteStream() out.writeByte(P_CONNECT_RESPONSE) out.writeString(targetId) out.writeString(targetPort) out.writeBoolean(False) self.send(out) else: debug("Connection request from %s at port %s" % (targetId, targetPort)) out = ByteStream() out.writeByte(P_CONNECT_RESPONSE) out.writeString(targetId) out.writeString(targetPort) out.writeBoolean(True) self.send(out) conn = NodeConnection(self, targetId, targetPort, False) conn.state = CS_ACCEPTED conn.start() self.connections.append(conn) elif p == P_CONNECT_RESPONSE: targetId = b.readString() targetPort = b.readString() errorCode = b.readByte() state = CS_ACCEPTED if errorCode == ERR_NO_NODE: state = CS_FAILED_NO_NODE elif errorCode == ERR_REJECTED: state = CS_FAILED_REJECTED self.updateConnectionState(targetId, targetPort, state, True) if state == CS_ACCEPTED: self.startConnection(targetId, targetPort, True) else: self.removeConnection(targetId, targetPort, True) elif p == P_TUNNEL_INFO: targetId = b.readString() targetPort = b.readString() privIp = b.readString() privPort = b.readInt() pubIp = b.readString() pubPort = b.readInt() conn = self.getConnection(targetId, targetPort) if conn != None: conn.gotTunnelInfo(privIp, privPort, pubIp, pubPort) else: debug("Got connection info for non existant connection to %s:%s" % (targetId, targetPort)) elif p == P_RELAY_PACKET: targetId = b.readString() targetPort = b.readString() data = b.readString() conn = self.getConnection(targetId, targetPort) if conn: conn.injectRelayRead(data)