-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
122 lines (103 loc) · 3.49 KB
/
worker.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
import { Router } from './router.js'
import { MakeSave } from './generator.js'
const router = new Router();
router.cors();
function ValidateArr(field, requiredLen)
{
if(!field || field.length != requiredLen)
return false;
for(var i = 0; i < requiredLen; i++)
{
if(!ValidateInt(field[i]))
{
return false;
}
}
return true;
}
function ValidateInt(field)
{
return typeof field === 'number' && Number.isInteger(field);
}
function ValidateString(field) {
return field && typeof field === 'string';
}
function generateGUID() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + '4' + s4().substring(0, 3) + '-' + s4() + '-' + s4() + s4() + s4();
}
async function checkturnstile(request)
{
const body = await request.json();
const token = body.turnstile;
const ip = request.headers.get('CF-Connecting-IP');
let formData = new FormData();
formData.append('secret', ''); // your turnstile secret
formData.append('response', token);
formData.append('remoteip', ip);
const url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
const result = await fetch(url, {
body: formData,
method: 'POST',
});
const outcome = await result.json();
return outcome;
}
async function NotifyUsed(username, ip)
{
// webhook notify
}
router.post('/gen/', async ({env, req}) => {
if(!checkturnstile(req)) {
return Response.json({ "code": -1, "detail": "No valid captcha passed"});
}
let body = null;
try {
body = await req.json();
} catch (error) {
return Response.json({ "code": -1, "detail": "No valid body specified"});
}
if(!ValidateInt(body.totalplaytime) || !ValidateInt(body.damagetaken) || !ValidateInt(body.stepstaken)
|| !ValidateInt(body.crystalsbroken) || !ValidateInt(body.killersstunned) || !ValidateInt(body.totaldeaths)
|| !ValidateInt(body.totalpurchase) || !ValidateInt(body.roundswon) || !ValidateInt(body.totalenkephalin)
|| !ValidateString(body.playername)
|| !ValidateArr(body.HasBeenToMap, 45) || !ValidateArr(body.bossUnlocked, 4)
|| !ValidateArr(body.HasEncounteredAlt, 36) || !ValidateArr(body.HasEncountered, 89)
|| !ValidateArr(body.Achievements, 128) || !ValidateArr(body.HasSurvivedBoss, 4)
|| !ValidateArr(body.HasSurvivedAlt, 35) || !ValidateArr(body.HasSurvived, 89)) {
return Response.json({ "code": -1, "detail": "No valid parameters provided"});
}
const last_gen_info_text = await env.DB.get(body.playername);
if(last_gen_info_text)
{
var last_gen_info = JSON.parse(last_gen_info_text);
last_gen_info.last_seen = Date.now();
await env.DB.put(body.playername, JSON.stringify(last_gen_info));
return Response.json({"code": 0, "generated": last_gen_info.last_generated }, { status:200 });
}
const generated = MakeSave(body.playername, body);
var gen_info = {
gen_time: Date.now(),
last_generated: generated,
last_seen: Date.now(),
ip: req.headers.get('CF-Connecting-IP')
};
await env.DB.put(body.playername, JSON.stringify(gen_info));
await NotifyUsed(body.playername, gen_info.ip);
return Response.json({"code": 0, "generated": generated }, { status:200 });
})
import HTML from "./_index.html";
router.get('/', ({env, req}) => {
return new Response(HTML, {
headers: { "Content-Type": "text/html" }
});
})
export default {
async fetch(request, env, ctx) {
return router.handle(request, env, ctx)
}
}