aboutsummaryrefslogtreecommitdiff
path: root/server/index.js
diff options
context:
space:
mode:
authorkaotisk <kaotisk@arching-kaos.org>2024-10-10 07:42:13 +0300
committerkaotisk <kaotisk@arching-kaos.org>2024-10-10 07:42:13 +0300
commit8281be7ba874e116ccc43e41d1275c1d531c54dc (patch)
tree9f2495682205807db74842674b4bedfbc27b0daf /server/index.js
parent3c508f9bf96bc425eb183f676214ed9b9d14b84e (diff)
downloadarching-kaos-radio-8281be7ba874e116ccc43e41d1275c1d531c54dc.tar.gz
arching-kaos-radio-8281be7ba874e116ccc43e41d1275c1d531c54dc.tar.bz2
arching-kaos-radio-8281be7ba874e116ccc43e41d1275c1d531c54dc.zip
Implementation
Diffstat (limited to 'server/index.js')
-rwxr-xr-xserver/index.js171
1 files changed, 171 insertions, 0 deletions
diff --git a/server/index.js b/server/index.js
new file mode 100755
index 0000000..4f2e8b8
--- /dev/null
+++ b/server/index.js
@@ -0,0 +1,171 @@
+const http = require("node:http");
+const fs = require("node:fs");
+
+// const welcomeMessage = require("./routes/default/index.js");
+// const getNodeInfo = require('./routes/getNodeInfo/index.js');
+// const getPeers = require('./routes/getPeers/index.js');
+// const getZblock = require('./routes/getZblock/index.js');
+// const getZlatest = require('./routes/getZLatest/index.js');
+// const getSblock = require('./routes/getSBlock/index.js');
+// const getSlatest = require('./routes/getSLatest/index.js');
+
+// const akLogMessage = require('./lib/akLogMessage');
+function akLogMessage(type, message)
+{
+ console.log(type+": "+message);
+}
+
+const serverOptions = { keepAliveTimeout: 60000 };
+
+function printRequest(req)
+{
+ console.log(req.connection.remoteAddress);
+ console.log(req.headers);
+ console.log(req.method, req.url);
+ console.log('HTTP/' + req.httpVersion);
+}
+
+function respondError(res, errorMessage)
+{
+ res.writeHead(404, { 'Content-Type': 'application/json'});
+ res.end(JSON.stringify({
+ error: errorMessage
+ }));
+}
+
+function respondJSON(res, hash)
+{
+ test = /[0-9a-f]{128}/
+ if (test.test(hash))
+ {
+ res.writeHead(200, { 'Content-Type': 'application/json'});
+ res.end(JSON.stringify(JSON.parse(fs.readFileSync('../hashes/' + hash))));
+ } else {
+ respondError(res, 'Not hash');
+ }
+}
+
+function testRootRoute(req, res)
+{
+ notImplemented(req, res);
+}
+
+function testRoute(req, res)
+{
+ respondError(res, "Mpla mpla");
+}
+
+function applicationRoutes(req, res, args)
+{
+ if (args[4] === 'undefined')
+ {
+ respondError(res, 'Bye');
+ return;
+ }
+ switch(args[3])
+ {
+ case 'json': respondJSON(res, args[4]); break;
+ default: notImplemented(req, res);
+ }
+}
+
+// Audio returns
+function respondOGG(res, hash)
+{
+ res.writeHead(200, { 'Content-Type': 'audio/ogg'});
+ test = /[0-9a-f]{128}/
+ console.log(test.test(hash))
+ res.end(fs.readFileSync('../hashes/' + hash));
+}
+
+function audioRoutes(req, res, args)
+{
+ if (args.length < 4 || args[4] === '')
+ {
+ respondError(res, 'No hash');
+ return;
+ }
+ if (args.length < 3 || args[3] === '')
+ {
+ respondError(res, 'No filetype/extension');
+ return;
+ }
+ switch(args[3])
+ {
+ case 'ogg': respondOGG(res, args[4]); break;
+ default: notImplemented(req, res);
+ }
+}
+
+function getLatestList(req, res)
+{
+ res.writeHead(200, { 'Content-Type': 'application/json'});
+ res.end(JSON.stringify(JSON.parse(fs.readFileSync('../hashes/list'))));
+}
+
+function getRoutes(req, res)
+{
+ var args = req.url.split('/');
+ if (args[1] === 'v0' && args.length > 2 && args[2] !== ""){
+ switch(args[2])
+ {
+ case 'test': testRoute(req, res); break;
+ case 'list': getLatestList(req, res); break;
+ case 'application': applicationRoutes(req, res, args); break;
+ case 'audio': audioRoutes(req, res, args); break;
+ default: notImplemented(req, res);
+ }
+ }
+ else {
+ notImplemented(req, res);
+ }
+}
+
+function postRoutes(req, res)
+{
+ switch(req.url)
+ {
+ default: notImplemented(req, res);
+ }
+}
+
+function notImplemented(req, res)
+{
+ res.writeHead(404, { 'Content-Type': 'application/json'});
+ res.end(JSON.stringify({
+ url: req.url,
+ error: 'not implemented'
+ }));
+}
+
+function processMethod(req, res)
+{
+ switch(req.method)
+ {
+ case 'GET': getRoutes(req, res); break;
+ case 'POST': postRoutes(req, res); break;
+ default: notImplemented(req, res);
+ }
+}
+
+function checkIfAllowedIP(address)
+{
+ return address.startsWith('fc') ? true : false;
+}
+
+function requestParser(req, res)
+{
+ printRequest(req);
+ akLogMessage('INFO', `Incoming from [${req.connection.remoteAddress}]:${req.socket._peername.port} @ ${req.headers.host}${req.url}`);
+ if (checkIfAllowedIP(req.connection.remoteAddress)){
+ res.setHeader('Access-Control-Allow-Origin', '*');
+ processMethod(req, res);
+ }
+ else {
+ res.end();
+ }
+}
+
+const server = http.createServer(serverOptions, requestParser);
+
+server.listen(8010);