This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
userapi.js
309 lines (304 loc) · 14 KB
/
userapi.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import httpserver from "https://raw.githubusercontent.com/txthinking/denolib/master/httpserver.js";
import { now, md5, ok, sh1, s2b, b2s, joinhostport, echo, splithostport } from "https://raw.githubusercontent.com/txthinking/denolib/master/f.js";
export default async function () {
httpserver.path("/userapi/get_setting", async (r) => {
var rows = await db.query("select * from setting where k='simulate_payment' or k='site_name' or k='announcement' or k='site_telegram' or k='site_domain' or k='txthinking_payments' or k='recaptcha' or k='recaptcha_site_key'");
return ok(rows);
});
httpserver.path("/userapi/signup", async (r) => {
var j = await r.json();
var recaptcha = (await db.query("select * from setting where k='recaptcha'"))[0].v;
var recaptcha_secret_key = (await db.query("select * from setting where k='recaptcha_secret_key'"))[0].v;
if (recaptcha == "true") {
var res = await fetch("https://www.google.com/recaptcha/api/siteverify", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams({
secret: recaptcha_secret_key,
response: j.recaptcha_token,
}).toString(),
});
var r = await res.json();
if (!r.success) {
throw JSON.stringify(r);
}
}
var row = (await db.query("select * from setting where k='site_domain'"))[0];
if (!row.v) {
throw "Please configure the site_domain before signup";
}
var row = (await db.query("select * from setting where k='enable_signup'"))[0];
if (row.v != "true") {
throw "Registration is temporarily closed. 注册暂时关闭。";
}
var rows = await db.query("select * from user where username=? limit 1", [j.username]);
if (rows.length) {
throw "Username exists";
}
var username = j.username;
var password = md5(j.password);
var p0 = (await db.query("select * from setting where k='start_port' limit 1"))[0].v;
var p1 = (await db.query("select * from setting where k='last_port' limit 1"))[0].v;
var port = p1 == 0 ? p0 : p1;
var port0, port1, port2, port3;
for (;;) {
port++;
if ((await db.query("select * from user where port0=? or port1=? or port2=? or port3=? limit 1", [port, port, port, port])).length == 0 && (await db.query("select * from instance where single_port=? or address like ? limit 1", [port, `%:${port}`])).length == 0) {
port0 = port;
break;
}
}
for (;;) {
port++;
if ((await db.query("select * from user where port0=? or port1=? or port2=? or port3=? limit 1", [port, port, port, port])).length == 0 && (await db.query("select * from instance where single_port=? or address like ? limit 1", [port, `%:${port}`])).length == 0) {
port1 = port;
break;
}
}
for (;;) {
port++;
if ((await db.query("select * from user where port0=? or port1=? or port2=? or port3=? limit 1", [port, port, port, port])).length == 0 && (await db.query("select * from instance where single_port=? or address like ? limit 1", [port, `%:${port}`])).length == 0) {
port2 = port;
break;
}
}
for (;;) {
port++;
if ((await db.query("select * from user where port0=? or port1=? or port2=? or port3=? limit 1", [port, port, port, port])).length == 0 && (await db.query("select * from instance where single_port=? or address like ? limit 1", [port, `%:${port}`])).length == 0) {
port3 = port;
break;
}
}
var row = await db.c("user", { username, password, port0, port1, port2, port3, transfer: 0 });
return ok();
});
httpserver.path("/userapi/signin", async (r) => {
var j = await r.json();
var username = j.username;
var password = md5(j.password);
var rows = await db.query("select * from user where username=? and password=? limit 1", [username, password]);
if (!rows.length) {
throw "Username or password is not correct";
}
var token = await c.encrypt("id", rows[0].id);
return ok({
token,
});
});
httpserver.path("/userapi/get_user_row", async (r) => {
var j = await r.json();
var id = await c.decrypt("id", j.token);
var row = await db.r("user", id);
return ok(row);
});
httpserver.path("/userapi/update_password", async (r) => {
var j = await r.json();
var id = await c.decrypt("id", j.token);
var password = md5(j.password);
var row = await db.u("user", { id, password });
return ok(row);
});
httpserver.path("/userapi/get_vip_rows", async (r) => {
var rows = await db.query("select * from vip");
return ok(rows);
});
httpserver.path("/userapi/get_product_rows", async (r) => {
var rows = await db.query("select * from product where isdeleted = 1");
return ok(rows);
});
httpserver.path("/userapi/create_payment", async (r) => {
var j = await r.json();
var id = await c.decrypt("id", j.token);
var row = await db.r("user", id);
if (row.baned == 2) {
throw "Baned";
}
var p = await db.r("product", j.id);
if (p.isdeleted == 2) {
throw "Product does not exist";
}
if (j.method == "simulate_payment") {
if ((await db.query("select * from setting where k='simulate_payment'"))[0].v != "true") {
throw "This payment method closed";
}
var row = {
user_id: id,
product_id: j.id,
paymentid: "",
paymentmethod: j.method,
status: 1,
};
row = await db.c("payment", row);
return ok({
url: `./simulate_payment.html?id=${row.id}`,
});
}
if (j.method == "txthinking_payments") {
if ((await db.query("select * from setting where k='txthinking_payments'"))[0].v != "true") {
throw "This payment method closed";
}
var key = (await db.query("select * from setting where k='txthinking_payments_key' limit 1"))[0].v;
var domain = (await db.query("select * from setting where k='site_domain' limit 1"))[0].v;
var row = {
user_id: id,
product_id: j.id,
paymentid: "",
paymentmethod: j.method,
status: 1,
};
row = await db.c("payment", row);
var res = await fetch("https://api.txthinking.com/payments/create", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
Key: key,
OrderID: row.id.toString(),
Webhook: `https://${domain}/userapi/payment_callback`,
USD: p.price_usd,
}),
});
var q = new URLSearchParams(await res.json());
return ok({
url: `./cryptocurrency_payment.html?${q.toString()}`,
});
}
throw "Invalid payment method";
});
httpserver.path("/userapi/payment_callback", async (r) => {
var j = await r.json();
var id = j.OrderID;
var row = await db.r("payment", id);
var key = (await db.query("select * from setting where k='txthinking_payments_key' limit 1"))[0].v;
if (row.status == 2) {
return ok();
}
var paymentid = "";
if (row.paymentmethod == "txthinking_payments") {
if (j.Key != key) {
throw "Hacking";
}
paymentid = j.B + ":" + j.Txn;
}
row.status = 2;
row.paymentid = paymentid;
await db.u("payment", row);
var p = await db.r("product", row.product_id);
var rows = await db.query("select * from user_vip where user_id=?", [row.user_id]);
for(var i=0;i<rows.length;i++){
await db.u("user_vip", { id: rows[i].id, expiration: 0 });
if(rows[i].expiration > now()){
await db.c("task", { name: 'expiration', user_id: rows[i].user_id, data: JSON.stringify({user_id: rows[i].user_id}) });
}
}
var rows = await db.query("select * from user_vip where user_id=? and vip_id=? limit 1", [row.user_id, p.vip_id]);
if (!rows.length) {
await db.c("user_vip", {
user_id: row.user_id,
vip_id: p.vip_id,
expiration: now() + p.duration,
});
}
if (rows.length) {
rows[0].expiration = rows[0].expiration < now() ? now() + p.duration : rows[0].expiration + p.duration;
await db.u("user_vip", rows[0]);
}
await db.c("task", { name: 'new_vip', user_id: row.user_id, data: JSON.stringify({user_id: row.user_id, vip_id: p.vip_id}) });
return ok();
});
httpserver.path("/userapi/get_user_vip_rows", async (r) => {
var j = await r.json();
var id = await c.decrypt("id", j.token);
var rows = await db.query("select * from user_vip where user_id=? and expiration>?", [id, parseInt(Date.now() / 1000)]);
return ok(rows);
});
httpserver.path("/block/domain.txt", async (r) => {
var s = (await db.query("select * from setting where k='domainlist'"))[0].v;
return new Response(s);
});
httpserver.path("/block/cidr4.txt", async (r) => {
var s = (await db.query("select * from setting where k='cidr4list'"))[0].v;
return new Response(s);
});
httpserver.path("/block/cidr6.txt", async (r) => {
var s = (await db.query("select * from setting where k='cidr6list'"))[0].v;
return new Response(s);
});
httpserver.path("/brook_link.txt", async (r) => {
var id = await c.decrypt("id", new URL(r.url).searchParams.get("token"));
var user = await db.r("user", id);
var key = (await db.query('select * from setting where k="key" limit 1'))[0].v;
var password = md5(`${key}${user.id}`);
var path = md5(`${key}${user.username}`);
var l = [];
if(user.instance_ids){
var rows = await db.query("select * from instance where id in ?", [user.instance_ids.split(':')]);
rows.forEach((v) => {
var [ip, _] = splithostport(v.address);
if (v.kind == 1) {
if (v.enable_brook_server == 2) {
var q = new URLSearchParams();
q.set("server", joinhostport(ip, user.port1));
q.set("password", password);
q.set("name", v.name);
l.push(`brook://server?${q.toString()}`);
q.set("udpovertcp", "true");
q.set("name", v.name + "(udpovertcp)");
l.push(`brook://server?${q.toString()}`);
}
if (v.enable_brook_wsserver == 2) {
var q = new URLSearchParams();
q.set("wsserver", "ws://" + joinhostport(ip, user.port2));
q.set("password", password);
q.set("name", v.name);
l.push(`brook://wsserver?${q.toString()}`);
}
if (v.enable_brook_wssserver == 2) {
var q = new URLSearchParams();
q.set("wssserver", "wss://" + joinhostport(v.domain, user.port3));
q.set("password", password);
q.set("address", joinhostport(ip, user.port3));
if (v.enable_brook_wssserver_withoutbrookprotocol == 2) {
q.set("withoutBrookProtocol", "true");
}
if (v.wssserver_kind == 2 || v.wssserver_kind == 3) {
q.set("ca", v.ca);
}
q.set("name", v.name);
l.push(`brook://wssserver?${q.toString()}`);
}
}
if (v.kind == 2) {
var q = new URLSearchParams();
q.set("wssserver", "wss://" + joinhostport(v.domain, v.single_port) + "/" + path);
q.set("password", password);
q.set("address", joinhostport(ip, v.single_port));
if (v.single_iswithoutbrookprotocol == 2) {
q.set("withoutBrookProtocol", "true");
}
if (v.single_kind == 3 || v.single_kind == 4) {
q.set("ca", v.ca);
}
q.set("name", v.name);
l.push(`brook://wssserver?${q.toString()}`);
}
});
}
var vip_ids = (await db.query('select * from user_vip where user_id=? and expiration>?', [user.id, now()])).map(v=>v.vip_id);
vip_ids.push((await db.query('select * from vip where level=0'))[0].id);
var rows = await db.query("select * from brook_link where (vip_or_user=1 and vip_id in ?) or (vip_or_user=2 and user_id=?)", [vip_ids, id]);
rows.forEach((v) => {
l.push(v.brook_link);
});
return new Response(l.join("\n"));
});
httpserver.path("/userapi/get_task_count", async (r) => {
var j = await r.json();
var id = await c.decrypt("id", j.token);
var rows = await db.query("select * from task where isdeleted=1 and user_id=?", [id]);
return ok({count: rows.length});
});
}