如何使用socks4a从node.js连接到隐藏服务

How to connect to hidden service from node.js using socks4a?

本文关键字:连接 隐藏 服务 js node 何使用 socks4a      更新时间:2023-09-26

TOR文档说SOCKS4a需要连接到一个隐藏的服务。有一个实现SOCKS4a的节点库。

假设一个人知道一个隐藏服务的.onion地址,一个人应该如何用https连接到这个隐藏服务,从一个node.js应用程序获取一个在线网页?欢迎提供一个代码示例。

更准确地说,我已经为Openshift安装了Tor墨盒,但连接和使用它对我来说是不清楚的。我在Tor栈交换上创建了一个相关的问题

通常TOR隐藏服务不使用HTTPS,因为TOR已经为通过网络路由的数据包提供了强大的加密。但有些服务确实使用它。我最近需要与TOR隐藏服务背后的web API进行通信。我是这样做的

安装TOR。在debian/Ubuntu系统上:

sudo apt-get install tor

对于其他系统,请查看官方网站的相应安装指南。

使用下面的nodejs脚本从TOR隐藏服务中获取网页。默认情况下,它将获取DuckDuckGo的home age,这是由他们的v3洋葱地址提供的。

const url = require('url');
const http = require('http');
const https = require('https');
const SocksProxyAgent = require('socks-proxy-agent');
// Use the SOCKS_PROXY env var if using a custom bind address or port for your TOR proxy:
const proxy = process.env.SOCKS_PROXY || 'socks5h://127.0.0.1:9050';
console.log('Using proxy server %j', proxy);
// The default HTTP endpoint here is DuckDuckGo's v3 onion address:
const endpoint = process.argv[2] || 'https://duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion';
console.log('Attempting to GET %j', endpoint);
// Prepare options for the http/s module by parsing the endpoint URL:
let options = url.parse(endpoint);
const agent = new SocksProxyAgent(proxy);
// Here we pass the socks proxy agent to the http/s module:
options.agent = agent;
// Depending on the endpoint's protocol, we use http or https module:
const httpOrHttps = options.protocol === 'https:' ? https : http;
// Make an HTTP GET request:
httpOrHttps.get(options, res => {
    // Print headers on response:
    console.log('Response received', res.headers);
    // Pipe response body to output stream:
    res.pipe(process.stdout);
});
  • 将上述脚本保存到文件tor-http-fetch.js
  • 安装socks-proxy-agent npm模块:npm install socks-proxy-agent
  • node tor-http-fetch.js 获取自己的洋葱服务:node ./tor-http-fetch.js "http://your-tor-hidden-service.onion"

上面的脚本可以使用http或https url。

详情请点击此处

祝你好运!