node.js 并表达:如何等待 udp 响应

node.js and express : how to wait for udp response

我正在学习node.js这里的编程,遇到了一个问题。

当 express 收到 POST 请求时,它应该使用 dgram 模块通过 UDP 进行 radius 身份验证。 Radius 认证的响应稍后出现,但是 POST 请求回调已经退出并且 req 和 res 变量丢失并且无法使用相同的 TCP 连接进行响应。

如果我应该等待 radius 响应(相当快),我应该如何在 node.js & express 中等待?

下面的代码执行基本的 POST 处理和 radius 身份验证。

非常感谢您的任何提示。


    var http = require('http');
    var express = require('express');
    var bodyParser = require('body-parser');
    var radius = require('radius');
    var dgram = require('dgram');

    var radius_secret = 'really_secret';
    var radius_server = '127.0.0.1';
    var radius_port = '1812';

    function handleAuthentication(req, res) {
        console.log("BODY:",req.body);
        var client = dgram.createSocket("udp4");
        var account = req.body.account; 
        var password = req.body.password;
        var packet = {
            code: 'Access-Request',
            secret: radius_secret,
            identifier: 0,
            attributes: [
                ['NAS-IP-Address', radius_server],
                ['User-Name', account + "@exampledomain.something"],
                ['User-Password', password]
            ]
        };

        client.on('error', function(e) {
                throw e;
            });

        client.on('message', function(msg, rinfo) {
            var radius_response = radius.decode({packet: msg, secret: radius_secret});
            console.log(radius_response);
        });

        var encoded = radius.encode(packet);
        client.send(encoded, 0, encoded.length, radius_port, radius_server);
    }

    var app = express();
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false}));

    app.post('/authenticate', function(req, res) {
        // Authenticate against radius server
        handleAuthentication(req, res);

        // The radius response is not received yet, and we will exit the post request
        // here and loose the req and res. 
    });

    var server = http.createServer(app);
    server.listen(80);

您需要在 client 回调中做出响应。因此,例如,在消息后响应:

client.on('message', function(msg, rinfo) {
     var radius_response = radius.decode({packet: msg, secret: radius_secret});
     console.log(radius_response);
     res.send('somethingCalculatedWithUDPResponse');
 });

您应该在 error 回调中以类似的方式处理它。

resreq 不会 "die" 当函数结束时,它们仍然可以被作为回调传递的闭包引用。

如果您无法在 handleAuthentication 函数中使用 res,或者出于某些代码设计目的不想响应那里的客户端,您可以使用新的 JS async/await 函数。

app.post('/authenticate', async function(req, res) {
    // Authenticate against radius server
    try{
        let udpResponse = await handleAuthentication(req, res);
    } catch (error){
        // handle error. use 'res' to notify about error
    }

    // if ok
    res.send("your response")

    //or better
    res.send(udpResponse)
});

在函数内:

client.on('message', function(msg, rinfo) {
        var radius_response = radius.decode({packet: msg, secret: radius_secret});
        return radius_response;
    });

Async/await 会将函数包装到一个 promise 中,并且 return 语句将在后台相应地转换为 resolve/reject,然后您可以在函数外做任何您想做的事情.