-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
185 lines (154 loc) · 5.5 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var fs = require('fs');
var express = require('express');
var https = require('https');
var bodyParser = require('body-parser');
const winston = require('winston');
const storage = require('node-persist');
const logDir = '../working/log';
var key = fs.readFileSync('wallet/server-key.pem');
var cert = fs.readFileSync('wallet/server-crt.pem');
var ca = fs.readFileSync('wallet/ca-crt.pem');
var https_options = {
key: key,
cert: cert,
ca: ca
};
var PORT = 7443;
var HOST = 'viveksam.southindia.cloudapp.azure.com';
var recognizedToken = fs.readFileSync('wallet/token').toString();
/*
Functions
*/
function validateToken(token) {
token = token.trim();
recognizedToken = recognizedToken.trim();
logger.debug('Token : %s', token);
logger.debug('recognized Token : %s', recognizedToken);
return token === recognizedToken;
}
function exitHandler(options, err) {
if (options.cleanup) logger.debug('Server Exiting...');
if (err) logger.debug(err.stack);
if (options.exit) {
logger.debug('Server Exiting...');
process.exit();
}
}
//initialize the storage
storage.init({
dir: '../working/data',
stringify: JSON.stringify,
parse: JSON.parse,
encoding: 'utf8',
logging: false, // can also be custom logging function
ttl: false, // ttl* [NEW], can be true for 24h default or a number in MILLISECONDS
expiredInterval: 12 * 60 * 1000, // every 2 minutes the process will clean-up the expired cache
// in some cases, you (or some other service) might add non-valid storage files to your
// storage dir, i.e. Google Drive, make this true if you'd like to ignore these files and not throw an error
forgiveParseErrors: false
});
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
// Create the log directory if it does not exist
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
const tsFormat = () => (new Date()).toLocaleTimeString();
var now = new Date();
var logfile_name = `${logDir}`+ '/' + now.getFullYear() + "-"+ now.getMonth() + "-" + now.getDate() +'server.log';
var logger = new (winston.Logger)({
transports: [
// colorize the output to the console
new (winston.transports.Console)({
timestamp: tsFormat,
colorize: true,
level: 'info'
}),
new (winston.transports.File)({
filename: logfile_name,
timestamp: tsFormat,
datePattern: 'yyyy-MM-dd',
prepend: true,
level: 'silly'
})
]
});
app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
server = https.createServer(https_options, app).listen(PORT);
// routes
app.get('/', function (req, res) {
res.header('Content-type', 'text/html');
logger.info('Request from IP %s', req.ip);
return res.end('meetingpoint Server, testing service, just to see a reply...');
});
/*
app.get('/hey', function(req, res) {
res.header('Content-type', 'text/html');
return res.end('Hey!');
});
app.post('/ho', function(req, res) {
res.header('Content-type', 'text/html');
return res.end('Ho!');
});
*/
app.get('/showvalues', function(req, res) {
logger.info('Received /showvalues request from IP %s', req.ip);
res.json({gettesting: "123"})
});
app.post('/showhosts', function(req, res) {
logger.info('Received /showhosts request from IP %s', req.ip);
var token = req.body.token;
var validationResult = validateToken(token);
var allwanIPs="{";
if(validationResult == true) {
logger.info('Replied /showhosts request');
storage.forEach(async function(datum) {
// use datum.key and datum.value
logger.debug('for each Host : %s, WanIP : %s',datum.key,datum.value);
allwanIPs+=datum.key+":"+datum.value+",";
})
.then (() => {
allwanIPs=allwanIPs.slice(0, -1);
allwanIPs+="}";
logger.debug("List : %s",allwanIPs)
res.json({'operation': "showhosts",'status': "SUCCESS", 'hosts': allwanIPs});
})
.catch(err => logger.error(err));
} else {
res.json({testingshowhosts: "123"});
}
});
app.post('/addhost', function(req, res) {
logger.info('Received /addhost request from IP %s', req.ip);
var token = req.body.token;
var validationResult = validateToken(token);
if(validationResult == true) {
//store the replace the data in the store
var host = req.body.hostMachine.toString();
var wanIP = req.body.hostWANIP.toString();
logger.debug("Host : %s, wanIP : %s.", host, wanIP);
storage.setItem(host, wanIP)
.then(() => {
logger.info('Setting Host : %s.',host);
return storage.getItem(host);
})
.then(value =>
logger.info(`'wanIP : ${value}.`)
)
.catch(err => logger.error(err));
logger.info('Replied /addhost request');
res.json({operation: "addhost",status: "SUCCESS"});
} else {
res.json({testingaddhost: "123"});
}
});
logger.info('HTTPS Server listening on %s:%s', HOST, PORT);