记录下通过OpenWrt实现校园网Portal认证

其实新版的校园网是通过验证mac地址来实现对连接设备的验证,那我们只需要在路由器端完成认证就可以了。
最简单的办法就是上篇文章说的,只不过有个小问题,24小时需要重新认证登录。这个问题解决起来也很简单,只需要定时post一个认证包就可以。最好的实现方法是利用shell脚本实现,奈何我技术有限,对shell有些畏惧,所以这里用golang实现一下(python交叉编译后openwrt用不了,完了我那openwrt装不上pip。。。。。人麻了)

代码实现

/**
 * @Author: Ne-21
 * @Description: 配合crontab进行portal认证
 * @File: main.go
 * @Version: 1.0.0
 * @Date: 2022/3/5
 */

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net"
    "net/http"
    url2 "net/url"
    "os"
    "strings"
)

func init() {
    // 初始化日志
    logFile, err := os.OpenFile(`/tmp/tmp/campus.log`, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        panic(err)
    }
    // 设置存储位置
    log.SetOutput(logFile)
}

func PostData(url string, ip string, mac string) {
    refer := "http://61.240.137.242:8888/hw/HBHUAWEI/login?apmac=11-11-11-11-11-11&userip=" + ip + "&nasip=221.192.23.190&user-mac=" + mac

    urlValues := url2.Values{}
    urlValues.Add("mobile", "你的账号")
    urlValues.Add("mobile_english", "")
    urlValues.Add("password", "你的密码")
    urlValues.Add("password_english", "")
    urlValues.Add("auth_type", "account")
    urlValues.Add("enterprise_id", "51")
    urlValues.Add("enterprise_url", "HBHUAWEI")
    urlValues.Add("site_id", "4662")
    urlValues.Add("client_mac", mac)
    urlValues.Add("nas_ip", "221.192.23.190")
    urlValues.Add("wlanacname", "None")
    urlValues.Add("user_ip", ip)
    urlValues.Add("3rd_ip", "None")
    urlValues.Add("ap_mac", "None")
    urlValues.Add("vlan", "11-11-11-11-11-11")
    urlValues.Add("ssid", "None")
    urlValues.Add("vlan_id", "None")
    urlValues.Add("ip", "None")
    urlValues.Add("ac_ip", "None")
    urlValues.Add("from", "None")
    urlValues.Add("sn", "None")
    urlValues.Add("gw_id", "None")
    urlValues.Add("gw_address", "None")
    urlValues.Add("gw_port", "None")
    urlValues.Add("url", "None")
    urlValues.Add("language_tag", "0")

    reqBody := urlValues.Encode()
    client := &http.Client{}
    req, err := http.NewRequest("POST", url, strings.NewReader(reqBody))
    req.Header.Add("Proxy-Connection", "keep-alive")
    req.Header.Add("Accept", "*/*")
    req.Header.Add("X-Requested-With", "XMLHttpRequest")
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
    req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62")
    req.Header.Add("Origin", "http://61.240.137.242:8888")
    req.Header.Add("Referer", refer)
    req.Header.Add("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
    resp, err := client.Do(req)

    var result map[string]interface{}
    body, err := ioutil.ReadAll(resp.Body)
    if err == nil {
        err = json.Unmarshal(body, &result)
    }
    log.Println(result)
    for k, v := range result {
        fmt.Println(k, ":", v)
    }
    defer resp.Body.Close()
}

func Mac() (mac string) {
    // 获取本机的MAC地址
    interfaces, err := net.Interfaces()
    if err != nil {
        panic("Poor soul, here is what you got: " + err.Error())
    }
    for _, inter := range interfaces {
        if inter.Name == "wan" || inter.Name == "eth0.2" {
            mac := inter.HardwareAddr
            return mac.String()
        } else {
            continue
        }
    }
    return ""
}

func Ip() (ip string) {
    // 获取本机的IP地址
    interfaces, err := net.Interfaces()
    if err != nil {
        panic("Poor soul, here is what you got: " + err.Error())
    }
    for _, inter := range interfaces {
        if inter.Name == "wan" || inter.Name == "eth0.2" {
            addrs, _ := inter.Addrs()
            for _, v := range addrs {
                ipv4 := v.(*net.IPNet).IP.To4()
                if ipv4 != nil {
                    return ipv4.String()
                }
            }
        }
    }
    return ""
}

func main() {
    // http://61.240.137.242:8888/hw/HBHUAWEI/login?apmac=11-11-11-11-11-11&userip=10.255.202.150&nasip=221.192.23.190&user-mac=20:76:93:43:84:57
    mac := Mac()
    fmt.Println("wan MAC = ", mac)
    ip := Ip()
    fmt.Println("wan IP = ", ip)
    PostData("http://61.240.137.242:8888/hw/internal_auth", ip, mac)
}

具体参数还需要抓包分析,再进行编译,编译完大概4兆左右,将二进制文件(main)上传到路由器

改一下main的权限
chmod +x main
添加定时任务
crontab -e
0/30 * * * * /tmp/main

标题:记录下通过OpenWrt实现校园网Portal认证
作者:Ne-21
文章地址:https://blog.gocos.cn/archives/214.html
转载请注明出处,谢谢!
THE END
分享
二维码
打赏
< <上一篇
下一篇>>