Node.js прокси socks5 с пересылкой запросов SOAP - PullRequest
0 голосов
/ 13 июня 2019

Я пытаюсь воспроизвести эту команду ssh с помощью node.js: ssh -D 1080 -N user@host -i key.pem, чтобы использовать этот туннель для выполнения запросов SOAP.

Моя реализация API SOAP API nodeAP.js с использованием порта 1080 выглядит следующим образом (и работает с запущенной командой ssh):

const
    Agent = require('socks5-https-client/lib/Agent'),
    request = require('request'),
    soap = require('strong-soap').soap,
    url = 'https://example.com/wsdl', //path to wsdl
    options = {};

const req = request.defaults({
    agentClass: Agent,
    agentOptions: {
        socksPort: 1080
    }
})

options.request = req

const payload = {
    field1: 'value1',
    field2: 'value2',
    fieldn: 'valuen'
}

soap.createClient(url, options, function (err, client) {
    if (err) console.log(err)
    let description = client.describe()

    console.log(JSON.stringify(description.ExampleService.ExamplePort.example_method))

    client.example_method(payload, function (err, result, envelope, soapHeader) {
        if (err) console.log(err)
        else console.log(JSON.stringify(result))
    })
})

И все работает так, как ожидается, client.describe() и client.example_method().

Проблема возникает при попытке создания носковпрокси с узлом.Полный (цензурированный) код ниже:

const
    Agent = require('socks5-https-client/lib/Agent'),
    socks = require('socksv5'),
    Client = require('ssh2').Client,
    request = require('request'),
    config = {
        localProxy: {
            host: '127.0.0.1',
            port: 1080
        },
        sshConfig: {
            host: 'hostIP',
            port: 22,
            username: 'user',
            privateKey: require('fs').readFileSync('./key.pem')
        }
    },
    soap = require('strong-soap').soap,
    url = 'https://example.com/wsdl', //path to wsdl
    options = {};

const req = request.defaults({
    agentClass: Agent,
    agentOptions: {
        socksPort: 1080
    }
})

options.request = req

const payload = {
    field1: 'value1',
    field2: 'value2',
    fieldn: 'valuen'
}

let conn = new Client()
let server = socks.createServer(function (info, accept, deny) {
    conn.on('ready', function () {
        conn.forwardOut(info.srcAddr,
            info.srcPort,
            info.dstAddr,
            info.dstPort,
            function (err, stream) {
                if (err) {
                    conn.end()
                    return deny()
                }

                let clientSocket
                if (clientSocket = accept(true)) {
                    stream.pipe(clientSocket).pipe(stream).on('close', function () {
                        conn.end()
                    })
                } else {
                    conn.end()
                }
            })
    }).on('error', function (err) {
        deny()
    }).connect(config.sshConfig)
}).listen(config.localProxy.port, config.localProxy.host, function () {
    console.log('SOCKSv5 proxy server started on ' + config.localProxy.host + ':' + config.localProxy.port)

    soap.createClient(url, options, function (err, client) {
        if (err) console.log(err)
        let description = client.describe()

        console.log(JSON.stringify(description.ExampleService.ExamplePort.example_method)) // works : print the json response

        client.example_method(payload, function (err, result, envelope, soapHeader) {
            if (err) console.log(err)
            else console.log(JSON.stringify(result))
        })
    }) // get an error
}).useAuth(socks.auth.None())

Описание метода example_method хорошо напечатано, поэтому я уверен, что туннелирование работает (потому что методы не могут работать без него, так как естьОграничение IP, которое принимает только запросы от хост-сервера).Ошибка возникает при вызове client.example_method ().Вывод:

Error: SOCKS connection failed. Connection not allowed by ruleset.
    at Socket.<anonymous> (xxxxx/node_modules/socks5-client/lib/Socket.js:246:25)
    at Object.onceWrapper (events.js:285:13)
    at Socket.emit (events.js:197:13)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:145:17)

Есть идеи, чего не хватает?Спасибо!

...