Exemple #1
0
    def build_agent(self, proxy, headers):
        """Build an agent for this request
        """
        fragments = common.parse_proxy(proxy)
        pool = self.build_pool()
        if fragments.host:
            # add proxy authentication header
            auth = base64.b64encode("%s:%s" %
                                    (fragments.username, fragments.password))
            headers['Proxy-Authorization'] = ["Basic " + auth.strip()]
            # generate the agent
            endpoint = endpoints.TCP4ClientEndpoint(
                reactor,
                fragments.host,
                int(fragments.port),
                timeout=self.settings.timeout)
            agent = client.ProxyAgent(endpoint, reactor=reactor, pool=pool)
        else:
            agent = client.Agent(reactor,
                                 connectTimeout=self.settings.timeout,
                                 pool=pool)

        agent = client.ContentDecoderAgent(agent,
                                           [('gzip', client.GzipDecoder)])
        # XXX if use same cookie for all then works...
        # cookies usually empty
        if proxy in self.cookiejars:
            cj = self.cookiejars[proxy]
        else:
            cj = cookielib.CookieJar()
            self.cookiejars[proxy] = cj
        agent = client.CookieAgent(agent, cj)
        return agent
Exemple #2
0
 def __call__(self, req):
     serviceUrl = yield self._node_handle._master_proxy.lookupService(self._name)
     
     protocol, rest = serviceUrl.split('://', 1)
     host, port_str = rest.rsplit(':', 1)
     port = int(port_str)
     
     assert protocol == 'rosrpc'
     
     conn = yield endpoints.TCP4ClientEndpoint(reactor, host, port).connect(util.AutoServerFactory(lambda addr: tcpros.Protocol()))
     try:
         conn.sendString(tcpros.serialize_dict(dict(
             callerid=self._node_handle._name,
             service=self._name,
             md5sum=self._type._md5sum,
             type=self._type._type,
         )))
         
         header = tcpros.deserialize_dict((yield conn.receiveString()))
         # XXX do something with header
         
         # request could be sent before header is received to reduce latency...
         x = StringIO.StringIO()
         self._type._request_class.serialize(req, x)
         data = x.getvalue()
         conn.sendString(data)
         
         result = ord((yield conn.receiveByte()))
         data = yield conn.receiveString()
         if result: # success
             defer.returnValue(self._type._response_class().deserialize(data))
         else:
             raise ServiceError(data)
     finally:
         conn.transport.loseConnection()
Exemple #3
0
	def run(self):
		if self.proxy:
			end_point = endpoints.TCP4ClientEndpoint(reactor, self.proxy[0], self.proxy[1])
			self._http_agent = sock.SOCKS5Agent(reactor, proxyEndpoint=end_point)
		else:
			self._http_agent = Agent(reactor, connectTimeout = 500)
		return self._poll()
Exemple #4
0
    def setUp(self):
        self.remote_helper = None
        self.job_counter = 1
        self.hook_counter = 1
        self.temp_dir_counter = 1

        self.pendingJobs = {}
        self.pendingProcs = {}
        self.pendingLists = {}
        self.completedProcs = {}  # only holds a jobid -> name map

        # assign temporary ports starting from 32768
        # FIXME: should be configurable!
        self.temp_port = 32768

        self.absWorkdir = None

        self.tearingDown = False
        self.tearDownDeferred = None

        factory = SSHClientFactory(self.runner)
        endpoint = endpoints.TCP4ClientEndpoint(reactor, self.host.getHost(),
                                                self.host.getPort())
        d = endpoint.connect(factory)
        d.addCallback(self.startConnection)

        self.setupDeferred = defer.Deferred()
        return self.setupDeferred
