banner
miaoer

miaoer

喵二の小博客 https://www.miaoer.net xLog 分站
tg_channel
telegram
bilibili

武漢大學宿舍校園網 OpenWrt 上網認證

引言#

眾所周知,WHU 的校園網限制三台設備,並且晚上七點之後會直接限速到 20Mbps。

所以如果寢室有網口,並且此網口能夠正常上網的話,那麼外接一個搭載 OpenWrt 設備的路由器,簡直可以讓體驗翻倍。它不僅能夠讓你的寢室共用一個帳號來分攤網費…… 以及連接老校友的米家設備,還能在晚上七點之後別人限速時,自己暢遊網絡世界,簡直爽歪歪!

目前已知網口能用的宿舍,基本都是新建的,比如信部 18 舍,其餘宿舍需要自行探索。先用電腦測試一下網口能不能使用,如果可以用的話,再弄一台設備進行安裝即可。

安裝準備#

想採取此方案的話,你需要做好以下準備:

  1. 一台搭載 OpenWrt 系統的路由器 / 軟路由
  2. 認證腳本
  3. SSH 軟件(最好帶有 sftp)

本文採用喵二醬編譯的 CatWrt 為示例,在系統方面基本上沒要求。

腳本需要依賴 bash 以及 curl 如果你是原版 OpenWrt 的話需需要自行檢查組件是否已經安裝,另外建議預裝了 curl 就別再裝了以免出現問題。

1

網絡認證的腳本在 Github 上是開源的,項目鏈接:https://github.com/7Ji/auto-whu-standard

這裡我們直接使用其中的 auto-whu.sh 進行一些小小的修改即可使用。


腳本如下:

