Proxy Service

Published at: Jan. 22, 2019, 4:09 a.m. in the Documentation

For the convenience of working with external data sources, we added the ability to use a proxy (socks and http(s)).

Remember, data passing through any proxies can be intercepted by a third party, do not send sensitive or confidential information through them!

This service is temporarily free (but in the future, a fee will be charged for use).
At the same time, Smappi Proxy allows you to make money, upload your socks and http(s) proxies and get rewarded if someone uses them.

Proxy usage

This is a manual method for working with a proxy, which is suitable for any situation.

In the simplest case, it is enough to import the proxy module and call the next function, the first call activates work with the proxy, later the next call will switch to the new proxy according to the list from Smappi Proxy:

const { proxy } = require('smappi-cl');

function next () {
    let ips = [];
    for (let i = 0; i < 10; i++) {
        proxy.next();
        let response = request.get('https://proxy.smappi.org/check').json();
        ips.push(response.ip_addr);
    }
    return ips;
}

You can pass to the next desired protocol, such as "http" (the default is "socks"). Or you can specify undefined if you want all protocols to be used, however some of the proxies will have one external IP address (for example, when http is used as a mirror for socks).

proxy.next({protocol: 'http'});

To get information about the used proxy, you should call the current or currentHref method:

const { proxy } = require('smappi-cl');

function next () {
    for (let i = 0; i < 10; i++) {
        proxy.next();
        proxy.current();
        // Return: {host: 'proxy.smappi.org', port: 33123, protocol: 'socks', speed: 0.7409, cnt: 1, ipaddr: '178.128.139.241'}
        proxy.currentHref();
        // Return: socks://178.128.139.241:33123
    }
}

If you want a proxy not to be used again, you can call proxy.skip():

const { proxy } = require('smappi-cl');

function nextAndSkip () {
    let ips = [];
    for (let i = 0; i < 10; i++) {
        proxy.next();
        let response = request.get('https://proxy.smappi.org/check').json();
        ips.push(response.ip_addr);
        proxy.skip();
    }
    return ips;
}

If you switch the proxy (call the skip method), then the previous proxy will be marked as rejected and will not be used again.

Helper for working with request

To simplify working with Request Library, the withProxy method was invented, the condition function is passed to it, if the condition is met, then the response is considered correct, otherwise the next proxy in the list is switched from Smappi Proxy, but no more than 10 times.

Short example:

request.withProxy(resp => resp.status == 200).get('https://proxy.smappi.org/check')

Full example:

const { request } = require('smappi-cl');

function condition (response) {
    return response.status == 200 && response.content.indexOf('Blocked') == -1;
}

function withProxy () {
    var ips = [];
    for (let i = 0; i < 10; i++) {
        let response = request.withProxy(condition)
            .get('https://proxy.smappi.org/check')
            .json();
        ips.push(response.ip_addr);
    }
    return ips;
}