【问题标题】:How do I get a client's IP address from inside a soap service?(nodejs)如何从 soap 服务中获取客户端的 IP 地址?(nodejs)
【发布时间】:2022-12-13 06:16:21
【问题描述】:

我使用 soap package 编写了一个 soapService,我将一个快速服务器传递给它。

问题是服务器可以从 2 个不同的网络接口获取请求,而我想知道请求来自哪个网络接口。

我的解决方案是获取客户端的 IP 并确定它来自使用哪个接口

require("os").NetworkInterfaces()

但我找不到如何获取客户的 IP

我试过了: this.req.ip, this.httpHeaders["x-forwarded-for"] || this.req.connection.remoteAddres 但它是未定义的

编辑:我想添加一个最小的测试示例。

创建了3个文件: soapserver.js(包括我想从其中获取 ip 的 soap 服务) client.js(调用 soapservice) check_username.wsdl(用于创建服务)

soapserver.js:

var soap = require('soap');
var http = require('http');
const util = require('util');
const app = require("express")()

var myService = {
    CheckUserName_Service: {
        CheckUserName_Port: {
            checkUserName: function(args, soapCallback) { 
                console.log('checkUserName: Entering function..');
                console.log(args);
                /*
                 * Where I'm trying to get clietn's IP address
                 */
                soapCallback("{'username found'}");
            }
        }
    }   
};


var xml = require('fs').readFileSync('check_username.wsdl', 'utf8');
var server = require("http").Server(app);    
app.get('/', (req, res) => {
    res.send("Hello World!");
    console.log(req);
    console.log(req.connection.remoteAddress);
    console.log(req.ip);
});

var port = 8000;
server.listen(port);

var soapServer = soap.listen(server, '/test', myService, xml);
soapServer.log = function(type, data) {
    console.log('Type: ' + type + ' data: ' + data);
};

console.log('SOAP service listening on port ' + port);

客户端.js:

"use strict";

var soap = require('strong-soap').soap;
var url = 'http://localhost:8000/test?wsdl';

var options = { endpoint: 'http://localhost:8000/test'};
var requestArgs = { userName: "TEST_USER" };
soap.createClient(url, options, function(err, client) {
  if (err) {
      console.error("An error has occurred creating SOAP client: " , err);  
  } else {
      var description = client.describe();
      console.log("Client description:" , description);
      var method = client.checkUserName;
      method(requestArgs, function(err, result, envelope, soapHeader) {
        //response envelope
        console.log('Response Envelope: \n' + envelope);
        //'result' is the response body
        console.log('Result: \n' + JSON.stringify(result));
      });
  }
});

检查用户名.wsdl

<definitions name = "CheckUserNameService"
   targetNamespace = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
   xmlns = "http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:tns = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
   xmlns:xsd = "http://www.w3.org/2001/XMLSchema">

   <message name = "CheckUserNameRequest">
      <part name = "userName" type = "xsd:string"/>
   </message>
   <message name = "CheckUserNameResponse">
      <part name = "status" type = "xsd:string"/>
   </message>
   <portType name = "CheckUserName_PortType">
      <operation name = "checkUserName">
         <input message = "tns:CheckUserNameRequest"/>
         <output message = "tns:CheckUserNameResponse"/>
      </operation>
   </portType>

   <binding name = "CheckUserName_Binding" type = "tns:CheckUserName_PortType">
      <soap:binding style = "rpc"
         transport = "http://schemas.xmlsoap.org/soap/http"/>
      <operation name = "checkUserName">
         <soap:operation soapAction = "checkUserName"/>
         <input>
            <soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
         </input>
         <output>
            <soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
         </output>
      </operation>
   </binding>

   <service name = "CheckUserName_Service">
      <documentation>WSDL File for CheckUserNameService</documentation>
      <port binding = "tns:CheckUserName_Binding" name = "CheckUserName_Port">
         <soap:address
            location = "http://www.examples.com/CheckUserName/" />
      </port>
   </service>
</definitions>

【问题讨论】:

  • 您已经在 /test 上启动了您的 SOAP 服务器,但试图从 / 获取 clientIP?我在这里错过了什么吗? req.connection.remoteAddress 将为您提供 /test 上的客户端 IP。
  • @JishanShaikh 不,我想在 soap 服务代码中做类似的事情,我在其中留下了“我试图获取客户端 IP 地址的地方”评论