#!/bin/bash
# Online check
check_online() {
    ping -w1 -W1 -c 1 baidu.com 1>/dev/null 2>&1 
    [[ $? = 0 ]] && echo "Network is already up" && return 0
    return 1
}
# Check online and immediately exit if is running by systemd
check_online && [[ $? = 0 ]] && [[ ! -z "$INVOCATION_ID" ]] && exit
echo "Warning: running auto-whu when already online is dangerous, you may get your account banned for too many login requests. Use systemd and the bundled service and timer file to manage auto-whu instead. Check the repo for more info: https://github.com/7Ji/auto-whu-standard"
# Help message
help () {
    echo "Usage: $0 -u [username] -p [password] -n [network] -m [manual network] -u [url] -c [config file] -f -s -h"
    echo "      -u username, should be a number of 13 digits"
    echo "      -p password, any value not empty"
    echo "      -n network, single-digit number from 0 to 3, 0 for CERNET, 1 for China Telcom, 2 for China Unicom, 3 for China Mobile"
    echo "      -m a manually specified network name, replace the -n option"
    echo "      -c config file, path to the configuration file"
    echo "      -a eportal authorization URL, DO NOT SET IT unless you totally understand it"
    echo "      -f foreground mode, ignore the systemd check"
    echo "      -s skip check for sanity for username, password and network"
    echo "      -h print this message"
    echo "      *notice that all other arguments will overwrite the value provided by the config file"
}
# Check arguments
[[ $# = 0 ]] && help && exit
while [[ $# -ge 1 ]]; do
    if [[ "$1" = '-u' ]]; then
        ARG_USERNAME="$2"
        shift
    elif [[ "$1" = '-p' ]]; then
        ARG_PASSWORD="$2"
        shift
    elif [[ "$1" = '-n' ]]; then 
        ARG_NETWORK="$2"
        shift
    elif [[ "$1" = '-m' ]]; then
        ARG_NETWORK_MANUAL="$2"
        shift
    elif [[ "$1" = '-a' ]]; then
        ARG_URL="$2"
        shift
    elif [[ "$1" = '-c' ]]; then
        ARG_CONFIG="$2"
        shift
    elif [[ "$1" = '-f' ]]; then
        ARG_IGNORE_SYSTEMD='1'
    elif [[ "$1" = '-s' ]]; then
        ARG_IGNORE_SANITY='1'
    elif [[ "$1" = '-h' ]]; then
        help && exit
    fi
    shift
done    
# Check and read configuration file if neccessary
if [[ ! -z "$ARG_CONFIG" ]]; then
    [[ ! -f "$ARG_CONFIG" ]] && echo "ERROR: The configuration file '$ARG_CONFIG' you've provided does not exist."
    [[ ! -r "$ARG_CONFIG" ]] && echo "ERROR: Not allowed to read the configuration file '$ARG_CONFIG', check your permission"
    source "$ARG_CONFIG"
fi
[[ ! -z "$ARG_USERNAME" ]] && USERNAME=$ARG_USERNAME
[[ ! -z "$ARG_PASSWORD" ]] && PASSWORD=$ARG_PASSWORD
[[ ! -z "$ARG_NETWORK" ]] && NETWORK=$ARG_NETWORK
[[ ! -z "$ARG_NETWORK_MANUAL" ]] && NETWORK_MANUAL=$ARG_NETWORK_MANUAL
[[ ! -z "$ARG_URL" ]] && URL=$ARG_URL
[[ ! -z "$ARG_IGNORE_SYSTEMD" ]] && IGNORE_SYSTEMD='1'
[[ ! -z "$ARG_IGNORE_SANITY" ]] && IGNORE_SANITY='1'
# Default value downgrading
[[ -z "$NETWORK" && -z "$NETWORK_MANUAL" ]] && NETWORK='0' && echo "Neither network number nor manual network name was set, defaulting network to 0(CERNET)"
[[ -z "$URL" ]] && URL='http://172.19.1.9:8080/eportal/InterFace.do?method=login' && echo "Using default eportial authorization URL 'http://172.19.1.9:8080/eportal/InterFace.do?method=login'"
# Check systemd
if [[ -z "$INVOCATION_ID" && "$IGNORE_SYSTEMD" != 1 ]]; then
    echo "You are running this script manually or in a non-systemd environment, it's better to manage this script with systemd."
    echo "Check the github repo to learn how to use this script properly: https://github.com/7Ji/auto-whu-standard"
    echo "You can set IGNORE_SYSTEMD='1' in the config file or use the argument -f to ignore this check"
fi
# Check intergrity or sanity. return code 1 for insanity.
if [[ "$IGNORE_SANITY" != 1 ]]; then
    echo "Starting sanity check for username, password and network, you can set IGNORE_SANITY='1' in config file, or use argument -n to ignore this check."
    [[ ! "$USERNAME" =~ ^[0-9]{13}$ ]] && echo "ERROR:The username '$USERNAME' you provided is not a number of 13 digits" && exit 1
    [[ -z "$PASSWORD" ]] && echo "ERROR:You've specified an empty password" && exit 1
    [[ ! "$NETWORK" =~ ^[0-3]$ && -z "$NETWORK_MANUAL" ]] && echo "ERROR:You've specified a network number not supported, only 0-3 is supported, 0 for CERNET(default), 1 for China Telcom, 2 for China Unicom, 3 for China Mobile" && exit 1
    echo "Sanity check pass."
fi
# Network number conversion
if [[ -z "$NETWORK_MANUAL" ]]; then
    if [[ "$NETWORK" = 0 ]]; then
        NETWORK_STRING=Internet
    elif [[ "$NETWORK" = 1 ]]; then
        NETWORK_STRING=dianxin
    elif [[ "$NETWORK" = 2 ]]; then
        NETWORK_STRING=liantong
    else   
        NETWORK_STRING=yidong
    fi
else
    NETWORK_STRING=$NETWORK_MANUAL
fi
# Authorization
echo "Trying to authorize..."
curl -d "userId=$USERNAME&password=$PASSWORD&service=$NETWORK_STRING&queryString=`curl baidu.com | grep -oP "(?<=\?).*(?=\')" | sed 's/&/%2526/g' | sed 's/=/%253D/g'`&operatorPwd=&operatorUserId=&validcode=&passwordEncrypt=false" $URL 1>/dev/null 2>&1 
check_online && [[ $? = 0 ]] && exit
echo "Failed to authorize, you may need to check your account info and credit and network connection"

食用方法#

你可以新建一個腳本,自行命名即可,我這裡統一使用auto-whu.sh名字,然後把腳本內容複製進去。

通過 sftp 的工具把此腳本上傳到 Catwrt 的 /usr/sbin/目錄下,並且使用

chmod 777 /usr/sbin/auto-whu.sh

將腳本賦予可執行權限。

這裡我們可以使用 Termius 等帶有 sftp 文件傳輸的 SSH 客戶端,當然你喜歡的話也可以 vi vim nano 另外複製在編輯器的終端。

2

測試命令#

直接通過在命令行運行

/usr/sbin/auto-whu.sh -u your_student_account -p your_password -n 0 -f

來進行認證了。

運行命令之後,如果設備成功上線,會輸出:

image-20241221140333731

root@CatWrt:~# /usr/sbin/auto-whu.sh -u your_student_account -p your_password -n 0 -f
Warning: running auto-whu when already online is dangerous, you may get your account banned for too many login requests. Use systemd and the bundled service and timer file to manage auto-whu instead. Check the repo for more info: https://github.com/7Ji/auto-whu-standard
Using default eportial authorization URL 'http://172.19.1.9:8080/eportal/InterFace.do?method=login'
Starting sanity check for username, password and network, you can set IGNORE_SANITY='1' in config file, or use argument -n to ignore this check.
Sanity check pass.
Trying to authorize...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   521  100   521    0     0   130k      0 --:--:-- --:--:-- --:--:--  254k
Failed to authorize, you may need to check your account info and credit and network connection

如果設備已經在線,重複認證會輸出:

image-20241221140257011

root@CatWrt:~# /usr/sbin/auto-whu.sh -u your_student_account -p your_password -n 0 -f
Network is already up
Warning: running auto-whu when already online is dangerous, you may get your account banned for too many login requests. Use systemd and the bundled service and timer file to manage auto-whu instead. Check the repo for more info: https://github.com/7Ji/auto-whu-standard
Using default eportial authorization URL 'http://172.19.1.9:8080/eportal/InterFace.do?method=login'
Starting sanity check for username, password and network, you can set IGNORE_SANITY='1' in config file, or use argument -n to ignore this check.
Sanity check pass.
Trying to authorize...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (52) Empty reply from server
Network is already up

此方法在設備重啟之後,會自動退出認證並且佔用一個校園網帳號的設備數,開關無感認證對此情況無影響。因此每次重啟之後都需要把之前的設備踢掉,再進入設備後台執行命令。

添加自啟#

在測試完成確認無誤後,添加一個開機自啟任務,這樣的話每次重啟只需要把之前的設備踢掉,路由器會自己進行認證。操作方法如下:

打開 系統 - 啟動項

image-20241221135800402

拉到最下面,在 exit0 前面添加一行 /usr/sbin/auto-whu.sh -u your_student_account -p your_password -n 0 -f

image-20241221135905771

這樣的話,腳本命令就會隨著設備啟動而執行了。

總結#

經過上述操作之後,你就可以在 WHU 的宿舍愉快使用校園網了。當然,最好還是用一個帳號哦。

image

腳本參數 - 附錄#

-u [username] 聲明登錄用戶名,應為 13 位數字

-p [password] 聲明密碼,不應為空字段

-n [network] 聲明登錄網絡類型,0-3 的整數,0 為教育網(默認),1 為電信,2 為聯通,3 為移動

-m [network_manual] 手動聲明網絡名稱,會覆蓋 -n 參數,例如教育網在此處為 -m Internet,除非後期網絡情況有變,或你計劃把 auto-whu 使用在非武大校園網的環境中,否則不應該使用此參數

-c [config file] 配置文件路徑,將會從中讀取用戶名、密碼、網絡類型、手動網絡名稱、驗證 URL、是否檢測 systemd、各變量合法性等,這些選項將會被命令行提供的參數覆蓋(例如,-u 會覆蓋配置文件中的USERNAME項)

-a [authorization URL] eportal 的驗證 URL,只推薦非武大校園網環境的用戶聲明此項。如果你自行抓包發現武大校園網的驗證方法有變動,你應當 fork 本 repo 後修改並提出 pull request。

-f 開啟前台模式,將會禁用 systemd 檢測

-s 跳過參數合法性檢查,包括禁用 13 位數字用戶名檢查,非空密碼檢查,0-3 整數網絡編號檢查

-h 打印幫助文本


例如,一位用戶名為 2024300000000 的用戶,他的密碼是 123456

/usr/sbin/auto-whu.sh -u 2024300000000 -p 123456 -n 0 -f

希望登錄 電信 網絡,他應該使用下面這條命令 (-f 可以省略):

/usr/sbin/auto-whu.sh -u 2017300000000 -p 123456 -m dianxin -f

參考#

此文由 Mix Space 同步更新至 xLog 原始鏈接為 https://www.miaoer.net/posts/network/whu-openwrt-authentication

Footnotes#

  1. https://www.miaoer.net/posts/network/catwrt

  2. https://www.miaoer.net/posts/blog/ssh-connection-to-openwrt

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。