-
Notifications
You must be signed in to change notification settings - Fork 10
/
ql.js
204 lines (189 loc) · 4.7 KB
/
ql.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
'use strict';
const got = require('got');
require('dotenv').config();
const { readFile } = require('fs/promises');
const path = require('path');
const qlDir = '/ql';
const fs = require('fs');
let Fileexists = fs.existsSync('/ql/data/config/auth.json');
let authFile="";
if (Fileexists)
authFile="/ql/data/config/auth.json"
else
authFile="/ql/config/auth.json"
//const authFile = path.join(qlDir, 'config/auth.json');
const api = got.extend({
prefixUrl: 'http://127.0.0.1:5600',
retry: { limit: 0 },
});
async function getToken() {
const authConfig = JSON.parse(await readFile(authFile));
return authConfig.token;
}
module.exports.getEnvs = async () => {
const token = await getToken();
const body = await api({
url: 'api/envs',
searchParams: {
searchValue: 'JD_COOKIE',
t: Date.now(),
},
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
},
}).json();
return body.data;
};
module.exports.getEnvsCount = async () => {
const data = await this.getEnvs();
return data.length;
};
module.exports.addEnv = async (cookie, remarks) => {
const token = await getToken();
const body = await api({
method: 'post',
url: 'api/envs',
params: { t: Date.now() },
json: [{
name: 'JD_COOKIE',
value: cookie,
remarks,
}],
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json();
return body;
};
module.exports.updateEnv = async (cookie, eid, remarks) => {
const token = await getToken();
const body = await api({
method: 'put',
url: 'api/envs',
params: { t: Date.now() },
json: {
name: 'JD_COOKIE',
value: cookie,
_id: eid,
remarks,
},
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json();
return body;
};
module.exports.updateEnv11 = async (cookie, eid, remarks) => {
const token = await getToken();
const body = await api({
method: 'put',
url: 'api/envs',
params: { t: Date.now() },
json: {
name: 'JD_COOKIE',
value: cookie,
id: eid,
remarks,
},
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json();
return body;
};
module.exports.DisableCk = async (eid) => {
const token = await getToken();
const body = await api({
method: 'put',
url: 'api/envs/disable',
params: { t: Date.now() },
body: JSON.stringify([eid]),
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json();
return body;
};
module.exports.EnableCk = async (eid) => {
const token = await getToken();
const body = await api({
method: 'put',
url: 'api/envs/enable',
params: { t: Date.now() },
body: JSON.stringify([eid]),
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json();
return body;
};
module.exports.getstatus = async(eid) => {
const envs = await this.getEnvs();
var tempid = 0;
for (let i = 0; i < envs.length; i++) {
tempid = 0;
if (envs[i]._id) {
tempid = envs[i]._id;
}
if (envs[i].id) {
tempid = envs[i].id;
}
if (tempid == eid) {
return envs[i].status;
}
}
return 99;
};
module.exports.getEnvById = async(eid) => {
const envs = await this.getEnvs();
var tempid = 0;
for (let i = 0; i < envs.length; i++) {
tempid = 0;
if (envs[i]._id) {
tempid = envs[i]._id;
}
if (envs[i].id) {
tempid = envs[i].id;
}
if (tempid == eid) {
return envs[i].value;
}
}
return "";
};
module.exports.getEnvByPtPin = async (Ptpin) => {
const envs = await this.getEnvs();
for (let i = 0; i < envs.length; i++) {
var tempptpin = decodeURIComponent(envs[i].value.match(/pt_pin=([^; ]+)(?=;?)/) && envs[i].value.match(/pt_pin=([^; ]+)(?=;?)/)[1]);
if(tempptpin==Ptpin){
return envs[i];
}
}
return "";
};
module.exports.delEnv = async (eid) => {
const token = await getToken();
const body = await api({
method: 'delete',
url: 'api/envs',
params: { t: Date.now() },
body: JSON.stringify([eid]),
headers: {
Accept: 'application/json',
authorization: `Bearer ${token}`,
'Content-Type': 'application/json;charset=UTF-8',
},
}).json();
return body;
};