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
|
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 requestParser(req, res)
{
printRequest(req);
akLogMessage('INFO', `Incoming from [${req.connection.remoteAddress}]:${req.socket._peername.port} @ ${req.headers.host}${req.url}`);
}
const server = http.createServer(serverOptions, requestParser);
server.listen(8010);
|