自动登录深信服上网控制-golang版本

因为电脑挂了,造成之前所写的python版本的认证小程序也没有环境执行了,所以改用golang写了一版,胜在不依赖于任何环境,我也懒得装python了,就这样挺好,虽然编译出来有点大(7M多),但是可以拷贝到哪都可以直接执行

package main

import (
    "crypto/tls"
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "net/url"
    "strings"
    "time"

    "github.com/axgle/mahonia"
)

var MacMap = map[string]string{
    // wzp_important虚拟机
    "10.20.38.16": "00:50:56:af:0e:a1",
    // PC11276
    "10.20.38.172": "2c:27:d7:26:fe:75",
    // DELL XPS15
    "10.20.38.25": "14:fe:b5:a3:d5:39",
    // LVS_test虚拟机
    "10.20.38.59": "00:50:56:af:5f:a0",
}

func Contains(s, substr string) bool {
    return strings.Index(s, substr) != -1
}

func mac() string {
    // 获取本机的MAC地址
    var result string
    interfaces, err := net.Interfaces()
    if err != nil {
        panic("Error : " + err.Error())
    }
    for _, inter := range interfaces {
        mac := inter.HardwareAddr //获取本机MAC地址
        result = strings.TrimSpace(mac.String())
        if len(result) > 0 {
            break
        }

    }
    //fmt.Println("MAC: ", result)
    return result
}

func checkMac(mac string) bool {
    result := false
    if len(mac) <= 0 {
        return false
    }
    mac = strings.TrimSpace(mac)
    for _, v := range MacMap {
        if strings.EqualFold(v, mac) {
            result = true
        }
    }
    return result
}

func NetAuth() {
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    // 定义配置信息
    resultURL := "https://深信服IP/cgi-bin/showflux.cgi"
    username := "用户名"
    password := "密码"
    authURL := "http://深信服IP/webAuth/"

    // 验证是否认证
    reqest, err := http.NewRequest("GET", resultURL, nil)
    if err != nil {
        fmt.Println("请求是否验证信息构造失败!")
        panic(err)
    }

    reqest.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
    reqest.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
    reqest.Header.Add("Accept-Encoding", "gzip, deflate")
    reqest.Header.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3")
    reqest.Header.Add("Connection", "keep-alive")
    reqest.Header.Add("Host", authURL)
    reqest.Header.Add("Referer", authURL)
    reqest.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0")
    client := &http.Client{Transport: tr}
    response, err := client.Do(reqest)
    if err != nil {
        fmt.Println("请求是否验证信息构造失败!", err)
        panic(err)
    }
    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        fmt.Println("读取深信服返回报文(是否验证)错误!")
        panic(err)
    }
    stringBody := string(body)
    // 字符集转化
    bodyStr := mahonia.NewDecoder("gbk").ConvertString(stringBody)
    // 关闭request,防止EOF错误
    reqest.Close = true

    //fmt.Println(bodyStr)
    if Contains(bodyStr, "未登录") {
        // 尚未认证,开始认证
        v := url.Values{}
        v.Set("username", username)
        v.Add("password", password)
        v.Add("rememberPwd", "1")
        v.Add("pwd", password)
        v.Add("secret", "true")

        reqest, err = http.NewRequest("POST", authURL, strings.NewReader(v.Encode()))

        if err != nil {
            fmt.Println("请求验证信息构造失败!")
            panic(err)
        }

        response, err = client.Do(reqest)

        if err != nil {
            fmt.Println("请求深信服认证错误!")
            panic(err)
        }

        body, err = ioutil.ReadAll(response.Body)
        fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "未认证,认证成功!")
        if err != nil {
            fmt.Println("读取深信服返回报文错误!")
            panic(err)
        }

        //fmt.Println(string(body))
    } else {
        fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "已认证,继续监听!")
    }
    // 释放资源
    defer response.Body.Close()
}

//http://10.20.247.250/webAuth/
func main() {
    if !checkMac(mac()) {
        fmt.Println("合法性检查失败,请勿非法拷贝!")
        //fmt.Println("本机MAC地址:" + mac())
        return
    }
    fmt.Println(time.Now().Format("2006-01-02 15:04:05"), "合法性检查结果:", checkMac(mac()))

    for {
        go NetAuth()
        time.Sleep(60 * time.Second)
    }

}

标签: golang