Exemple #5
0
    def __init__(self, dp_addr, dr_socket, dst_connected_callback=None):
        self.log = logging.getLogger('dr2dp.dr')
        self.dr_socket = dr_socket

        self.dst_connected_callback = dst_connected_callback
        #self.dst_protocol = None

        # Connect to the dst (DR2DP_DP)
        self.dstFactory = Factory()
        self.dstFactory.protocol = DstProtocol
        self.dstFactory.dr2dp_dr = self
        self.dstFactory.src_protocol = None

        endpoint = endpoints.TCP4ClientEndpoint(reactor, dp_addr[0],
                                                dp_addr[1])
        d = endpoint.connect(self.dstFactory)
        d.addCallback(self.dst_connected)
        d.addErrback(self.dst_failed)

        # Connect to the src (DR)
        self.srcFactory = Factory()
        self.srcFactory.protocol = SrcProtocol
        self.srcFactory.dr2dp_dr = self
        self.srcFactory.dst_protocol = None

        # Remove previous instance of socket if it already exists.
        if os.path.exists(self.dr_socket):
            os.remove(self.dr_socket)
        print "starting socket"
        endpoint = endpoints.UNIXServerEndpoint(reactor, self.dr_socket)
        endpoint.listen(self.srcFactory)
        if self.dst_connected_callback:
            # Let anyone that wants to know that we've connected to the
            # destination and that the unix socket is up and ready
            self.dst_connected_callback()
Exemple #6
0
    def login(self, _: EventDispatcher, host: "tuple[str, int]",
              player: "tuple[int, str]") -> None:
        server = endpoints.TCP4ClientEndpoint(reactor, *host)
        self.connection = RboCI(*player, Main.handlers)

        connecting = server.connect(self.connection)
        connecting.addCallbacks(self.registering, self.ioError)
Exemple #7
0
def main(reactor):
    tor = yield txtorcon.connect(
        reactor,
        endpoints.TCP4ClientEndpoint(reactor, "localhost", 9251),
    )
    ep = tor.create_authenticated_onion_endpoint(
        80,
        auth=AuthBasic([
            ("alice", "0GaFhnbunp0TxZuBhejhxg"),
            "bob",
        ]),
    )

    def on_progress(percent, tag, msg):
        print('%03d: %s' % (percent, msg))

    txtorcon.IProgressProvider(ep).add_progress_listener(on_progress)
    print("Note: descriptor upload can take several minutes")

    port = yield ep.listen(server.Site(Simple()))
    print("Private key:\n{}".format(port.getHost().onion_key))
    hs = port.onion_service
    print("Clients:")
    for name in hs.client_names():
        print(
            "  {}: username={} token={}".format(
                hs.get_client(name).hostname, name,
                hs.get_client(name).auth_token), )
    print("hs {}".format(hs))
    print(type(hs))
    print(dir(hs))
    yield defer.Deferred()  # wait forever
Exemple #8
0
 def connectV4(self, peer, host, port):
     factory = TwistedClientFactory(peer)
     endpoint = endpoints.TCP4ClientEndpoint(reactor, host, port)
     print("Connecting to %s:%d" % (host, port))
     d = endpoint.connect(factory)
     d.addErrback(factory.failure, endpoint)
     pass
Exemple #9
0
    def __init__(self, *args, **kwargs):
        self.proxy_username = None
        if "proxy_host" in kwargs:
            port = 80
            if "proxy_port" in kwargs:
                port = kwargs["proxy_port"]
                del kwargs["proxy_port"]
            if "proxy_username" in kwargs:
                self.proxy_username = kwargs["proxy_username"]
                del kwargs["proxy_username"]
            if "proxy_password" in kwargs:
                self.proxy_password = kwargs["proxy_password"]
                del kwargs["proxy_password"]

            endpoint = endpoints.TCP4ClientEndpoint(reactor,
                                                    kwargs["proxy_host"], port)
            self.agent = client.ProxyAgent(endpoint)
            del kwargs["proxy_host"]
        else:

            from twisted.web import version as twisted_version

            if twisted_version.major < 14:
                # FIXME: for Ubuntu 12.04 Twisted 11? (until 2017/04)
                self.agent = client.Agent(reactor)
            else:
                contextFactory = WebClientContextFactory()
                self.agent = client.Agent(reactor, contextFactory)

        Twitter.__init__(self, *args, **kwargs)
Exemple #10
0
def main_loop():
    while True:
        fname = input('* enter file name or q[uit]: ')
        if not fname:
            fname = __file__
        if 'quit'.startswith(fname.strip().lower()):
            reactor.stop()
            break
        global the_file
        the_file = fname
        if not os.path.exists(the_file):
            LOG.info('\tfile not found {}'.format(the_file))
            continue
        with open(fname, 'br') as f:
            buffer = f.read()
            try:
                LOG.info('\tsending {} ...'.format(fname))
                if not options.twisted:
                    yield defer.maybeDeferred(sockSend,
                                              options.server,
                                              options.port,
                                              buffer,
                                              verbose=True,
                                              serialize=True,
                                              compress=not options.nocompress)
                else:
                    point = endpoints.TCP4ClientEndpoint(
                        reactor, options.server, options.port)
                    yield point.connect(SendStreamFactory(buffer))
            except Exception as x:
                LOG.error('Exception: {}'.format(x))
