98 lines
2.2 KiB
JavaScript
Executable File
98 lines
2.2 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const path = require('path');
|
|
const pkg = require('../package.json');
|
|
const fs = require('fs');
|
|
const version = pkg.version;
|
|
const PeerServer = require('../src').PeerServer;
|
|
const opts = require('optimist')
|
|
.usage('Usage: $0')
|
|
.options({
|
|
expire_timeout: {
|
|
demand: false,
|
|
alias: 't',
|
|
description: 'timeout (milliseconds)',
|
|
default: 5000
|
|
},
|
|
concurrent_limit: {
|
|
demand: false,
|
|
alias: 'c',
|
|
description: 'concurrent limit',
|
|
default: 5000
|
|
},
|
|
key: {
|
|
demand: false,
|
|
alias: 'k',
|
|
description: 'connection key',
|
|
default: 'peerjs'
|
|
},
|
|
sslkey: {
|
|
demand: false,
|
|
description: 'path to SSL key'
|
|
},
|
|
sslcert: {
|
|
demand: false,
|
|
description: 'path to SSL certificate'
|
|
},
|
|
port: {
|
|
demand: true,
|
|
alias: 'p',
|
|
description: 'port'
|
|
},
|
|
path: {
|
|
demand: false,
|
|
description: 'custom path',
|
|
default: '/'
|
|
},
|
|
allow_discovery: {
|
|
demand: false,
|
|
description: 'allow discovery of peers'
|
|
},
|
|
proxied: {
|
|
demand: false,
|
|
description: 'Set true if PeerServer stays behind a reverse proxy',
|
|
default: false
|
|
}
|
|
})
|
|
.boolean('allow_discovery')
|
|
.argv;
|
|
|
|
process.on('uncaughtException', function (e) {
|
|
console.error('Error: ' + e);
|
|
});
|
|
|
|
if (opts.sslkey || opts.sslcert) {
|
|
if (opts.sslkey && opts.sslcert) {
|
|
opts.ssl = {
|
|
key: fs.readFileSync(path.resolve(opts.sslkey)),
|
|
cert: fs.readFileSync(path.resolve(opts.sslcert))
|
|
};
|
|
|
|
delete opts.sslkey;
|
|
delete opts.sslcert;
|
|
} else {
|
|
console.error('Warning: PeerServer will not run because either ' +
|
|
'the key or the certificate has not been provided.');
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
const userPath = opts.path;
|
|
const server = PeerServer(opts, server => {
|
|
var host = server.address().address;
|
|
var port = server.address().port;
|
|
|
|
console.log(
|
|
'Started PeerServer on %s, port: %s, path: %s (v. %s)',
|
|
host, port, userPath || '/', version
|
|
);
|
|
});
|
|
|
|
server.on('connection', client => {
|
|
console.log(`Client connected: ${client.getId()}`);
|
|
});
|
|
|
|
server.on('disconnect', client => {
|
|
console.log(`Client disconnected: ${client.getId()}`);
|
|
});
|