node.js forward http request from 'net' server to express

4k views Asked by At

I'm running the flash socket policy server on port 8484. On the same port I need to receive http requests. I'm thinking about checking whether policy-file was requested (inside the if statement below), and if it wasn't - forwarding the http request to another port where express is running (let's say localhost:3000). How can I obtain that?

// flash socket policy server
var file = '/etc/flashpolicy.xml',
    host = 'localhost',
    port =  8484,
    poli = 'something';

var fsps = require('net').createServer(function (stream) {
    stream.setEncoding('utf8');
    stream.setTimeout(10000);
    stream.on('connect', function () {
        console.log('Got connection from ' + stream.remoteAddress + '.');
    });
    stream.on('data', function (data) {
        console.log(data);
        var test = /^<policy-file-request\/>/;
        if (test.test(data)) {
            console.log('Good request. Sending file to ' + stream.remoteAddress + '.')
            stream.end(poli + '\0');
        } else {
            console.log('Not a policy file request ' + stream.remoteAddress + '.');
            stream.end('HTTP\0');

            // FORWARD REQUEST TO localhost:3000 for example //

        }
    });
    stream.on('end', function () {
        stream.end();
    });
    stream.on('timeout', function () {
        console.log('Request from ' + stream.remoteAddress + ' timed out.');
        stream.end();
    });
});

require('fs').readFile(file, 'utf8', function (err, poli) {
    if (err) throw err;
    fsps.listen(port, host);
    console.log('Flash socket policy server running at ' + host + ':' + port + ' and serving ' + file);
});
1

There are 1 answers

3
Jacka On BEST ANSWER

I solved this problem a while ago, but have forgotten about the question :) The solution was to create a socket which made it possible to send and retrieve data between http express server and tcp flash policy server.

flash policy server:

var file = process.argv[2] || '/etc/flashpolicy.xml',
    host = process.argv[3] || 'localhost',
    port = process.argv[4] || 8484,
    poli = 'flash policy data\n',
    net  = require('net'),
    http = require('http');

var fsps = net.createServer(function (stream) {
    stream.setEncoding('utf8');
    stream.on('connect', function () {
        console.log('Got connection from ' + stream.remoteAddress + '.');
    });
    stream.on('data', function (data) {
        var test = /^<policy-file-request\/>/;
        if (test.test(data)) {
            console.log('Good request. Sending file to ' + stream.remoteAddress + '.')
            stream.end(poli + '\0');
        } else {
            console.log('Not a policy file request ' + stream.remoteAddress + '.');

            var serviceSocket = new net.Socket();
            serviceSocket.connect(3000, 'localhost', function () {
                console.log('>>>> Data from 8484 to 3000 >>>>\n', data.toString());
                serviceSocket.write(data);
            });
            serviceSocket.on("data", function (received_data) {
                console.log('<<<< Data from 3000 to 8484 to client <<<<\n', received_data.toString());
                stream.write(received_data);
            });
        }
    });
    stream.on('end', function () {
        console.log('tcp server disconnected');
    });
    stream.on('timeout', function () {
        console.log('Request from ' + stream.remoteAddress + ' timed out.');
    });
});

require('fs').readFile(file, 'utf8', function (err, poli) {
    if (err) throw err;
    fsps.listen(port, host);
    console.log('Flash socket policy server running at ' + host + ':' + port + ' and serving ' + file);
});

sample express server on localhost:3000:

var express = require('express')
  , http = require('http')
  , path = require('path')
  , app = express();

// all environments
app.set('port', process.env.PORT || 3000);

app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', function(req, res){
  res.send('Proper HTTP response');
});

app.post('/', function(req, res){
  console.log(req.body);
  res.send('Proper HTTP response');
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express HTTP server listening on port ' + app.get('port'));
});