在接收模式下使用 python 将 POST 请求发送到私有 OnionShare 服务器

问题描述 投票:0回答:1

我正在尝试使用这个简单的程序以接收模式将数据发送到私有 onionshare 服务器:

import socket
import requests

s = requests.session()
s.proxies['http'] = 'socks5h://127.0.0.1:9060'
s.proxies['https'] = 'socks5h://127.0.0.1:9060'

print(s.proxies)

url = 'http://*redacted*.onion'

headers = {
    'Content-Type': 'application/json'
}

data = {
    'key':'*redacted*'
}

try:
    r = s.post(url, headers=headers, json=data, proxies=s.proxies)
    print(r.text)
except Exception as e:
    print(e)

我不断收到此错误:

SOCKSHTTPConnectionPool(host='*redacted*.onion', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.contrib.socks.SOCKSConnection object at 0x7fdf710c5a50>: Failed to establish a new connection: 0x01: General SOCKS server failure'))

尝试了以下方法:

  • 尝试更改获取请求。收到同样的错误消息
  • 尝试连接到已知的洋葱地址:

https://www.facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion/

这有效。与仅卷曲它相同。

  • 尝试更改torrc中的端口值,这就是端口为9060的原因。
  • 尝试使用stem库来控制tor进程。这给了我类似的错误。根据我读到的有关 Stem 的内容,我觉得这是一条可行的道路,但不幸的是我还没有很幸运。
  • 尝试公开 onionshare 并向其发送 get 请求。这有效,所以问题似乎与 onionshare 服务私有时的客户端授权有关。
  • 尝试将带有目录的 ClientOnionAuthDir 添加到 torrc 文件中。我为 onionshare 服务创建了一个 .auth_private 文件,其中包含洋葱地址和私钥,格式为 :x25519:< private key >。 (我在更改 torrc 文件后重新启动了 tor 作为服务)。将其添加到 torrc 文件后,当尝试使用原始程序和通过 Stem 访问该服务时,我从 notification.log 中得到了以下内容:
[notice] Fail to decrypt descriptor for requested onion address. It is likely requiring client authorization.

请注意,我不确定 OnionShare 服务器期望什么内容类型。不幸的是,我在查看他们的文档时发现很少关于此事的信息。

python-3.x python-requests tor stem
1个回答
0
投票

我解决了客户端授权的问题。问题是我的 .auth_private 文件中的格式。我在洋葱地址和 x25519 之间缺少“描述符”。我也对程序进行了很大的改变,并开始使用stem。通过这些更改,我设法通过 get 请求从 onionshare 页面获取 html。

from stem import Signal
from stem.control import Controller
import requests

def request_over_tor(url):
    with Controller.from_port(port = 9061) as controller:
        # Provide your authentication here if needed
        # Signal Tor for a new circuit
        controller.authenticate(password='*redacted*')
        controller.signal(Signal.NEWNYM)
        
        proxies = {
            'http': 'socks5h://127.0.0.1:9060',
            'https': 'socks5h://127.0.0.1:9060',
        }
       
        response = requests.get(url, proxies=proxies)
        return response.text

hidden_service_url = 'http://*redacted*.onion'
response_content = request_over_tor(hidden_service_url)
print(response_content)
© www.soinside.com 2019 - 2024. All rights reserved.