mirror of
https://github.com/EQEmu/Server.git
synced 2026-09-02 20:56:37 +00:00
Changes to building
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# Runtime data
|
||||
pids
|
||||
*.pid
|
||||
*.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
coverage
|
||||
|
||||
# nyc test coverage
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||
.grunt
|
||||
|
||||
# node-waf configuration
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (http://nodejs.org/api/addons.html)
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
node_modules
|
||||
jspm_packages
|
||||
|
||||
# Optional npm cache directory
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
.eslintcache
|
||||
|
||||
# Optional REPL history
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
.yarn-integrity
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
# Building EQEmu Web Interface Reference Implementation
|
||||
|
||||
## Required Software
|
||||
- [NodeJS](https://nodejs.org)
|
||||
|
||||
## Install
|
||||
|
||||
First: Make sure you have required software installed.
|
||||
|
||||
Install 3rd Party Libraries first with the following command:
|
||||
npm install
|
||||
|
||||
|
||||
## Run
|
||||
|
||||
Run with either your favorite NodeJS process manager or with the following command:
|
||||
node .
|
||||
@@ -1,27 +0,0 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
var Auth = function (req, res, next) {
|
||||
var token = '';
|
||||
try {
|
||||
token = req.headers.authorization.substring(7);
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
res.sendStatus(401);
|
||||
return;
|
||||
}
|
||||
|
||||
jwt.verify(token, req.key, function(err, decoded) {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
res.sendStatus(401);
|
||||
return;
|
||||
}
|
||||
|
||||
req.token = decoded;
|
||||
next();
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'auth': Auth
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
var sodium = require('libsodium-wrappers-sumo');
|
||||
|
||||
var hash = sodium.crypto_pwhash_str('password', 3, 32768);
|
||||
console.log(hash);
|
||||
@@ -1,26 +0,0 @@
|
||||
var auth = require('../core/jwt_auth.js').auth;
|
||||
|
||||
function RegisterFunction(path, fn, app, api) {
|
||||
app.post(path, auth, function (req, res) {
|
||||
var params = req.body.params || [];
|
||||
|
||||
api.Call(fn, params)
|
||||
.then(function(value) {
|
||||
res.send({ response: value });
|
||||
})
|
||||
.catch(function(reason) {
|
||||
if(reason.message) {
|
||||
res.send({ status: reason.message });
|
||||
}
|
||||
else if(reason === 'Not connected to world server.') {
|
||||
res.send({ status: 'ENCONNECTED' });
|
||||
} else {
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterFunction
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
var endpoint = require('./endpoint.js');
|
||||
var auth = require('../../core/jwt_auth.js').auth;
|
||||
var sql = require('./sql.js');
|
||||
|
||||
var RegisterAPI = function(app, api) {
|
||||
endpoint.Register(app, api, 'account', 'account', 'id');
|
||||
|
||||
//Can register custom controller actions here.
|
||||
app.post('/api/data/account/search', auth, function (req, res) {
|
||||
sql.Search(req, res, 'account', 'id', ['id', 'name']);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterAPI
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
var auth = require('../../core/jwt_auth.js').auth;
|
||||
var sql = require('./sql.js');
|
||||
|
||||
var RegisterEndpoint = function(app, api, endpoint_verb, table_name, pkey) {
|
||||
app.get('/api/data/' + endpoint_verb + '/:' + pkey, auth, function (req, res) {
|
||||
sql.Retrieve(req, res, table_name, pkey);
|
||||
});
|
||||
|
||||
app.put('/api/data/' + endpoint_verb + '/:' + pkey, auth, function (req, res) {
|
||||
sql.CreateUpdate(req, res, table_name, pkey);
|
||||
});
|
||||
|
||||
app.delete('/api/data/' + endpoint_verb + '/:' + pkey, auth, function (req, res) {
|
||||
sql.Delete(req, res, table_name, pkey);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterEndpoint
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
var RegisterAPI = function(app, api) {
|
||||
require('./account.js').Register(app, api);
|
||||
require('./item.js').Register(app, api);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterAPI
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
var endpoint = require('./endpoint.js');
|
||||
var auth = require('../../core/jwt_auth.js').auth;
|
||||
var sql = require('./sql.js');
|
||||
|
||||
var RegisterAPI = function(app, api) {
|
||||
endpoint.Register(app, api, 'item', 'items', 'id');
|
||||
|
||||
//Can register custom controller actions here.
|
||||
app.post('/api/data/item/search', auth, function (req, res) {
|
||||
sql.Search(req, res, 'items', 'id', ['id', 'name', 'icon']);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterAPI
|
||||
}
|
||||
@@ -1,319 +0,0 @@
|
||||
var moment = require('moment');
|
||||
|
||||
function CreateReplace(table, body, fields) {
|
||||
try {
|
||||
var query = 'REPLACE INTO ' + table + ' VALUES(';
|
||||
var first = true;
|
||||
var args = [];
|
||||
|
||||
for(var idx in fields) {
|
||||
if(first) {
|
||||
first = false;
|
||||
} else {
|
||||
query += ',';
|
||||
}
|
||||
|
||||
query += '?';
|
||||
|
||||
var entry = fields[idx];
|
||||
if(entry.type === 12) {
|
||||
try {
|
||||
var d = new moment(body[entry.name]);
|
||||
|
||||
if(d.isValid()) {
|
||||
args.push(d.format('YYYY-MM-DD HH:mm:ss'));
|
||||
} else {
|
||||
args.push(null);
|
||||
}
|
||||
} catch(ex) {
|
||||
args.push(null);
|
||||
}
|
||||
} else {
|
||||
args.push(body[entry.name]);
|
||||
}
|
||||
}
|
||||
|
||||
query += ')';
|
||||
|
||||
return { 'query': query, 'args': args };
|
||||
} catch(ex) {
|
||||
return { 'query': '', 'args': [] };
|
||||
}
|
||||
}
|
||||
|
||||
function CreateUpdate(req, res, table, pkey) {
|
||||
req.mysql.getConnection(function(err, connection) {
|
||||
try {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
}
|
||||
|
||||
if(req.body[pkey] !== parseInt(req.params[pkey], 10)) {
|
||||
connection.release();
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
connection.query('SELECT * FROM ' + table + ' WHERE ' + pkey + '=? LIMIT 1', [req.params[pkey]], function (error, results, fields) {
|
||||
try {
|
||||
if(error) {
|
||||
console.log(error);
|
||||
connection.release();
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
var replace = CreateReplace(table, req.body, fields);
|
||||
if(replace.query === '') {
|
||||
connection.release();
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
connection.query(replace.query, replace.args, function(error, results, fields) {
|
||||
try {
|
||||
if(error) {
|
||||
console.log(error);
|
||||
connection.release();
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
connection.release();
|
||||
res.sendStatus(200);
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function Retrieve(req, res, table, pkey) {
|
||||
req.mysql.getConnection(function(err, connection) {
|
||||
try {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
}
|
||||
|
||||
connection.query('SELECT * FROM ' + table + ' WHERE ' + pkey + '=? LIMIT 1', [req.params[pkey]], function (error, results, fields) {
|
||||
try {
|
||||
if(results.length == 0) {
|
||||
connection.release();
|
||||
res.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = results[0];
|
||||
var ret = { };
|
||||
|
||||
for(var idx in result) {
|
||||
var value = result[idx];
|
||||
ret[idx] = value;
|
||||
}
|
||||
|
||||
connection.release();
|
||||
res.json(ret);
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function Delete(req, res, table, pkey) {
|
||||
req.mysql.getConnection(function(err, connection) {
|
||||
try {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
}
|
||||
|
||||
connection.query('DELETE FROM ' + table + ' WHERE ' + pkey + '=? LIMIT 1', [req.params[pkey]], function (error, results, fields) {
|
||||
try {
|
||||
if(error) {
|
||||
console.log(error);
|
||||
connection.release();
|
||||
res.sendStatus(400);
|
||||
return;
|
||||
}
|
||||
|
||||
connection.release();
|
||||
res.sendStatus(200);
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function getLimit(req, columns) {
|
||||
var limit = '';
|
||||
|
||||
var len = parseInt(req.body['length']);
|
||||
if(len > 100) {
|
||||
len = 100;
|
||||
}
|
||||
|
||||
if(req.body.hasOwnProperty('start') && len != -1) {
|
||||
limit = 'LIMIT ' + req.body['start'] + ', ' + req.body['length'];
|
||||
}
|
||||
|
||||
return limit;
|
||||
}
|
||||
|
||||
function getOrder(req, columns) {
|
||||
var order = '';
|
||||
|
||||
if (req.body.hasOwnProperty('order') && req.body['order'].length) {
|
||||
var orderBy = [];
|
||||
for(var i = 0; i < req.body['order'].length; ++i) {
|
||||
var columnIdx = parseInt(req.body['order'][i].column);
|
||||
var column = req.body['columns'][columnIdx];
|
||||
var columnId = column.data;
|
||||
var dir = req.body['order'][i].dir === 'asc' ? 'ASC' : 'DESC';
|
||||
orderBy.push(req.mysql.escapeId(columnId) + ' ' + dir);
|
||||
}
|
||||
|
||||
order = 'ORDER BY ' + orderBy.join(',');
|
||||
}
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
function filter(req, columns, args) {
|
||||
var where = '';
|
||||
var globalSearch = [];
|
||||
var columnSearch = [];
|
||||
|
||||
if (req.body.hasOwnProperty('search') && req.body['search'].value.length) {
|
||||
var searchTerm = req.body['search'].value;
|
||||
for(var i = 0; i < req.body['columns'].length; ++i) {
|
||||
var column = req.body['columns'][i];
|
||||
|
||||
if(column.searchable) {
|
||||
globalSearch.push(req.mysql.escapeId(column.data) + ' LIKE ?');
|
||||
args.push('%' + searchTerm + '%');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(var i = 0; i < req.body['columns'].length; ++i) {
|
||||
var column = req.body['columns'][i];
|
||||
var searchTerm = column.search.value;
|
||||
|
||||
if(searchTerm !== '' && column.searchable) {
|
||||
columnSearch.push(req.mysql.escapeId(column.data) + ' LIKE ?');
|
||||
args.push('%' + searchTerm + '%');
|
||||
}
|
||||
}
|
||||
|
||||
if(globalSearch.length) {
|
||||
where = globalSearch.join(' OR ');
|
||||
}
|
||||
|
||||
if(columnSearch.length) {
|
||||
if(where === '') {
|
||||
where = columnSearch.join(' AND ');
|
||||
} else {
|
||||
where += ' AND ';
|
||||
where += columnSearch.join(' AND ');
|
||||
}
|
||||
}
|
||||
|
||||
if(where !== '') {
|
||||
where = 'WHERE ' + where;
|
||||
}
|
||||
|
||||
return where;
|
||||
}
|
||||
|
||||
function Search(req, res, table, pkey, columns) {
|
||||
var args = [];
|
||||
var limit = getLimit(req, columns);
|
||||
var order = getOrder(req, columns);
|
||||
var where = filter(req, columns, args);
|
||||
|
||||
var query = 'SELECT ' + columns.join(', ') + ' FROM ' + table + ' ' + where + ' ' + order + ' ' + limit;
|
||||
|
||||
req.mysql.getConnection(function(err, connection) {
|
||||
try {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
return;
|
||||
}
|
||||
|
||||
connection.query(query, args, function (error, results, fields) {
|
||||
try {
|
||||
var ret = [];
|
||||
|
||||
for(var i in results) {
|
||||
var result = results[i];
|
||||
|
||||
var obj = { };
|
||||
for(var idx in result) {
|
||||
var value = result[idx];
|
||||
obj[idx] = value;
|
||||
}
|
||||
|
||||
ret.push(obj);
|
||||
}
|
||||
|
||||
connection.release();
|
||||
res.json(ret);
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
connection.release();
|
||||
res.sendStatus(500);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'CreateUpdate': CreateUpdate,
|
||||
'Retrieve': Retrieve,
|
||||
'Delete': Delete,
|
||||
'Search': Search,
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
const common = require('./common.js');
|
||||
|
||||
var RegisterEQW = function(app, api) {
|
||||
common.Register('/api/eqw/getconfig', 'EQW::GetConfig', app, api);
|
||||
common.Register('/api/eqw/islocked', 'EQW::IsLocked', app, api);
|
||||
common.Register('/api/eqw/lock', 'EQW::Lock', app, api);
|
||||
common.Register('/api/eqw/unlock', 'EQW::Unlock', app, api);
|
||||
common.Register('/api/eqw/getplayercount', 'EQW::GetPlayerCount', app, api);
|
||||
common.Register('/api/eqw/getzonecount', 'EQW::GetZoneCount', app, api);
|
||||
common.Register('/api/eqw/getlaunchercount', 'EQW::GetLauncherCount', app, api);
|
||||
common.Register('/api/eqw/getloginservercount', 'EQW::GetLoginServerCount', app, api);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterEQW
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
var RegisterAPI = function(app, api) {
|
||||
require('./eqw.js').Register(app, api);
|
||||
require('./token.js').Register(app);
|
||||
require('./data').Register(app, api);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterAPI
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
const sodium = require('libsodium-wrappers-sumo');
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
var RegisterToken = function(app) {
|
||||
app.post('/api/token', function (req, res) {
|
||||
try {
|
||||
req.mysql.getConnection(function(err, connection) {
|
||||
if(err) {
|
||||
console.log(err);
|
||||
res.sendStatus(500);
|
||||
connection.release();
|
||||
return;
|
||||
}
|
||||
|
||||
connection.query('SELECT password FROM account WHERE name = ? LIMIT 1', [req.body.username], function (error, results, fields) {
|
||||
if(results.length == 0) {
|
||||
res.sendStatus(401);
|
||||
connection.release();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if(sodium.crypto_pwhash_str_verify(results[0].password, req.body.password)) {
|
||||
var expires = Math.floor(Date.now() / 1000) + (60 * 60 * 24 * 7);
|
||||
var token = jwt.sign({ username: req.body.username, exp: expires }, req.key);
|
||||
res.send({token: token, expires: expires});
|
||||
connection.release();
|
||||
} else {
|
||||
res.sendStatus(401);
|
||||
connection.release();
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch(ex) {
|
||||
res.sendStatus(500);
|
||||
console.log(ex);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterToken
|
||||
}
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
const fs = require('fs');
|
||||
const settings = JSON.parse(fs.readFileSync('settings.json', 'utf8'));
|
||||
const key = fs.readFileSync(settings.key, 'utf8');
|
||||
|
||||
var server;
|
||||
if(settings.https.enabled) {
|
||||
const options = {
|
||||
key: fs.readFileSync(settings.https.key),
|
||||
cert: fs.readFileSync(settings.https.cert)
|
||||
};
|
||||
|
||||
server = require('https').createServer();
|
||||
} else {
|
||||
server = require('http').createServer();
|
||||
}
|
||||
|
||||
const servertalk = require('./network/servertalk_api.js');
|
||||
const websocket_iterface = require('./ws/ws_interface.js');
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const bodyParser = require('body-parser');
|
||||
const uuid = require('node-uuid');
|
||||
const jwt = require('jsonwebtoken');
|
||||
var mysql = require('mysql').createPool(settings.db);
|
||||
|
||||
var api = new servertalk.api();
|
||||
var wsi = new websocket_iterface.wsi(server, key, api);
|
||||
api.Init(settings.servertalk.addr, settings.servertalk.port, false, settings.servertalk.key);
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
app.use(function(req, res, next) {
|
||||
res.header("Access-Control-Allow-Origin", "*");
|
||||
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
|
||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
|
||||
next();
|
||||
});
|
||||
|
||||
//make sure all routes can see our injected dependencies
|
||||
app.use(function (req, res, next) {
|
||||
req.servertalk = api;
|
||||
req.mysql = mysql;
|
||||
req.key = key;
|
||||
next();
|
||||
});
|
||||
|
||||
app.get('/', function (req, res) {
|
||||
res.send({ status: "online" });
|
||||
});
|
||||
|
||||
require('./http').Register(app, api);
|
||||
require('./ws').Register(wsi, api);
|
||||
|
||||
server.on('request', app);
|
||||
server.listen(settings.port, function () { console.log('Listening on ' + server.address().port) });
|
||||
-27
@@ -1,27 +0,0 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEowIBAAKCAQEAoijNhaW4sH2yLEQOUCNLSU0qIGnr9mxewEPXSNURKFExC1WE
|
||||
ah983xy+WTbKjakH6Rp2OwCvLxNIu6QBKRgcJ963ICWY7ysn4bU2Q2KoSJgAEel8
|
||||
UMDHWYfyyAPdr4DUwUw7YMf4LBThCGBC5DTPilZiVqQNyOf8KL5w/oKcavVMddod
|
||||
eBNE1ewoxVveHN6WUDkYQKZK2AsrpNG6TjfJc3wI3Z722tRHui4E772l/sD0SuEj
|
||||
41pBzOG0VM7DHwUpHQosnvnwx9kjefPNE/uvo14PuzP5yYG2h2PFkQ7uuXjK2/le
|
||||
iVcyap/zgheOHjlYmOJGT1cnVSodv+rY56eilwIDAQABAoIBAQAM/sAZqcI3Qpt4
|
||||
uKt8+Jcb9Lcfid2DDgQ53DXwfEK3vGn1wpCuAH/9UUxf0ehBmf4sTBaVe+SOHTmC
|
||||
8A23wVrgRxTd2qV65TZ4/BCxLcLWrney98cioZBYOHDYXpbxbZ2fMADCLMRSpAm0
|
||||
piI2L5VCPNH8p4EDTLQEf96GRulKGOWETeVNai3C7Ept6Fxv0YIiiER8j2oPsb1O
|
||||
LVCBKBPsNs0IlabJAzfDnaqdfWzuLWIT0L4w/qvzfwkdM8tVxch2zjEVosbz4ser
|
||||
rPO3tle3mobgDvXrW9jEYkIpOtEqCS7l4ybidVuEfY55KlkZ7rGBQ2N1jbLvKjb5
|
||||
AUyHUBchAoGBAOKjzzBPB/mofycF8iF1QJwripGTDUGM7aXBS0Clp4mh0ksvBsUf
|
||||
Zg+Qnzr2xZaN53lU65xQlMrebMJow4iJj71VesF9FWPPNbIhh7eMTX4pABcKZNvc
|
||||
Y0iFf5XZAl3LFdDocQSuB3j5WLNrjSFMBZuYUiZhgiRadtcdpQr+O4lbAoGBALcq
|
||||
ltbFogxXoo7/CIajbYdNUGbba96jQMOzC1D7yeim1MTtDNGs56ZhDjFZepMRMyfX
|
||||
/Z7iqxjZQQ1m1THtuiM4g+ug08EYI8G/7DYO5DqMABGFb3vKU9ilhYASqfznpKMJ
|
||||
2sl/d5j8ocS7crkKwR8Tbo3ZG8NgObQNTL+mIFR1AoGAJS66zzIoHM2IDt7q2pJi
|
||||
Bz0dfsShaB+23XrY3cJPukTSO4N7mNuN4v/XH9VclVayozVLclnGD4JuVXbanYv0
|
||||
CRv9B8F9wOI97PuTSIm8LPaNDTqnUWrW3w8H34261ah768o2wI3MrAw8gTMj9FKE
|
||||
mQJkd+eHcm9lD+XNLgCHxAECgYBiMQ2t00L89NnraKLscp4b44GPsl9QehoVD12o
|
||||
q2JhO1Ziv2WY3eVNV0hhgkNopdbTrEGFNKRebNEn2xG9c2DO0tQ9s/jw0f0RN87s
|
||||
Z+1HyZebzPmn1h4+zPUVZGwGbTPgRz8nuBKoS/541bg5pJ9FBojEuDfe9C3a7SpQ
|
||||
r0EzpQKBgBmYrKi07wTUSZ3TjHWvOK75XhJ5pOdfbuDZk+N02jzhmihzI2M/Sh7s
|
||||
l1gavtY9o9JGUAW35L/Ju4X1Xgm3t5Cg9+4n6ecOfSKP9nJpgj1EvHyWvw9t8ZSg
|
||||
V9M0Hf5EoSPWuEj+mlWrIuvV/HgkouUVqDzUm6wUuyTqdTCgUQrA
|
||||
-----END RSA PRIVATE KEY-----
|
||||
@@ -1,148 +0,0 @@
|
||||
const servertalk = require('./servertalk_client.js');
|
||||
const uuid = require('node-uuid');
|
||||
|
||||
class ServertalkAPI
|
||||
{
|
||||
Init(addr, port, ipv6, credentials) {
|
||||
this.client = new servertalk.client();
|
||||
this.client.Init(addr, port, ipv6, 'WebInterface', credentials);
|
||||
this.pending_calls = {};
|
||||
this.subscriptions = {};
|
||||
var self = this;
|
||||
|
||||
this.client.on('connecting', function() {
|
||||
//console.log('Connecting...');
|
||||
});
|
||||
|
||||
this.client.on('connect', function(){
|
||||
//console.log('Connected');
|
||||
});
|
||||
|
||||
this.client.on('close', function(){
|
||||
//console.log('Closed');
|
||||
});
|
||||
|
||||
this.client.on('error', function(err){
|
||||
});
|
||||
|
||||
this.client.on('message', function(opcode, packet) {
|
||||
if(opcode == 47) {
|
||||
var response = Buffer.from(packet).toString('utf8');
|
||||
try {
|
||||
var res = JSON.parse(response);
|
||||
|
||||
if(res.id) {
|
||||
if(self.pending_calls.hasOwnProperty(res.id)) {
|
||||
var entry = self.pending_calls[res.id];
|
||||
|
||||
if(res.error) {
|
||||
var reject = entry[1];
|
||||
reject(res.error);
|
||||
} else {
|
||||
var resolve = entry[0];
|
||||
resolve(res.response);
|
||||
}
|
||||
|
||||
delete self.pending_calls[res.id];
|
||||
}
|
||||
}
|
||||
} catch(ex) {
|
||||
console.log('Error processing response from server:\n', ex);
|
||||
}
|
||||
} else if(opcode == 104) {
|
||||
var message = Buffer.from(packet).toString('utf8');
|
||||
try {
|
||||
var msg = JSON.parse(message);
|
||||
|
||||
if(msg.event) {
|
||||
if(self.subscriptions.hasOwnProperty(msg.event)) {
|
||||
var subs = self.subscriptions[msg.event];
|
||||
|
||||
for(var idx in subs) {
|
||||
try {
|
||||
var sub = subs[idx];
|
||||
sub.emit('subscriptionMessage', msg);
|
||||
} catch(ex) {
|
||||
console.log('Error dispatching subscription message', ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(ex) {
|
||||
console.log('Error processing response from server:\n', ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Call(method, args, timeout) {
|
||||
if(!timeout) {
|
||||
timeout = 15000
|
||||
}
|
||||
|
||||
var self = this;
|
||||
return new Promise(
|
||||
function(resolve, reject) {
|
||||
if(!self.client.Connected()) {
|
||||
reject('Not connected to world server.');
|
||||
return;
|
||||
}
|
||||
|
||||
var id = uuid.v4();
|
||||
|
||||
self.pending_calls[id] = [resolve, reject];
|
||||
|
||||
var c = { id: id, method: method, params: args };
|
||||
self.client.Send(47, Buffer.from(JSON.stringify(c)));
|
||||
|
||||
setTimeout(function() {
|
||||
delete self.pending_calls[id];
|
||||
reject('Request timed out after ' + timeout + 'ms');
|
||||
}, timeout);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Notify(method, args) {
|
||||
var c = { method: method, params: args };
|
||||
client.Send(47, Buffer.from(JSON.stringify(c)));
|
||||
}
|
||||
|
||||
Subscribe(event_id, who) {
|
||||
this.Unsubscribe(event_id, who);
|
||||
|
||||
var subs = this.subscriptions[event_id];
|
||||
if(subs) {
|
||||
//console.log('Subscribe', who.uuid, 'to', event_id);
|
||||
subs[who.uuid] = who;
|
||||
} else {
|
||||
//console.log('Subscribe', who.uuid, 'to', event_id);
|
||||
this.subscriptions[event_id] = { };
|
||||
this.subscriptions[event_id][who.uuid] = who;
|
||||
//Tell our server we have a subscription for event_id
|
||||
}
|
||||
}
|
||||
|
||||
Unsubscribe(event_id, who) {
|
||||
var subs = this.subscriptions[event_id];
|
||||
if(subs) {
|
||||
//console.log('Unsubscribe', who.uuid, 'from', event_id);
|
||||
delete subs[who.uuid];
|
||||
|
||||
if(Object.keys(subs).length === 0) {
|
||||
delete this.subscriptions[event_id];
|
||||
//Tell our server we no longer have a subscription for event_id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UnsubscribeAll(who) {
|
||||
for(var sub_idx in this.subscriptions) {
|
||||
this.Unsubscribe(sub_idx, who);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'api': ServertalkAPI
|
||||
}
|
||||
@@ -1,299 +0,0 @@
|
||||
var net = require('net');
|
||||
var sodium = require('libsodium-wrappers');
|
||||
const EventEmitter = require('events');
|
||||
|
||||
var ServertalkPacketType =
|
||||
{
|
||||
ServertalkClientHello: 1,
|
||||
ServertalkServerHello: 2,
|
||||
ServertalkClientHandshake: 3,
|
||||
ServertalkClientDowngradeSecurityHandshake: 4,
|
||||
ServertalkMessage: 5,
|
||||
};
|
||||
|
||||
class ServertalkClient extends EventEmitter
|
||||
{
|
||||
Init(addr, port, ipv6, identifier, credentials) {
|
||||
this.m_addr = addr;
|
||||
this.m_identifier = identifier;
|
||||
this.m_credentials = credentials;
|
||||
this.m_connecting = false;
|
||||
this.m_port = port;
|
||||
this.m_ipv6 = ipv6;
|
||||
this.m_encrypted = false;
|
||||
this.m_connection = null;
|
||||
this.m_buffer = Buffer.alloc(0);
|
||||
this.m_public_key_ours = null;
|
||||
this.m_private_key_ours = null;
|
||||
this.m_nonce_ours = null;
|
||||
this.m_public_key_theirs = null;
|
||||
this.m_nonce_theirs = null;
|
||||
this.m_shared_key = null;
|
||||
|
||||
var self = this;
|
||||
setInterval(function() { self.Connect(); }, 100);
|
||||
}
|
||||
|
||||
Send(opcode, p) {
|
||||
try {
|
||||
var out;
|
||||
if(this.m_encrypted) {
|
||||
if(p.length == 0) {
|
||||
p = Buffer.alloc(1);
|
||||
}
|
||||
|
||||
out = Buffer.alloc(6);
|
||||
out.writeUInt32LE(p.length + sodium.crypto_secretbox_MACBYTES, 0);
|
||||
out.writeUInt16LE(opcode, 4);
|
||||
|
||||
var cipher = sodium.crypto_box_easy_afternm(p, this.m_nonce_ours, this.m_shared_key);
|
||||
this.IncrementUint64(this.m_nonce_ours);
|
||||
|
||||
out = Buffer.concat([out, Buffer.from(cipher)], out.length + cipher.length);
|
||||
} else {
|
||||
out = Buffer.alloc(6);
|
||||
out.writeUInt32LE(p.length, 0);
|
||||
out.writeUInt16LE(opcode, 4);
|
||||
out = Buffer.concat([out, p], out.length + p.length);
|
||||
}
|
||||
|
||||
this.InternalSend(ServertalkPacketType.ServertalkMessage, out);
|
||||
} catch(ex) {
|
||||
this.emit('error', new Error(ex));
|
||||
}
|
||||
}
|
||||
|
||||
Connected() {
|
||||
return this.m_connection && !this.m_connecting;
|
||||
}
|
||||
|
||||
Connect() {
|
||||
if (this.m_port == 0 || this.m_connection || this.m_connecting) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.m_connecting = true;
|
||||
|
||||
this.emit('connecting');
|
||||
|
||||
var self = this;
|
||||
this.m_connection = net.connect({port: this.m_port, host: this.m_addr}, function() {
|
||||
self.m_connection.on('close', function(had_error) {
|
||||
self.emit('close');
|
||||
self.m_connection = null;
|
||||
self.m_encrypted = false;
|
||||
});
|
||||
|
||||
self.m_connection.on('data', function(buffer) {
|
||||
self.ProcessData(buffer);
|
||||
});
|
||||
|
||||
self.SendHello();
|
||||
self.m_connecting = false;
|
||||
});
|
||||
|
||||
this.m_connection.on('error', function() {
|
||||
self.emit('close');
|
||||
self.m_connection = null;
|
||||
self.m_connecting = false;
|
||||
});
|
||||
}
|
||||
|
||||
ProcessData(buffer) {
|
||||
this.m_buffer = Buffer.concat([this.m_buffer, buffer], this.m_buffer.length + buffer.length);
|
||||
this.ProcessReadBuffer();
|
||||
}
|
||||
|
||||
SendHello() {
|
||||
var p = Buffer.alloc(0);
|
||||
this.InternalSend(ServertalkPacketType.ServertalkClientHello, p);
|
||||
}
|
||||
|
||||
InternalSend(type, p) {
|
||||
if(!this.m_connection) {
|
||||
return;
|
||||
}
|
||||
|
||||
var out = Buffer.alloc(5);
|
||||
out.writeUInt32LE(p.length, 0);
|
||||
out.writeUInt8(type, 4);
|
||||
|
||||
if (p.length > 0) {
|
||||
out = Buffer.concat([out, p], out.length + p.length);
|
||||
}
|
||||
|
||||
this.m_connection.write(out);
|
||||
}
|
||||
|
||||
ProcessReadBuffer() {
|
||||
var current = 0;
|
||||
var total = this.m_buffer.length;
|
||||
|
||||
while (current < total) {
|
||||
var left = total - current;
|
||||
|
||||
var length = 0;
|
||||
var type = 0;
|
||||
if (left < 5) {
|
||||
break;
|
||||
}
|
||||
|
||||
length = this.m_buffer.readUInt32LE(current);
|
||||
type = this.m_buffer.readUInt8(current + 4);
|
||||
|
||||
if (current + 5 + length > total) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (length == 0) {
|
||||
var p = Buffer.alloc(0);
|
||||
switch (type) {
|
||||
case ServertalkPacketType.ServertalkServerHello:
|
||||
this.ProcessHello(p);
|
||||
break;
|
||||
case ServertalkPacketType.ServertalkMessage:
|
||||
this.ProcessMessage(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
var p = this.m_buffer.slice(current + 5, current + 5 + length);
|
||||
switch (type) {
|
||||
case ServertalkPacketType.ServertalkServerHello:
|
||||
this.ProcessHello(p);
|
||||
break;
|
||||
case ServertalkPacketType.ServertalkMessage:
|
||||
this.ProcessMessage(p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
current += length + 5;
|
||||
}
|
||||
|
||||
if (current == total) {
|
||||
this.m_buffer = Buffer.alloc(0);
|
||||
}
|
||||
else {
|
||||
this.m_buffer = this.m_buffer.slice(current);
|
||||
}
|
||||
}
|
||||
|
||||
ProcessHello(p) {
|
||||
this.m_encrypted = false;
|
||||
this.m_public_key_ours = null;
|
||||
this.m_public_key_theirs = null;
|
||||
this.m_private_key_ours = null;
|
||||
this.m_nonce_ours = null;
|
||||
this.m_nonce_theirs = null;
|
||||
this.m_shared_key = null;
|
||||
|
||||
try {
|
||||
var enc = p.readUInt8(0) == 1 ? true : false;
|
||||
if (enc) {
|
||||
if (p.length == (1 + sodium.crypto_box_PUBLICKEYBYTES + sodium.crypto_box_NONCEBYTES)) {
|
||||
this.m_public_key_theirs = p.slice(1, 1 + sodium.crypto_box_PUBLICKEYBYTES);
|
||||
this.m_nonce_theirs = p.slice(1 + sodium.crypto_box_PUBLICKEYBYTES, 1 + sodium.crypto_box_PUBLICKEYBYTES + sodium.crypto_box_NONCEBYTES);
|
||||
this.m_encrypted = true;
|
||||
this.SendHandshake(false);
|
||||
|
||||
this.emit('connect');
|
||||
}
|
||||
else {
|
||||
this.emit('error', new Error('Could not process hello, size !=', 1 + sodium.crypto_box_PUBLICKEYBYTES + sodium.crypto_box_NONCEBYTES));
|
||||
}
|
||||
} else {
|
||||
this.SendHandshake(false);
|
||||
|
||||
this.emit('connect');
|
||||
}
|
||||
} catch(ex) {
|
||||
this.emit('error', new Error(ex));
|
||||
}
|
||||
}
|
||||
|
||||
ProcessMessage(p) {
|
||||
try {
|
||||
var length = p.readUInt32LE(0);
|
||||
var opcode = p.readUInt16LE(4);
|
||||
if(length > 0) {
|
||||
var data = p.slice(6);
|
||||
|
||||
if(this.m_encrypted) {
|
||||
var message_len = length - sodium.crypto_secretbox_MACBYTES;
|
||||
|
||||
var decrypted = sodium.crypto_box_open_easy_afternm(data, this.m_nonce_theirs, this.m_shared_key);
|
||||
|
||||
this.IncrementUint64(this.m_nonce_theirs);
|
||||
|
||||
this.emit('message', opcode, decrypted);
|
||||
} else {
|
||||
this.emit('message', opcode, data);
|
||||
}
|
||||
} else {
|
||||
this.emit('message', opcode, Buffer.alloc(0));
|
||||
}
|
||||
} catch(ex) {
|
||||
this.emit('error', new Error(ex));
|
||||
}
|
||||
}
|
||||
|
||||
SendHandshake() {
|
||||
var handshake;
|
||||
|
||||
if(this.m_encrypted) {
|
||||
var keypair = sodium.crypto_box_keypair();
|
||||
this.m_public_key_ours = keypair.publicKey;
|
||||
this.m_private_key_ours = keypair.privateKey;
|
||||
this.m_nonce_ours = Buffer.from(sodium.randombytes_buf(sodium.crypto_box_NONCEBYTES));
|
||||
this.m_shared_key = sodium.crypto_box_beforenm(this.m_public_key_theirs, this.m_private_key_ours);
|
||||
|
||||
this.m_public_key_theirs = null;
|
||||
this.m_private_key_ours = null;
|
||||
|
||||
var message = Buffer.alloc(this.m_identifier.length + this.m_credentials.length + 2);
|
||||
message.write(this.m_identifier, 0);
|
||||
message.write(this.m_credentials, this.m_identifier.length + 1);
|
||||
|
||||
var ciphertext = sodium.crypto_box_easy_afternm(message, this.m_nonce_ours, this.m_shared_key);
|
||||
|
||||
handshake = Buffer.concat([Buffer.from(this.m_public_key_ours), Buffer.from(this.m_nonce_ours), Buffer.from(ciphertext)], sodium.crypto_box_PUBLICKEYBYTES + sodium.crypto_box_NONCEBYTES + ciphertext.length);
|
||||
this.IncrementUint64(this.m_nonce_ours);
|
||||
|
||||
this.m_public_key_ours = null;
|
||||
} else {
|
||||
handshake = Buffer.alloc(this.m_identifier.length + this.m_credentials.length + 2);
|
||||
handshake.write(this.m_identifier, 0);
|
||||
handshake.write(this.m_credentials, this.m_identifier.length() + 1);
|
||||
}
|
||||
|
||||
this.InternalSend(ServertalkPacketType.ServertalkClientHandshake, handshake);
|
||||
}
|
||||
|
||||
IncrementUint64(value) {
|
||||
var bytes = [];
|
||||
for(var i = 0; i < 8; ++i) {
|
||||
bytes[i] = value[i];
|
||||
}
|
||||
|
||||
bytes[0] += 1;
|
||||
for(i = 0; i < 7; ++i) {
|
||||
if(bytes[i] >= 0x100) {
|
||||
bytes[0] = 0;
|
||||
bytes[i + 1] += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(bytes[7] >= 0x100) {
|
||||
bytes[7] = 0;
|
||||
}
|
||||
|
||||
for(var i = 0; i < 8; ++i) {
|
||||
value[i] = bytes[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'client': ServertalkClient
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
{
|
||||
"name": "wi",
|
||||
"version": "1.0.0",
|
||||
"description": "Web interface connection for EQEmu",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "KimLS",
|
||||
"license": "GPL-3.0",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.15.2",
|
||||
"express": "^4.14.0",
|
||||
"hammerjs": "^2.0.8",
|
||||
"jsonwebtoken": "^7.2.1",
|
||||
"libsodium": "^0.4.8",
|
||||
"libsodium-wrappers": "^0.4.8",
|
||||
"libsodium-wrappers-sumo": "^0.4.8",
|
||||
"moment": "^2.17.1",
|
||||
"mysql": "^2.12.0",
|
||||
"node-uuid": "^1.4.7",
|
||||
"ws": "^1.1.1"
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
{
|
||||
"db": {
|
||||
"connectionLimit": 10,
|
||||
"host": "localhost",
|
||||
"user": "root",
|
||||
"password": "blink",
|
||||
"database": "eqdb"
|
||||
},
|
||||
"servertalk": {
|
||||
"addr": "localhost",
|
||||
"port": "9101",
|
||||
"key": "ujwn2isnal1987scanb"
|
||||
},
|
||||
"https": {
|
||||
"enabled": false,
|
||||
"key": "key.pem",
|
||||
"cert": "cert.pem"
|
||||
},
|
||||
"port": 9080,
|
||||
"key": "key.pem"
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
const WebSocket = require('ws');
|
||||
const ws = new WebSocket('ws://localhost:9080');
|
||||
|
||||
ws.on('open', function open() {
|
||||
ws.send(JSON.stringify({authorization: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IktTcHJpdGUxIiwiZXhwIjoxNDg2MzI4MDI3LCJpYXQiOjE0ODU3MjMyMjd9.fJUeSQsxb5C13ICANox81YdE5yImkrVw-lRCP3O40-E', method: 'EQW::ZoneUpdate::Subscribe'}));
|
||||
ws.send(JSON.stringify({authorization: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IktTcHJpdGUxIiwiZXhwIjoxNDg2MzI4MDI3LCJpYXQiOjE0ODU3MjMyMjd9.fJUeSQsxb5C13ICANox81YdE5yImkrVw-lRCP3O40-E', method: 'EQW::ClientUpdate::Subscribe'}));
|
||||
});
|
||||
|
||||
ws.on('message', function(data, flags) {
|
||||
console.log(data);
|
||||
});
|
||||
@@ -1,18 +0,0 @@
|
||||
const common = require('./wi_common.js');
|
||||
|
||||
var RegisterEQW = function(wsi, api) {
|
||||
common.Register('EQW::GetConfig', wsi, api);
|
||||
common.Register('EQW::IsLocked', wsi, api);
|
||||
common.Register('EQW::Lock', wsi, api);
|
||||
common.Register('EQW::Unlock', wsi, api);
|
||||
common.Register('EQW::GetPlayerCount', wsi, api);
|
||||
common.Register('EQW::GetZoneCount', wsi, api);
|
||||
common.Register('EQW::GetLauncherCount', wsi, api);
|
||||
common.Register('EQW::GetLoginServerCount', wsi, api);
|
||||
common.RegisterSubscription('EQW::ZoneUpdate', wsi, api);
|
||||
common.RegisterSubscription('EQW::ClientUpdate', wsi, api);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterEQW
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
var RegisterAPI = function(wsi, api) {
|
||||
require('./eqw.js').Register(wsi, api);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
'Register': RegisterAPI
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
function Register(name, wsi, api) {
|
||||
wsi.Register(name,
|
||||
function(request) {
|
||||
api.Call(name, request.params)
|
||||
.then(function(value) {
|
||||
wsi.Send(request, value);
|
||||
})
|
||||
.catch(function(reason) {
|
||||
wsi.SendError(request, reason);
|
||||
});
|
||||
}, true);
|
||||
}
|
||||
|
||||
function RegisterSubscription(event, wsi, api) {
|
||||
wsi.Register(event + '::Subscribe', function(request) {
|
||||
api.Subscribe(event, request.ws);
|
||||
});
|
||||
|
||||
wsi.Register(event + '::Unsubscribe', function(request) {
|
||||
api.Unsubscribe(event, request.ws);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'Register': Register,
|
||||
'RegisterSubscription': RegisterSubscription
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
const WebSocketServer = require('ws').Server;
|
||||
const jwt = require('jsonwebtoken');
|
||||
const uuid = require('node-uuid');
|
||||
|
||||
class WebSocketInterface
|
||||
{
|
||||
constructor(server, key, api) {
|
||||
this.wss = new WebSocketServer({ server: server });
|
||||
this.methods = {};
|
||||
var self = this;
|
||||
|
||||
this.wss.on('connection', function connection(ws) {
|
||||
self.ws = ws;
|
||||
ws.uuid = uuid.v4();
|
||||
ws.on('message', function incoming(message) {
|
||||
try {
|
||||
var request = JSON.parse(message);
|
||||
request.ws = ws;
|
||||
|
||||
if(request.method) {
|
||||
var method = self.methods[request.method];
|
||||
if(!method) {
|
||||
self.SendError(request, 'Method not found: ' + request.method);
|
||||
return;
|
||||
}
|
||||
|
||||
if(method.requires_auth) {
|
||||
if(!request.authorization) {
|
||||
self.SendError(request, 'Authorization Required');
|
||||
return;
|
||||
}
|
||||
|
||||
jwt.verify(request.authorization, key, function(err, decoded) {
|
||||
if(err) {
|
||||
self.SendError(request, 'Authorization Required');
|
||||
return;
|
||||
}
|
||||
|
||||
request.token = decoded;
|
||||
method.fn(request);
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
method.fn(request);
|
||||
|
||||
} else {
|
||||
self.SendError(request, 'No method supplied');
|
||||
}
|
||||
|
||||
} catch(ex) {
|
||||
console.log('Error parsing message:', ex);
|
||||
self.SendError(null, 'No method supplied');
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', function() {
|
||||
api.UnsubscribeAll(ws);
|
||||
});
|
||||
|
||||
ws.on('subscriptionMessage', function(msg) {
|
||||
self.SendRaw(msg);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Register(method, fn, requires_auth) {
|
||||
var entry = { fn: fn, requires_auth: requires_auth };
|
||||
this.methods[method] = entry;
|
||||
}
|
||||
|
||||
SendError(request, msg) {
|
||||
try {
|
||||
if(this.ws) {
|
||||
var error = {};
|
||||
|
||||
if(request && request.id) {
|
||||
error.id = request.id;
|
||||
}
|
||||
|
||||
error.error = msg;
|
||||
this.ws.send(JSON.stringify(error));
|
||||
}
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
}
|
||||
}
|
||||
|
||||
Send(request, value) {
|
||||
try {
|
||||
if(this.ws) {
|
||||
var response = {};
|
||||
|
||||
if(request && request.id) {
|
||||
response.id = response.id;
|
||||
}
|
||||
|
||||
response.response = value;
|
||||
this.ws.send(JSON.stringify(response));
|
||||
}
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
}
|
||||
}
|
||||
|
||||
SendRaw(obj) {
|
||||
try {
|
||||
this.ws.send(JSON.stringify(obj));
|
||||
} catch(ex) {
|
||||
console.log(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
'wsi': WebSocketInterface
|
||||
}
|
||||
Reference in New Issue
Block a user