Our experts are always on hand to help answer your questions, get you started, and grow your presence online. You can email us any time
How can I use TOR as a proxy in Javascript?
Tor, an acronym for The Onion Router, is an open-source software that is free to use and promotes anonymous interactions online. It routes internet traffic through a global volunteer-driven overlay network comprising over seven thousand relays. The use of Tor increases the challenge of tracing a user's online activities.
Node.js is an open-source, cross-platform JavaScript runtime environment compatible with various operating systems like Windows, Linux, Unix, macOS, among others. It operates on the V8 JavaScript engine and allows the execution of JavaScript code outside a web browser. Node.js enables developers to utilize JavaScript for writing command line tools and server-side scripting.
This guide outlines two common methods to utilize TOR as a proxy using Javascript language.
The initial method involves installing the Tor Browser, which can be downloaded from the official Tor website. The website provides instructions to download a version compatible with your operating system. After the download is complete, the next step is to install the browser on your device. The installation process is straightforward and similar to that of any other software or application.
Download Tor Browser for Windows
Download Tor Browser for macOS
Download Tor Browser for Android
The first time you launch the Tor Browser, connection to the Tor network must be established. This can be achieved by clicking the "Connect" button found on the browser's interface. This step is required only during the initial use of the Tor Browser. It is meant to ensure a secure and private connection to the Tor network.
After the connection is established, you can use the Tor Browser as a proxy to connect to a URL. The Tor Browser opens the SOCKS proxy on 127.0.0.1:9150.
socks-proxy-agentpackage is required in this scenario. This is the package which is a module that connects to a specified SOCKS proxy server, and can be used with the built-in http and https modules.
socks-proxy-agentpackage.
yarn add socks-proxy-agent // -- or -- npm install --save socks-proxy-agent
Below script is written in Javascript language to show you how to use tor as a proxy.
const http = require('http'); const https = require('https'); const { SocksProxyAgent } = require('socks-proxy-agent'); const agent = new SocksProxyAgent( 'socks://127.0.0.1:9150' ); // use tor as a proxy to connect http service // http.get('http://2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion', { agent }, (res) => { // console.log(res.headers); // res.pipe(process.stdout); // }); // use tor as a proxy to connect https service https.get('https://api.ipify.org', { agent }, (res) => { console.log(res.headers); res.pipe(process.stdout); });
When you execute this script using Javascript language, it will show the IP address of your Tor instance exit node, instead of your IP address. This is the result of using Tor as a proxy in Javascript language.
The second method introduced is by utilizing Docker. Docker is a software platform designed to facilitate building, testing, and deploying applications with speed. It organizes software into uniform units known as containers, which incorporate all the necessary components for the software's operation, such as libraries, system tools, code, and runtime. Docker enables swift deployment and scaling of applications in any environment, ensuring the smooth running of your code.
To utilize Docker to run Tor instance, execute the below command:
$ docker run --rm -p 9050:9050 osminogin/tor-simple Mar 27 01:36:09.530 [notice] Tor 0.4.8.10 running on Linux with Libevent 2.1.12-stable, OpenSSL 3.1.2, Zlib 1.3, Liblzma 5.4.5, Libzstd 1.5.5 and Unknown N/A as libc. Mar 27 01:36:09.530 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://support.torproject.org/faq/staying-anonymous/ Mar 27 01:36:09.530 [notice] Read configuration file "/etc/tor/torrc". Mar 27 01:36:09.533 [warn] You specified a public address '0.0.0.0:9050' for SocksPort. Other people on the Internet might find your computer and use it as an open proxy. Please don't allow this unless you have a good reason. Mar 27 01:36:09.533 [notice] Opening Socks listener on 0.0.0.0:9050 Mar 27 01:36:09.534 [notice] Opened Socks listener connection (ready) on 0.0.0.0:9050 Mar 27 01:36:09.534 [warn] Fixing permissions on directory /var/lib/tor
The docker run command runs a command in a new container, pulling the image if needed and starting the container. In this example, it pulls a public Tor image from the Docker Hub registry. Use --rm flag to specify that the container should be removed when it exits. Use -p flag to publish a container port 9050 to the host.
Then, you can try to execute the below Javascript script. This script shows how to use tor as a proxy using Javascript language.
const http = require('http'); const https = require('https'); const { SocksProxyAgent } = require('socks-proxy-agent'); const agent = new SocksProxyAgent( 'socks://127.0.0.1:9050' ); // use tor as a proxy to connect http service // http.get('http://2gzyxa5ihm7nsggfxnu52rck2vv4rvmdlkiu3zzui5du4xyclen53wid.onion', { agent }, (res) => { // console.log(res.headers); // res.pipe(process.stdout); // }); // use tor as a proxy to connect https service https.get('https://api.ipify.org', { agent }, (res) => { console.log(res.headers); res.pipe(process.stdout); });
Note that when you run Tor instance using Docker, the SOCKS port is 9050, which differs than the SOCKS port used in Tor Browser. This is to ensure that there is no conflict when your system runs Tor instance and Tor Browser at the same time.
This guide demonstrates two common ways of how to use TOR as a proxy using Javascript language.
Our experts are always on hand to help answer your questions, get you started, and grow your presence online. You can email us any time