ictgw-auth 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ictgw_auth-1.0.0/.gitignore +6 -0
- ictgw_auth-1.0.0/LICENSE +22 -0
- ictgw_auth-1.0.0/PKG-INFO +43 -0
- ictgw_auth-1.0.0/README.md +31 -0
- ictgw_auth-1.0.0/net_auth.py +160 -0
- ictgw_auth-1.0.0/pyproject.toml +30 -0
ictgw_auth-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 jklincn
|
|
4
|
+
Copyright (c) 2026 henry
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: ictgw-auth
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Command-line login for the ICT campus network (gw.ict.ac.cn)
|
|
5
|
+
Project-URL: Homepage, https://github.com/henrychou0312/ictgw_auth
|
|
6
|
+
Project-URL: Repository, https://github.com/henrychou0312/ictgw_auth
|
|
7
|
+
Author-email: henry <henry.chou0312@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Python: >=3.6
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# net_auth
|
|
14
|
+
|
|
15
|
+
中科院计算所校园网(`gw.ict.ac.cn`,深澜 SRun 认证)命令行登录工具。
|
|
16
|
+
|
|
17
|
+
- 纯 HTTP 请求实现,**零第三方依赖**,单文件,Python ≥ 3.6
|
|
18
|
+
- 未认证时 IPv6 可用,可直接通过 PyPI 安装
|
|
19
|
+
|
|
20
|
+
## 安装
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install ictgw-auth
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
也可以不安装,直接拷贝 `net_auth.py` 到目标机器运行:`python3 net_auth.py`。
|
|
27
|
+
|
|
28
|
+
## 使用
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
net_auth
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
- 未登录:提示输入账号和密码(密码不回显、不保存),登录后显示账号信息;
|
|
35
|
+
- 已登录:显示账号、IP、已用流量、已用时长,并询问是否退出登录(默认 `N`,保持登录)。
|
|
36
|
+
|
|
37
|
+
## 发布
|
|
38
|
+
|
|
39
|
+
推送形如 `v1.0.0` 的 tag 后,GitHub Actions 会自动构建并通过 Trusted Publishing 发布到 PyPI,版本号取自 tag。
|
|
40
|
+
|
|
41
|
+
## 致谢
|
|
42
|
+
|
|
43
|
+
本项目基于 [jklincn/ict_auth](https://github.com/jklincn/ict_auth)(MIT)改写:将 Playwright 无头浏览器方案替换为直接调用深澜认证接口。
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# net_auth
|
|
2
|
+
|
|
3
|
+
中科院计算所校园网(`gw.ict.ac.cn`,深澜 SRun 认证)命令行登录工具。
|
|
4
|
+
|
|
5
|
+
- 纯 HTTP 请求实现,**零第三方依赖**,单文件,Python ≥ 3.6
|
|
6
|
+
- 未认证时 IPv6 可用,可直接通过 PyPI 安装
|
|
7
|
+
|
|
8
|
+
## 安装
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pip install ictgw-auth
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
也可以不安装,直接拷贝 `net_auth.py` 到目标机器运行:`python3 net_auth.py`。
|
|
15
|
+
|
|
16
|
+
## 使用
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
net_auth
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
- 未登录:提示输入账号和密码(密码不回显、不保存),登录后显示账号信息;
|
|
23
|
+
- 已登录:显示账号、IP、已用流量、已用时长,并询问是否退出登录(默认 `N`,保持登录)。
|
|
24
|
+
|
|
25
|
+
## 发布
|
|
26
|
+
|
|
27
|
+
推送形如 `v1.0.0` 的 tag 后,GitHub Actions 会自动构建并通过 Trusted Publishing 发布到 PyPI,版本号取自 tag。
|
|
28
|
+
|
|
29
|
+
## 致谢
|
|
30
|
+
|
|
31
|
+
本项目基于 [jklincn/ict_auth](https://github.com/jklincn/ict_auth)(MIT)改写:将 Playwright 无头浏览器方案替换为直接调用深澜认证接口。
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"""Command-line login for the ICT campus network (SRun portal at gw.ict.ac.cn)."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import getpass
|
|
5
|
+
import hashlib
|
|
6
|
+
import hmac
|
|
7
|
+
import json
|
|
8
|
+
import sys
|
|
9
|
+
import time
|
|
10
|
+
import urllib.parse
|
|
11
|
+
import urllib.request
|
|
12
|
+
|
|
13
|
+
PORTAL = "https://gw.ict.ac.cn"
|
|
14
|
+
AC_ID = "1"
|
|
15
|
+
N, TYPE, ENC = "200", "1", "srun_bx1"
|
|
16
|
+
ALPHA = "LVoJPiCN2R8G90yg+hmFHuacZ1OWMnrsSTXkYpUq/3dlbfKwv6xztjI7DeBE45QA"
|
|
17
|
+
STD_ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
18
|
+
MASK = 0xFFFFFFFF
|
|
19
|
+
|
|
20
|
+
# Talk to the portal directly, ignoring any http(s)_proxy settings.
|
|
21
|
+
_opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def api(path, **params):
|
|
25
|
+
"""Call a JSONP endpoint of the portal and return the decoded JSON."""
|
|
26
|
+
params.update(callback="cb", _=int(time.time() * 1000))
|
|
27
|
+
url = PORTAL + path + "?" + urllib.parse.urlencode(params)
|
|
28
|
+
with _opener.open(url, timeout=10) as resp:
|
|
29
|
+
text = resp.read().decode("utf-8")
|
|
30
|
+
return json.loads(text[text.index("(") + 1 : text.rindex(")")])
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# ---------------------------------------------------------------- SRun crypto
|
|
34
|
+
def _to_words(s, with_len):
|
|
35
|
+
v = [0] * ((len(s) + 3) // 4)
|
|
36
|
+
for i, ch in enumerate(s):
|
|
37
|
+
v[i >> 2] = (v[i >> 2] | (ord(ch) << (8 * (i & 3)))) & MASK
|
|
38
|
+
if with_len:
|
|
39
|
+
v.append(len(s))
|
|
40
|
+
return v
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def _from_words(v):
|
|
44
|
+
return "".join(
|
|
45
|
+
chr(w & 0xFF) + chr(w >> 8 & 0xFF) + chr(w >> 16 & 0xFF) + chr(w >> 24 & 0xFF)
|
|
46
|
+
for w in v
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def x_encode(msg, key):
|
|
51
|
+
"""XXTEA variant used by SRun (``encode`` in Portal.js)."""
|
|
52
|
+
if not msg:
|
|
53
|
+
return ""
|
|
54
|
+
v, k = _to_words(msg, True), _to_words(key, False)
|
|
55
|
+
k += [0] * (4 - len(k))
|
|
56
|
+
n = len(v) - 1
|
|
57
|
+
z, d = v[n], 0
|
|
58
|
+
for _ in range(6 + 52 // (n + 1)):
|
|
59
|
+
d = (d + 0x9E3779B9) & MASK
|
|
60
|
+
e = d >> 2 & 3
|
|
61
|
+
for p in range(n + 1):
|
|
62
|
+
y = v[(p + 1) % (n + 1)]
|
|
63
|
+
m = (z >> 5 ^ y << 2) + ((y >> 3 ^ z << 4) ^ (d ^ y)) + (k[p & 3 ^ e] ^ z)
|
|
64
|
+
z = v[p] = (v[p] + m) & MASK
|
|
65
|
+
return _from_words(v)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def encode_info(info, token):
|
|
69
|
+
raw = x_encode(json.dumps(info, separators=(",", ":"), ensure_ascii=False), token)
|
|
70
|
+
b64 = base64.b64encode(raw.encode("latin-1")).decode()
|
|
71
|
+
return "{SRBX1}" + b64.translate(str.maketrans(STD_ALPHA, ALPHA))
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
# ------------------------------------------------------------------- actions
|
|
75
|
+
def login(username, password):
|
|
76
|
+
challenge = api("/cgi-bin/get_challenge", username=username, ip="")
|
|
77
|
+
token, ip = challenge["challenge"], challenge["client_ip"]
|
|
78
|
+
hmd5 = hmac.new(token.encode(), password.encode(), hashlib.md5).hexdigest()
|
|
79
|
+
info = encode_info(
|
|
80
|
+
{"username": username, "password": password, "ip": ip, "acid": AC_ID, "enc_ver": ENC},
|
|
81
|
+
token,
|
|
82
|
+
)
|
|
83
|
+
chksum = token.join(["", username, hmd5, AC_ID, ip, N, TYPE, info])
|
|
84
|
+
res = api(
|
|
85
|
+
"/cgi-bin/srun_portal",
|
|
86
|
+
action="login",
|
|
87
|
+
username=username,
|
|
88
|
+
password="{MD5}" + hmd5,
|
|
89
|
+
os="Linux",
|
|
90
|
+
name="Linux",
|
|
91
|
+
double_stack=0,
|
|
92
|
+
chksum=hashlib.sha1(chksum.encode()).hexdigest(),
|
|
93
|
+
info=info,
|
|
94
|
+
ac_id=AC_ID,
|
|
95
|
+
ip=ip,
|
|
96
|
+
n=N,
|
|
97
|
+
type=TYPE,
|
|
98
|
+
)
|
|
99
|
+
return res.get("error") == "ok", res
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def logout(username, ip):
|
|
103
|
+
res = api("/cgi-bin/srun_portal", action="logout", username=username, ip=ip, ac_id=AC_ID)
|
|
104
|
+
return res.get("error") == "ok", res
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def human_bytes(n):
|
|
108
|
+
for unit in ("B", "KB", "MB", "GB"):
|
|
109
|
+
if n < 1024:
|
|
110
|
+
return "{:.2f} {}".format(n, unit)
|
|
111
|
+
n /= 1024
|
|
112
|
+
return "{:.2f} TB".format(n)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def show(info):
|
|
116
|
+
secs = int(info.get("sum_seconds", 0))
|
|
117
|
+
print(" User : {}".format(info.get("user_name", "")))
|
|
118
|
+
print(" IP : {}".format(info.get("online_ip", "")))
|
|
119
|
+
print(" Used flow : {}".format(human_bytes(int(info.get("sum_bytes", 0)))))
|
|
120
|
+
print(" Used time : {}h {}m".format(secs // 3600, secs % 3600 // 60))
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def error_text(res):
|
|
124
|
+
return res.get("error_msg") or res.get("suc_msg") or res.get("error") or str(res)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def main():
|
|
128
|
+
try:
|
|
129
|
+
status = api("/cgi-bin/rad_user_info")
|
|
130
|
+
if status.get("error") == "ok":
|
|
131
|
+
print("Status: Online")
|
|
132
|
+
show(status)
|
|
133
|
+
if input("Log out? [y/N] ").strip().lower() not in ("y", "yes"):
|
|
134
|
+
return 0
|
|
135
|
+
ok, res = logout(status["user_name"], status["online_ip"])
|
|
136
|
+
print("Logout successful." if ok else "Logout failed: " + error_text(res))
|
|
137
|
+
return 0 if ok else 1
|
|
138
|
+
|
|
139
|
+
print("Status: Offline")
|
|
140
|
+
username = input("Username: ").strip()
|
|
141
|
+
password = getpass.getpass("Password: ")
|
|
142
|
+
ok, res = login(username, password)
|
|
143
|
+
if not ok:
|
|
144
|
+
print("Login failed: " + error_text(res))
|
|
145
|
+
return 1
|
|
146
|
+
print("Login successful.")
|
|
147
|
+
status = api("/cgi-bin/rad_user_info")
|
|
148
|
+
if status.get("error") == "ok":
|
|
149
|
+
show(status)
|
|
150
|
+
return 0
|
|
151
|
+
except KeyboardInterrupt:
|
|
152
|
+
print()
|
|
153
|
+
return 130
|
|
154
|
+
except (OSError, ValueError, KeyError) as e:
|
|
155
|
+
print("Error: cannot talk to {}: {}".format(PORTAL, e), file=sys.stderr)
|
|
156
|
+
return 1
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
if __name__ == "__main__":
|
|
160
|
+
sys.exit(main())
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ictgw-auth"
|
|
7
|
+
dynamic = ["version"]
|
|
8
|
+
description = "Command-line login for the ICT campus network (gw.ict.ac.cn)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.6"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "henry", email = "henry.chou0312@gmail.com" }]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Homepage = "https://github.com/henrychou0312/ictgw_auth"
|
|
17
|
+
Repository = "https://github.com/henrychou0312/ictgw_auth"
|
|
18
|
+
|
|
19
|
+
[project.scripts]
|
|
20
|
+
net_auth = "net_auth:main"
|
|
21
|
+
|
|
22
|
+
[tool.hatch.version]
|
|
23
|
+
source = "vcs"
|
|
24
|
+
fallback-version = "0.0.0"
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.wheel]
|
|
27
|
+
only-include = ["net_auth.py"]
|
|
28
|
+
|
|
29
|
+
[tool.hatch.build.targets.sdist]
|
|
30
|
+
only-include = ["net_auth.py", "README.md", "LICENSE"]
|