@derhuerst/socks-agent-with-socket-ttl

Node.js SOCKS5 Agent with a configurable sockets TTL.

Usage no npm install needed!

<script type="module">
  import derhuerstSocksAgentWithSocketTtl from 'https://cdn.skypack.dev/@derhuerst/socks-agent-with-socket-ttl';
</script>

README

socks-agent-with-socket-ttl

Node.js SOCKS5 Agent with a configurable sockets time to live (TTL).

npm version ISC-licensed minimum Node.js version support me via GitHub Sponsors chat with me on Twitter

Installation

npm install @derhuerst/socks-agent-with-socket-ttl

Usage

Let's say you want to send requests through the Tor network, via your locally install Tor node, which exposes a SOCKS5 proxy on 127.0.0.1:9050. You have configured it to pick a new circuit (the Tor term for the chain of Tor nodes your traffic is sent through) every 60s, usually giving you a new public IP every time; But that only applies to new sockets you're creating, exiting ones will keep their old circuit and thus their old public IP. Therefore, you want to keep sockets open (e.g. for HTTP Keep-Alive) for 60s, to improve response latency, but closes them every 60s to make use of your changing public IP.

const socksAgentWithSocketTTL = require('@derhuerst/socks-agent-with-socket-ttl')
const fetch = require('node-fetch')

const torSocks5Agent = socksAgentWithSocketTTL({
    // connect to local Tor node
    host: '127.0.0.1',
    port: 9050,
    // close sockets when they get 60s old
    socketTTL: 60 * 1000,
})

while (true) {
    const res = await fetch('https://wtfismyip.com/text', {
        agent: torSocks5Agent,
        headers: {
            'user-agent': '<identifier of your program>',
            'connection': 'keep-alive',
        },
    })
    const ip = await res.text()
    console.log('public IP is', ip)

    await new Promise(r => setTimeout(r, 10 * 1000)) // wait for 10s
}

Contributing

If you have a question or need support using socks-agent-with-socket-ttl, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use the issues page.