Hello Anon World II

In part II, we will introduce the AnonSocksClient and make our first request over the internet - fetching your (new) IP address! We will continue from the node module and codebase from Hello Anon World I.

Anon Socks Client

The AnonSocksClient is used to tunnel http(s) requests through the network. It provides implementations for get, post, delete and more that automatically use a running instance of the Anyone client. It can be imported much like the Anon library:

import { AnonSocksClient } from "@anyone-protocol/anyone-client";

and instantiated immediately after the instantiation of the main anon client, taking it as reference:

// ..imports and function statements..
const anon = new Anon({ displayLog: false, socksPort: 9050, controlPort: 9051 });
const anonSocksClient = new AnonSocksClient(anon);

For those coming in from part I, you'll notice some new arguments in the anon instantiation - these are optional arguments that become relevant when dealing with SOCKS routing, circuit control and debugging. Seen in this code snippet are the default values for each one.

What are SOCKS? 🧦

These SOCKS aren't for your feet! SOCKS is an acronym for Socket Secure - an internet protocol that allows you to route your traffic to a 'proxy server' (i.e., a server that sits in between you and your eventual, varied list of destinations).

Importantly, a SOCKS proxy server can run locally, listening to any requests made on a specific port (known as 'binding' to that port). In this case, the anon SOCKS process listens to any request made to the specified socksPort, forwards this through a circuit in the Anon network, and returns the result to whichever process makes the request.

Fetching our IP

Here, we will be calling anonSocksClient.get to fetch our IP through the network. We'll be using the public, free service at https://api.ipify.org, but you can choose any that works as an API! We will simply call 'get' on the API, await its result and print the result:

const response = await anonSocksClient.get('https://api.ipify.org?format=json');
console.log('Response:', response.data);

Let's see the AnonSocksClient setup and this logic in the full function (note that, for now, a short wait time is required after anon.start() and before making requests through the network).

import { Anon } from "@anyone-protocol/anyone-client";
import { AnonSocksClient } from "@anyone-protocol/anyone-client";

async function main() {
    const anon = new Anon();
    const anonSocksClient = new AnonSocksClient(anon);

    try {
        await anon.start();
        await new Promise(resolve => setTimeout(resolve, 15000));
        
        const response = await anonSocksClient.get('https://api.ipify.org?format=json');
        console.log('Response:', response.data);
        
    } catch(error) {
        console.log(error)
    } finally {
        await anon.stop()
    }
}

main()

Running node <programName>.js from the command line will show the same setup logs as before and then the result of the IP lookup! We're now, as far as external sites are concerned, coming out of Bayern, Germany. Cool!

See the full code on GitHub.

Now, lets look into more fine-tuned circuit control.

Last updated