-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (83 loc) · 2.96 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
// Example simple server with very simple API key handling, HTTPS.
const raw_translate = require ('./raw-translate');
const https = require('https');
const fs = require('fs');
const url = require('url');
var apikeys = [];
try {
apikeys = fs.readdirSync('./apikeys');
} catch {};
apikeys.length > 0 || console.log("*** No API keys folder present or folder empty. Proceeding without API access control.");
const hex2data = hex => {
if (hex.length %2 !== 0)
return null; // Must be even number of hex characters
let c = 0;
const bytes = [];
for (bytes; c < hex.length; c += 2) {
let b = parseInt(hex.substr(c, 2), 16);
if (isNaN(b))
return null; // Bad data input
bytes.push(b);
}
return bytes;
};
console.log("*** Attempting to read HTTPS key.pem and certificate.pem from ./cert/ folder, not included in this repo.");
console.log("*** Obtain these files from your ISP, a commercial certificate, or by generating your own certificate.");
const options = {
key: fs.readFileSync('cert/key.pem'),
cert: fs.readFileSync('cert/cert.pem')
};
if (parseInt(process.env.PORT) === NaN) {
console.log("Please set environment variable PORT to define which port to serve.");
return 1;
}
console.log("");
console.log("Creating https server at port " + process.env.PORT)
https.createServer(options, function (req, res) {
console.log("Handling " + req.url);
const query = url.parse(req.url, true).query;
const pathname = url.parse(req.url, true).pathname;
if (pathname != '/translate') {
console.log("Unknown function " + pathname);
res.writeHead(400);
res.end("Unknown function " + pathname);
return;
}
const loraPort = parseInt(query.p);
if (isNaN(loraPort) || loraPort < 1) {
console.log("missing port");
res.writeHead(400);
res.end("Missing parameter 'p' (port, integer)");
return;
}
const loraData = query.d;
if (typeof(loraData) !== 'string') {
console.log("missing data");
res.writeHead(400);
res.end("Missing parameter 'd' (data), should be string of hexadecimal digits");
return;
}
console.log("port: " + loraPort);
console.log("data: " + loraData);
const bytes = hex2data(loraData);
if (bytes === null) {
console.log("bad data");
res.writeHead(400);
res.end("Bad parameter 'd' (data), hexadecimal digits could not be converted");
return;
}
if (apikeys.length > 0) {
const apikey = query.k;
console.log ("key: " + apikey);
if (apikeys.findIndex(k=>k==apikey) == -1) {
console.log("bad/missing key");
res.writeHead(400);
res.end("API key does not match a registerred key");
return;
}
}
const object = raw_translate(bytes, loraPort);
console.log(object);
res.writeHead(200);
res.end(JSON.stringify(object));
}).listen(process.env.PORT);