纯python-tor协议实现

torp的Python项目详细描述


扭转Python VersionsBuild StatusCoverage Status

Tor协议的纯Python实现。 torpy可用于通过Tor Network与clearnet主机或隐藏服务通信。

功能

  • 不需要Stem或正式的Tor客户
  • 支持v2隐藏服务(v2 specification
  • 支持basicstealth授权协议
  • 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

贡献

  • 使用它
  • 代码审查是值得赞赏的
  • 打开Issue,发送PR

待办事项

  • []实现v3隐藏服务specification
  • []重构单元格序列化/反序列化
  • []更多单元测试
  • []使用asyncio重写库
  • []实施洋葱服务

许可证

根据Apache许可证2.0版授权

参考文献

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java我能在这个程序中更好地使用内存吗?   为什么我的Java while循环迭代了一半   java IntelliJ IDEA不在构建时复制资源   socket仅在Java TCP服务器输出上检查客户端断开连接   java游戏物理摩擦   java片段onClick调用方法   symja数学分析器中无法识别java Abs[x]   java在使用泛型时创建二进制搜索树类的实例?   java在外键约束表上的添加和删除   语法java表达式的含义,如果有条件   java创建内联对象并作为参数传递   是否有相当于Redis排序集(zset)的Java数据结构   java找不到适合的方法(无参数)   音频文件操作给定字节帧的音量Java   Eclipse4不以JavaWebStart启动   java如何使用org在JSON对象中获取嵌套的键元素。json?   java与Jackson的反序列化:“org.codehaus.Jackson.map.JsonMappingException:无法反序列化[projectname]的实例。”   字符串的Java正则表达式   spring集成上的java检测缺火指令