This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.js
119 lines (104 loc) · 3.68 KB
/
db.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
const { QuickDB } = require("quick.db");
const db = new QuickDB({ filePath: "db.sqlite" });
async function addServer (id) {
if (db.get(`servers.${id}`)) return;
await db.set(`servers.${id}`, {
proxies: {},
users: {},
limit: 3
});
}
async function removeServer (id) {
if (!db.get(`servers.${id}`)) return;
await db.set(`servers.${id}`, {});
}
async function getUser (id, userId) {
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
let user = await db.get(`servers.${id}.users.${userId}`) || {
count: 0,
links: {}
};
return success(user);
}
async function setUser (id, userId, user) {
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
await db.set(`servers.${id}.users.${userId}`, user);
return success();
}
async function getLimit (id) {
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
let limit = await db.get(`servers.${id}.limit`) || 3;
return success(limit);
}
async function setLimit (id, limit) {
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
await db.set(`servers.${id}.limit`, limit);
return success();
}
async function reset (id, userId) {
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
if (userId) {
let user = await db.get(`servers.${id}.users.${userId}`) || {
count: 0,
links: {}
};
console.log(user);
user.count = 0;
await db.set(`servers.${id}.users.${userId}`, user);
} else {
let users = await db.get(`servers.${id}.users`) || {};
console.log(users);
Object.keys(users).forEach(async (key) => {
let user = await db.get(`servers.${id}.users.${key}`) || {
count: 0,
links: {}
};
console.log(user);
user.count = 0;
await db.set(`servers.${id}.users.${key}`, user);
});
}
}
async function addLink (id, type, link) {
if (type.startsWith("__")) return error("Invalid link type.");
if (!/https?:\/\/.+\..+/i.test(link)) return error("Invalid link.");
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
let links = await db.get(`servers.${id}.proxies.${type}`) || [];
if (links.includes(link)) return error("Link already exists.");
await db.push(`servers.${id}.proxies.${type}`, link);
return success();
}
async function removeLink (id, type, link) {
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
let links = await db.get(`servers.${id}.proxies.${type}`) || [];
if (!links.includes(link)) return error("Link does not exist.");
links = links.filter(l => l !== link);
await db.set(`servers.${id}.proxies.${type}`, links);
return success();
}
async function getTypes (id) {
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
let proxies = await db.get(`servers.${id}.proxies`);
proxies = Object.entries(proxies).filter(([type, links]) => links.length > 0);
proxies = Object.fromEntries(proxies);
return success(Object.keys(proxies));
}
async function getLinks (id, type) {
if (!db.get(`servers.${id}`)) return error("Server not found, please contact support.");
let proxies = await db.get(`servers.${id}.proxies.${type}`) || [];
if (proxies.length <= 0) return error("No links found.");
return success(proxies);
}
function error (message) {
return {
status: false,
message: message
}
}
function success (data) {
return {
status: true,
data: data
}
}
module.exports = { addServer, removeServer, reset, addLink, removeLink, setTimeout, getTypes, getLinks, getUser, setUser, getLimit, setLimit };