multiserver
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -45,7 +45,7 @@ var compose = require('./compose') | ||
stringify: function (scope) { | ||
if (!scope) scope = 'public' | ||
if (!scope) scope = 'device' | ||
return plugs | ||
.filter(function (plug) { | ||
return plug.scope() === scope || | ||
(plug.scope() === 'public' && scope === 'private') | ||
var _scope = plug.scope() | ||
return Array.isArray(_scope) ? ~_scope.indexOf(scope) : _scope === scope | ||
}) | ||
@@ -68,1 +68,2 @@ .map(function (plug) { return plug.stringify(scope) }) | ||
{ | ||
"name": "multiserver", | ||
"description": "write a server which works over many protocols at once, or connect to the same", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"homepage": "https://github.com/dominictarr/multiserver", | ||
@@ -6,0 +6,0 @@ "repository": { |
@@ -16,13 +16,22 @@ var net | ||
module.exports = function (opts) { | ||
// Choose a dynamic port between 49152 and 65535 | ||
// https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic,_private_or_ephemeral_ports | ||
var port = opts.port || Math.floor(49152 + (65535 - 49152 + 1) * Math.random()) | ||
var host = opts.host || opts.scope && scopes.host(opts.scope) || 'localhost' | ||
var scope = opts.scope || 'device' | ||
// FIXME: does this even work anymore? | ||
opts.allowHalfOpen = opts.allowHalfOpen !== false | ||
function isScoped (s) { | ||
return s === scope || Array.isArray(scope) && ~scope.indexOf(s) | ||
} | ||
return { | ||
name: 'net', | ||
scope: function() { return opts.scope || 'public' }, | ||
scope: function() { | ||
return scope | ||
}, | ||
server: function (onConnection) { | ||
var port = opts.port | ||
var host = opts.host || opts.scope && scopes.host(opts.scope) || 'localhost' | ||
console.log('Listening on ' + host + ':' + port + ' (multiserver net plugin)') | ||
var server = net.createServer(opts, function (stream) { | ||
var addr = stream.address() | ||
onConnection(toDuplex(stream)) | ||
@@ -76,6 +85,10 @@ }).listen(port, host) | ||
stringify: function (scope) { | ||
var host = scope == 'public' && opts.external || opts.host || scope && scopes.host(scope) || 'localhost' | ||
return ['net', host, opts.port].join(':') | ||
scope = scope || 'device' | ||
if(!isScoped(scope)) return | ||
var _host = (scope == 'public' && opts.external) || scopes.host(scope) | ||
if(!_host) return null | ||
return ['net', _host, port].join(':') | ||
} | ||
} | ||
} | ||
@@ -39,3 +39,3 @@ var socks = require('socks').SocksClient; | ||
console.error('unable to find local tor server.') | ||
console.error('will be able receive tor connections') // << ??? | ||
console.error('will be able receive tor connections') // << ??? | ||
return | ||
@@ -105,4 +105,5 @@ } | ||
}, | ||
stringify: function () { | ||
if(opts && !opts.server) return | ||
stringify: function (scope) { | ||
if(scope !== opts.scope) return null | ||
if(opts && !opts.server) return null | ||
return ['onion', opts.host, opts.port].join(':') | ||
@@ -109,0 +110,0 @@ } |
@@ -1,2 +0,2 @@ | ||
var toPull = require('stream-to-pull-stream') | ||
var toDuplex = require('stream-to-pull-stream').duplex | ||
var net = require('net') | ||
@@ -12,8 +12,8 @@ var fs = require('fs') | ||
const addr = 'unix:' + socket | ||
const scope = opts.scope || 'device' | ||
opts = opts || {} | ||
return { | ||
name: 'unix', | ||
scope: function() { return opts.scope || 'public' }, | ||
server: function (onConnection) { | ||
scope: function() { return scope }, | ||
server: !opts.server ? null : function (onConnection) { | ||
if(started) return | ||
@@ -23,3 +23,5 @@ console.log("listening on socket", addr) | ||
var server = net.createServer(opts, function (stream) { | ||
onConnection(toPull.duplex(stream)) | ||
stream = toDuplex(stream) | ||
stream.address = addr | ||
onConnection(stream) | ||
}).listen(socket) | ||
@@ -54,3 +56,3 @@ | ||
var started = false | ||
var stream = net.connect(opts) | ||
var stream = net.connect(opts.path) | ||
.on('connect', function () { | ||
@@ -60,3 +62,5 @@ if(started) return | ||
cb(null, toPull.duplex(stream)) | ||
var _stream = toDuplex(stream) | ||
_stream.address = addr | ||
cb(null, _stream) | ||
}) | ||
@@ -86,5 +90,6 @@ .on('error', function (err) { | ||
}, | ||
stringify: function () { | ||
if(opts && !opts.server) return | ||
return ['unix', opts.path].join(':') | ||
stringify: function (_scope) { | ||
if(scope !== _scope) return null | ||
if(opts && !opts.server) return null | ||
return ['unix', socket].join(':') | ||
} | ||
@@ -91,0 +96,0 @@ } |
@@ -6,15 +6,52 @@ var WS = require('pull-ws') | ||
var scopes = require('multiserver-scopes') | ||
var http = require('http') | ||
function safe_origin (origin, address, port) { | ||
//if the connection is not localhost, we shouldn't trust | ||
//the origin header. So, use address instead of origin | ||
//if origin not set, then it's definitely not a browser. | ||
if(!(address === '::1' || address === '128.0.0.1') || origin == undefined) | ||
return 'ws:' + address + (port ? ':' + port : '') | ||
//note: origin "null" (as string) can happen a bunch of ways | ||
// it can be a html opened as a file | ||
// or certain types of CORS | ||
// https://www.w3.org/TR/cors/#resource-sharing-check-0 | ||
// and webworkers if loaded from data-url? | ||
if(origin === 'null') | ||
return 'ws:null' | ||
//a connection from the browser on localhost, | ||
//we choose to trust this came from a browser. | ||
return origin.replace(/^http/, 'ws') | ||
} | ||
module.exports = function (opts) { | ||
opts = opts || {} | ||
opts.binaryType = (opts.binaryType || 'arraybuffer') | ||
var scope = opts.scope || 'device' | ||
function isScoped (s) { | ||
return s === scope || Array.isArray(scope) && ~scope.indexOf(s) | ||
} | ||
var secure = opts.server && !!opts.server.key | ||
return { | ||
name: 'ws', | ||
scope: function() { return opts.scope || 'public' }, | ||
scope: function() { return opts.scope || 'device' }, | ||
server: function (onConnect) { | ||
if(!WS.createServer) return | ||
opts.host = opts.host || opts.scope && scopes.host(opts.scope) || 'localhost' | ||
var server = WS.createServer(opts, function (stream) { | ||
stream.address = 'ws:'+stream.remoteAddress + (stream.remotePort ? ':'+stream.remotePort : '') | ||
// Choose a dynamic port between 49152 and 65535 | ||
// https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Dynamic,_private_or_ephemeral_ports | ||
opts.port = opts.port || Math.floor(49152 + (65535 - 49152 + 1) * Math.random()) | ||
var server = opts.server || http.createServer(opts.handler) | ||
var ws_server = WS.createServer(Object.assign({}, opts, {server: server}), function (stream) { | ||
stream.address = safe_origin( | ||
stream.headers.origin, | ||
stream.remoteAddress, | ||
stream.remotePort | ||
) | ||
onConnect(stream) | ||
@@ -59,4 +96,6 @@ }) | ||
}, | ||
stringify: function () { | ||
if(!WS.createServer) return | ||
stringify: function (scope) { | ||
scope = scope || 'device' | ||
if(!isScoped(scope)) return null | ||
if(!WS.createServer) return null | ||
var port | ||
@@ -68,6 +107,10 @@ if(opts.server) | ||
var host = (scope == 'public' && opts.external) || scopes.host(scope) | ||
//if a public scope was requested, but a public ip is not available, return | ||
if(!host) return null | ||
return URL.format({ | ||
protocol: secure ? 'wss' : 'ws', | ||
slashes: true, | ||
hostname: opts.host || 'localhost', //detect ip address | ||
hostname: host, | ||
port: (secure ? port == 443 : port == 80) ? undefined : port | ||
@@ -74,0 +117,0 @@ }) |
@@ -24,4 +24,4 @@ var tape = require('tape') | ||
var net = Net({port: 4848, scope: 'public'}) | ||
var ws = Ws({port: 4849, scope: 'public'}) | ||
var net = Net({port: 4848, scope: 'device'}) | ||
var ws = Ws({port: 4849, scope: 'device'}) | ||
var shs = Shs({keys: keys, appKey: appKey, auth: function (id, cb) { | ||
@@ -52,8 +52,9 @@ requested = id | ||
var server_addr = | ||
'fake:peer.ignore~nul:what;'+multi.stringify() | ||
'fake:peer.ignore~nul:what;'+multi.stringify('device') | ||
//"fake" in a unkown protocol, just to make sure it gets skipped. | ||
tape('connect to either server', function (t) { | ||
t.ok(multi.stringify('device')) | ||
multi.client(server_addr, function (err, stream) { | ||
@@ -70,3 +71,2 @@ if(err) throw err | ||
console.log("OUTPUT", data) | ||
// close() | ||
t.end() | ||
@@ -80,4 +80,2 @@ }) | ||
console.log(multi.stringify()) | ||
multi_ws.client(server_addr, function (err, stream) { | ||
@@ -84,0 +82,0 @@ if(err) throw err |
@@ -44,7 +44,7 @@ var tape = require('tape') | ||
t.equal( | ||
net.stringify(), | ||
net.stringify('device'), | ||
'net:localhost:4848' | ||
) | ||
t.equal( | ||
ws.stringify(), | ||
ws.stringify('device'), | ||
'ws://localhost:4848' | ||
@@ -57,14 +57,14 @@ ) | ||
t.equal( | ||
combined.stringify(), | ||
net.stringify()+'~'+shs.stringify() | ||
combined.stringify('device'), | ||
net.stringify('device')+'~'+shs.stringify('device') | ||
) | ||
t.equal( | ||
combined_ws.stringify(), | ||
combined_ws.stringify('device'), | ||
ws.stringify()+'~'+shs.stringify() | ||
) | ||
console.log(Compose([net, shs]).stringify()) | ||
console.log(Compose([net, shs]).stringify('device')) | ||
t.equal( | ||
MultiServer([combined, combined_ws]).stringify(), | ||
[combined.stringify(), combined_ws.stringify()].join(';') | ||
[combined.stringify('device'), combined_ws.stringify('device')].join(';') | ||
) | ||
@@ -88,3 +88,3 @@ | ||
combined.client(combined.stringify(), function (err, stream) { | ||
combined.client(combined.stringify('device'), function (err, stream) { | ||
if(err) throw err | ||
@@ -113,3 +113,3 @@ pull( | ||
var close = combined.server(echo) | ||
var addr = combined.stringify() | ||
var addr = combined.stringify('device') | ||
console.log('addr', addr) | ||
@@ -136,5 +136,6 @@ | ||
Net({ | ||
scope: 'device', | ||
port: 4848, | ||
host: 'localhost', | ||
external: scopes.host('private') // unroutable IP, but not localhost (e.g. 192.168 ...) | ||
// external: scopes.host('private') // unroutable IP, but not localhost (e.g. 192.168 ...) | ||
}), | ||
@@ -145,4 +146,15 @@ shs | ||
var addr = combined.stringify('public') // returns external | ||
console.log('addr public scope', addr) | ||
//fake | ||
var fake_combined = Compose([ | ||
Net({ | ||
scope: 'local', | ||
port: 4848, | ||
//host: 'localhost', | ||
// external: scopes.host('local') // unroutable IP, but not localhost (e.g. 192.168 ...) | ||
}), | ||
shs | ||
]) | ||
var addr = fake_combined.stringify('local') // returns external | ||
console.log('addr local scope', addr) | ||
combined.client(addr, function (err, stream) { | ||
@@ -222,3 +234,4 @@ t.ok(err, 'should only listen on localhost') | ||
var ws = Ws({ | ||
host: 'domain.de', | ||
external: 'domain.de', | ||
scope: 'public', | ||
server: { | ||
@@ -228,3 +241,5 @@ key: null, | ||
}}) | ||
t.equal(ws.stringify(), 'ws://domain.de') | ||
t.equal(ws.stringify('public'), 'ws://domain.de') | ||
t.equal(ws.stringify('local'), null) | ||
t.equal(ws.stringify('device'), null) | ||
t.end() | ||
@@ -236,3 +251,4 @@ }) | ||
var ws = Ws({ | ||
host: 'domain.de', | ||
external: 'domain.de', | ||
scope: 'public', | ||
server: { | ||
@@ -242,3 +258,5 @@ key: true, | ||
}}) | ||
t.equal(ws.stringify(), 'wss://domain.de') | ||
t.equal(ws.stringify('public'), 'wss://domain.de') | ||
t.equal(ws.stringify('local'), null) | ||
t.equal(ws.stringify('device'), null) | ||
t.end() | ||
@@ -248,7 +266,10 @@ }) | ||
var onion = Onion({server:false}) | ||
var onion = Onion({server:false, scope: 'public'}) | ||
tape('onion plug, server false', function (t) { | ||
t.notOk(onion.stringify(), null) | ||
t.notOk(onion.stringify('public')) | ||
t.equal(onion.stringify('device'), null) | ||
t.equal(onion.stringify('local'), null) | ||
t.deepEqual( | ||
@@ -255,0 +276,0 @@ onion.parse('onion:3234j5sv346bpih2.onion:2349'), |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
218017
20
6514