标签: node.js soap soapserver


【解决方案1】:

好了,工作代码。在 Localhost 和 remoteProd(云托管)上进行了原型测试。

服务器.js

var soap = require('soap');
var http = require('http');
const util = require('util');
const fetch = require('node-fetch'); // npm i node-fetch@2.6.7, to avoid ESM import errors
const app = require("express")()

newApp = app;
var server = http.Server(app);  
var port = 8001;
server.listen(port);
console.log("http server started listening on " + port)

newApp.get('/', (req, res) => {
    clientIP = req.ip; // works
    // clientIP = req.connection.remoteAddress; // also works
    res.send("Hello World!");
    var myService = {
        CheckUserName_Service: {
            CheckUserName_Port: {
                checkUserName: function(args, soapCallback) { 
                    console.log('checkUserName: Entering function..');
                    /*
                     * Where I'm trying to get clietn's IP address
                     */
                    console.log(clientIP);
                    soapCallback("Client IP: " + clientIP);
                }
            }
        }   
    };
    var xml = require('fs').readFileSync('myservice.wsdl', 'utf8');
 
    var soapServer = soap.listen(server, '/test', myService, xml);
    soapServer.log = function(type, data) {
        //console.log('Type: ' + type + ' data: ' + data);
    };
    console.log('SOAP service attached on port ' + port);
    console.log('Waiting for client...');
});

// To execute / in order to start attachment of SOAP server to http server
fetch('http://localhost:8001', { method: 'GET' })
  .then((response) => {
    // do something with the response
    // console.log("fetch then");
  })
  .catch((error) => {
    // handle any errors
    console.log("fetch error" + error);
  })

客户端.js

"use strict";

var soap = require('strong-soap').soap;
var url = 'http://localhost:8001/test?wsdl';

var options = { endpoint: 'http://localhost:8001/test'};
var requestArgs = { userName: "TEST_USER" };
soap.createClient(url, options, function(err, client) {
  if (err) {
      console.error("An error has occurred creating SOAP client: " , err);  
  } else {
      var description = client.describe();
      //console.log("Client description:" , description);
        //console.log(description.CheckUserName_Service.CheckUserName_Port.checkUserName.output.body.elements)
      var method = client.checkUserName;
      method(requestArgs, function(err, result, envelope, soapHeader) {
        //response envelope
        //console.log("error "+ err) // null        
        //console.log(soapHeader); // undefined
        //console.log('Response Envelope: 
' + envelope);
        //'result' is the response body
        //console.log('Result' + result);
        console.log('Result: 
' + JSON.stringify(result));
      });
  }
});

我的服务.wsdl:请注意我重命名了它,我不知道为什么。

<definitions name = "CheckUserNameService"
   targetNamespace = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
   xmlns = "http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:tns = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
   xmlns:xsd = "http://www.w3.org/2001/XMLSchema">

   <message name = "CheckUserNameRequest">
      <part name = "userName" type = "xsd:string"/>
   </message>
   <message name = "CheckUserNameResponse">
      <part name = "status" type = "xsd:string"/>
   </message>
   <portType name = "CheckUserName_PortType">
      <operation name = "checkUserName">
         <input message = "tns:CheckUserNameRequest"/>
         <output message = "tns:CheckUserNameResponse"/>
      </operation>
   </portType>

   <binding name = "CheckUserName_Binding" type = "tns:CheckUserName_PortType">
      <soap:binding style = "rpc"
         transport = "http://schemas.xmlsoap.org/soap/http"/>
      <operation name = "checkUserName">
         <soap:operation soapAction = "checkUserName"/>
         <input>
            <soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
         </input>
         <output>
            <soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
         </output>
      </operation>
   </binding>

   <service name = "CheckUserName_Service">
      <documentation>WSDL File for CheckUserNameService</documentation>
      <port binding = "tns:CheckUserName_Binding" name = "CheckUserName_Port">
         <soap:address
            location = "http://www.examples.com/CheckUserName/" />
      </port>
   </service>
</definitions>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 2012-03-14
    • 2012-02-16
    相关资源
    最近更新 更多