Node.js forward http request from server 'net' for expression

I am running a flash socket policy server on port 8484. At the same port, I need to receive http requests. I am going to check if the policy file was requested (inside the if statement below), and if not, send the http request to another port where the express is running (say, localhost: 3000). How can i get this?

// 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);
});
+3
source share
1 answer

I solved this problem a while ago, but forgot about the question :) The solution was to create a socket that allowed sending and retrieving data between the 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'));
});
+1
source

All Articles