-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschool_auth_v1.0.py
More file actions
375 lines (318 loc) · 15.4 KB
/
Copy pathschool_auth_v1.0.py
File metadata and controls
375 lines (318 loc) · 15.4 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""
安徽理工大学 - 校园网自动认证工具 (AUST-ConnectEase)
依赖: pip install requests
运行: python school_auth_v1.0.py
"""
import tkinter as tk
from tkinter import ttk, messagebox
import threading
import json
import os
import re
try:
import requests
except ImportError:
import subprocess, sys
subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])
import requests
CONFIG_FILE = os.path.join(os.path.expanduser("~"), ".school_auth_config.json")
# ─────────────────────────────────────────────
# 校园网认证逻辑(AUST-ConnectEase)
# ─────────────────────────────────────────────
DRCOM_URL = "http://10.255.0.19/drcom/login"
ISP_MAP = {
"电信 (@aust)": "aust",
"联通 (@unicom)": "unicom",
"移动 (@cmcc)": "cmcc",
"教职工 (@jzg)": "jzg",
}
NET_TYPE = ["宽带(POST)", "WiFi(GET)"]
def parse_drcom_response(text: str) -> dict:
"""
解析 Dr.COM 返回的 JSONP 响应
格式: dr1003({...})
result=1 -> 成功, result=0 -> 失败
"""
# 提取 JSONP 中的 JSON 部分
json_match = re.search(r'\((\{.*\})\)', text, re.DOTALL)
if not json_match:
return {"success": False, "error": f"无法解析响应: {text[:100]}"}
try:
data = json.loads(json_match.group(1))
except json.JSONDecodeError:
return {"success": False, "error": f"JSON解析失败: {text[:100]}"}
result = data.get("result", -1)
if result == 1:
# 登录成功,附带一些信息
ip = data.get("v46ip", "")
note = f"(IP: {ip})" if ip else ""
return {"success": True, "note": note, "data": data}
if result == 0:
# 登录失败,尝试提取错误信息
msg = data.get("msg", "") or data.get("info", "") or data.get("err", "")
if not msg:
# 根据常见错误码判断
ecode = data.get("ecode", "")
error_map = {
"E2553": "密码错误,请检查后重试",
"E2901": "该账号已在线",
"E2905": "账户欠费,请充值",
}
msg = error_map.get(str(ecode), f"认证失败 (code={ecode})")
# 特判"已在线"
if "已在线" in msg or "online" in msg.lower():
return {"success": True, "note": "(该账号已在线)"}
return {"success": False, "error": msg}
return {"success": False, "error": f"未知结果码 result={result}"}
def login_drcom(username: str, password: str, isp_suffix: str, net_type: str) -> dict:
"""校园网 Dr.COM 认证"""
account = f"{username}@{isp_suffix}"
params = {
"callback": "dr1003",
"DDDDD": account,
"upass": password,
"0MKKey": "123456",
}
try:
session = requests.Session()
if net_type == "宽带(POST)":
resp = session.post(DRCOM_URL, data=params, timeout=10)
else:
resp = session.get(DRCOM_URL, params=params, timeout=10)
return parse_drcom_response(resp.text)
except requests.exceptions.ConnectionError:
return {"success": False,
"error": "无法连接认证服务器 (10.255.0.19)\n请确认已接入校园网 WiFi / 有线"}
except requests.exceptions.Timeout:
return {"success": False, "error": "认证服务器超时,请重试"}
except Exception as e:
return {"success": False, "error": str(e)}
# ─────────────────────────────────────────────
# 配置读写
# ─────────────────────────────────────────────
def load_config() -> dict:
try:
if os.path.exists(CONFIG_FILE):
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
pass
return {}
def save_config(data: dict):
try:
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
except Exception as e:
messagebox.showwarning("保存失败", f"配置保存失败:{e}")
# ─────────────────────────────────────────────
# GUI 主窗口
# ─────────────────────────────────────────────
class App(tk.Tk):
ACCENT = "#2563EB"
BG = "#F0F4F8"
CARD = "#FFFFFF"
GRAY = "#9CA3AF"
FONT = ("Microsoft YaHei", 10)
FONT_H = ("Microsoft YaHei", 11, "bold")
FONT_S = ("Microsoft YaHei", 9)
def __init__(self):
super().__init__()
self.title("AUST 校园认证助手")
self.resizable(False, False)
self.configure(bg=self.BG)
self._build_ui()
self._load_saved()
# ── 界面构建 ────────────────────────────────
def _build_ui(self):
self._build_header()
outer = tk.Frame(self, bg=self.BG, padx=20, pady=16)
outer.pack()
self._build_account_card(outer, row=0)
self._build_network_card(outer, row=1)
self._build_jwgl_placeholder(outer, row=2)
self._build_options(outer, row=3)
self._build_button(outer, row=4)
self._build_status(outer, row=5)
def _build_header(self):
h = tk.Frame(self, bg=self.ACCENT, pady=14)
h.pack(fill="x")
tk.Label(h, text="🏫 AUST 校园认证助手",
bg=self.ACCENT, fg="white",
font=("Microsoft YaHei", 14, "bold")).pack()
tk.Label(h, text="安徽理工大学 • 教务系统 & 校园网一键认证",
bg=self.ACCENT, fg="#BFDBFE", font=self.FONT_S).pack(pady=(2, 0))
def _lf(self, parent, title, row):
f = tk.LabelFrame(parent, text=title, bg=self.CARD, font=self.FONT_H,
fg="#1E3A5F", relief="flat", bd=1,
highlightthickness=1, highlightbackground="#D1D5DB",
padx=14, pady=10)
f.grid(row=row, column=0, sticky="ew", pady=(0, 10))
return f
def _build_account_card(self, parent, row):
card = self._lf(parent, "👤 账号信息", row)
for i, (label, attr, is_pwd) in enumerate([
("学号 / 工号", "_username", False),
("密 码", "_password", True),
]):
tk.Label(card, text=label, bg=self.CARD, font=self.FONT,
width=9, anchor="e").grid(row=i, column=0, padx=12, pady=6)
var = tk.StringVar()
setattr(self, attr, var)
tk.Entry(card, textvariable=var, show="*" if is_pwd else "",
font=self.FONT, width=26, relief="solid", bd=1
).grid(row=i, column=1, padx=12, pady=6, sticky="w")
def _build_network_card(self, parent, row):
card = self._lf(parent, "🌐 校园网设置", row)
tk.Label(card, text="运营商", bg=self.CARD, font=self.FONT,
width=9, anchor="e").grid(row=0, column=0, padx=12, pady=6)
self._isp_var = tk.StringVar(value=list(ISP_MAP.keys())[0])
ttk.Combobox(card, textvariable=self._isp_var,
values=list(ISP_MAP.keys()), state="readonly",
width=20, font=self.FONT
).grid(row=0, column=1, padx=12, pady=6, sticky="w")
tk.Label(card, text="网络类型", bg=self.CARD, font=self.FONT,
width=9, anchor="e").grid(row=1, column=0, padx=12, pady=6)
self._net_type_var = tk.StringVar(value=NET_TYPE[0])
ttk.Combobox(card, textvariable=self._net_type_var,
values=NET_TYPE, state="readonly",
width=20, font=self.FONT
).grid(row=1, column=1, padx=12, pady=6, sticky="w")
tk.Label(card, text="账号预览", bg=self.CARD, font=self.FONT,
width=9, anchor="e").grid(row=2, column=0, padx=12, pady=4)
self._preview_var = tk.StringVar()
tk.Label(card, textvariable=self._preview_var, bg=self.CARD,
fg=self.GRAY, font=("Consolas", 9)
).grid(row=2, column=1, padx=12, pady=4, sticky="w")
self._username.trace_add("write", self._update_preview)
self._isp_var.trace_add("write", self._update_preview)
self._update_preview()
def _update_preview(self, *_):
uid = self._username.get().strip() or "学号"
suffix = ISP_MAP.get(self._isp_var.get(), "aust")
self._preview_var.set(f"{uid}@{suffix}")
def _build_jwgl_placeholder(self, parent, row):
"""教务系统登录 - 待完善占位卡片"""
card = self._lf(parent, "📚 教务系统登录", row)
# 虚化的输入框(仅展示,不可交互)
for i, (label, placeholder) in enumerate([
("学号 / 工号", "与校园网账号相同"),
("密 码", "待完善功能"),
]):
tk.Label(card, text=label, bg=self.CARD, font=self.FONT,
width=9, anchor="e").grid(row=i, column=0, padx=12, pady=6)
entry = tk.Entry(card, font=self.FONT, width=26, relief="solid", bd=1,
bg="#F3F4F6", fg=self.GRAY, state="disabled",
disabledforeground=self.GRAY, disabledbackground="#F3F4F6")
entry.grid(row=i, column=1, padx=12, pady=6, sticky="w")
# 插入占位文字
entry.config(state="normal")
entry.insert(0, placeholder)
entry.config(state="disabled")
# 待完善标签
badge = tk.Label(card, text="⚙️ 功能开发中,敬请期待",
bg="#FEF3C7", fg="#92400E",
font=("Microsoft YaHei", 8),
relief="flat", padx=8, pady=3)
badge.grid(row=2, column=0, columnspan=2, padx=12, pady=(4, 2), sticky="w")
def _build_options(self, parent, row):
f = tk.Frame(parent, bg=self.BG)
f.grid(row=row, column=0, sticky="w", pady=2)
self._save_var = tk.BooleanVar(value=True)
self._do_net_var = tk.BooleanVar(value=True)
for text, var in [
("💾 保存配置(下次自动填入)", self._save_var),
("📡 认证校园网", self._do_net_var),
]:
tk.Checkbutton(f, text=text, variable=var,
bg=self.BG, font=self.FONT).pack(anchor="w")
def _build_button(self, parent, row):
self._btn = tk.Button(parent, text="🚀 一键认证",
bg=self.ACCENT, fg="white",
font=("Microsoft YaHei", 11, "bold"),
relief="flat", cursor="hand2",
padx=12, pady=9, command=self._start)
self._btn.grid(row=row, column=0, pady=(8, 6), sticky="ew")
def _build_status(self, parent, row):
card = tk.Frame(parent, bg=self.CARD, relief="flat", bd=1,
highlightthickness=1, highlightbackground="#D1D5DB",
padx=14, pady=10)
card.grid(row=row, column=0, sticky="ew")
tk.Label(card, text="认证状态", bg=self.CARD, fg=self.GRAY,
font=("Microsoft YaHei", 9, "bold")).pack(anchor="w", pady=(0, 4))
self._net_status = self._status_row(card, "校园网")
self._jwgl_status = self._status_row(card, "教务系统")
tk.Frame(card, bg="#E5E7EB", height=1).pack(fill="x", pady=6)
self._log = tk.Text(card, height=5, width=48, state="disabled",
bg="#F9FAFB", relief="flat", font=("Consolas", 9),
fg="#374151", wrap="word")
self._log.pack(fill="x")
def _status_row(self, parent, label):
f = tk.Frame(parent, bg=self.CARD)
f.pack(anchor="w", pady=1)
tk.Label(f, text=f"{label}:", bg=self.CARD, font=self.FONT_S,
width=9, anchor="w").pack(side="left")
var = tk.StringVar(value="⬜ 未认证")
tk.Label(f, textvariable=var, bg=self.CARD, font=self.FONT_S).pack(side="left")
return var
# ── 配置加载 ──
def _load_saved(self):
cfg = load_config()
if cfg.get("username"): self._username.set(cfg["username"])
if cfg.get("password"): self._password.set(cfg["password"])
if cfg.get("isp") and cfg["isp"] in ISP_MAP:
self._isp_var.set(cfg["isp"])
if cfg.get("net_type") and cfg["net_type"] in NET_TYPE:
self._net_type_var.set(cfg["net_type"])
if cfg.get("do_net") is not None:
self._do_net_var.set(cfg["do_net"])
# ── 认证主流程 ──
def _start(self):
username = self._username.get().strip()
password = self._password.get().strip()
isp_key = self._isp_var.get()
net_type = self._net_type_var.get()
if not username or not password:
messagebox.showwarning("提示", "请填写学号和密码")
return
if self._save_var.get():
save_config({
"username": username,
"password": password,
"isp": isp_key,
"net_type": net_type,
"do_net": self._do_net_var.get(),
})
self._log_msg("💾 配置已保存")
self._btn.config(state="disabled", text="认证中…")
self._net_status.set("⏳ 认证中…")
self._jwgl_status.set("⚙️ 待完善")
isp_suffix = ISP_MAP.get(isp_key, "aust")
threading.Thread(
target=self._run_auth,
args=(username, password, isp_suffix, net_type),
daemon=True
).start()
def _run_auth(self, username, password, isp_suffix, net_type):
if self._do_net_var.get():
self._log_msg(f"→ 校园网认证 {username}@{isp_suffix} [{net_type}]")
r = login_drcom(username, password, isp_suffix, net_type)
if r["success"]:
note = r.get("note", "")
self._net_status.set(f"✅ 认证成功 {note}".strip())
self._log_msg(f"✅ 校园网认证成功 {note}".strip())
else:
self._net_status.set("❌ 认证失败")
self._log_msg(f"❌ 校园网:{r['error']}")
else:
self._net_status.set("⬛ 已跳过")
self.after(0, lambda: self._btn.config(state="normal", text="🚀 一键认证"))
def _log_msg(self, msg: str):
def _update():
self._log.config(state="normal")
self._log.insert("end", msg + "\n")
self._log.see("end")
self._log.config(state="disabled")
self.after(0, _update)
if __name__ == "__main__":
app = App()
app.mainloop()