-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
242 lines (209 loc) · 6.28 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const fs = require('fs');
const http = require('http');
const https = require('https');
const path = require('path');
const cors = require('cors');
const express = require('express');
const db = require('./db');
const bodyParser = require('body-parser');
const safePort = require('./safePort');
const buildLogPkg = require('./buildLogPkg');
const port = safePort(process.argv);
const app = express();
const privateKey = fs.readFileSync('/etc/letsencrypt/live/replicataudio.com/privkey.pem', 'utf8');
const certificate = fs.readFileSync('/etc/letsencrypt/live/replicataudio.com/cert.pem', 'utf8');
const ca = fs.readFileSync('/etc/letsencrypt/live/replicataudio.com/chain.pem', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
app.use(cors());
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
const trackingImgs = fs.readdirSync(__dirname+'/public/ti/');
const logger = async function (req, res, next)
{
console.log(req.ip+' '+req.path);
if(req.path.includes('/ti/')){
let trackingTag = '';
for(const img of trackingImgs){
if(path.basename(req.path) === img){
trackingTag = path.parse(img).name;
break;
}
}
if(trackingTag !== ''){
const referer = req.headers.referer || 'unknown';
console.log('Hit tracking img: '+trackingTag);
const logPkg= {
'ip': req.ip,
'tag': 'ti_'+trackingTag,
'origin': referer
};
try{
await db.Actions.create(logPkg);
}
catch(err){
console.log(err);
}
}
}
next();
}
app.use(logger);
app.use(express.static('public'));
app.set('json spaces', 2);//JSON formatting
app.get('/', async (req, res) => {
res.send('OK!');
});
app.get('/status', async (req, res) => {
res.send('OK');
});
app.post('/log', async (req, res) => {
console.log(req.body.origin || 'sketch');
const required = {
'baseUrl':'required',
'method':'required',
'originalUrl':'_',
'query':'[]',
'params':'[]',
'body':'{}',
'ip':'required',
'tag':'required',
'origin':'required',
};
let logPkg = buildLogPkg(req.body, required);
if(logPkg.msg != 'OK'){
res.send(lockPkg.msg);
return;
}
try{
await db.Logs.create(logPkg.logPkg);
res.send('OK');
}catch(err){
console.log(err);
res.send('Err');
}
});
app.post('/action', async (req, res) => {
console.log(req.body || 'sketch');
const required = {
'ip': req.ip,
'tag': 'required',
'origin': 'required'
};
let logPkg = buildLogPkg(req.body, required);
if(logPkg.msg != 'OK'){
res.send(lockPkg.msg);
return;
}
await db.Actions.create(logPkg.logPkg);
res.send('OK');
});
app.post('/download', (req, res) => {
console.log('Hit download');
res.send('OK');
});
// app.get('/downloadAnalytics', async (req, res) => {
// const json = await db.Downloads.findAll();
// res.json(json);
// });
app.get('/logs', async (req, res) => {
const json = await db.Logs.findAll();
var filename = 'ra_logs.json'; // or whatever
var mimetype = 'application/json';
res.setHeader('Content-Type', mimetype);
res.setHeader('Content-disposition','attachment; filename='+filename);
res.send( json );
});
app.get('/actionsDL', async (req, res) => {
const { Op } = require("sequelize");
const limit = req.query.limit || 1000;
const query = {};
if (req.query.filter)
{
query.where = {
tag: {
[Op.like]: '%' + 'gw' + '%'
}
};
}
query.order = [
['id', 'DESC']
]
query.limit = limit;
const json = await db.Actions.findAll(query);
var filename = 'ra_actions.json'; // or whatever
var mimetype = 'application/json';
res.setHeader('Content-Type', mimetype);
res.setHeader('Content-disposition','attachment; filename='+filename);
res.send( json );
});
app.get('/actions', async (req, res) => {
const { Op } = require("sequelize");
const limit = req.query.limit || 1000;
const query = {};
if (req.query.filter)
{
query.where = {
tag: {
[Op.like]: '%' + req.query.filter + '%'
}
};
}
query.order = [
['id', 'DESC']
]
query.limit = limit;
const json = await db.Actions.findAll(query);
res.send(json);
});
app.get('/motd/lowfire', (req, res) => {
const title = "NEW UPDATE";
const body = "LowFire v22.12.19 is releasing today";
const link = "https://replicataudio.com/lowfire"
const tag = "update22_12_19";
const delimiter = "|";
res.send(title+delimiter+body+delimiter+link+delimiter+tag);
});
app.get('/motd/greenwave', (req, res) => {
const title = "Welcome";
const body = "Thanks for joining the GreenWave Beta!";
const link = "https://replicataudio.com"
const tag = "test";
const delimiter = "|";
res.send(title+delimiter+body+delimiter+link+delimiter+tag);
});
app.get('/motd/dolos', (req, res) => {
const title = "Welcome";
const body = "Thanks for choosing ReplicatAudio!";
const link = "https://replicataudio.com"
const tag = "release";
const delimiter = "|";
res.send(title+delimiter+body+delimiter+link+delimiter+tag);
});
app.get('/motd/generic', (req, res) => {
const title = "Yo!";
const body = "Thanks for choosing ReplicatAudio!";
const link = "https://replicataudio.com"
const tag = "test";
const delimiter = "|";
res.send(title+delimiter+body+delimiter+link+delimiter+tag);
});
// app.listen(port, () => {
// console.log(`Example app listening on port ${port}`);
// });
// Starting both http & https servers
const httpServer = http.createServer(app);
const httpsServer = https.createServer(credentials, app);
httpServer.listen(port-1, () => {
console.log('HTTP Server running on port '+port-1);
});
httpsServer.listen(port, () => {
console.log('HTTPS Server running on port '+port);
});