跳转到主要内容

Twisted客户端端点用于SOCKS{4,4a,5}

项目描述

https://img.shields.io/travis/habnabit/txsocksx/master.svg?style=flat-square https://img.shields.io/coveralls/habnabit/txsocksx/master.svg?style=flat-square

txsocksx

txsocksx 是适用于Twisted 10.1或更高版本的SOCKS4/4a和SOCKS5客户端端点。代码可在github上找到: https://github.com/habnabit/txsocksx

示例

以下示例假定您熟悉如何使用 Twisted端点。为了简单起见,大多数示例将使用SOCKS5。

认证

通过methods参数指定用于SOCKS5ClientEndpoint的认证方法。例如,要使用用户名spam和密码eggs进行连接

exampleEndpoint = SOCKS5ClientEndpoint(
    'example.com', 6667, proxyEndpoint, methods={'login': ('spam', 'eggs')})

然而,这将禁用匿名认证。要使用登录或匿名认证,必须指定两种方法

exampleEndpoint = SOCKS5ClientEndpoint(
    'example.com', 6667, proxyEndpoint, methods={'login': ('spam', 'eggs'),
                                                 'anonymous': ()})

methods字典必须始终将字符串映射到元组。

SOCKS4

SOCKS4没有认证,但有一个可配置的“用户ID”,默认为空字符串

exampleEndpoint = SOCKS4ClientEndpoint(
    'example.com', 6667, proxyEndpoint, user='spam')

通过tor连接到某个东西

要使用tor通过端口6667连接到example.com,需要创建一个包装tor服务器端点的SOCKS5ClientEndpoint

torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint)

从那里建立连接的过程与平常一样

deferred = exampleEndpoint.connect(someFactory)

txsocksx不会进行任何DNS解析,因此主机名example.com不会泄露;tor将直接接收主机名并自行进行DNS查找。

tor允许通过SOCKS4或SOCKS5进行连接,并且在使用SOCKS4客户端时不需要发送用户ID。

取消连接

有时人们会等得不耐烦,想要取消连接尝试。例如,在十秒后取消整个连接尝试

torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint)
deferred = exampleEndpoint.connect(someFactory)
reactor.callLater(10, deferred.cancel)

这是一个简单的示例;实际代码应该在deferred触发时取消由reactor.callLater返回的IDelayedCall。代码将如下所示

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)

制作HTTP请求

在15.0之前,Twisted的内置Agent HTTP客户端不支持在创建之前传递任意端点,因此txsocksx提供了一个Agent以实现最大的兼容性。

虽然txsocksx只需要Twisted 10.1,但txsocksx.http需要Twisted 12.1或更高版本。它的使用方式几乎与正常Agent的使用相同

torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
agent = SOCKS5Agent(reactor, proxyEndpoint=torServerEndpoint)
deferred = agent.request('GET', 'http://example.com/')

请注意,proxyEndpoint参数必须以关键字参数的形式传递。还有一个第二个可选的关键字-only参数,用于将额外参数传递给SOCKS5ClientEndpoint,以便在SOCKS5Agent构建它时使用

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/')

SOCKS5Agent通过TLSWrapClientEndpoint透明地支持HTTPS。

对于Twisted 15.0或更高版本的用户,SOCKS5Agent还实现了IAgentEndpointFactory

升级到TLS

有时人们希望在代理协商完成后立即切换到TLS。为此,有txsocksx.tls。在用TLSWrapClientEndpoint包装端点之后,连接将在代理协商完成后立即升级到使用TLS。

torServerEndpoint = TCP4ClientEndpoint(reactor, '127.0.0.1', 9050)
exampleEndpoint = SOCKS5ClientEndpoint('example.com', 6667, torServerEndpoint)
tlsEndpoint = TLSWrapClientEndpoint(exampleEndpoint)
deferred = tlsEndpoint.connect(someFactory)

通过代理代理

由于txsocksx的可组合设计,从一个SOCKS代理连接到另一个代理是极其简单的

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)

项目详细信息


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装软件包的信息。

源分布

txsocksx-1.15.0.2.tar.gz (19.4 kB 查看哈希值)

上传时间 源代码

构建分发版

txsocksx-1.15.0.2-py2-none-any.whl (23.6 kB 查看哈希值)

上传时间 Python 2

由以下提供支持