Package: txsocksx
txsocksxtxsocksxis SOCKS4/4a and SOCKS5 client endpoints forTwisted10.1 or
greater. The code is available on github:https://github.com/habnabit/txsocksxExamplesThese examples assume familiarity with how to useTwisted endpoints. For
simplicity, most of the examples will use SOCKS5.AuthenticatingOne specifies authentication methods to aSOCKS5ClientEndpointvia themethodsparameter. For example, to connect using the usernamespamand
passwordeggs:exampleEndpoint = SOCKS5ClientEndpoint(
    'example.com', 6667, proxyEndpoint, methods={'login': ('spam', 'eggs')})However, this will disable anonymous authentication. To use either login or
anonymous authentication, specify both methods:exampleEndpoint = SOCKS5ClientEndpoint(
    'example.com', 6667, proxyEndpoint, methods={'login': ('spam', 'eggs'),
                                                 'anonymous': ()})Themethodsdict must always map from a string to a tuple.SOCKS4SOCKS4 has no authentication, but does have a configurable “user ID” which
defaults to an empty string:exampleEndpoint = SOCKS4ClientEndpoint(
    'example.com', 6667, proxyEndpoint, user='spam')Connecting to a thing over torTo connect toexample.comon port 6667 over tor, one creates aSOCKS5ClientEndpointwrapping the endpoint of the tor server:torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint)Establishing the connection from there proceeds like usual:deferred = exampleEndpoint.connect(someFactory)txsocksxwill not do any DNS resolution, so the hostnameexample.comwill not leak; tor will receive the hostname directly and do the DNS lookup
itself.Tor allows connections by SOCKS4 or SOCKS5, and does not expect a user ID to be
sent when using the SOCKS4 client.Cancelling a connectionSometimes one tires of waiting and wants to abort the connection attempt. For
example, to abort the whole connection attempt after ten seconds:torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint)
deferred = exampleEndpoint.connect(someFactory)
reactor.callLater(10, deferred.cancel)This is a trivial example; real code should cancel theIDelayedCallreturned
byreactor.callLaterwhen the deferred fires. The code would then look like
this:torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint)
deferred = exampleEndpoint.connect(someFactory)
canceler = reactor.callLater(10, deferred.cancel)

def cancelCanceler(result):
    if canceler.active():
        canceler.cancel()
    return result
deferred.addBoth(cancelCanceler)Making HTTP requestsTwisted’s builtinAgentHTTP client did not support being handed an
arbitrary endpoint before 15.0, sotxsocksxprovides anAgentfor maximum
compatibility.Whiletxsocksxrequires only Twisted 10.1,txsocksx.httprequires Twisted
12.1 or greater. Its usage is almost identical to normalAgentusage:torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
agent = SOCKS5Agent(reactor, proxyEndpoint=torServerEndpoint)
deferred = agent.request('GET', 'http://example.com/')Note that theproxyEndpointparametermustbe passed as a keyword
argument. There is a second, optional, keyword-only argument for passing
additional arguments to theSOCKS5ClientEndpointasSOCKS5Agentconstructs it:torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
agent = SOCKS5Agent(reactor, proxyEndpoint=torServerEndpoint,
                    endpointArgs=dict(methods={'login': ('spam', 'eggs')}))
deferred = agent.request('GET', 'http://example.com/')SOCKS5Agenttransparently supports HTTPS viaTLSWrapClientEndpoint.For users with Twisted 15.0 or greater,SOCKS5Agentalso implementsIAgentEndpointFactory.Upgrading to TLSSometimes one wants to switch to speaking TLS as soon as the proxy negotiation
is finished. For that, there istxsocksx.tls. After wrapping an endpoint withTLSWrapClientEndpoint, the connection will be upgraded to using TLS
immediately after proxy negotiation finishes:torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint)
tlsEndpoint = TLSWrapClientEndpoint(exampleEndpoint)
deferred = tlsEndpoint.connect(someFactory)Proxying over a proxyBecause oftxsocksx’s composable design, it’s trivial to connect from one SOCKS
proxy to another:torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
firstProxyEndpoint = SOCKS5ClientEndpoint(
    'first-proxy.example.com', 1080, torServerEndpoint)
secondProxyEndpoint = SOCKS4ClientEndpoint(
    'second-proxy.example.com', 1080, firstProxyEndpoint)
finalHop = SOCKS5ClientEndpoint(
    'example.com', 113, secondProxyEndpoint)
deferred = finalHop.connect(someFactory)