-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinyi_white_list.js
119 lines (105 loc) · 3.23 KB
/
pinyi_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
/*
new Env('品易IP白名单');
55 23 * * *
pinyi_white_list.js
by:Shuai
品易自动加白名单
*/
const fs = require('fs');
const request = require('request');
// 配置业务编号 (neek) 和 API 密钥 (appkey)
let neek = process.env.PINYI_NEEK || '请输入你的业务编号';
let appkey = process.env.PINYI_APPKEY || '请输入你的API密钥';
const ipFileName = 'pinyiIp.txt';
const checkIpUrls = [
'http://ip-api.com/json',
'https://checkip.synology.com/',
'http://httpbin.org/ip'
];
// 读取保存的IP
const readSavedIp = () => fs.existsSync(ipFileName) ? fs.readFileSync(ipFileName, 'utf8').trim() : null;
// 保存IP
const saveIp = (ip) => fs.writeFileSync(ipFileName, ip);
// 获取当前IP
const getCurrentIp = async () => {
for (let url of checkIpUrls) {
try {
const currentIp = await new Promise((resolve, reject) => {
request.get({ url, timeout: 5000 }, (err, res, body) => {
if (err) {
reject(err);
} else {
const match = body.match(/((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)/);
resolve(match ? match[0] : null);
}
});
});
if (currentIp) {
const emojis = ['😊', '🚀', '🎉', '👍', '💡'];
const randomEmoji = emojis[Math.floor(Math.random() * emojis.length)];
console.log(`${randomEmoji} 当前IP: ${currentIp}`);
return currentIp;
}
} catch (err) {
console.log(`❗ 获取IP失败: ${err.code}`);
}
}
return null;
};
// 添加IP到白名单
const addIpToWhiteList = async (ip) => {
const url = `https://pycn.yapi.py.cn/index/index/save_white?neek=${neek}&appkey=${appkey}&white=${ip}`;
request.get(url, (err, res, body) => {
if (err) {
console.log(`❗ 添加IP失败: ${err}`);
} else if (body.includes('success')) {
console.log(`🎉 IP已成功添加到白名单: ${ip}`);
} else {
console.log(`❗ 添加IP失败: ${body}`);
}
});
};
// 删除白名单中的IP
const delWhiteIp = async (ip) => {
const url = `https://pycn.yapi.py.cn/index/index/del_white?neek=${neek}&appkey=${appkey}&white=${ip}`;
request.get(url, (err, res, body) => {
if (err) {
console.log(`❗ 删除IP失败: ${err}`);
} else {
console.log(`🗑️ 已删除白名单中的IP: ${ip}`);
}
});
};
// 获取白名单IP列表
const getWhiteIpList = async () => {
const url = `https://pycn.yapi.py.cn/index/index/white_list?neek=${neek}&appkey=${appkey}`;
return await new Promise((resolve, reject) => {
request.get(url, (err, res, body) => {
if (err) {
reject(err);
} else {
resolve(body);
}
});
});
};
// 主流程
const main = async () => {
const currentIp = await getCurrentIp();
if (!currentIp) {
console.log('❗ 无法获取当前IP,终止执行!');
return;
}
const savedIp = readSavedIp();
const whiteIpList = await getWhiteIpList();
if (savedIp && savedIp !== currentIp && whiteIpList.includes(savedIp)) {
await delWhiteIp(savedIp);
}
if (!whiteIpList.includes(currentIp)) {
await addIpToWhiteList(currentIp);
saveIp(currentIp);
} else {
console.log('😎 当前IP已在白名单中,无需添加。');
}
};
main();