This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.js
121 lines (110 loc) · 2.85 KB
/
api.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
/*
Developer Mr_artan
my telegram https://t.me/mr_saebi
note* : Be sure to install axios,moment,dotenv modules before running
*/
import axios from "axios";
import moment from "moment";
import dotenv from "dotenv";
dotenv.config();
class hiddifyApi {
constructor() {
this.adminSecret = process.env.ADMINSECRET;
this.axios = axios.create({
baseURL: process.env.MAIN_URL + "/" + process.env.PATHS + "/" + process.env.ADMINSECRET,
});
this.axios.defaults.headers.common["Accept"] = "application/json";
this.axios.defaults.headers.post["Content-Type"] = "application/json";
}
// generate uuid
generateuuid() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
// system
// check connect to api
is_connected() {
return this.axios
.get("/admin/get_data/")
.then((res) => {
if (res.data && typeof res.data === "object") return true;
return false;
})
.catch((err) => console.log(err));
}
// get sustem status
getSystemStatus() {
return this.axios
.get("/admin/get_data/")
.then((res) => {
return res.data.stats;
})
.catch((err) => {
return err;
});
}
// get user list
getUserList() {
return this.axios
.get("/api/v1/user/")
.then((res) => {
return res.data;
})
.catch((err) => {
return err;
});
}
// add servise
async addServise({ uuid, comment, name, day, traficc, telegram_id }) {
const data = {
added_by_uuid: this.adminSecret,
comment,
current_usage_GB: 0,
last_online: null,
last_reset_time: null,
mode: "no_reset",
name,
package_days: day,
start_date: moment().format("YYYY-MM-DD"),
telegram_id,
usage_limit_GB: traficc,
uuid,
};
return this.axios
.post("/api/v1/user/", data)
.then((res) => {
if (res.data) return uuid;
return false;
})
.catch((err) => {
return err;
});
}
// find servise
async findServise(uuid) {
// get all data
const data = await this.getUserList();
// search to data
const userData = data.find((e) => e.uuid == uuid);
if (!userData) return false;
this[userData.subData] = this.getDataFromSub(uuid);
return userData;
}
// subset find servise
getDataFromSub(url) {
return axios
.get(url + "/sub/")
.then((res) => {
const lines = res.data.split("\n");
const servers = lines.filter((line) => line.startsWith("vless://") || line.startsWith("trojan://") || line.startsWith("vemss://"));
return servers;
})
.catch((err) => {
return err;
});
}
}
export default hiddifyApi;