Exemple #11
0
 def endpoint(self, scheme, host, port):
     ep = None
     if scheme == 'http':
         ep = endpoints.TCP4ClientEndpoint(reactor, host, port)
     elif scheme == 'https':
         ep = endpoints.SSL4ClientEndpoint(reactor, host, port, context)
     return ep
Exemple #12
0
    def test_daphn3(self):
        host = self.localOptions['host']
        port = int(self.localOptions['port'])

        def failure(failure):
            log.msg("Failed to connect")
            self.report['censored'] = True
            self.report['mutation'] = 0
            raise Exception("Error in connection, perhaps the backend is censored")
            return

        def success(protocol):
            log.msg("Successfully connected")
            protocol.sendPayload()
            return protocol.d

        log.msg("Connecting to %s:%s" % (host, port))
        endpoint = endpoints.TCP4ClientEndpoint(reactor, host, port)
        daphn3_factory = Daphn3ClientFactory()
        daphn3_factory.steps = self.input
        daphn3_factory.report = self.report
        d = endpoint.connect(daphn3_factory)
        d.addErrback(failure)
        d.addCallback(success)
        return d
def main(reactor):
    # For the "single_hop=True" below to work, the Tor we're
    # connecting to must have the following options set:
    # SocksPort 0
    # HiddenServiceSingleHopMode 1
    # HiddenServiceNonAnonymousMode 1

    tor = yield txtorcon.connect(
        reactor,
        endpoints.TCP4ClientEndpoint(reactor, "localhost", 9351),
    )
    if False:
        ep = tor.create_onion_endpoint(
            80,
            version=3,
            single_hop=True,
        )
    else:
        ep = endpoints.serverFromString(reactor, "onion:80:version=3:singleHop=true")

    def on_progress(percent, tag, msg):
        print('%03d: %s' % (percent, msg))
    txtorcon.IProgressProvider(ep).add_progress_listener(on_progress)

    port = yield ep.listen(server.Site(Simple()))
    print("Private key:\n{}".format(port.getHost().onion_key))
    hs = port.onion_service
    print("Site on http://{}".format(hs.hostname))
    yield defer.Deferred()  # wait forever
Exemple #14
0
 def sockswrapper(self, proxy, url):
     dest = urlparse(url)
     assert dest.port is not None, 'Must specify port number.'
     endpoint = endpoints.TCP4ClientEndpoint(reactor, dest.hostname,
                                             dest.port)
     return SOCKSWrapper(reactor, proxy[1], proxy[2], endpoint,
                         self.timestamps)
Exemple #15
0
    def __init__(self, addr, client_connected_callback=None):

        # check that the host name obeys RFC 1123
        label = '[a-zA-Z0-9-]*[a-zA-Z0-9]'
        if ((len(addr[0]) > 255)
                or (not re.match('^(%s\.)*(%s)$' % (label, label), addr[0]))):
            print >> sys.stderr, 'ERROR: bad hostname [%s]' % addr[0]
            return

        # input parameters
        self.addr = addr
        self.client_connected_callback = client_connected_callback

        # instance of client protocol, after successful connection
        self.client_protocol = None

        # callback to use when response received from remora server
        self.response_callback = None

        self.client = Factory()
        self.client.protocol = RemoraClientProtocol
        self.client.remora = self

        # connect to remora server
        endpoint = endpoints.TCP4ClientEndpoint(reactor, addr[0], addr[1])
        d = endpoint.connect(self.client)
        d.addErrback(self.connection_failed)
Exemple #16
0
 def connectDst(self):
     dst_endpoint = endpoints.TCP4ClientEndpoint(reactor,
                                                 self.dst_addr[0],
                                                 self.dst_addr[1],
                                                 timeout=1)
     d = dst_endpoint.connect(self.dstFactory)
     d.addErrback(self.dstConnectionFailed)
Exemple #17
0
 def new_connection(self):
     """return a new connection."""
     d = Deferred()
     proto = CryptarchiveClientProtocol(self._username, self._password, d=d)
     ep = endpoints.TCP4ClientEndpoint(self.reactor, self._host, self._port)
     endpoints.connectProtocol(ep, proto)
     cp = yield d
     returnValue(cp)
