tor 匿名访问_如何使用tor匿名化Node js应用程序

tor 匿名访问

Like every developer, I find myself writing many scripts, bots and cron jobs, that regularly hit certain servers. Occasionally, I need these scripts to appear to be in random remote locations. Sometimes I want to test how my new bot protection behaves, or view my website or application from abroad, or access a website that’s not accessible from the country I’m in (I used to live in Turkey), or simply mask who I am online. To achieve this seamlessly and reliably, I needed a trustworthy and anonymous VPN or proxy.

像每个开发人员一样,我发现自己编写了许多脚本,机器人程序和cron作业,这些脚本经常在某些服务器上运行。 有时,我需要这些脚本似乎位于随机的远程位置。 有时我想测试我的新机器人保护程序的行为,或者从国外查看我的网站或应用程序,或者访问无法从我所在的国家访问的网站(我曾经居住在土耳其),或者只是掩盖我的身份线上。 为了无缝,可靠地实现这一目标,我需要一个可信赖的匿名VPN或代理。

I set up my own VPN which was a pretty good solution for some of my use cases. But it was complicated to change the location of VPN, it wasn’t anonymous, and it was costly when I wasn’t actively using it. After some thinking and some research, I’ve realised that Tor is perfect for most of my use cases. It’s reliable, anonymous, and free!

我设置了自己的VPN,对于某些用例来说,这是一个非常好的解决方案。 但是更改VPN的位置很复杂,它不是匿名的,当我不积极使用它时,它的成本很高。 经过一番思考和研究,我意识到Tor非常适合我的大多数用例。 它是可靠,匿名和免费的!

In this article, I’m going to explain how you can set up your own code to work over Tor and use its advantages for free. Let’s start by understanding what Tor is.

在本文中,我将解释如何设置自己的代码以在Tor上运行并免费使用其优势。 让我们从了解Tor是什么开始。

什么是Tor? (What is Tor?)

Tor, or as it used to be called “The Onion Router,” is a service that enables free anonymous communication over the Internet. It directs Internet traffic through free, worldwide, volunteer operated relays to mask the origin of requests. The Tor Project aims to help people protect themselves from companies, governments, or individuals who conduct traffic analysis or network surveillance.

Tor(以前称为“洋葱路由器”)是一项服务,可以通过Internet免费进行匿名通信。 它通过免费的,全球性的,由志愿者操作的中继来引导Internet通信,以掩盖请求的来源。 Tor项目旨在帮助人们保护自己免受进行流量分析或网络监控的公司,政府或个人的侵害。

They also developed Tor Browser to replace your usual browser for daily usage. For instance, if you’re worried about how companies like Facebook and Google monitor your every move to show you relevant ads, you might want to check out Tor Browser. It works like any other internet browser with some added features and options to increase anonymity. It uses Tor’s network to conceal who you are and what you do online. That’s why it’s also the most popular way to access the hidden part of the internet commonly known as the deep web. According to Tor Metrics, about two million people use Tor Browser every day.

他们还开发了Tor浏览器,以取代您日常使用的常规浏览器。 例如,如果您担心Facebook和Google这样的公司如何监控您的举动以向您显示相关广告,那么您可能想要查看Tor浏览器。 它与任何其他具有某些附加功能和选项以增加匿名性的Internet浏览器一样工作。 它使用Tor的网络来掩盖您的身份和在线工作。 这就是为什么它也是最受欢迎的访问Internet隐藏部分(通常称为Deep Web)的方法 。 根据Tor Metrics的数据 ,每天大约有200万人使用Tor浏览器。

What’s particularly interesting about Tor is how easy it is to set up a local Tor proxy and direct all our traffic through it. It’s also completely free.

Tor尤其有趣的是,设置本地Tor代理并通过它引导我们的所有流量非常容易。 它也是完全免费的。

设置Tor代理 (Setting up a Tor Proxy)

The most stable way of doing this is to run a Tor proxy ourselves and redirect our Node.js requests through it. Setting up your own proxy sounds complicated but it’s easy. I’ll explain an easy way and a not so easy way of doing this. Both are relatively simple so bear with me.

