Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
#!/usr/bin/env node
var WebSocketServer = require('websocket').server;
var http = require('http');
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
});
server.listen(8080, function() {
console.log((new Date()) + ' Server is listening on port 8080');
});
wsServer = new WebSocketServer({
httpServer: server,
// You should not use autoAcceptwssockets for production
// applications, as it defeats all standard cross-origin protection
// facilities built into the protocol and the browser. You should
// *always* verify the connection's origin and decide whether or not
// to accept it.
autoAcceptConnections: false
});
wsServer.binaryType = 'arraybuffer';
//wsServer.setEncoding('binary');
function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}
var server = http.createServer(function(request, response) {
console.log((new Date()) + ' Received request for ' + request.url);
response.writeHead(404);
response.end();
}).on('error', (err) => {
// handle errors here
setTimeout( serverStartListening, 500);
});
serverStartListening = function() {
server.listen(30035, function() {
console.warn((new Date()) + ' Server is listening on port 30035');
});
};
var wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
function originIsAllowed(origin) {
// put logic here to detect whether the specified origin is allowed.
return true;
}
var clients = {};
var connection;
wsServer.on('request', function(request) {
if (!originIsAllowed(request.origin)) {
request.reject();
console.log( (new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
return;
console.log("AutoParsed (1):", auto, data);
}
} catch (err) {
console.log(err);
console.log("AutoParsed (2):", auto, data);
}
}
} catch (err) {
console.log(err);
}
};
/**
* WebSocket server
*/
var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket request is just
// an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});
// This callback function is called every time someone
// tries to connect to the WebSocket server
wsServer.on('request', function(request) {
console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
// accept connection - you should check 'request.origin' to make sure that
// client is connecting from your website
// (http://en.wikipedia.org/wiki/Same_origin_policy)
var connection = request.accept(null, request.origin);
clients.push(connection);
console.log((new Date()) + ' Connection accepted.');
return [{path:'local', description:'Connected Device', type : "net"}];
};
Espruino.onready = function(data) { Espruino.connect("local");};
Espruino.onwrite = function(data) { ws.send(data); }
ws.onmessage = function (event) { Espruino.received(event.data); };
ws.onclose = function (event) { Espruino.disconnect(); };
`);
});
server.listen(httpPort);
log("Web IDE is now available on http://localhost:"+httpPort);
/* Start the WebSocket relay - allows standard Websocket MQTT communications */
var WebSocketServer = require('websocket').server;
var wsServer = new WebSocketServer({
httpServer: server,
autoAcceptConnections: false
});
var ideConnections = [];
wsServer.on('request', function(request) {
// request.reject() based on request.origin?
var connection = request.accept('serial', request.origin);
ideConnections.push(connection);
log('Web IDE Connection accepted.');
connection.on('message', function(message) {
if (message.type === 'utf8') writeCallback(message.utf8Data);
});
connection.on('close', function(reasonCode, description) {
log('Web IDE Connection closed.');
var conIdx = ideConnections.indexOf(connection);
if (conIdx>=0) ideConnections.splice(conIdx,1);
log.debug('Port: ' + port);
// Socket server for capturing response send from service worker.js
socketServer = https.createServer({
key: fs.readFileSync(privateKeyFile),
cert: fs.readFileSync(certificateFile)
}, function(req, res) {
req.type = 'https';
log.debug((new Date()) + ' Received request for ' + req.url);
res.writeHead(404);
res.end();
});
socketServer.listen(WEB_SOCKET_PORT, function() {
log.info((new Date()) + ' Socket Server is listening on port ' + WEB_SOCKET_PORT);
});
var WebSocketServer = require('websocket').server;
wsServer = new WebSocketServer({
httpServer: socketServer,
autoAcceptConnections: false
});
wsServer.on('request', function(request) {
var connection = request.accept('echo-protocol', request.origin);
log.info((new Date()) + ' Connection accepted.');
connection.on('message', function(message) {
var jsonFileContents = [];
// read exisitng file and add contents to it
if (fs.existsSync(responseFilePath)) {
var data = fs.readFileSync(responseFilePath, {encoding : 'utf8'});
try {
jsonFileContents = JSON.parse(data);
}
catch(e)
res.end();
} else {
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('404 Not Found.\n');
}
});
var addr = parseAddr(argv._[0] || '', {port: 443, host: '0.0.0.0'});
server.listen(addr.port, addr.host, function() {
var addr = server.address();
console.log('listening on ' + addr.address + ':' + addr.port);
});
var wsServer = new WebSocketServer({
httpServer: server,
key: key,
cert: cert
});
wsServer.on('request', function(request) {
var url = urlParse(request.resource, true);
var args = url.pathname.split('/').slice(1);
var action = args.shift();
var params = url.query;
if (action == 'tunnel') {
createTunnel(request, params.port, params.host);
} else {
request.reject(404);
}
});
(function main() {
var http_server = http.createServer(httpServerRequest);
http_server.listen(port, function() {
console.log("Listening on port " + port);
});
var wsserver = new WebSocketServer({
httpServer: http_server,
});
wsserver.on('request', wsServerRequest);
})();
create(){
logger.debug('Starting webSocket server.');
let WebSocketServer = require('websocket').server;
this.wsServer = new WebSocketServer({
httpServer : this.httpServer,
autoAcceptConnections : false
});
this.wsServer.on(WS_SERVER_EVENTS.request, this.onRequest.bind(this));
this.wsServer.on(WS_SERVER_EVENTS.error, this.onError.bind(this));
this.bindEvents();
logger.debug('WebSocket server is started.');
return this.eventEmitter;
}
}
function removePlayer(uuid)
{
for(var i = 0; i < players.length; i++)
{
if(players[i].uuid === uuid)
{
players.splice(i, 1);
break;
}
}
}
var players = [], clients = [];
var server = new WebSocketServer({httpServer: httpServer});
server.on("request", function(request)
{
var connection = request.accept(null, request.origin);
//Message
connection.on("message", function(message)
{
var data = JSON.parse(message.utf8Data);
//Connected
if(data.type === "connected")
{
players.push(new Player(data.uuid, data.color));
clients.push(new Client(data.uuid, connection));