Last active
September 1, 2021 12:32
-
-
Save journey-ad/73d900a675b090b9f2c94a1945e6ec19 to your computer and use it in GitHub Desktop.
GiWiFi认证登录脚本
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
import os | |
import re | |
import time | |
import json | |
import argparse | |
import requests | |
import netifaces | |
from getpass import getpass | |
from urllib.parse import urlparse, parse_qs | |
SCRIPT_VERSION = "1.0.3.2" | |
HEADERS = { | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.25 Safari/537.36', | |
'accept-encoding': 'gzip, deflate, br', | |
'accept-language': 'zh-CN,zh-TW;q=0.8,zh;q=0.6,en;q=0.4,ja;q=0.2', | |
'cache-control': 'max-age=0' | |
} | |
PARSER = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, | |
description='GiWiFi认证登录脚本', | |
epilog='(c) 2018 journey.ad') | |
PARSER.add_argument('-g', '--gateway', type=str, help='网关IP') | |
PARSER.add_argument('-u', '--username', type=str, help='用户名') | |
PARSER.add_argument('-p', '--password', type=str, help='密码') | |
PARSER.add_argument('-q', '--quit', action='store_true', help='登出') | |
PARSER.add_argument('-d', '--daemon', action='store_true', help='在后台守护运行') | |
PARSER.add_argument('-v', '--verbose', action='store_true', help='额外输出一些技术性信息') | |
PARSER.add_argument('-V', '--version', action='version', | |
version='giwifi-auth-helper {}'.format(SCRIPT_VERSION)) | |
CONFIG = PARSER.parse_args() | |
if not CONFIG.quit: | |
if not CONFIG.gateway: | |
CONFIG.gateway = netifaces.gateways()['default'][netifaces.AF_INET][0] | |
if not CONFIG.password: | |
CONFIG.gateway = input('请输入网关地址(%s):' % (CONFIG.gateway)) or CONFIG.gateway | |
if not CONFIG.username: | |
CONFIG.username = input('请输入上网账号:') | |
if not CONFIG.password: | |
CONFIG.password = getpass('请输入账号密码:') | |
else: | |
if not CONFIG.gateway: | |
CONFIG.gateway = netifaces.gateways()['default'][netifaces.AF_INET][0] | |
CONFIG.gateway = input('请输入网关地址(%s):' % (CONFIG.gateway)) or CONFIG.gateway | |
def main(): | |
logcat('正在获取网关信息…') | |
try: | |
authUrl = requests.get('http://%s:8062/redirect' % (CONFIG.gateway), timeout=5).url | |
authParmas = {k: v[0] for k, v in parse_qs(urlparse(authUrl).query).items()} | |
loginPage = requests.get('http://login.gwifi.com.cn/cmps/admin.php/api/login/?' + urlparse(authUrl).query, headers=HEADERS, timeout=5).text | |
pagetime = re.search(r'name="page_time" value="(.*?)"', loginPage).group(1) | |
sign = re.search(r'name="sign" value="(.*?)"', loginPage).group(1) | |
except requests.exceptions.ConnectionError: | |
logcat('连接失败,请检查网关地址是否正确') | |
return | |
except requests.exceptions.Timeout: | |
logcat('连接超时,可能已超出上网区间') | |
return | |
except AttributeError: | |
logcat('解析失败,可能网关设备重启或系统已更新') | |
return | |
authState = getAuthState(authParmas, sign) | |
if CONFIG.quit: | |
logout(authParmas) | |
if not authState: | |
return | |
else: | |
if authState['auth_state'] == 2: | |
printStatus(authParmas, authState) | |
logcat('你已登录,无需再次登录') | |
else: | |
data = { | |
'access_type': authState['access_type'], | |
'acsign': authState['sign'], | |
'btype': 'pc', | |
'client_mac': authState['client_mac'], | |
'contact_phone': '400-038-5858', | |
'devicemode': '', | |
'gw_address': authParmas['gw_address'], | |
'gw_id': authParmas['gw_id'], | |
'gw_port': authParmas['gw_port'], | |
'lastaccessurl': '', | |
'logout_reason': authState['logout_reason'], | |
'mac': authParmas['mac'], | |
'name': CONFIG.username, | |
'online_time': authState['online_time'], | |
'page_time': pagetime, | |
'password': CONFIG.password, | |
'sign': sign, | |
'station_cloud': 'login.gwifi.com.cn', | |
'station_sn': authState['station_sn'], | |
'suggest_phone': '400-038-5858', | |
'url': 'http://www.baidu.com', | |
'user_agent': '', | |
} | |
if CONFIG.verbose: | |
logcat(data) | |
result = login(data) | |
if result['status']: | |
authState = getAuthState(authParmas, sign) | |
printStatus(authParmas, authState) | |
if authState['auth_state'] == 2: | |
logcat('认证成功') | |
else: | |
logcat('认证失败') | |
else: | |
logcat('认证失败,提示信息:%s' % (result['info'])) | |
def login(data): | |
logcat('正在尝试认证…') | |
try: | |
resp = json.loads(requests.post('http://login.gwifi.com.cn/cmps/admin.php/api/loginaction', data=data, timeout=5).text) | |
result = { | |
'status': False, | |
'info': None | |
} | |
if CONFIG.verbose: | |
logcat(resp) | |
if 'wifidog/auth' in resp['info']: | |
requests.get(resp['info']) | |
result['status'] = True | |
else: | |
result['info'] = resp['info'] | |
except requests.exceptions.Timeout: | |
logcat('连接超时,可能已超出上网区间') | |
finally: | |
return result | |
def logout(authParmas): | |
try: | |
resp = json.loads(requests.get('http://%s/getApp.htm?action=logout' % (authParmas['gw_address'])).text) | |
except requests.exceptions.Timeout: | |
logcat('连接超时,可能已超出上网区间') | |
return | |
if resp['resultCode'] == 0: | |
logcat('下线成功') | |
else: | |
logcat('下线失败') | |
def getAuthState(authParmas, sign): | |
try: | |
params = { | |
'ip': authParmas['ip'], | |
'mac': authParmas['mac'], | |
'sign': sign, | |
'callback': '' | |
} | |
resp = json.loads(requests.get('http://%s:%s/wifidog/get_auth_state' % (authParmas['gw_address'], authParmas['gw_port']), params=params, timeout=5).text[1:-1]) | |
except KeyError: | |
logcat('所需参数不存在') | |
return False | |
except requests.exceptions.Timeout: | |
logcat('连接超时,可能已超出上网区间') | |
return False | |
if CONFIG.verbose: | |
logcat(resp) | |
if resp['resultCode'] == 0: | |
return json.loads(resp['data']) | |
else: | |
return False | |
def printStatus(authParmas, authState): | |
if not CONFIG.verbose: | |
clear() | |
print( | |
'''-------------------------------------------- | |
SSID: %s | |
AP MAC: %s | |
GateWay: %s | |
IP: %s | |
MAC: %s | |
Station SN: %s | |
Logged: %s | |
--------------------------------------------''' | |
% ( | |
authParmas['gw_id'], | |
authParmas['apmac'], | |
authParmas['gw_address'], | |
authParmas['ip'], | |
authParmas['mac'], | |
authState['station_sn'], | |
'yes' if(authState['auth_state'] == 2) else 'no' | |
) | |
) | |
def clear(): | |
os.system('cls' if os.name == 'nt' else 'clear') | |
def logcat(msg, level='I'): | |
print('%s %s: %s' % (time.ctime().split(' ')[-2], level, msg)) | |
if __name__ == '__main__': | |
if CONFIG.daemon: | |
while True: | |
main() | |
time.sleep(30) | |
else: | |
main() | |
input() |
大佬,看下 吧,我把 150行 return result改为return False后 报错变成
01:11:28 I: 正在尝试认证…
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/untitled/giwifi.py", line 208, in
main()
File "C:/Users/Administrator/PycharmProjects/untitled/giwifi.py", line 105, in main
if result['status']:
TypeError: 'bool' object is not subscriptable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
大佬问下,运行报错这个是为什么,最先3.70以为版本问题,升级到3.7.1也是,我也抓了包看也是和你讲的一样的,看了网站源码不过不知道怎么去掉隐藏样式,指点下可以不,以下报错内容。
`Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
RESTART: C:\Users\Administrator\Desktop\giwifi\giwifi-master\giwifi-master\giwifi1.py
22:53:54 I: 正在获取网关信息…
22:53:56 I: 正在尝试认证…
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\giwifi\giwifi-master\giwifi-master\giwifi1.py", line 118, in login
resp = json.loads(requests.post('http://login.gwifi.com.cn/cmps/admin.php/api/loginaction', data=data, timeout=5).text)
File "G:\python3.7.1\lib\json_init_.py", line 348, in loads
return _default_decoder.decode(s)
File "G:\python3.7.1\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "G:\python3.7.1\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 4 column 1 (char 6)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\giwifi\giwifi-master\giwifi-master\giwifi1.py", line 216, in
main()
File "C:\Users\Administrator\Desktop\giwifi\giwifi-master\giwifi-master\giwifi1.py", line 102, in main
result = login(data)
File "C:\Users\Administrator\Desktop\giwifi\giwifi-master\giwifi-master\giwifi1.py", line 137, in login
return result
UnboundLocalError: local variable 'result' referenced before assignment