纯python-tor协议实现
torp的Python项目详细描述
扭转
Tor协议的纯Python实现。 torpy可用于通过Tor Network与clearnet主机或隐藏服务通信。
功能
- 不需要Stem或正式的Tor客户
- 支持v2隐藏服务(v2 specification)
- 支持basic和stealth授权协议
- 为requests提供简单的TorHttpAdapter
- 提供简单的socks5代理
注意:本产品独立于Tor®匿名软件生产,且The Tor Project对质量、适用性或其他任何方面均不作任何保证。
控制台示例
有几个控制台实用程序用于测试客户端
一个简单的HTTP/HTTPS请求:
$ torpy_cli --url https://ifconfig.me --header "User-Agent""curl/7.37.0" Downloading new consensus from gabelmoo authority Connecting to guard node 144.217.94.84:443 (ezrin; Tor 0.3.5.8)... Sending: GET https://ifconfig.me Creating new circuit #80000001 with 144.217.94.84:443 (ezrin; Tor 0.3.5.8) router... Building 3 hops circuit... Extending the circuit #80000001 with 95.211.147.99:9001 (RNVR217; Tor 0.3.5.7)... Extending the circuit #80000001 with 45.33.43.215:443 (a0xo; Tor 0.3.5.8)... Creating stream #1 attached to #80000001 circuit... Stream #1: connecting to ('ifconfig.me', 443) Response status: 200 > 45.33.43.215 Stream #1: closing... Closing guard connections... Destroy circuit #80000001
创建Socks5代理以通过Tor网络中继请求:
$ torpy_socks -p 1050 --hops 3
Downloading new consensus from bastet authority
Connecting to guard node 45.77.80.140:9001 (sparklingengine; Tor 0.2.9.11)...
Creating new circuit #80000001 with 45.77.80.140:9001 (sparklingengine; Tor 0.2.9.11) router...
Building 3 hops circuit...
Extending the circuit #80000001 with 185.13.39.197:443 (Neldoreth; Tor 0.3.5.8)...
Extending the circuit #80000001 with 77.247.181.163:22 (amartysen; Tor 0.3.5.8)...
Start socks proxy at 127.0.0.1:1050
...
torpy模块还有一个命令行界面:
$ python3.7 -m torpy --url https://facebookcorewwwi.onion --to-file index.html Downloading new consensus from bastet authority Connecting to guard node 5.9.173.35:443 (killto; Tor 0.2.9.16)... Sending: GET https://facebookcorewwwi.onion Creating new circuit #80000001 with 5.9.173.35:443 (killto; Tor 0.2.9.16) router... Building 3 hops circuit... Extending the circuit #80000001 with 54.39.151.167:9001 (DeusVult; Tor 0.3.5.8)... Extending the circuit #80000001 with 171.25.193.78:443 (DFRI4; Tor 0.3.3.9)... Creating stream #1 attached to #80000001 circuit... Stream #1: connecting to ('facebookcorewwwi.onion', 443) Extending #80000001 circuit for hidden service facebookcorewwwi.onion... Rendezvous established (CellRelayRendezvousEstablished()) Iterate over responsible dirs of the hidden service Iterate over introduction points of the hidden service Create circuit for hsdir Creating new circuit #80000002 with 5.9.173.35:443 (killto; Tor 0.2.9.16) router... Building 0 hops circuit... Extending the circuit #80000002 with 45.77.196.39:9001 (Fractalia; Tor 0.2.9.16)... Creating stream #2 attached to #80000002 circuit... Stream #2: connecting to hsdir Stream #2: closing... Destroy circuit #80000002 Creating new circuit #80000003 with 5.9.173.35:443 (killto; Tor 0.2.9.16) router... Building 0 hops circuit... Extending the circuit #80000003 with 213.136.81.89:9001 (unnamed314; Tor 0.3.5.8)... Introduced (CellRelayIntroduceAck()) Destroy circuit #80000003 Creating stream #3 attached to #80000001 circuit... Stream #3: connecting to ('www.facebookcorewwwi.onion', 443) Extending #80000001 circuit for hidden service facebookcorewwwi.onion... Response status: 200 Writing to file index.html Stream #1: closing... Stream #3: closing... Closing guard connections... Destroy circuit #80000001
使用示例
如何向clearnet主机或隐藏服务发送一些数据的基本示例:
fromtorpyimportTorClienthostname='ifconfig.me'# It's possible use onion hostname here as welltor=TorClient()# Choose random guard node and create 3-hops circuitwithtor.create_circuit(3)ascircuit:# Create tor stream to hostwithcircuit.create_stream((hostname,80))asstream:# Now we can communicate with hoststream.send(b'GET / HTTP/1.0\r\nHost: %s\r\n\r\n'%hostname.encode())recv=stream.recv(1024)
torhttpadapter是一个方便的用于requests library的tor适配器。 以下示例显示了TorHttpAdapter在多线程HTTP请求中的用法:
frommultiprocessing.poolimportThreadPoolfromtorpy.http.requestsimporttor_requests_sessionwithtor_requests_session()ass:# returns requests.Session() objectlinks=['http://nzxj65x32vh2fkhk.onion','http://facebookcorewwwi.onion']*2withThreadPool(3)aspool:pool.map(s.get,links)
有关更多示例,请参见test_integration.py
安装
pip3 install torpy
贡献
待办事项
- []实现v3隐藏服务specification
- []重构单元格序列化/反序列化
- []更多单元测试
- []使用asyncio重写库
- []实施洋葱服务
许可证
根据Apache许可证2.0版授权