-
Notifications
You must be signed in to change notification settings - Fork 1
/
xiequ_white_list.py
150 lines (132 loc) · 4.78 KB
/
xiequ_white_list.py
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
"""
new Env('携趣IP白名单');
5 0-23 * * * xiequ_white_list.py
export XIEQU_UID='xxxxxx'
export XIEQU_UKEY='xxxxxxxxxxxxxxxxx'
by:xmo
"""
import requests
import time
import re
import sys
import os
print("")
# uid&ukey从配置读取
uid = os.getenv("XIEQU_UID")
ukey = os.getenv("XIEQU_UKEY")
# uid = ""
if not uid:
print(">>>无uid退出,请先export XIEQU_UID=你自己的uid")
sys.exit(0)
# ukey = ""
if not ukey:
print(">>>无ukey退出,请先export XIEQU_UKEY=你自己的ukey")
sys.exit(0)
# 加载通知
def load_send():
global send
cur_path = os.path.abspath(os.path.dirname(__file__))
sys.path.append(cur_path)
if os.path.exists(cur_path + "/sendNotify.py"):
try:
from sendNotify import send
except:
send=False
print(">>>加载通知服务失败~")
else:
send=False
print(">>>加载通知服务失败~")
load_send()
msgn = 0
print(">>>开始执行...")
msg = ">>>开始执行..."
# 置空IP地址
ip = ""
# 尝试使用pubyun.com网站获取公网IP
try:
url = "https://www.pubyun.com/dyndns/getip/"
response = requests.get(url)
ip = response.text.strip()
print(f">>>使用pubyun.com获取公网IP成功:{ip}")
msg = msg + "\n" + f">>>使用pubyun.com获取公网IP成功:{ip}"
except requests.RequestException as e:
print(">>>使用pubyun.com获取公网IP失败,尝试其他方式...")
msg = msg + "\n" + ">>>使用pubyun.com获取公网IP失败,尝试其他方式..."
#ip = ""
# 尝试使用synology.com网站获取公网IP
if not ip:
url = "https://checkip.synology.com/"
def get_external_ip():
site = requests.get(url)
grab = re.findall('([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)', site.text)
address = grab[0]
return address
ip = get_external_ip()
if not ip:
print(">>>使用synology.com获取公网IP失败,尝试其他方式...")
msg = msg + "\n" + ">>>使用synology.com获取公网IP失败,尝试其他方式..."
else:
print(f">>>使用synology.com获取公网IP成功:{ip}")
msg = msg + "\n" + f">>>使用synology.com获取公网IP成功:{ip}"
#ip = ""
# 尝试使用httpbin.org网站获取公网IP
if not ip:
try:
url = "http://httpbin.org/ip"
response = requests.get(url)
data = response.json()
ip = data['origin']
print(f">>>使用httpbin.org获取公网IP成功:{ip}")
msg = msg + "\n" + f">>>使用httpbin.org获取公网IP成功:{ip}"
except requests.RequestException as e:
print(">>>使用httpbin.org获取公网IP失败,请检查网络连接或其他问题。")
msg = msg + "\n" + ">>>使用httpbin.org获取公网IP失败,请检查网络连接或其他问题。"
#ip = ""
# 判断是否获取到公网IP
if not ip:
print(">>>未获取到公网IP,过程终止...")
msg = msg + "\n" + ">>>未获取到公网IP,过程终止..."
else:
# 判断当前ip是否在白名单内
# 查询IP白名单
get_url = f"http://op.xiequ.cn/IpWhiteList.aspx?uid={uid}&ukey={ukey}&act=get"
response = requests.get(get_url)
print(f">>>执行前查询白名单IP:{response.text}")
msg = msg + "\n" + f">>>执行前查询白名单IP:{response.text}"
if ip in response.text:
print(">>>当前ip在白名单内,过程终止...")
msgn = 1
msg = msg + "\n" + ">>>当前ip在白名单内,过程终止..."
else:
ipn = response.text.split(',')
if len(ipn) == 5:
# 删除所有已有的白名单 IP
time.sleep(1)
delete_url = f"http://op.xiequ.cn/IpWhiteList.aspx?uid={uid}&ukey={ukey}&act=del&ip=all"
response = requests.get(delete_url)
print(f">>>清空白名单IP:{response.text}")
msg = msg + "\n" + f">>>清空白名单IP:{response.text}"
# 添加IP到白名单
time.sleep(1)
add_url = f"http://op.xiequ.cn/IpWhiteList.aspx?uid={uid}&ukey={ukey}&act=add&ip={ip}"
response = requests.get(add_url)
print(f">>>添加白名单IP:{response.text}")
msg = msg + "\n" + f">>>添加白名单IP:{response.text}"
time.sleep(10)
if msgn == 0:
# 查询IP白名单
get_url = f"http://op.xiequ.cn/IpWhiteList.aspx?uid={uid}&ukey={ukey}&act=get"
response = requests.get(get_url)
print(f">>>执行后查询白名单IP:{response.text}")
msg = msg + "\n" + f">>>执行后查询白名单IP:{response.text}"
print(">>>执行结束...")
print("")
print("")
if msgn == 0:
print("开始发送通知...")
print("通知标题:携趣IP白名单")
print("")
msg = msg + "\n" + ">>>执行结束..." + "\n\n" + "本通知 by xmo"
send("携趣IP白名单", msg)
else:
print("白名单未变更,取消发送通知...")