-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhitelistGUI.py
56 lines (48 loc) · 1.96 KB
/
whitelistGUI.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
import tkinter as tk
import ipaddress
import traceback
def check_ip(event=None):
try:
ip = entry.get().strip()
with open('whitelists.txt', 'r') as file:
whitelist = file.read().splitlines()
check_ret=check_ip_in_whitelist(ip, whitelist)
if check_ret==True:
result_label.config(text=ip+" 在白名单中!",foreground="green")
elif check_ret==False:
result_label.config(text=ip+" 不在白名单中!",foreground="red")
else:
result_label.config(text="输入的地址或地址段错误❌",foreground="orange")
entry.delete(0, tk.END) # 查询完后清空文本框内容
except Exception as e:
traceback.print_exc()
result_label.config(text="无法打开白名单文件!")
result_label.config(text="发生异常:{}".format(str(e)))
def check_ip_in_whitelist(ip, whitelist):
try:
ip_obj = ipaddress.ip_address(ip)
for item in whitelist:
if '/' in item: # IP地址段
if ipaddress.ip_address(ip) in ipaddress.ip_network(item):
return True
elif ':' in item: # IPv6地址或地址段
if ipaddress.ip_address(ip) in ipaddress.ip_network(item, False):
return True
else: # 单个IP地址
if ipaddress.ip_address(ip) == ipaddress.ip_address(item):
return True
except Exception as e:
return -1
return False
def clear_result(event=None):
result_label.config(text="")
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
entry.bind("<Return>", check_ip) # 绑定回车键触发check_ip函数
result_label = tk.Label(root, text="")
result_label.pack()
# 设置快捷键
root.bind('<Control-KeyPress-greater>', check_ip) # 绑定Ctrl+>键触发check_ip函数
root.bind('<BackSpace>', clear_result) # 绑定Backspace键触发clear_result函数
root.mainloop()