def accepter(): while True: h = self.waitfor('Waiting for connections on %s:%s' % (self.lhost, self.lport)) while True: try: if self.type == socket.SOCK_STREAM: sock, rhost = listen_sock.accept() else: data, rhost = listen_sock.recvfrom(4096) listen_sock.connect(rhost) sock = listen_sock self.unrecv(data) sock.settimeout(self.timeout) break except socket.error as e: if e.errno == errno.EINTR: continue h.failure() self.exception("Socket failure while waiting for connection") sock = None return self.rhost, self.rport = rhost[:2] r = remote(self.rhost, self.rport, sock = sock) h.success('Got connection from %s on port %d' % (self.rhost, self.rport)) if callback: if not blocking: t = context.Thread(target = callback, args = (r,)) t.daemon = True t.start() else: callback(r) else: self.connections.put(r)
def _connect(self, fam, typ): sock = None timeout = self.timeout with self.waitfor('Opening connection to %s on port %d' % (self.rhost, self.rport)) as h: for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ, 0, socket.AI_PASSIVE): self.family, self.type, self.proto, _canonname, sockaddr = res if self.type not in [socket.SOCK_STREAM, socket.SOCK_DGRAM]: continue h.status("Trying %s" % sockaddr[0]) sock = socket.socket(self.family, self.type, self.proto) if timeout is not None and timeout <= 0: sock.setblocking(0) else: sock.setblocking(1) sock.settimeout(timeout) try: sock.connect(sockaddr) return sock except socks.ProxyError: raise except socket.error: pass self.error("Could not connect to %s on port %d" % (self.rhost, self.rport))