Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var tlsCheckOptions = ['cert', 'key', 'pfx', 'ca'];
var usingSSL = false;
Object.keys(options).forEach(function(k) {
httpOptions[k] = options[k];
if (tlsCheckOptions.indexOf(k) > -1) {
usingSSL = true;
}
});
// If any tls options were specified, use ssl and not plain
httpOptions.plain = (usingSSL) ? false : true;
httpOptions.ssl = (usingSSL) ? true : false;
this.server = spdy.createServer(httpOptions);
// internal server for z2z, allways ssl: false, plain: true
this.spdyServer = spdy.createServer({
windowSize: 1024 * 1024,
plain: true,
ssl: false
});
var ValidWSUrls = [
/^\/events$/, // /events
/^\/events\?.+$/, // /events?topic=query:where type="led"
/^\/servers\/(.+)\/events/, // /servers/BC2832FD-9437-4473-A4A8-AC1D56B12C61/events
/^\/peers\/(.+)$/, // /peers/123123...
/^\/peer-management$/, // /peer-management
];
function match(request) {
return ValidWSUrls.some(function(re) {
return re.test(request.url);
// write an head after opening a stream
// means the client receives the pushed stream before
// move it before res.push to try
res.writeHead(200);
res.end('hello world!');
stream.write('hello world');
});
server.on('connection', function(socket) {
console.log('muahhaa');
socket.setted = 'aaa';
});
var agent = spdy.createAgent({
host: 'localhost',
port: 1443,
rejectUnauthorized: false
});
agent.on('push', function(stream) {
console.log('Push received from parent request', stream.connection.associated.myid);
});
function startConn(myid) {
var req = https.request({
host: 'localhost',
method: 'POST',
agent: agent,
path: '/'
this.ws = ws;
this.connectionId = u.query.connectionId;
this.ws._socket.removeAllListeners('data'); // Remove WebSocket data handler.
this.ws._socket.on('end', function() {
clearInterval(self._pingTimer);
self.emit('end');
});
this.ws.on('error', function(err) {
clearInterval(self._pingTimer);
self.emit('error', err);
});
this.agent = spdy.createAgent(SpdyAgent, {
host: this.name,
port: 80,
socket: this.ws._socket,
spdy: {
plain: true,
ssl: false
}
});
// TODO: Remove this when bug in agent socket removal is fixed.
this.agent.maxSockets = 150;
this.agent.on('push', this.onPushData.bind(this));
this.agent.on('error', function(err) {
self.close();
self.emit('error', err);
});
opts = opts || {};
if (opts.rejectUnauthorized !== true) {
opts.rejectUnauthorized = false;
}
this.opts = opts;
this.encoder = encoder(this, channels);
opts.spdy = opts.spdy || {};
opts.spdy.maxChunk = 0;
opts.spdy.decompress = false;
opts.maxSockets = opts.maxSockets || 4096;
this.agent = spdy.createAgent(opts);
this._nextId = 1;
this._channels = {};
this._awayting = {};
var that = this;
this.agent.on('error', function(err) {
// if we force close an agent, some random errors might occur
if (that._closing || that._closed) {
return;
}
that.emit('error', err);
});
this.encoder.on('error', this.emit.bind(this, 'error'));
var EventEmitter = require('events').EventEmitter;
var path = require('path');
var util = require('util');
var uuid = require('uuid');
var spdy = require('spdy');
var Logger = require('./logger');
var WebSocket = require('./web_socket');
// monkey patch spdy connection to get access to ping event
var originalPingHandler = spdy.Connection.prototype._handlePing;
spdy.Connection.prototype._handlePing = function() {
this.socket.emit('spdyPing', this);
originalPingHandler.apply(this, arguments);
};
function calculatePeerUrl(url, name){
var wsUrl = url.replace(/^http/, 'ws');
var peerPath = '/peers/' + name;
if(wsUrl.indexOf('/', wsUrl.length - 1) === -1) {
wsUrl = wsUrl + peerPath;
} else {
wsUrl = wsUrl.slice(0, wsUrl.length - 1) + peerPath;
}
return wsUrl;
}
var PeerClient = module.exports = function(url, server) {
var EventEmitter = require('events').EventEmitter;
var path = require('path');
var util = require('util');
var uuid = require('uuid');
var spdy = require('spdy');
var Logger = require('./logger');
var WebSocket = require('./web_socket');
// monkey patch spdy connection to get access to ping event
var originalPingHandler = spdy.Connection.prototype._handlePing;
spdy.Connection.prototype._handlePing = function() {
this.socket.emit('spdyPing', this);
originalPingHandler.apply(this, arguments);
};
function calculatePeerUrl(url, name){
var wsUrl = url.replace(/^http/, 'ws');
var peerPath = '/peers/' + name;
if(wsUrl.indexOf('/', wsUrl.length - 1) === -1) {
wsUrl = wsUrl + peerPath;
} else {
wsUrl = wsUrl.slice(0, wsUrl.length - 1) + peerPath;
}
return wsUrl;
}
});
fs.writeFileSync(certPath, pems.private + pems.cert, { encoding: "utf-8" });
}
const fakeCert = fs.readFileSync(certPath);
options.https.key = options.https.key || fakeCert;
options.https.cert = options.https.cert || fakeCert;
if(!options.https.spdy) {
options.https.spdy = {
protocols: ["h2", "http/1.1"]
};
}
this.listeningApp = spdy.createServer(options.https, app);
} else {
this.listeningApp = http.createServer(app);
}
// Proxy websockets without the initial http request
// https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade
websocketProxies.forEach(function(wsProxy) {
this.listeningApp.on("upgrade", wsProxy.upgrade);
}, this);
}
var fmt = mergeFormatters(options.formatters);
this.acceptable = fmt.acceptable;
this.formatters = fmt.formatters;
this.proxyEvents = [
'clientError',
'close',
'connection',
'error',
'listening',
'secureConnection'
];
if (options.spdy) {
this.spdy = true;
this.server = spdy.createServer(options.spdy);
} else if (options.http2) {
// http2 module is not available < v8.4.0 (only with flag <= 8.8.0)
// load http2 module here to avoid experimental warning in other cases
if (!http2) {
try {
http2 = require('http2');
patchResponse(http2.Http2ServerResponse);
patchRequest(http2.Http2ServerRequest);
// eslint-disable-next-line no-empty
} catch (err) {}
}
assert(
http2,
'http2 module is not available, ' +
'upgrade your Node.js version to >= 8.8.0'
global.logger = require('./logger');
//App stuff
if (environment.match(/^development/)) {
environment = 'development';
}
var http = require('http'),
https = require('https'),
path = require('path'),
spdy = require('spdy');
// Use SPDY to talk to backing services. Seems like a great fit, minimise TLS connection
// overhead and multiplex requests to the same limited number of domains.
https.globalAgent = spdy.createAgent({
host: 'www.google.com',
port: 443
});
var rootDir = path.join(__dirname, '..');
var app = require('./appBuilder').getApp(environment, rootDir, argv.REQUIRE_BASE_URL);
// Set a port for the app. There are several different ways to do this:
// - In production-like environments we fall back to the config/ directory.
// - On PaaS products like Heroku, we use the $PORT environment variable that they set.
// - When running a Procfile locally (like with Foreman) we need to override $PORT
// by setting $SPOTLIGHT_PORT, so that our local Nginx connects to the right place.
var port = process.env.SPOTLIGHT_PORT || process.env.PORT || app.get('port');
app.set('port', port);
}
this.prevUrl = this.currentUrl
var opts = parseUrl(this.currentUrl)
opts.method = 'HEAD'
opts.headers = {
'Cookie': this.cookies.prepare(this.currentUrl),
'User-Agent': this.userAgent
}
var protocol = opts.protocol === 'https:' ? https : http
if (opts.protocol === 'https:') {
opts.agent = spdy.createAgent({
host: opts.hostname || opts.host,
port: opts.port || 443
})
// There's a case where spdy deadlocks, as it attempts to fall back to http/1.x;
// see https://github.com/indutny/node-spdy/blob/v3.4.4/lib/spdy/agent.js#L121-L127,
// https://github.com/indutny/node-spdy/blob/v3.4.4/lib/spdy/agent.js#L157-L160 and
// https://github.com/indutny/node-spdy/blob/v3.4.4/lib/spdy/agent.js#L66
// Fix: overwrite `.createSocket()` with node core's,
// as spdy's `Agent.prototype._getCreateSocket()` is bugged
opts.agent.createSocket = https.Agent.prototype.createSocket
// If a custom agent is used, by default all connection-level
// errors will result in an uncaught exception
// (See https://github.com/indutny/node-spdy#usage)
opts.agent.on('error', function (error) {