-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
214 lines (186 loc) · 6.58 KB
/
index.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
require('dotenv').config();
const cors = require('cors');
const rateLimit = require('express-rate-limit');
const bodyParser = require('body-parser');
const express = require("express");
const endpointLoader = require("./api/endpointLoader");
const um = require('./api/v1/db/UserManager');
const cast = require("./utils/Cast");
const logs = require('./utils/Logs');
const path = require('path');
const multer = require('multer');
const fs = require('fs');
const requestIp = require('request-ip');
const {OAuth2Client} = require('google-auth-library');
const ipaddr = require('ipaddr.js');
const { promisify } = require('util');
const mcache = require('memory-cache');
require('colors');
function escapeXML(unsafe) {
unsafe = String(unsafe);
return unsafe.replace(/[<>&'"\n]/g, c => {
switch (c) {
case '<': return '<';
case '>': return '>';
case '&': return '&';
case '\'': return ''';
case '"': return '"';
case '\n': return ' '
}
});
};
function error(res, code, error) {
res.status(code);
res.header("Content-Type", 'application/json');
res.json({ "error": error });
}
const app = express();
const PORT = Number(process.env.PORT) || 8080;
const MAXVIEWS = Number(process.env.MaxViews) || 10000; // it will take up to 10000 views then reset after
const VIEWRESETRATE = Number(process.env.ViewResetRate) || 1000 * 60 * 60; // reset every hour
const upload = multer({
dest: 'tmp/uploads/',
limits: { fileSize: ((Number(process.env.UploadSize)) || 5) * 1024 * 1024 } // 5mb - max size per asset
});
app.use(cors({
origin: '*', // this gets overwritten by some endpoints (all that use your login)
utilsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}));
app.use(bodyParser.json());
app.use(express.urlencoded({
limit: process.env.ServerSize,
extended: false
}));
app.use(express.json({
limit: process.env.ServerSize
}));
app.set('trust proxy', 1);
app.use(rateLimit({
validate: {
trustProxy: true,
xForwardedForHeader: true,
},
windowMs: 5000, // 150 requests per 5 seconds
limit: 150,
standardHeaders: 'draft-7',
legacyHeaders: false,
}));
app.use(requestIp.mw());
const Cast = new cast();
const UserManager = new um();
(async () => {
await UserManager.init(MAXVIEWS, VIEWRESETRATE);
// log when starting and what time zone (utc+-x)
console.log(`Starting at ${new Date().toLocaleString('en-US', { timeZone: 'CST' })} CST...`.green);
/*
app.get("/test", (req, res) => {
res.sendFile(path.join(__dirname, 'test.html'));
});
*/
app.use((req, res, next) => {
// get the actuall ip
req.realIP = ipaddr.process(process.env.isCFTunnel === "true" ? req.get("CF-Connecting-IP") : req.clientIp);
if (req.realIP.kind() === 'ipv6') {
req.realIP = req.realIP.toNormalizedString();
} else {
req.realIP = req.realIP.toIPv4MappedAddress().toNormalizedString();
}
next();
});
// ip banning
app.use(async (req, res, next) => {
if (await UserManager.isIPBanned(req.realIP)) {
return error(res, 418, "You are banned from using this service."); // 418 for the sillies
}
next();
});
app.get("/robots.txt", (req, res) => {
res.sendFile(path.join(__dirname, 'robots.txt'));
});
function cumulative_file_size_limit(utils) {
return async function (req, res, next) {
const unlink = async () => {
if (req.files.jsonFile)
await utils.unlinkAsync(req.files.jsonFile[0].path);
if (req.files.thumbnail)
await utils.unlinkAsync(req.files.thumbnail[0].path);
for (let asset of req.files.assets) {
await utils.unlinkAsync(asset.path);
}
}
const maxCombinedSize = (Number(process.env.CumulativeUploadSize) || 32) * 1024 * 1024;
let combinedSize = 0;
if (req.files.jsonFile) combinedSize += req.files.jsonFile[0].size;
if (req.files.thumbnail) combinedSize += req.files.thumbnail[0].size;
if (req.files.assets)
for (let asset of req.files.assets)
combinedSize += asset.size;
if (combinedSize > maxCombinedSize) {
await unlink();
return utils.error(res, 400, "Files too big");
}
next();
}
}
function save_cache(key, body, duration) {
mcache.put(key, body, duration * 1000);
}
function has_cache(key) {
return !!mcache.get(key);
}
function get_cache(key) {
return mcache.get(key);
}
function get_key(req) {
return "__express__" + (req.originalUrl || req.url);
}
function handle_page(page) {
return Math.max(0, Number(page) || 0);
}
endpointLoader(app, 'v1/routes', {
UserManager: UserManager,
homeDir: path.join(__dirname, "./"),
Cast: Cast,
escapeXML: escapeXML,
error: error,
env: process.env,
upload: upload,
uploadCooldown: Number(process.env.UploadCooldown) || 1000 * 60 * 8,
unlinkAsync: promisify(fs.unlink),
path: path,
PORT: PORT,
handle_page,
logs,
googleOAuth2Client: OAuth2Client,
ipaddr,
cache: {
save: save_cache,
has: has_cache,
get: get_cache,
key: get_key
},
rateLimiter: rateLimit,
cumulative_file_size_limit: cumulative_file_size_limit,
cors: () => cors({
origin: function (origin, callback) {
const whitelist = [process.env.HomeURL, "http://localhost:5173", "http://test.mydomain.com:5173"];
const idxWebPreview = ".cloudworkstations.dev"; //project idx sigma development
if (!origin || whitelist.indexOf(origin) !== -1 || origin.endsWith(idxWebPreview)) {
callback(null, true)
} else {
callback(null, false)
}
},
}),
});
app.use((err, req, res, next) => {
if (err instanceof multer.MulterError) {
return error(res, 400, `One of your assets is too large. The maximum size is ${Number(process.env.UploadSize) || 5}mb.`);
}
console.error(err);
error(res, 500, "InternalError");
})
app.listen(PORT, () => {
console.log(`API is listening on port ${PORT}`);
});
})();