Skip to content

Commit 0b81dab

Browse files
committed
Support nodes
1 parent d12dd88 commit 0b81dab

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

app.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def();
3333

3434
const nodes = await api.getNodes();
3535

36-
const ws = new Ws({ url, accessToken: api.accessToken, DB: DBType });
36+
const ws = new Ws({ url, accessToken: api.accessToken, DB: DBType, api });
3737

3838
ws.logs();
3939

@@ -42,9 +42,10 @@ def();
4242

4343
const ws = new Ws({
4444
url,
45-
accessToken: api.accessToken,
45+
accessToken: `${api.accessToken}d`,
4646
DB: DBType,
4747
node,
48+
api,
4849
});
4950

5051
ws.logs();
@@ -78,7 +79,7 @@ if (process.env.NODE_ENV.includes("production")) {
7879
}
7980

8081
// Api server
81-
if (Boolean(process.env?.API_ENABLE) === true) {
82+
if (process.env?.API_ENABLE === "true") {
8283
const PORT = process.env?.API_PORT;
8384

8485
let address = new Server().CleanAddress(

config.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,25 @@ class Ws {
1212
*/
1313
constructor(params) {
1414
let patch = params.node ? `node/${params.node}` : "core";
15+
this.access_token = params.accessToken;
16+
this.params = params;
1517

16-
const url = `${Boolean(process.env.SSL) === true ? "wss" : "ws"}://${
18+
const url = `${process.env.SSL === "true" ? "wss" : "ws"}://${
1719
params.url
1820
}/api/${patch}/logs?interval=${process.env.FETCH_INTERVAL_LOGS_WS}&token=${
19-
params.accessToken
21+
this.access_token
2022
}`;
2123
const db = new DBAdapter(params.DB);
2224
const ws = new WebSocket(url);
25+
26+
ws.on("unexpected-response", async (error, response) => {
27+
const token = await params.api.token();
28+
29+
const _ws = new Ws({ ...params, accessToken: token });
30+
31+
_ws.logs();
32+
});
33+
2334
const user = new User();
2435
const ipGuard = new IPGuard(new DBSqlite3());
2536

@@ -92,6 +103,19 @@ class Api {
92103
"Content-Type": "application/x-www-form-urlencoded",
93104
},
94105
});
106+
107+
this.axios.interceptors.response.use(
108+
(value) => value,
109+
async (error) => {
110+
if (
111+
error?.response?.data?.detail === "Could not validate credentials"
112+
) {
113+
await this.token();
114+
}
115+
116+
return error;
117+
},
118+
);
95119
}
96120

97121
/**
@@ -109,8 +133,11 @@ class Api {
109133
});
110134

111135
this.accessToken = data.access_token;
136+
this.axios.defaults.headers.common.Authorization = `Bearer ${data.access_token}`;
112137
this.accessTokenType = data.token_type;
113138
this.accessTokenExpireAt = new Date() + 1000 * 60 * 60;
139+
140+
return data.access_token;
114141
} catch (e) {
115142
console.error(e);
116143
}
@@ -122,6 +149,8 @@ class Api {
122149
try {
123150
const { data } = await this.axios.get("/nodes");
124151

152+
if (!data) return nodes;
153+
125154
nodes = data
126155
.filter((item) => item.status === "connected")
127156
.map((item) => item.id);

telegram/tg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { join } = require("path");
88
const { File } = require("../utils");
99

1010
function tg() {
11-
if (Boolean(process.env.TG_ENABLE) === false) return;
11+
if (process.env.TG_ENABLE === "false") return;
1212

1313
let bot = null;
1414

types.js

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @property {string} accessToken
2626
* @property {object} DB
2727
* @property {string} node
28+
* @property {Object} api
2829
*/
2930

3031
/**

utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function banIP(ip, email) {
1414

1515
childProcess.on("close", (code) => {
1616
if (code === 0) {
17-
if (Boolean(process.env.TG_ENABLE) === true)
17+
if (process.env.TG_ENABLE === "true")
1818
globalThis.bot.api.sendMessage(
1919
process.env.TG_ADMIN,
2020
`${email}: IP ${ip} banned successfully.
@@ -195,7 +195,7 @@ class IPGuard {
195195
usersCsv = usersCsv.split("\r\n").map((item) => item.split(","));
196196
}
197197

198-
if (usersCsv.some((item) => item[0] === data.email) === false)
198+
if (usersCsv && usersCsv.some((item) => item[0] === data.email) === false)
199199
usersCsv = null;
200200

201201
let userCsv = null;

0 commit comments

Comments
 (0)