Created
September 13, 2018 14:01
-
-
Save beaulian/2030cd697b1544fade42f2be0196aa25 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
import sys | |
import json | |
import time | |
import random | |
import requests | |
import platform | |
import traceback | |
import subprocess | |
from urllib.parse import urljoin | |
#debug | |
DEBUG = False | |
# url | |
base_url = "https://controller.shanghaitech.edu.cn:8445" | |
login_url = urljoin(base_url, "/PortalServer/Webauth/webAuthAction!login.action") | |
sync_url = urljoin(base_url, "/PortalServer/Webauth/webAuthAction!syncPortalAuthResult.action") | |
# header | |
base_header = { | |
"Accept": "*/*", | |
"Content-Type": "application/x-www-form-urlencoded", | |
"User-Agent": "ShanghaiTech_Network_AutoLoginer_V2 Python3" | |
} | |
# timeout | |
timeout = 10 | |
# session | |
global_session = requests.Session() | |
def current_time_str(): | |
return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())) | |
def gen_random_str(n : int = 32): | |
_char = "abcdefghijklmnopqrstuvwxyz0123456789".upper() | |
return "".join((_char[random.randint(0, len(_char) - 1)] for _ in range(n))) | |
def print_error(msg : str = ""): | |
print("!!!error: ", msg) | |
sys.exit(1) | |
def login(username : str, password : str): | |
postdata = { | |
"userName": username, | |
"password": password, | |
"hasValidateCode": "false", | |
"validCode": "", | |
"hasValidateNextUpdatePassword": "true" | |
} | |
header = dict(base_header, **{"JSESSIONID": gen_random_str()}) | |
try: | |
res = global_session.post(login_url, data=postdata, headers=header, timeout=timeout) | |
except requests.exceptions.ConnectTimeout: | |
print_error("连接超时!") | |
except requests.exceptions.ConnectionError: | |
print_error("请检查电脑是否连上校内wifi或者以太网!") | |
res.encoding = "utf-8" | |
return json.loads(res.text) | |
def sync(ip : str, xsrf_token : str): | |
postdata = { "clientIp": ip, "browserFlag": "zh" } | |
header = dict(base_header, **{ | |
"JSESSIONID": gen_random_str(), | |
"X-Requested-With": "XMLHttpRequest", | |
"X-XSRF-TOKEN": xsrf_token, | |
}) | |
try: | |
res = global_session.post(sync_url, data=postdata, headers=header, timeout=timeout) | |
except requests.exceptions.ConnectTimeout: | |
print_error("连接超时!") | |
except requests.exceptions.ConnectionError: | |
print_error("请检查电脑是否连上校内wifi或者以太网!") | |
res.encoding = "utf-8" | |
return json.loads(res.text) | |
def main(): | |
username = input("请输入账号: ") | |
password = input("请输入密码: ") | |
if username is None or password is None: | |
print_error("请输入账号或密码!") | |
test = 0 | |
while test < 5: | |
print("检查网络是否可用...") | |
ping_command = ('ping -W 1 -c 1', 'ping -w 1000 -n 1')['Windows' in platform.system()] | |
domain_name = "www.qq.com" | |
s = subprocess.Popen(" ".join([ping_command, domain_name]), | |
shell=True, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
s.wait() | |
time.sleep(3) | |
if s.returncode == 0: | |
print("你的电脑已经可以访问互联网了,窗口将在3秒后退出.") | |
time.sleep(3) | |
break | |
print("开始登录...") | |
login_res = login(username, password) | |
if DEBUG: | |
print(login_res) | |
status = login_res["data"]["accessStatus"] | |
if status != 200 or login_res["token"] is None: | |
print_error(login_res["message"]) | |
print("登录成功!") | |
ip = login_res["data"]["ip"] | |
sessionId = login_res["data"]["sessionId"] | |
token = login_res["token"][6:] | |
for i in range(1, 16): | |
print("开始第{c}次同步...".format(c=i)) | |
sync_res = sync(ip, token) | |
if DEBUG: | |
print(sync_res) | |
status = sync_res["data"]["portalAuthStatus"] | |
if status == 0: | |
pass | |
elif status == 1: | |
print("认证成功!") | |
break | |
elif status == 2: | |
print_error("认证失败!") | |
else: | |
error_code = sync_res["data"]["portalErrorCode"] | |
if error_code == 5: | |
print_error("超过最大允许登录的设备数量(3个)!") | |
elif error_code == 101: | |
print_error("密码错误!") | |
else: | |
print_error("认证失败!") | |
time.sleep(2) | |
time.sleep(2) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment