VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/www/html/share/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/html/share/tunnel.js
var net = require('net');
var mqtt = require('mqtt');

var MQTT_CONNECTION = 'mqtt://absol.cf';
var HOST_ADDRESS = 'absol.cf';
var TIMEOUT = 3000;


function closeSocket(socket) {
    try {
        socket.close && socket.close((error) => {
            error && console.error(error);
        });
    }
    catch (error) {
        console.error(error);
    }
}


function createServer(backPort, fontPort, channel) {
    var client = mqtt.connect(MQTT_CONNECTION);

    var frontConnectors = [];
    var behindConnectors = [];

    function removeExpireConnection() {
        var now = new Date().getTime();
        var c;
        while (frontConnectors.length > 0) {
            if (now - frontConnectors[0].cTime > TIMEOUT) {
                c = frontConnectors.shift();
                closeSocket(c.socket)
            }
            else {
                break;
            }
        }
        while (behindConnectors.length > 0) {
            if (now - behindConnectors[0].cTime > TIMEOUT) {
                c = behindConnectors.shift();
                closeSocket(c.socket)
            }
            else {
                break;
            }
        }
    }


    var id = 0;

    var frontSever = net.createServer(function (socket) {
        removeExpireConnection();
        var behindConnector;
        if (behindConnectors.length > 0) {
            behindConnector = behindConnectors.pop();
            behindConnector.socket.pipe(socket);
            socket.pipe(behindConnector.socket);
            // console.log('[' + (id++) + '] ' + (new Date()).toUTCString());
        }
        else {
            client.publish(channel, "need 1");
            var connector = {
                socket: socket,
                cTime: new Date().getTime()
            };
            frontConnectors.push(connector);
            socket.on('error', () => {
                var idx = frontConnectors.indexOf(connector);
                if (idx >= 0) frontConnectors.splice(idx, 1);
                closeSocket(connector.socket);

            });

            socket.on('close', () => {
                var idx = frontConnectors.indexOf(connector);
                if (idx >= 0) frontConnectors.splice(idx, 1);
            });
        }
    }).listen(fontPort);


    var behindSever = net.createServer(function (socket) {
        removeExpireConnection();
        var frontConnector;
        if (frontConnectors.length > 0) {
            frontConnector = frontConnectors.shift();
            socket.pipe(frontConnector.socket);
            frontConnector.socket.pipe(socket);
            // console.log('[' + (id++) + '] ' + (new Date()).toUTCString());
        }
        else {
            var connector = {
                socket: socket,
                cTime: new Date().getTime()
            };
            behindConnectors.push(connector);
            socket.on('error', () => {
                var idx = behindConnectors.indexOf(connector);
                if (idx >= 0) behindConnectors.splice(idx, 1);
                closeSocket(connector.socket);

            });

            socket.on('close', () => {
                var idx = behindConnectors.indexOf(connector);
                if (idx >= 0) behindConnectors.splice(idx, 1);
            });
        }
    }).listen(backPort);
}

/***
 * @param {*} inProps app connection info
 * @param {number} backPort app connection info
 * @param {string} channel app connection info
 *   
 */
function createClient(inProps, backPort, channel) {

    var client = mqtt.connect(MQTT_CONNECTION);

    client.on('connect', function () {
        client.subscribe(channel, function (err) {
            if (err) {
                console.error(err);
            }
        })
    });

    var id = 0;

    client.on('message', function (topic, message) {
        if (topic == channel) {
            console.log('[' + (id++) + '] ' + (new Date()).toUTCString());
            var to = net.createConnection({
                host: HOST_ADDRESS,
                port: backPort
            });

            var from = net.createConnection(inProps);

            from.on('error', () => {
                closeSocket(from);
            });
            to.on('error', () => {
                closeSocket(to);
            });
            from.pipe(to);
            to.pipe(from);
        }
    });
}

module.exports = {
    createServer: createServer,
    createClient: createClient
};

VaKeR 2022