1

ネット ソケット接続の最初のパケットのヘッダーを見て、そこから何をするかを決定したいと考えています。

TCP の例 (機能するが役に立たない)

これは私の問題を解決しないため役に立ちません。理論的には問題が解決可能であることを示しているだけです。

'use strict';

var net = require('net');
var http = require('http');

var http80 = http.createServer(function (req, res) {
  res.end('Hello, World!');
});

var tcp80 = net.createServer(function (socket) {

  socket.once('data', function (chunk) {

    if (/http\/1/i.test(chunk.toString())) {
      console.log("looks like http, continue");
      http80.emit('connection', socket);
    } else {
      console.log("looks like tcp, die");
      socket.end();
    }

    socket.pause();
    process.nextTick(function () {
      socket.emit('data', chunk);
      socket.resume();
    });
  });

});

tcp80.listen(80, function () {
  console.log('listening on 80');
});

TLS の例 (機能しません)

これは私が実際にやろうとしていることですが、うまくいきません:

'use strict';

var net = require('net');
var sni = require('sni');
var https = require('https');
var tlsOpts = require('localhost.daplie.com-certificates').merge({});

var https443 = https.createServer(tlsOpts, function (req, res) {
  res.end('Hello, Encrypted World!');
});

var tcp443 = net.createServer(function (socket) {

  // looking at the first packet, this is the gold
  socket.once('data', function (chunk) {

    // undo the read, more or less
    socket.pause();
    process.nextTick(function () {
      socket.emit('data', chunk);
      socket.resume();
    });

    if (/^tcp\.example\.com/i.test(sni(chunk))) {
      console.log("TODO: handle as raw tls / tcp");
      return;
    }

    console.log("handling as https");
    https443.emit('connection', socket);
  });

});

tcp443.listen(443, function () {
  console.log('listening on 443');
});

readableイベントを手動で発行するだけでなくdata、チャンクと ing を使用してイベントを手動で発行しようとしましresumeたが、上記の例のように機能するのではなく、これらすべてのケースでハングしているようです。

4

0 に答える 0