def sync_with_node(self, finalize): finalize = json.loads(finalize) sender_server_ip, sender_server_id = finalize["myserveraddr"] if self.node.curr_outgoing_conns < self.node.MAX_OUTGOING_CONN and ( sender_server_ip, sender_server_id) not in self.node.connected_servers: host, port = sender_server_ip.split(":") point = TCP4ClientEndpoint(reactor, str(host), int(port)) xpr = XpeerClientProtocol(None, self.node) self.node.xpeerprotocols.append(xpr) d = connectProtocol(point, xpr) d.addCallback(self.node.gotProtocol) self.node.curr_outgoing_conns += 1 self.node.Portal.update_connections_count( self.node.curr_outgoing_conns, self.node.curr_incoming_conns) elif self.node.curr_outgoing_conns >= self.node.MAX_OUTGOING_CONN: reload_node_dict() print("{} REACHED MAX OUTGOING CONNECTION.".format(self.nodename))
def execute(args: Namespace) -> None: from hathor.crypto.util import decode_address from hathor.stratum import StratumClient from hathor.wallet.exceptions import InvalidAddress address = None if args.address is not None: try: decode_address(args.address) address = args.address except InvalidAddress: print('The given address is invalid') sys.exit(-1) miner = StratumClient(proc_count=args.nproc, address=address) miner.start() point = TCP4ClientEndpoint(reactor, args.host, args.port) connectProtocol(point, miner) reactor.run()
def _handleTubRequest(self, header, targetTubID): """ Proxy to the destination which is responsible for the target TubID. :param bytes header: The first part of the Foolscap negotiation which will need to be passed along to the proxy target. :param bytes targetTubID: The TubID which was requested. """ try: _, (ip, port_number) = self.factory.route_mapping()[targetTubID] except KeyError: raise NegotiationError("unknown TubID %s" % (targetTubID,)) if not ip: raise NegotiationError("TubID not yet available %s" % (targetTubID,)) # Now proxy to ip:port_number proxy(self, TCP4ClientEndpoint(self.factory.reactor, ip, port_number), header)
def main(): obj_hash = hash_broadcast_replicate() # endpoint = TCP4ServerEndpoint(reactor, 5999) # mcx = MyFactory()#peer1 initialization # endpoint.listen(mcx) PEER_LIST = [ "localhost:5999", "localhost:5998", "localhost:5997", "localhost:5996" ] #4 node p2p n/w hosted on local host having different ports for peer in PEER_LIST: endpoint = TCP4ServerEndpoint(reactor, 5999) mcx = MyFactory() # peers initialization endpoint.listen(mcx) host, port = peer.split(":") point = TCP4ClientEndpoint(reactor, host, int(port)) d = connectProtocol(point, MyProtocol(mcx, 2)) d.addCallback(gotProtocol) reactor.run()
def _getEndpoint(self, scheme, host, port): kwargs = {} if self._connectTimeout is not None: kwargs['timeout'] = self._connectTimeout kwargs['bindAddress'] = self._bindAddress if scheme == 'http': return TCP4ClientEndpoint(self._reactor, host, port, **kwargs) elif scheme == 'shttp': return SOCKS5ClientEndpoint(self._reactor, self._sockshost, self._socksport, host, port, **kwargs) elif scheme == 'httpo': return SOCKS5ClientEndpoint(self._reactor, self._sockshost, self._socksport, host, port, **kwargs) elif scheme == 'https': return SSL4ClientEndpoint(self._reactor, host, port, self._wrapContextFactory(host, port), **kwargs) else: raise SchemeNotSupported("Unsupported scheme: %r" % (scheme, ))
def connect_to(self, ip, port): """This method connect to a node *'as a client'* [description] :param ip: id address :type ip: str :param port: port number :type port: int *@Override from ClientProtocol* """ def to_do(protocol): protocol.send_handshake() time.time(60) protocol.send_get_blockchain() connection_point = TCP4ClientEndpoint(reactor, ip, int(port)) d = connectProtocol(connection_point, P2Protocol(self.factory, node_type=2)) d.addCallback(to_do)
def setUpClass(cls): assert (cls.channel) # set by client implementation # if channel.startswith("serial:"): # serial_device=channel[7:] # self.transport=SerialPort(RemoteTestProtocol(), serial_device, reactor) # logging.info("Connecting via {0} ...".format(serial_device)) if cls.channel.startswith("tcp:"): prefix, host, port = cls.channel.split(':') port = int(port) logging.info("Connecting via {0}:{1} ...".format(host, port)) cls.endpoint = TCP4ClientEndpoint(reactor, host, port) cls.protocol = None d = connectProtocol(cls.endpoint, RemoteTestClientProtocol()) d.addCallback(cls.gotProtocol) # start blocking reactor with event loop in separate thread Thread(target=reactor.run, args=(False, )).start()
def create_client(reactor, host, port): """ Start a Nats Protocol and connect it to a NATS endpoint over TCP4 which subscribes to a subject. """ log.info("Start client.") point = TCP4ClientEndpoint(reactor, host, port) nats_protocol = txnats.io.NatsProtocol( verbose=False, on_connect=lambda np: np.sub("happy", "6", on_msg=on_happy_msg)) # Because NatsProtocol implements the Protocol interface, Twisted's # connectProtocol knows how to connected to the endpoint. connecting = connectProtocol(point, nats_protocol) # Log if there is an error making the connection. connecting.addErrback(lambda np: log.info("{p}", p=np)) # Log what is returned by the connectProtocol. connecting.addCallback(lambda np: log.info("{p}", p=np)) return connecting
def i2pTunnelCreated(self): # BOB is now listening for a tunnel. # BOB only listens on TCP4 (for now). clientEndpoint = TCP4ClientEndpoint(self._reactor, self.inhost, self.inport) # Wrap the client Factory. wrappedFactory = BOBClientFactoryWrapper(self._clientFactory, self._bobEndpoint, I2PAddress(self.localDest), self.tunnelNick, self.removeTunnelWhenFinished) wrappedFactory.setDest(self.dest) d = clientEndpoint.connect(wrappedFactory) def checkProto(proto): if proto is None: self.deferred.cancel() return proto d.addCallback(checkProto) # When the Deferred returns an IProtocol, pass it on. d.chainDeferred(self.deferred)
def __listenerStarted(cls, result, reactor, gateAddr, gatePort, callbackPort, callbackProtocolFactory, point, connectD): gateKey = (gateAddr, gatePort) if cls.GATES.has_key(gateKey): cls.logger.info("Already connected.") cls.__controlConnected("Already Connected", callbackPort, cls.GATES[gateKey][1], callbackProtocolFactory, point, connectD) #d = Deferred() else: cls.logger.info("Starting outbound connection to gate.") gatePoint = TCP4ClientEndpoint(reactor, gateAddr, gatePort) gateProtocol = GateProtocol() cls.GATES[gateKey] = (gatePoint, gateProtocol) d = connectProtocol(gatePoint, gateProtocol) d.addCallback(cls.__controlConnected, callbackPort, gateProtocol, callbackProtocolFactory, point, connectD) d.addErrback(cls.__controlConnectFailed, gateKey, connectD)
def handle_hello(self, data): self.remote_nodeid = data['data'] if self.remote_nodeid == self.nodeid: print("Connected to myself.") self.transport.loseConnection() else: host_ip = str(data['remote_ip']) self.lc_ping.start(60) self.send_addr() if host_ip not in self.factory.connection_list: self.factory.peers[self.remote_nodeid] = self self.factory.pair[self.remote_nodeid] = host_ip ip = host_ip.split(':')[0] port = host_ip.split(':')[1] self.factory.connection_list.add(host_ip) self.factory.host_list.add(host_ip) point = TCP4ClientEndpoint(reactor, ip, int(port)) d = connectProtocol(point, MyProtocol(factory)) d.addCallback(gotProtocol)
def test_uninvited_pubrel(host, port): record = [ Frame(send=True, data=Connect(client_id="test_pubrel", flags=ConnectFlags(clean_session=True))), Frame(send=False, data=ConnACK(session_present=False, return_code=0)), Frame(send=True, data=PubREL(packet_identifier=1234)), Frame(send=False, data=PubCOMP(packet_identifier=1234)), Frame(send=True, data=Disconnect()), ConnectionLoss(), ] r = SelectReactor() f = ReplayClientFactory(r, record) e = TCP4ClientEndpoint(r, host, port) e.connect(f) r.run() return Result("uninvited_pubrel", f.success, f.reason, f.client_transcript)
def _request(self, request, method): """ Helper method that sends the given HTTP request. """ # Copy the headers from the request. headers = Headers() for (key, value) in request.headers.iteritems(): headers.addRawHeader(key, value) # If a username and password are given, then add basic authentication. if (self.options.username is not None and self.options.password is not None): auth = "%s:%s" % (self.options.username, self.options.password) auth = auth.encode("base64").strip() headers.addRawHeader('Authorization', 'Basic ' + auth) # Determine if the user has configured a proxy server. url_parts = urlparse.urlparse(request.url) proxy = self.options.proxy.get(url_parts.scheme, None) # Construct an agent to send the request. if proxy is not None: (hostname, port) = proxy.split(":") endpoint = TCP4ClientEndpoint(reactor, hostname, int(port), timeout=self.options.timeout) agent = ProxyAgent(endpoint) else: agent = NewAgent(reactor, self.contextFactory, connectTimeout=self.options.timeout) url = request.url.encode("utf-8") producer = StringProducer(request.message or "") response = yield agent.request(method, url, headers, producer) # Construct a simple response consumer and give it the response body. consumer = StringResponseConsumer() response.deliverBody(consumer) yield consumer.getDeferred() consumer.response = response defer.returnValue(consumer)
def connect(self, reconnecting=False): if self.connecting and not reconnecting: return self.connecting = True end_point = TCP4ClientEndpoint(reactor, self.host, self.port, 10) d = end_point.connect(Factory.forProtocol(GraphiteProtocol)) def success(connection): self.connecting = False log.info('Connected to {replica}', replica=self) self.connection = connection def failed(error): log.error('Connect to {replica} failed: {error}', replica=self, error=error) reactor.callLater(10, self.connect, True) d.addCallbacks(success, failed)
def download(self, name, url, path): # keep track of current download self.current_download_path = path self.current_download_url = url # initialize the progress bar mirror_url = url.format(self.common.settings['mirror']) self.progressbar.set_fraction(0) self.progressbar.set_text(_('Downloading {0}').format(name)) self.progressbar.show() self.refresh_gtk() if self.common.settings['update_over_tor']: print _('Updating over Tor') from twisted.internet.endpoints import TCP4ClientEndpoint from txsocksx.http import SOCKS5Agent torEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050) # default mirror gets certificate pinning, only for requests that use the mirror if self.common.settings['mirror'] == self.common.default_mirror and '{0}' in url: agent = SOCKS5Agent(reactor, VerifyTorProjectCert(self.common.paths['torproject_pem']), proxyEndpoint=torEndpoint) else: agent = SOCKS5Agent(reactor, proxyEndpoint=torEndpoint) else: if self.common.settings['mirror'] == self.common.default_mirror and '{0}' in url: agent = Agent(reactor, VerifyTorProjectCert(self.common.paths['torproject_pem'])) else: agent = Agent(reactor) # actually, agent needs to follow redirect agent = RedirectAgent(agent) # start the request d = agent.request('GET', mirror_url, Headers({'User-Agent': ['torbrowser-launcher']}), None) self.file_download = open(path, 'w') d.addCallback(self.response_received).addErrback(self.download_error) if not reactor.running: reactor.run()
def _parseClient(self, host=None, port=None, socksHostname=None, socksPort=None, socksUsername=None, socksPassword=None): if port is not None: port = int(port) if socksHostname is None: socksHostname = '127.0.0.1' if socksPort is not None: socksPort = int(socksPort) ep = None if socksPort is not None: ep = TCP4ClientEndpoint(reactor, socksHostname, socksPort) return TorClientEndpoint( host, port, socks_endpoint=ep, socks_username=socksUsername, socks_password=socksPassword, )
def bootstrap_peer(self, host, port): """ This will ask for send_get_peers() and if it's successful it will add it to the available peers. :param host: host to connect to :param port: port to connect to """ self.logger.info('Bootstrapping peer {0}:{1}'.format(host, str(port))) client = TCP4ClientEndpoint(self.reactor, host, port, self.timeout) d = client.connect(self.factory) # in case of connection ok, add the callbacks for get_peers and call send_get_peers def connection_ok(protocol): protocol.d = defer.Deferred() def get_peers_ok(_): self.logger.info('Successfully got peers from {0}:{1}'.format( host, port)) protocol.d.addCallback(get_peers_ok) def get_peers_error(reason): error_message = reason.getErrorMessage() self.logger.error( 'Error when getting peers from {0}:{1}: {2}'.format( host, port, error_message)) protocol.close_connection() protocol.d.addErrback(get_peers_error) protocol.send_get_peers() d.addCallback(connection_ok) # in case of connection error just show in debug def connection_error(reason): self.logger.warning( 'Error while connecting to {0}:{1}: {2}'.format( host, port, reason.getErrorMessage())) d.addErrback(connection_error)
def get_ballot_information(prev_result): destination = TCP4ClientEndpoint(reactor, self.twisted_ballotregulator_ip, self.twisted_ballotregulator_port) requestsearchuser_deferred = connectProtocol(destination, AMP()) def requestsearchuser_connected(ampProto): return ampProto.callRemote(OnlineBallotRegulator_SearchBallotsAvailableForAllBallots) requestsearchuser_deferred.addCallback(requestsearchuser_connected) def done(result): ''' Returns the record ascociated with the ballot_id :param result: :return: ''' unpickled_result = pickle.loads(result['ok']) # Transform the list results into a dictionary. record_list = [] for record in unpickled_result: mapper = {} mapper['ballot_id'] = record[0] mapper['ballot_name'] = record[1] mapper['ballot_address'] = record[2] mapper['timestamp'] = record[3] # Append each row's dictionary to a list record_list.append(mapper) # Check the available ballots for *our* ballot for record in record_list: if record['ballot_id'] == ballot_id: return record # If we reach here we havent found *our* ballot in the list available raise BallotNotAvailable(ballot_id) def done_errback(failure): raise failure.raiseException() parsed_results = requestsearchuser_deferred.addCallback(done).addErrback(done_errback) return parsed_results
def __init__( self, host: str, port: int, maximum_buffer: int = 1000, level=logging.NOTSET, _reactor=None, ): super().__init__(level=level) self.host = host self.port = port self.maximum_buffer = maximum_buffer self._buffer = deque() # type: Deque[logging.LogRecord] self._connection_waiter = None # type: Optional[Deferred] self._producer = None # type: Optional[LogProducer] # Connect without DNS lookups if it's a direct IP. if _reactor is None: from twisted.internet import reactor _reactor = reactor try: ip = ip_address(self.host) if isinstance(ip, IPv4Address): endpoint = TCP4ClientEndpoint( _reactor, self.host, self.port) # type: IStreamClientEndpoint elif isinstance(ip, IPv6Address): endpoint = TCP6ClientEndpoint(_reactor, self.host, self.port) else: raise ValueError("Unknown IP address provided: %s" % (self.host, )) except ValueError: endpoint = HostnameEndpoint(_reactor, self.host, self.port) factory = Factory.forProtocol(Protocol) self._service = ClientService(endpoint, factory, clock=_reactor) self._service.startService() self._stopping = False self._connect()
def setUp(self): """ Create a L{MemCachePool}. """ TestCase.setUp(self) self.reactor = StubReactor() self.pool = MemCachePool(TCP4ClientEndpoint(self.reactor, MC_ADDRESS.host, MC_ADDRESS.port), maxClients=5, reactor=self.reactor) realClientFactory = self.pool.clientFactory self.clientFactories = [] def capturingClientFactory(*a, **k): cf = realClientFactory(*a, **k) self.clientFactories.append(cf) return cf self.pool.clientFactory = capturingClientFactory
def test_reserved_packet_0(host, port): record = [ Frame(send=True, data=Connect(client_id="test_reserved0", flags=ConnectFlags(clean_session=True))), Frame(send=False, data=ConnACK(session_present=False, return_code=0)), Frame( send=True, # v pkt 0 right here data=b"\x00\x13\x00\x04MQTT\x04\x02\x00\x02\x00\x07test123"), ConnectionLoss() ] r = SelectReactor() f = ReplayClientFactory(r, record) e = TCP4ClientEndpoint(r, host, port) e.connect(f) r.run() return Result("reserved_pkt0", f.success, f.reason, f.client_transcript)
def start(self) -> None: # Connect without DNS lookups if it's a direct IP. try: ip = ip_address(self.host) if isinstance(ip, IPv4Address): endpoint = TCP4ClientEndpoint( self.hs.get_reactor(), self.host, self.port ) elif isinstance(ip, IPv6Address): endpoint = TCP6ClientEndpoint( self.hs.get_reactor(), self.host, self.port ) except ValueError: endpoint = HostnameEndpoint(self.hs.get_reactor(), self.host, self.port) factory = Factory.forProtocol(Protocol) self._service = ClientService(endpoint, factory, clock=self.hs.get_reactor()) self._service.startService() self._connect()
def requestRegisterNewBallot(ballot_name, ballot_options_array, ballot_end_date): ballot_options_array_pickled = pickle.dumps(ballot_options_array) destination_deferred = yield TCP4ClientEndpoint(reactor, ballotregulator_ip, ballotregulator_port) connection_deferred = yield connectProtocol(destination_deferred, AMP()) result_deferred = yield connection_deferred.callRemote( OnlineBallotRegulator_RegisterBallotId, ballot_name=ballot_name, ballot_options_array_pickled=ballot_options_array_pickled, ballot_end_date=ballot_end_date) def format_results(result): return result['ballot_address'] # Inlinecallback return value. returnValue(format_results(result_deferred))
def start_tor(self): """ This function executes the workflow of starting the hidden service and returning its hostname """ self.info_callback("Attempting to start onion service on port: {} " "...".format(self.port)) if str(self.tor_control_host).startswith('unix:'): control_endpoint = UNIXClientEndpoint(reactor, self.tor_control_host[5:]) else: control_endpoint = TCP4ClientEndpoint(reactor, self.tor_control_host, self.tor_control_port) d = txtorcon.connect(reactor, control_endpoint) d.addCallback(self.create_onion_ep) d.addErrback(self.setup_failed) # TODO: add errbacks to the next two calls in # the chain: d.addCallback(self.onion_listen) d.addCallback(self.print_host)
def handle_peers(self, data): log("Recieved peers from node " + str(self.remotePeer) + " - Total : " + str(len(data['peers']))) # print data['peers'] for node, info in data['peers'].iteritems(): log("Trying to connect to node - " + node ) if(node == self.factory.nodeId): log("That's me in the recieved peer list - IGNORING", "warning") continue if(node in self.factory.peers): log("Already connected to this node - IGNORING", "warning") continue if info['type'] != "SERVER": log("That node is a "+ data['peers'][node]['type'] +" - IGNORING", "warning"); continue location = info['location'] host, port = location.split(":") point = TCP4ClientEndpoint(reactor, host, int(port)) d = connectProtocol(point, MyPeerServer(self.factory)) d.addCallback(gotProtocol)
def main(reactor, duration): concurrency = 50 factory = ServerFactory() factory.protocol = CloseConnection interface = '127.0.0.%d' % (int(time()) % 254 + 1,) port = reactor.listenTCP(0, factory, interface=interface) client = Client( reactor, TCP4ClientEndpoint( reactor, port.getHost().host, port.getHost().port, bindAddress=(interface, 0))) d = client.run(concurrency, duration) def cleanup(passthrough): d = port.stopListening() d.addCallback(lambda ignored: passthrough) return d d.addCallback(cleanup) return d
def __init__(self, host, port, socks_endpoint=None, socks_username=None, socks_password=None, tls=False, **kw): if host is None or port is None: raise ValueError('host and port must be specified') self.host = host self.port = int(port) self.socks_endpoint = socks_endpoint self.socks_username = socks_username self.socks_password = socks_password self.tls = tls if self.tls and not _HAVE_TLS: raise ValueError( "'tls=True' but we don't have TLS support" ) # backwards-compatibility: you used to specify a TCP SOCKS # endpoint via socks_hostname= and socks_port= kwargs if self.socks_endpoint is None: try: self.socks_endpoint = TCP4ClientEndpoint( reactor, kw['socks_hostname'], kw['socks_port'], ) # XXX should deprecation-warn here except KeyError: pass # this is a separate "if" from above in case socks_endpoint # was None but the user specified the (old) # socks_hostname/socks_port (in which case we do NOT want # guessing_enabled if self.socks_endpoint is None: self._socks_port_iter = iter(self.socks_ports_to_try) self._socks_guessing_enabled = True else: self._socks_guessing_enabled = False
def deferExpectedMessages(self, messages, timeout=0.5): """ Connect a StateMachineClient to the chat server on localhost, port 6667, and allow it to run for up to ``timeout`` seconds. ``messages`` is a list of ``Tx`` or ``Rx`` instances representing the messages to send and the messages expected from the server, in order. """ endpoint = TCP4ClientEndpoint(reactor, 'localhost', 6667) d = endpoint.connect(SimpleFactory(StateMachineClient, messages, self)) # This part gets a little hairy. In order to shut down the protocol's # connection, reactor.callLater needs to access the protocol, so I # have to do it in a callback. Then I also need to access the # delayed call object, so I need to set up a callback inside another # applied to the same Deferred. # This Deferred will fire when the client successfully connects to # the server, so the timeout will begin from that point. @d.addCallback def cbSetTimeout(proto): delayed = reactor.callLater(timeout, proto.transport.loseConnection) # I need to make sure the delayed timeout call gets cancelled # so it doesn't call loseConnection twice (it might be called from # within the protocol). @d.addBoth def cbCancelTimeout(passthrough): if delayed.active(): delayed.cancel() return passthrough # Returning the state machine's Deferred from this callback # implicitly "chains" it onto the deferred from endpoint.connect... return proto.stateMachine.deferred # ...so I can return the Deferred from endpoint.connect directly, and # trial will wait until it fires to record success or failure. return d
def _getAgent(self, request, timeout, delay_time): proxy = request.meta.get('proxy') if proxy: scheme = _parsed(request.url)[0] proxyHost, proxyPort, _, _ = _parsed(proxy) creds = request.headers.getRawHeaders('Proxy-Authorization', None) creds_02 = creds[0].encode(self.auth_encoding) if isinstance( creds, list) else creds proxyPort = int(proxyPort) if isinstance(proxyPort, bytes) else proxyPort proxyHost = proxyHost.decode(self.auth_encoding) logger.warning(*self.lfm.crawled( "Spider", self.spider.name, '正在通过代理<%s>下载,延迟了%6.3f,时间为:' % (proxy, delay_time), { 'request': request, 'time': time.clock() })) logger.info( *self.lfm.crawled('Spider', self.spider.name, '使用代理:%s:%s' % (proxyHost, str(proxyPort)), request)) if scheme == b'https': proxyConfig = (proxyHost, proxyPort, creds_02) request.headers.removeHeader('Proxy-Authorization') return TunnelingAgent(reactor, proxyConfig, contextFactory=self._contextFactoryProxy, connectTimeout=timeout, bindAddress=self._bindAddress, pool=self._pool) else: endpoint = TCP4ClientEndpoint(reactor, proxyHost, proxyPort) return ProxyAgent(endpoint=endpoint, pool=self._pool) else: return self._Agent(reactor, contextFactory=self._contextFactory, connectTimeout=timeout, bindAddress=self._bindAddress, pool=self._pool)
def establish(self, timeout=None): """ Tries to establish a client connection. Args: timeout: the timeout in seconds Returns: True if the connection was established, and False if it wasn't. """ Connection.establish(self, timeout) # Create and configure the endpoint if (not self._useSSL): endpoint = TCP4ClientEndpoint(reactor, self.__host, self._port, timeout, None) else: keyPath = self._certificatesDirectory + "/" + "server.key" certificatePath = self._certificatesDirectory + "/" + "server.crt" try: endpoint = SSL4ClientEndpoint( reactor, self.__host, self._port, ssl.DefaultOpenSSLContextFactory(keyPath, certificatePath), timeout) except Exception: raise ConnectionException( "The key, the certificate or both were not found") # Establish the connection self._deferred = endpoint.connect(self._factory) self.__working = True def _handleError(error): self.__working = False self._setError(Connection._prettyPrintTwistedError(error)) def _handleConnection(error): self.__working = False self._deferred.addCallback(_handleConnection) self._deferred.addErrback(_handleError) # Wait until it's ready while (self.__working): sleep(0.1) return self._error == None