banner
miaoer

miaoer

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

武漢大学の寮のキャンパスネットワーク OpenWrt 上のインターネット認証

引言#

広く知られているように、WHU のキャンパスネットワークは 3 台のデバイスに制限されており、夜の 7 時以降は直接速度制限が 20Mbps になります。

したがって、もし寮にネットポートがあり、そのポートが正常にインターネットに接続できる場合、OpenWrt デバイスを搭載したルーターを接続することで、体験が倍増します。それは、あなたの寮が 1 つのアカウントを共有してネット料金を分担できるだけでなく……、古い校友の米家デバイスに接続でき、夜の 7 時以降に他の人が速度制限されている時に、自分だけがネットの世界を自由に楽しむことができるのです。まさに爽快です!

現在、使用可能なネットポートがある寮は基本的に新しく建てられたもので、例えば信部 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 の寮で快適にキャンパスネットワークを使用できるようになります。もちろん、できれば 1 つのアカウントを使用するのがベストです。

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 です。非武大キャンパスネットワーク環境のユーザーにのみこの項目を宣言することをお勧めします。武大キャンパスネットワークの認証方法が変更された場合は、このリポジトリをフォークして修正し、プルリクエストを提出する必要があります。

-f フォアグラウンドモードを有効にし、systemd の検出を無効にします。

-s ユーザー名、パスワード、ネットワークの合法性チェックをスキップします。

-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

参考#

Auto WHU for standard linux distributions i.e. Arch Linux, Ubuntu, etc. With systemd in mind, this version is much more concise than the openwrt version.

この記事は 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

読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。