-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
235 lines (210 loc) · 5.33 KB
/
Copy pathindex.js
File metadata and controls
235 lines (210 loc) · 5.33 KB
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
// Packages
const bodyParser = require('body-parser');
const path = require('node:path');
const express = require('express');
const cors = require('cors');
const DB = require('fshdb');
const svgCaptcha = require('svg-captcha');
let nanoid;
(async()=>{
const nanid = await import('nanoid');
nanoid = nanid.nanoid;
})();
// Options
const providerKeyLength = 12;
const captchaIDLength = 16;
const captchaExpire = 2 * 60 * 1000; // 2 mins
const verifiedIDLength = 16;
const verifiedExpire = 2 * 60 * 1000; // 2 mins
const cleanupTime = 10 * 60 *1000; // 10 mins
const captchaTextBase = {
width: 300,
height: 100,
noise: 4,
color: true,
background: '#181818'
};
// DBs
const providers = new DB('databases/providers.json', { compact: true });
const captchaStore = new Map(); // In memory
const verifiedStore = new Map(); // In memory
setInterval(()=>{
let now = Date.now();
[...captchaStore.entries()]
.forEach(([key, value]) => value.expires<now?captchaStore.delete(key):null);
[...verifiedStore.entries()]
.forEach(([key, value]) => value<now?captchaStore.delete(key):null);
}, cleanupTime)
/* Errors */
process.on('uncaughtException', function(err) {
console.log('Error!');
console.log(err);
});
/* Utility functions */
function getCookie(req, name) {
let cookies = req.headers.cookie;
name += '=';
cookies = String(cookies)
.split(' ')
.filter(cookie => cookie.startsWith(name))[0]
?.split(';')[0]
?.split('=')[1];
return cookies ?? '';
}
let tokenCache = {};
async function getUser(req) {
let cook = getCookie(req, 'FshAccountToken');
if (!cook) return;
if (tokenCache[cook]) return tokenCache[cook];
let res = await fetch('https://account.fsh.plus/api/me', { headers: { cookie: 'FshAccountToken='+cook } });
if (!res.ok) return;
res = await res.json();
tokenCache[cook] = res.id;
return res.id;
}
// Setup
const PORT = 3005;
const app = express();
app.use(cors());
app.use(bodyParser.json());
// Static resources
app.use('/media', express.static('media'));
// Main pages
app.get('/', async function(req, res) {
res.sendFile(path.join(__dirname, './pages/index.html'));
});
app.get('/widget.js', async function(req, res) {
res.sendFile(path.join(__dirname, './pages/widget.js'));
});
app.get('/test', async function(req, res) {
res.sendFile(path.join(__dirname, './pages/test.html'));
});
app.get('/panel', async function(req, res) {
if (!await getUser(req)) {
res.sendFile(path.join(__dirname, './pages/login.html'));
} else {
res.sendFile(path.join(__dirname, './pages/panel.html'));
}
});
// API V1
app.get('/api/v1/captcha', (req, res) => {
let key = req.query['key'];
if (!key || (typeof key).toLowerCase()!=='string' || key.length!==providerKeyLength) {
res.status(400);
res.json({
err: true,
msg: 'Key required'
});
return;
}
let prov = providers.get(key);
let site = req.query['site'];
if (!site) {
res.status(400);
res.json({
err: true,
msg: 'Site required'
});
return;
}
try {
site = new URL(site);
} catch(err) {
res.status(400);
res.json({
err: true,
msg: 'Site invalid'
});
return;
}
if (!prov.domains[site.hostname]) {
res.status(400);
res.json({
err: true,
msg: 'Site / provider missmatch'
});
return;
}
// Generate
let method = prov.domains[site.hostname];
const captchaId = nanoid(captchaIDLength);
let data = { image: '', result: '', input: '' };
let captcha;
switch(method.method) {
case 'text':
captcha = svgCaptcha.create({
...captchaTextBase,
size: method.length,
ignoreChars: '0oO1lLI'
});
data = { image: captcha.data, result: captcha.text, input: 'text' };
break;
case 'math':
captcha = svgCaptcha.createMathExpr({
...captchaTextBase,
mathMin: method.min,
mathMax: method.max,
mathOperator: method.operators
});
data = { image: captcha.data, result: captcha.text, input: 'text' };
break;
}
captchaStore.set(captchaId, {
result: data.result,
provider: key,
expires: Date.now() + captchaExpire
});
res.json({
id: captchaId,
input: data.input,
image: data.image
});
});
app.post('/api/v1/verify', (req, res) => {
let id = req.body['id'];
let input = req.body['input'];
if (!id || !input) {
res.status(400);
res.json({
err: true,
msg: 'invalid body'
});
return;
}
const stored = captchaStore.get(id);
if (!stored || Date.now() > stored.expires) {
res.status(400);
res.json({
err: true,
msg: 'invalid id'
});
return;
}
const match = stored.result.toLowerCase() === input.toLowerCase();
providers.push(stored.provider+'.requests', {
passed: match,
telemetry: {
ua: req.headers['user-agent']
}
});
captchaStore.delete(id);
if (!match) {
res.json({ success: false });
return;
}
let verid = nanoid(verifiedIDLength);
verifiedStore.set(verid, Date.now()+verifiedExpire);
res.json({ success: true, verid });
});
app.get('/api/v1/check', (req, res) => {
res.json({ verified: verifiedStore.has(req.query('id')||'') });
});
// 404
app.use(function(req, res) {
res.status(404);
res.sendFile(path.join(__dirname, './pages/404.html'));
});
// Listen
app.listen(PORT, ()=>{
console.log('server running on '+PORT);
});