'How do I programmatically use tor

My tor is connected to 127.0.0.1:9051, Its said that it not an http proxy so how can I really connect to websites programmatically using it (preferably node.js)? (Trying to connect using http GET)

Like is there a specific way of sending requests?

Thanks in advance 🙏



Solution 1:[1]

You can use Axios for request and set proxy to TOR SOCKS proxy. Like below

const axios = require('axios');
const SocksProxyAgent = require('socks-proxy-agent');
const proxyOptions = `socks5://$127.0.0.1:9050`;
const httpsAgent = new SocksProxyAgent(proxyOptions);
const baseUrl = 'https://example.com'
const client = axios.create({baseUrl, httpsAgent});
client.get('/something').then(res => res.data);

Solution 2:[2]

To the people who are looking for an updated answer here what's working for me. Make sure your tor instance running on another terminal tab or in the background.

It's just a regular axios setup but we need to pass the httpsAgent parameter created by socks-proxy-agent library.

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

const run = async () => {
  try {
    const httpsAgent = new SocksProxyAgent('socks://127.0.0.1:9050')

    const result = await axios({
      httpsAgent,
      method: 'get',
      url: 'https://api.ipify.org'
    })

    console.log(result.data)
    // 185.220.101.68
  } catch (err) {
    console.log(err.message)
  }
}

run()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Damian Grzanka
Solution 2 ozgrozer