Exemple #18
0
def sockswrapper(proxy, dest, ssl=False):
    endpoint = endpoints.TCP4ClientEndpoint(reactor, dest[0], dest[1])
    print "Connecting:", dest
    return socksclient.SOCKSWrapper(reactor,
                                    proxy[0],
                                    proxy[1],
                                    endpoint,
                                    ssl=ssl)
Exemple #19
0
 def xmlrpc_createConnection(self, cid):
     self.proxy.timeStamp.incrementClock(self.proxy.serverId)
     host, port, _ = config.ADDR_PORT[str(cid)]
     if cid in self.proxy.factory.peers: return 0
     point = endpoints.TCP4ClientEndpoint(reactor, host, port + 500)
     d = point.connect(self.proxy.factory)
     d.addCallback(self.proxy.greeting, cid)
     return 0
Exemple #20
0
    def setUp(self):
        params = self.clientParams()

        self.endpoint = endpoints.TCP4ClientEndpoint(reactor, "localhost", 5672)
        self.factory = amqp.AMQPFactory(**params)
        self.client = amqp.AMQPService(self.endpoint, self.factory, **params)
        self.client.startService()

        yield sleep(1)
Exemple #21
0
 def endpoint(self, scheme, host, port):
     ep = None
     if scheme == 'http':
         ep = endpoints.TCP4ClientEndpoint(reactor, host, port)
     elif scheme == 'https':
         from twisted.internet import ssl
         ep = endpoints.SSL4ClientEndpoint(reactor, host, port,
                                           ssl.ClientContextFactory())
     return ep
Exemple #22
0
    def test_main():
        server = TestServer(('', 4000))

        clientFactory = Factory()
        clientFactory.protocol = Protocol
        endpoint = endpoints.TCP4ClientEndpoint(reactor, '127.0.0.1', 4000)
        d = endpoint.connect(clientFactory)
        d.addCallback(client_connected)
        reactor.run()
Exemple #23
0
 def make_client(listening_port):
     addr = listening_port.getHost()
     self.agent = client.ProxyAgent(
         endpoints.TCP4ClientEndpoint(
             self.reactor,
             addr.host,
             addr.port,
         ),
         self.reactor,
     )
Exemple #24
0
def main(reactor):
    endpoint = endpoints.TCP4ClientEndpoint(reactor, "127.0.0.1", 8001)
    prot = yield endpoint.connect(protocol.Factory.forProtocol(amp.AMP))
    res1 = yield prot.callRemote(twisted_book_configure.GetCapitalize)
    res2 = yield prot.callRemote(twisted_book_configure.GetExclaim)
    print(res1['value'], res2['value'])
    yield prot.callRemote(twisted_book_configure.SetCapitalize, value=0.5)
    yield prot.callRemote(twisted_book_configure.SetExclaim, value=0.5)
    res1 = yield prot.callRemote(twisted_book_configure.GetCapitalize)
    res2 = yield prot.callRemote(twisted_book_configure.GetExclaim)
    print(res1['value'], res2['value'])
Exemple #25
0
def main(reactor):
    # a simple Web site; could be any other listening service of course
    res = resource.Resource()
    res.putChild(
        b'',
        static.Data("<html>Hello, onion-service world!</html>", 'text/html'))

    def on_progress(percent, tag, msg):
        print('%03d: %s' % (percent, msg))

    # We are using launch() here instead of connect() because
    # filesystem services are very picky about the permissions and
    # ownership of the directories involved. If you *do* want to
    # connect to e.g. a system service or Tor Browser Bundle, it's way
    # more likely to work to use Ephemeral services

    tor = yield txtorcon.connect(
        reactor,
        endpoints.TCP4ClientEndpoint(reactor, "localhost",
                                     txtorcon.util.default_control_port()),
    )

    # NOTE: you should put these somewhere you've thought about more
    # and made proper permissions for the parent directory, etc. A
    # good choice for a system-wide Tor is /var/lib/tor/<whatever>
    # The parent directory must be mode 700
    os.mkdir("hs_parent")
    os.chmod("hs_parent", 0o700)
    hs_dir = './hs_parent/hs_dir'

    print("Creating stealth-authenticated hidden-service, keys in: {}".format(
        hs_dir))
    ep = tor.create_authenticated_filesystem_onion_endpoint(
        80,
        hs_dir=hs_dir,
        auth=txtorcon.AuthStealth(['alice', 'bob']),
        group_readable=True,
    )

    print("Note: descriptor upload can take several minutes")

    txtorcon.IProgressProvider(ep).add_progress_listener(on_progress)

    port = yield ep.listen(server.Site(res))
    hs = port.getHost().onion_service
    for name in hs.client_names():
        client = hs.get_client(name)
        print("  {}: {}".format(name, client.hostname))
        print("     auth token: {}".format(client.auth_token))
        print("    private key: {}..".format(client.private_key[:40]))
        print("    HidServAuth {} {}".format(client.hostname,
                                             client.auth_token))
    yield tor.protocol.on_disconnect
    print("disconnected")
