-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinzan_white_list.js
354 lines (339 loc) · 12.5 KB
/
pinzan_white_list.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
new Env('品赞IP白名单');
35 0-23 * * * pinzan_white_list.js
by:xmo
品赞白名单自动替换
WxPusher一对一:设置WP_APP_TOKEN_ONE和WP_APP_MAIN_UID自动启动
*/
// 定义no(业务编号)、userId(用户id)、password(登录密码)、getkey(套餐提取密匙)、signkey(签名密匙)
let no = '';
let userId = '';
let password = '';
let getkey = '';
let signkey = '';
if (process.env.PINZAN_NO) {
no = process.env.PINZAN_NO;
}
if (process.env.PINZAN_USERID) {
userId = process.env.PINZAN_USERID;
}
if (process.env.PINZAN_PASSWORD) {
password = process.env.PINZAN_PASSWORD;
}
if (process.env.PINZAN_GETKEY) {
getkey = process.env.PINZAN_GETKEY;
}
if (process.env.PINZAN_SIGNKEY) {
signkey = process.env.PINZAN_SIGNKEY;
}
if (no == '') {
console.log('请先定义export PINZAN_NO="业务编号"');
process.exit(0);
}
if (userId == '') {
console.log('请先定义export PINZAN_USERID="用户id"');
process.exit(0);
}
if (password == '') {
console.log('请先定义export PINZAN_PASSWORD="登录密码"');
process.exit(0);
}
if (getkey == '') {
console.log('请先定义export PINZAN_GETKEY="套餐提取密匙"');
process.exit(0);
}
if (signkey == '') {
console.log('请先定义export PINZAN_SIGNKEY="签名密匙"');
process.exit(0);
}
// 一对一通知
let WP_APP_TOKEN_ONE = '';
let WP_APP_MAIN_UID = '';
if (process.env.WP_APP_TOKEN_ONE) {
WP_APP_TOKEN_ONE = process.env.WP_APP_TOKEN_ONE;
}
if (process.env.WP_APP_MAIN_UID) {
WP_APP_MAIN_UID = process.env.WP_APP_MAIN_UID;
}
const fs = require('fs');
const request = require('request');
const notify = require('./sendNotify');
const CryptoJS = require('crypto-js')
const ipFileName = 'pinzanIp.txt';
function readSavedIp() {
try {
const data = fs.readFileSync(ipFileName, 'utf8');
return data.trim();
} catch (error) {
return null;
}
}
function saveIp(ip) {
fs.writeFileSync(ipFileName, ip);
}
// 获取当前IP
async function getCurrentIp(checkipurl) {
const getIpUrl = checkipurl;
try {
let currentIP = await new Promise((resolve, reject) => {
request.get(getIpUrl, (getIpError, getIpResponse, currentIP) => {
if (getIpError) {
reject(getIpError);
} else {
resolve(currentIP);
}
});
});
emojis = ['😊', '😎', '🚀', '🎉', '👍', '💡'];
randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
var reg = /((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}/g;
const arrcurrentIP = currentIP.match(reg);
if (arrcurrentIP) {
currentIP = arrcurrentIP[0];
console.log(randomEmoji + ' 当前IP:', currentIP);
await delay(2000);
return currentIP;
} else {
console.log('💡 未获取到公网IPV4地址,返回空信息。详情:', currentIP);
return null;
}
} catch (error) {
console.error('💡 获取当前IP发生错误:', error);
return null;
}
}
// 添加IP到白名单
async function addIpToWhiteList(currentIP) {
// `https://service.ipzan.com/whiteList-add?no=${no}&sign=${sign}&ip=${currentIP}`
const data = `${password}:${getkey}:${Date.now() / 1000}`
const key = CryptoJS.enc.Utf8.parse(signkey);
const encryptedData = CryptoJS.AES.encrypt(data, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7,
});
const sign = encryptedData.ciphertext.toString();
const addIpUrl = `https://service.ipzan.com/whiteList-add?no=${no}&sign=${sign}&ip=${currentIP}`;
try {
const addIpResponse = await new Promise((resolve, reject) => {
request.get(addIpUrl, (addIpError, addIpResponse, addIpBody) => {
if (addIpError) {
reject(addIpError);
} else {
resolve({ response: addIpResponse, body: addIpBody });
}
});
});
emojis = ['😊', '😎', '🚀', '🎉', '👍', '💡'];
randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
successCondition = addIpResponse.body.includes('添加成功');
message = successCondition ? `🎉 IP地址已更新:${currentIP}` : `💡 IP地址添加失败: ${addIpResponse.body}`;
title = successCondition ? "品赞白名单更换成功 ✅" : "品赞白名单更换失败 ❌";
console.log(randomEmoji + ' 添加IP到白名单的响应:', addIpResponse.body);
await delay(2000);
return { success: successCondition, title, message };
} catch (error) {
console.error('💡 添加IP到白名单发生错误:', error);
message = `'💡 IP地址添加失败:',${error}`;
return { success: false, title: "品赞白名单更换失败 ❌", message };
}
}
// 获取白名单IP
async function getwhiteip() {
//https://service.ipzan.com/whiteList-get?no=${no}&userId=${userId}
const getIpUrl = `https://service.ipzan.com/whiteList-get?no=${no}&userId=${userId}`;
const getIpResponse = await new Promise((resolve, reject) => {
request.get(getIpUrl, (getIpError, getIpResponse, getIpBody) => {
if (getIpError) {
reject(getIpError);
} else {
resolve({ response: getIpResponse, body: getIpBody });
}
});
});
console.log('💡 获取当前白名单的响应:', getIpResponse.body);
await delay(2000);
return getIpResponse.body;
}
// 删除白名单IP
async function delwhiteip(oldip) {
//https://service.ipzan.com/whiteList-del?no=${no}&userId=${userId}&ip=${oldip}
const delIpUrl = `https://service.ipzan.com/whiteList-del?no=${no}&userId=${userId}&ip=${oldip}`;
const delIpResponse = await new Promise((resolve, reject) => {
request.get(delIpUrl, (delIpError, delIpResponse, delIpBody) => {
if (delIpError) {
reject(delIpError);
} else {
resolve({ response: delIpResponse, body: delIpBody });
}
});
});
console.log('💡 白名单中删除上次IP:', oldip, ',', delIpResponse.body);
await delay(2000);
return delIpResponse.body;
}
// 发送通知
async function sendNotification(messageInfo) {
console.log('')
const { title, message } = messageInfo;
notify.sendNotify(title, message);
}
async function main() {
console.log('')
let currentIP = null;
if (!currentIP) {
console.log('💡 使用ident.me获取当前IP……');
currentIP = await getCurrentIp('http://ident.me/');
if (!currentIP) {
console.log('💡 使用ident.me返回当前IP为空!');
}
}
if (!currentIP) {
console.log('💡 使用ip-api.com获取当前IP……');
currentIP = await getCurrentIp('http://ip-api.com/json');
if (!currentIP) {
console.log('💡 使用ip-api.com返回当前IP为空!');
}
}
if (!currentIP) {
console.log('💡 使用synology.com获取当前IP……');
currentIP = await getCurrentIp('https://checkip.synology.com/');
if (!currentIP) {
console.log('💡 使用synology.com返回当前IP为空!');
}
}
if (!currentIP) {
console.log('💡 使用httpbin.org获取当前IP……');
currentIP = await getCurrentIp('http://httpbin.org/ip');
if (!currentIP) {
console.log('💡 使用httpbin.org返回当前IP为空!');
}
}
const oldip = await readSavedIp();
if (currentIP) {
const whiteip = await getwhiteip();
if (oldip) {
if (oldip.includes(currentIP) == false) {
if (whiteip.includes(oldip) == true) {
await delwhiteip(oldip);
}
}
}
if (whiteip.includes(currentIP) == true) {
console.log('😎 当前IP在白名单中,终止添加');
} else {
console.log('💡 当前IP不在白名单响应中,尝试添加');
resultMessage = await addIpToWhiteList(currentIP);
await sendNotification(resultMessage);
const wxpusherResponse = await wxpusherNotify(
resultMessage.title,
resultMessage.message
);
}
if (oldip){
if (oldip.includes(currentIP) == false) {
saveIp(currentIP);
} else {
// console.log('存储IP与当前IP一致');
}
} else {
saveIp(currentIP);
}
} else {
resultMessage = { success: false, title: "品赞获取公网IP失败 ❌", message: "💡 获取公网IP返回空信息,终止执行!" };
await sendNotification(resultMessage);
const wxpusherResponse = await wxpusherNotify(
resultMessage.title,
resultMessage.message
);
}
}
main();
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function wxpusherNotify(text, desp, strsummary = "") {
return new Promise((resolve) => {
if (WP_APP_TOKEN_ONE && WP_APP_MAIN_UID) {
var WPURL = "https://xip.ipzan.com/";
if (strsummary && strsummary.length > 96) {
strsummary = strsummary.substring(0, 95) + "...";
}
let uids = [];
for (let i of WP_APP_MAIN_UID.split(";")) {
if (i.length != 0)
uids.push(i);
};
let topicIds = [];
//desp = `<font size="3">${desp}</font>`;
desp = desp.replace(/[\n\r]/g, '<br>'); // 默认为html, 不支持plaintext
desp = `<section style="width: 24rem; max-width: 100%;border:none;border-style:none;margin:2.5rem auto;" id="shifu_imi_57"
donone="shifuMouseDownPayStyle('shifu_imi_57')">
<section
style="margin: 0px auto;text-align: left;border: 2px solid #212122;padding: 10px 0px;box-sizing:border-box; width: 100%; display:inline-block;"
class="ipaiban-bc">
<section style="margin-top: 1rem; float: left; margin-left: 1rem; margin-left: 1rem; font-size: 1.3rem; font-weight: bold;">
<p style="margin: 0; color: black">
${text}
</p>
</section>
<section style="display: block;width: 0;height: 0;clear: both;"></section>
<section
style="margin-top:20px; display: inline-block; border-bottom: 1px solid #212122; padding: 4px 20px; box-sizing:border-box;"
class="ipaiban-bbc">
<section
style="width:25px; height:25px; border-radius:50%; background-color:#212122;display:inline-block;line-height: 25px"
class="ipaiban-bg">
<p style="text-align:center;font-weight:1000;margin:0">
<span style="color: #ffffff;font-size:20px;">📢</span>
</p>
</section>
<section style="display:inline-block;padding-left:10px;vertical-align: top;box-sizing:border-box;">
</section>
</section>
<section style="margin-top:0rem;padding: 0.8rem;box-sizing:border-box;">
<p style=" line-height: 1.6rem; font-size: 1.1rem; ">
${desp}
</p>
</section>
</section>
</section>`;
const body = {
appToken: `${WP_APP_TOKEN_ONE}`,
content: `${desp}`,
summary: `${text} ${strsummary}`,
contentType: 2,
topicIds: topicIds,
uids: uids,
url: `${WPURL}`,
};
const options = {
url: `http://wxpusher.zjiecode.com/api/send/message`,
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
timeout: 15000,
};
request.post(options, (err, resp, data) => {
try {
if (err) {
console.log("WxPusher 发送通知调用 API 失败!!\n");
console.log(err);
} else {
data = JSON.parse(data);
if (data.code === 1000) {
console.log("WxPusher 发送通知消息成功!\n");
}
}
} catch (e) {
$.logErr(e, resp);
}
finally {
resolve(data);
}
});
} else {
resolve();
}
});
}