最稳定的方法是自己运行Tor代理,并通过它重定向Node.js请求。 设置自己的代理听起来很复杂,但是很容易。 我将解释一个简单的方法,而不是那么简单的方法。 两者都相对简单,所以请多多包涵。

Usually, when you install software like a proxy, the process is different depending on which operating system you’re running. Installing and configuring on Mac server would be different from a Linux server, so doing both would require repetitive efforts. This is what I call the not so easy way and it will be explained later. Now, let’s explain the cross-platform way.

通常,当您安装诸如代理之类的软件时,该过程会有所不同,具体取决于您所运行的操作系统。 Mac服务器上的安装和配置与Linux服务器上的安装和配置不同,因此两者都需要反复努力。 这就是我所说的不太容易的方法 ,稍后将对此进行说明。 现在,让我们解释一下跨平台的方式。

简单的方法 (Easy way)

As mentioned above, the easy way is to install and configure Tor cross-platform using Docker. Docker is a containerization solution that lets you run your software inside a virtual-machine-like environment to isolate it from external interference. It also allows you to run your software in a managed environment so that you can reliably and easily run it cross-platform.

如上所述,简单的方法是使用Docker安装和配置Tor跨平台。 Docker是一种容器化解决方案,可让您在类似虚拟机的环境中运行软件,以使其免受外部干扰。 它还允许您在受管环境中运行软件,以便可以可靠,轻松地跨平台运行该软件。

If you don’t already have Docker, install the correct community edition for your operating system here. After installing and starting it, we’ll run the Docker version of Tor proxy in our machine. Thankfully, someone took the time to set it up inside a Docker container and open-sourced it. We just need to run the image they published. Run this command below on your favorite shell/terminal:

如果您还没有Docker,请在此处为您的操作系统安装正确的社区版本。 安装并启动它后,我们将在我们的机器上运行Docker版本的Tor代理。 值得庆幸的是,有人花了一些时间在Docker容器中进行设置并将其开源。 我们只需要运行他们发布的图像。 在您喜欢的shell /终端上运行以下命令:

docker run -it -p 9050:9050 -d dperson/torproxy

This command downloads thedperson/torproxy image from the public Docker Hub and runs it locally on your Docker setup. It also exposes this proxy on port 9050 so you can connect your scripts to the proxy over this port.

该命令从公共Docker Hub下载dperson/torproxy映像,并在Docker设置上本地运行。 它还在端口9050上公开了该代理,因此您可以通过此端口将脚本连接到代理。

没那么容易的方法 (Not so easy way)

If you’ve already set up Tor on the previous step, you can skip this one. I’ll explain how to do it without using Docker.

如果您已在上一步中设置了Tor,则可以跳过这一步。 我将解释不使用Docker的情况。

To set up Tor without using Docker, we need to install it manually.

要在不使用Docker的情况下设置Tor,我们需要手动安装。

对于Ubuntu / Debian (For Ubuntu/Debian)

To install Tor on Ubuntu/Debian, we need to update the source list of apt and then install Tor with apt. There are slight differences depending on the version of the operating system you're using. Tor Project already has a document with the steps and the exact code you need to run based on your operating system. Instead of repeating that information here, I recommend you take a look at their document here.

要在Ubuntu / Debian上安装Tor,我们需要更新apt的源列表,然后使用apt安装Tor。 根据您使用的操作系统版本,会有细微的差异。 Tor Project已经有一个文档,其中包含根据您的操作系统运行的步骤和确切的代码。 建议您在此处查看其文档 ,而不是在此重复该信息。

对于Mac OSX (For Mac OSX)

It’s easier for OSX compared to Linux distributions. There are two popular package managers for Mac, Homebrew and Macports. Install one of them if you haven’t already. If you don’t know which one to choose, I can recommend Homebrew as I’m quite satisfied with it:

与Linux发行版相比,OSX更容易。 Mac有两种流行的软件包管理器:Homebrew和Macports。 如果尚未安装其中之一。 如果您不知道选择哪个,我可以推荐Homebrew,因为我对此很满意:

Depending on which package manager you chose to install, run either:

根据您选择安装的程序包管理器,运行以下任一程序:

brew install tor

or

要么

sudo port install tor

在没有Docker的情况下运行Tor (Running Tor without Docker)

