-
Notifications
You must be signed in to change notification settings - Fork 2
/
tunet.py
executable file
·205 lines (180 loc) · 5.45 KB
/
tunet.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
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import collections
import hashlib
import hmac
import json
import requests
import sys
import time
import urllib
import b64mod
import xxtea
session = requests.Session()
BASE_URL = 'https://auth.tsinghua.edu.cn'
def get_challenge(username, ip='', double_stack=1, off_campus=True,
tm=time.time()):
params = collections.OrderedDict([
('callback', '_'),
('username', username if off_campus else (username + '@tsinghua')),
('ip', ip),
('double_stack', double_stack),
('_', int(tm * 1000))
])
r = session.get(BASE_URL + '/cgi-bin/get_challenge', params=params).text
r = json.loads(r[2:-1])
try:
assert r['error'] == 'ok'
except AssertionError:
print(r)
raise
return r
def login(username, password, challenge, ip='', double_stack=1, ac_id=1,
off_campus=True, tm=time.time()):
challenge = challenge.encode()
info = collections.OrderedDict([
('username', username if off_campus else (username + '@tsinghua')),
('password', password),
('ip', ip),
('acid', str(ac_id)),
("enc_ver", "srun_bx1")
])
info_hash = b64mod.encode(xxtea.encode(
json.dumps(info, separators=(',', ':')).encode(), challenge))
checksum = hashlib.sha1(
challenge +
username.encode() + challenge +
hmac.new(challenge).hexdigest().encode() + challenge +
str(ac_id).encode() + challenge +
ip.encode() + challenge +
b'200' + challenge + b'1' + challenge +
b'{SRBX1}' + info_hash
).hexdigest()
params = collections.OrderedDict([
('callback', '_'),
('action', 'login'),
('username', username if off_campus else (username + '@tsinghua')),
('org_password', password),
('password', '{MD5}' + hmac.new(challenge).hexdigest()),
('ac_id', 1),
('ip', ip),
('double_stack', double_stack),
('info', b'{SRBX1}' + info_hash),
('chksum', checksum),
('n', 200),
('type', 1),
('_', int(tm * 1000))
])
r = session.get(BASE_URL + '/cgi-bin/srun_portal',
params=params).text
r = json.loads(r[2:-1])
try:
assert r['error'] == 'ok'
except AssertionError:
print(r)
raise
return r
def logout(username, challenge, ip='', double_stack=1, ac_id=1,
off_campus=True, tm=time.time()):
challenge = challenge.encode()
info = collections.OrderedDict([
('username', username if off_campus else (username + '@tsinghua')),
# ('password', password),
('ip', ip),
('acid', str(ac_id)),
("enc_ver", "srun_bx1")
])
info_hash = b64mod.encode(xxtea.encode(
json.dumps(info, separators=(',', ':')).encode(), challenge))
checksum = hashlib.sha1(
challenge +
username.encode() + challenge +
# hmac.new(challenge).hexdigest().encode() + challenge +
str(ac_id).encode() + challenge +
ip.encode() + challenge +
b'200' + challenge + b'1' + challenge +
b'{SRBX1}' + info_hash
).hexdigest()
params = collections.OrderedDict([
('callback', '_'),
('action', 'logout'),
('username', username if off_campus else (username + '@tsinghua')),
# ('org_password', password),
# ('password', '{MD5}' + hmac.new(challenge).hexdigest()),
('ac_id', 1),
('ip', ip),
('double_stack', double_stack),
('info', b'{SRBX1}' + info_hash),
('chksum', checksum),
('n', 200),
('type', 1),
('_', int(tm * 1000))
])
r = session.get(BASE_URL + '/cgi-bin/srun_portal',
params=params).text
r = json.loads(r[2:-1])
try:
assert r['error'] == 'ok'
except AssertionError:
print(r)
raise
return r
def status():
r = session.head(BASE_URL + '/srun_portal_pc.php')
if r.next is None:
return None
else:
return urllib.parse.parse_qsl(urllib.parse.urlparse(r.next.url).query)
def usage():
print('SRUN Client [Protocol: TUNet 2018 web]\n'
'Author: wangqr <[email protected]>'
'\n'
'usage:\n'
' tunet login <username> <password>\n'
' tunet logout [<username>]\n'
' tunet status\n'
'\n'
'options:\n'
' -4, -6 Force IPv4/IPv6')
exit()
def main():
global BASE_URL
if '-4' in sys.argv:
BASE_URL = 'https://auth4.tsinghua.edu.cn'
sys.argv.remove('-4')
elif '-6' in sys.argv:
BASE_URL = 'https://auth6.tsinghua.edu.cn'
sys.argv.remove('-6')
if len(sys.argv) < 2:
usage()
elif sys.argv[1] == 'login' and len(sys.argv) >= 4:
usr = sys.argv[2]
pwd = sys.argv[3]
r = get_challenge(usr)
r = login(usr, pwd, r['challenge'])
print(r)
elif sys.argv[1] == 'logout':
r = status()
if r is None:
print('Not online')
return
print(r)
for x, y in r:
if x == 'username':
usr = y
break
r = get_challenge(usr)
r = logout(usr, r['challenge'])
print(r)
pass
elif sys.argv[1] == 'status':
r = status()
if r is None:
print('Not online')
else:
print(r)
pass
else:
usage()
if __name__ == '__main__':
main()