Exemple #26
0
    def _publisher_thread(self, url):
        while True:
            try:
                proxy = rosxmlrpc.Proxy(xmlrpc.Proxy(url),
                                        self._node_handle._name)
                value = yield proxy.requestTopic(self._name, [['TCPROS']])

                protocol, host, port = value
                conn = yield endpoints.TCP4ClientEndpoint(
                    reactor, host, port).connect(
                        util.AutoServerFactory(lambda addr: tcpros.Protocol()))
                try:
                    conn.sendString(
                        tcpros.serialize_dict(
                            dict(
                                message_definition=self._type._full_text,
                                callerid=self._node_handle._name,
                                topic=self._name,
                                md5sum=self._type._md5sum,
                                type=self._type._type,
                            )))
                    header = tcpros.deserialize_dict((yield
                                                      conn.receiveString()))
                    self._connections[conn] = header.get('callerid', None)
                    try:
                        while True:
                            data = yield conn.receiveString()
                            msg = self._type().deserialize(data)
                            try:
                                self._callback(msg)
                            except:
                                traceback.print_exc()

                            self._last_message = msg
                            self._last_message_time = self._node_handle.get_time(
                            )

                            old, self._message_dfs = self._message_dfs, []
                            for df in old:
                                df.callback(msg)
                    finally:
                        del self._connections[conn]
                finally:
                    conn.transport.loseConnection()
            except (error.ConnectionDone, error.ConnectionLost,
                    error.ConnectionRefusedError):
                pass
            except Exception:
                traceback.print_exc()

            yield util.wall_sleep(
                1
            )  # pause so that we don't repeatedly reconnect immediately on failure
Exemple #27
0
    def connectDst(self):
        """
        Create connection to the VPN server.
        """

        print "Connecting VPN"
        print "ipaddr %s port %s" % (self._vpn_ipaddr, self._vpn_port)

        endpoint = endpoints.TCP4ClientEndpoint(reactor,
                self._vpn_ipaddr, self._vpn_port, timeout=1)

        connection = endpoint.connect(self.dstFactory)
        connection.addErrback(self.dstConnectionFailed)
    def connectionMade(self):
        """
        We've connected to a src. Make a connection to the dst (CCP).
        """
        dstFactory = Factory()
        dstFactory.protocol = DstProtocol
        dstFactory.src_protocol = self

        endpoint = endpoints.TCP4ClientEndpoint(reactor,
                                                self.factory.dstaddr[0],
                                                self.factory.dstaddr[1])

        d = endpoint.connect(dstFactory)
        d.addCallback(self.dstConnected)
Exemple #29
0
    def _sendCommands(self, commands):
        def gotProtocol(proto):
            log.msg('Telnet protocol created', debug=True, system=LOG_SYSTEM)
            d = proto.sendCommands(commands)
            return d

        log.msg('Creating telnet connection', debug=True, system=LOG_SYSTEM)

        factory = TelnetFactory(self.username, self.password)

        point = endpoints.TCP4ClientEndpoint(reactor, self.host, self.port)
        d = point.connect(factory)
        d.addCallback(gotProtocol)
        return d
Exemple #30
0
    def _create_tcp_connection(self):

        def host_verified(proto):
            return proto

        def got_protocol(proto):
            proto.connection_secure_d.addCallback(host_verified)
            return proto.connection_secure_d

        factory = SSHClientFactory(self.fingerprint)
        endpoint = endpoints.TCP4ClientEndpoint(reactor, self.host, self.port)
        d = endpoint.connect(factory)
        d.addCallback(got_protocol)
        return d