Luckily this part is very simple. Simply run:

幸运的是,这部分非常简单。 只需运行:

tor

Or, to run it in the background:

或者,在后台运行它:

tor &

This command starts the Tor proxy and exposes it over port 9050.

此命令启动Tor代理,并通过端口9050公开它。

测试我们的Tor连接 (Testing Our Tor Connection)

Now we have our very own Tor entry point up and running, let’s test that it does what we want it to do. An easy way to do this is to make an HTTP request through Tor to a website to see what they think our IP address is. You can use any HTTP client--curl, httpie or wget, for example — and make a request to any "What Is My IP Address Solution." Here’s an example command I invoked and the result:

现在我们已经建立并运行了自己的Tor入口点,让我们测试一下它是否可以完成我们想要的工作。 一种简单的方法是通过Tor向网站发出HTTP请求,以查看他们认为我们的IP地址是什么。 您可以使用任何HTTP客户httpie (例如curlhttpiewget并向任何“我的IP地址解决方案是什么”发出请求。 这是我调用的示例命令和结果:

Image for post

(Berlin, Germany)

(柏林,德国)

Now let’s use our local Tor Socks5 Proxy for the same request:

现在,让我们使用本地的Tor Socks5代理进行相同的请求:

Image for post

(Roubaix, France)

(法国鲁拜)

ifconfig.me (or any website that I access through Tor) now thinks that I’m in France even though I’m in Germany.

ifconfig.me(或我通过Tor访问的任何网站)现在都认为我在法国,即使我在德国。

It’s also possible to select the country you want to appear to be in. Simply provide the -l option to your Docker command with the ISO country code and Tor should use an exit node in that country. Example:

也可以选择要显示的国家/地区。只需向Docker命令提供-l选项以及ISO国家/地区代码,Tor应该在该国家/地区使用出口节点。 例:

docker run -it -p 9050:9050 -d dperson/torproxy -l "US"

Keep in mind that, thanks to its complicated nature, Tor can be too slow for a reliable Netflix or similar streaming experience. It’s discouraged to use it for those purposes.

请记住,由于其复杂性,对于可靠的Netflix或类似的流媒体体验而言,Tor可能太慢。 不建议将其用于这些目的。

将Node.js脚本连接到Tor (Connecting Node.js scripts to Tor)

The next step is to configure our code so that all the requests generated from our Node.js code will go through this proxy. Most HTTP clients let you specify a proxy or an HTTP agent to be used. A popular Socks Proxy agent you can use in your scripts is socks-proxy-agent. Below is an implementation using Node’s built-in HTTP client https:

下一步是配置我们的代码,以便从我们的Node.js代码生成的所有请求都将通过此代理。 大多数HTTP客户端允许您指定要使用的代理或HTTP代理。 您可以在脚本中使用的流行的Socks Proxy代理是socks-proxy-agent 。 以下是使用Node的内置HTTP客户端https

const https = require('https');
const { SocksProxyAgent } = require('socks-proxy-agent');


const agent = new SocksProxyAgent('socks5h://127.0.0.1:9050');


https.get('https://ifconfig.me', {
  agent
}, res => {
  res.pipe(process.stdout);
});

Here’s another example implementation, this time using the popular JavaScript HTTP client library, Axios:

这是另一个示例实现,这次使用流行JavaScript HTTP客户端库Axios

const { SocksProxyAgent } = require('socks-proxy-agent');
const axios = require('axios');


const agent = new SocksProxyAgent('socks5h://127.0.0.1:9050');


axios({
  url: 'https://ifconfig.me',
  httpsAgent: agent,
})
.then(({
  data
}) => {
  console.log(data);
});

最后的话 (Final Words)

Congratulations — your script just got a lot harder to track and identify. Use this ability wisely!

祝贺您-您的脚本变得更加难以跟踪和识别。 明智地使用此功能!

Let me know what you think about this article and what applications you have could benefit from this approach.

让我知道您对本文的看法以及使用此方法可以受益的应用程序。

资源资源 (Resources)

翻译自: https://medium.com/better-programming/how-to-anonymize-node-js-applications-using-tor-bc233356b163

tor 匿名访问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值