forked from Not-D4rkCipherX/Dropee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
180 lines (154 loc) · 5.35 KB
/
utils.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
const fs = require("fs");
const colors = require("colors");
const { DateTime } = require("luxon");
const path = require("path");
require("dotenv").config();
function _isArray(obj) {
if (Array.isArray(obj) && obj.length > 0) {
return true;
}
try {
const parsedObj = JSON.parse(obj);
return Array.isArray(parsedObj) && parsedObj.length > 0;
} catch (e) {
return false;
}
}
// Hàm để ghi đè biến môi trường
const envFilePath = path.join(__dirname, ".env");
function updateEnv(variable, value) {
// Đọc file .env
fs.readFile(envFilePath, "utf8", (err, data) => {
if (err) {
console.log("Không thể đọc file .env:", err);
return;
}
console.log(value, variable);
// Tạo hoặc cập nhật biến trong file
const regex = new RegExp(`^${variable}=.*`, "m");
const newData = data.replace(regex, `${variable}=${value}`);
// Kiểm tra nếu biến không tồn tại trong file, thêm vào cuối
if (!regex.test(data)) {
newData += `\n${variable}=${value}`;
}
// Ghi lại file .env
fs.writeFile(envFilePath, newData, "utf8", (err) => {
if (err) {
console.error("Không thể ghi file .env:", err);
} else {
console.log(`Đã cập nhật ${variable} thành ${value}`);
}
});
});
}
function sleep(seconds = null) {
if (seconds && typeof seconds === "number") return new Promise((resolve) => setTimeout(resolve, seconds * 1000));
let DELAY_BETWEEN_REQUESTS = process.env.DELAY_BETWEEN_REQUESTS && _isArray(process.env.DELAY_BETWEEN_REQUESTS) ? JSON.parse(process.env.DELAY_BETWEEN_REQUESTS) : [1, 5];
if (seconds && Array.isArray(seconds)) {
DELAY_BETWEEN_REQUESTS = seconds;
}
min = DELAY_BETWEEN_REQUESTS[0];
max = DELAY_BETWEEN_REQUESTS[1];
return new Promise((resolve) => {
const delay = Math.floor(Math.random() * (max - min + 1)) + min;
setTimeout(resolve, delay * 1000);
});
}
function saveToken(id, token) {
const tokens = JSON.parse(fs.readFileSync("token.json", "utf8"));
tokens[id] = token;
fs.writeFileSync("token.json", JSON.stringify(tokens, null, 4));
}
function getToken(id) {
const tokens = JSON.parse(fs.readFileSync("token.json", "utf8"));
return tokens[id] || null;
}
function isExpiredToken(token) {
const [header, payload, sign] = token.split(".");
const decodedPayload = Buffer.from(payload, "base64").toString();
try {
const parsedPayload = JSON.parse(decodedPayload);
const now = Math.floor(DateTime.now().toSeconds());
if (parsedPayload.exp) {
const expirationDate = DateTime.fromSeconds(parsedPayload.exp).toLocal();
this.log(colors.cyan(`Token hết hạn vào: ${expirationDate.toFormat("yyyy-MM-dd HH:mm:ss")}`));
const isExpired = now > parsedPayload.exp;
this.log(colors.cyan(`Token đã hết hạn chưa? ${isExpired ? "Đúng rồi bạn cần thay token" : "Chưa..chạy tẹt ga đi"}`));
return isExpired;
} else {
this.log(colors.yellow(`Token vĩnh cửu không đọc được thời gian hết hạn`));
return false;
}
} catch (error) {
this.error(colors.red(`Lỗi rồi: ${error.message}`));
return true;
}
}
function generateRandomHash() {
const characters = "0123456789abcdef";
let hash = "0x"; // Bắt đầu bằng "0x"
for (let i = 0; i < 64; i++) {
// 64 ký tự cho hash
const randomIndex = Math.floor(Math.random() * characters.length);
hash += characters[randomIndex];
}
return hash;
}
function getRandomElement(arr) {
const randomIndex = Math.floor(Math.random() * arr.length);
return arr[randomIndex];
}
function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function loadData(file) {
try {
const datas = fs.readFileSync(file, "utf8").replace(/\r/g, "").split("\n").filter(Boolean);
if (datas?.length <= 0) {
return [];
}
return datas;
} catch (error) {
return [];
}
}
async function saveData(data, filename) {
fs.writeFileSync(filename, data.join("\n"));
}
function log(msg, type = "info") {
const timestamp = new Date().toLocaleTimeString();
switch (type) {
case "success":
console.log(`[${timestamp}] [*] ${msg}`.green);
break;
case "custom":
console.log(`[${timestamp}] [*] ${msg}`.magenta);
break;
case "error":
console.log(`[${timestamp}] [!] ${msg}`.red);
break;
case "warning":
console.log(`[${timestamp}] [*] ${msg}`.yellow);
break;
default:
console.log(`[${timestamp}] [*] ${msg}`.blue);
}
}
function saveItem(id, value, filename) {
const data = JSON.parse(fs.readFileSync(filename, "utf8"));
data[id] = value;
fs.writeFileSync(filename, JSON.stringify(data, null, 4));
}
function getItem(id, filename) {
const data = JSON.parse(fs.readFileSync(filename, "utf8"));
return data[id] || null;
}
function getOrCreateJSON(id, value, filename) {
let item = getItem(id, filename);
if (item) {
return item;
}
item = saveItem(id, value, filename);
return item;
}
module.exports = { _isArray, getRandomNumber, updateEnv, saveToken, getToken, isExpiredToken, generateRandomHash, getRandomElement, loadData, saveData, log, getOrCreateJSON, sleep };