mirror of
https://github.com/bitinflow/server.git
synced 2026-03-13 13:35:53 +00:00
allow client to request previous id for reuse
This commit is contained in:
@@ -27,12 +27,15 @@ var opt = {
|
|||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
|
|
||||||
opt.uri = 'http://' + opt.host + ':' + opt.port + opt.path;
|
var base_uri = 'http://' + opt.host + ':' + opt.port + opt.path;
|
||||||
|
|
||||||
var internal;
|
var internal;
|
||||||
var upstream;
|
var upstream;
|
||||||
|
var prev_id;
|
||||||
|
|
||||||
(function connect_proxy() {
|
(function connect_proxy() {
|
||||||
|
opt.uri = base_uri + ((prev_id) ? prev_id : '');
|
||||||
|
|
||||||
request(opt, function(err, res, body) {
|
request(opt, function(err, res, body) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('upstream not available: %s', err.message);
|
console.error('upstream not available: %s', err.message);
|
||||||
@@ -43,6 +46,9 @@ var upstream;
|
|||||||
var port = body.port;
|
var port = body.port;
|
||||||
var host = opt.host;
|
var host = opt.host;
|
||||||
|
|
||||||
|
// store the id so we can try to get the same one
|
||||||
|
prev_id = body.id;
|
||||||
|
|
||||||
console.log('your url is: %s', body.url);
|
console.log('your url is: %s', body.url);
|
||||||
|
|
||||||
// connect to remote tcp server
|
// connect to remote tcp server
|
||||||
@@ -52,6 +58,7 @@ var upstream;
|
|||||||
connect_internal();
|
connect_internal();
|
||||||
|
|
||||||
upstream.on('end', function() {
|
upstream.on('end', function() {
|
||||||
|
console.log('> upstream connection terminated');
|
||||||
|
|
||||||
// sever connection to internal server
|
// sever connection to internal server
|
||||||
// on reconnect we will re-establish
|
// on reconnect we will re-establish
|
||||||
|
|||||||
44
server.js
44
server.js
@@ -4,6 +4,19 @@ var http = require('http');
|
|||||||
var net = require('net');
|
var net = require('net');
|
||||||
var FreeList = require('freelist').FreeList;
|
var FreeList = require('freelist').FreeList;
|
||||||
|
|
||||||
|
var argv = require('optimist')
|
||||||
|
.usage('Usage: $0 --port [num]')
|
||||||
|
.options('port', {
|
||||||
|
default: '80',
|
||||||
|
describe: 'listen on this port for outside requests'
|
||||||
|
})
|
||||||
|
.argv;
|
||||||
|
|
||||||
|
if (argv.help) {
|
||||||
|
require('optimist').showHelp();
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
|
||||||
// here be dragons
|
// here be dragons
|
||||||
var HTTPParser = process.binding('http_parser').HTTPParser;
|
var HTTPParser = process.binding('http_parser').HTTPParser;
|
||||||
var ServerResponse = http.ServerResponse;
|
var ServerResponse = http.ServerResponse;
|
||||||
@@ -230,9 +243,30 @@ server.on('connection', function(socket) {
|
|||||||
|
|
||||||
server.on('request', function(req, res) {
|
server.on('request', function(req, res) {
|
||||||
|
|
||||||
// generate new shit for client
|
// ignore favicon
|
||||||
var id = rand_id();
|
if (req.url === '/favicon.ico') {
|
||||||
|
res.writeHead(404);
|
||||||
|
return res.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
var match = req.url.match(/\/([a-z]{4})?/);
|
||||||
|
|
||||||
|
// user can request a particular set of characters
|
||||||
|
// will be given if not already taken
|
||||||
|
// this is useful when the main server is restarted
|
||||||
|
// users can keep testing with their expected ids
|
||||||
|
var requested_id;
|
||||||
|
if (match && match[1]) {
|
||||||
|
requested_id = match[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
var id = requested_id || rand_id();
|
||||||
|
if (wait_list[id]) {
|
||||||
|
// new id
|
||||||
|
id = rand_id();
|
||||||
|
}
|
||||||
|
|
||||||
|
// generate new shit for client
|
||||||
if (wait_list[id]) {
|
if (wait_list[id]) {
|
||||||
wait_list[id].forEach(function(waiting) {
|
wait_list[id].forEach(function(waiting) {
|
||||||
waiting.end();
|
waiting.end();
|
||||||
@@ -247,7 +281,7 @@ server.on('request', function(req, res) {
|
|||||||
var url = 'http://' + id + '.' + req.headers.host;
|
var url = 'http://' + id + '.' + req.headers.host;
|
||||||
|
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
res.end(JSON.stringify({ url: url, port: port }));
|
res.end(JSON.stringify({ url: url, id: id, port: port }));
|
||||||
});
|
});
|
||||||
|
|
||||||
// user has 5 seconds to connect before their slot is given up
|
// user has 5 seconds to connect before their slot is given up
|
||||||
@@ -279,5 +313,7 @@ server.on('request', function(req, res) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(8000);
|
server.listen(argv.port, function() {
|
||||||
|
log.info('server listening on port: %d', server.address().port);
|
||||||
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user