Skip to content

Commit

Permalink
🔖 发布一个版本: 初始版本v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
owwkmidream committed Mar 7, 2024
1 parent c90c478 commit 0931835
Show file tree
Hide file tree
Showing 12 changed files with 782 additions and 2 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: '发布'

on:
workflow_dispatch:
push:
tags:
- '*'

permissions:
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.22
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/config.toml
/Auto-Ruijie-Tool.log
/.idea
248 changes: 248 additions & 0 deletions assets.go

Large diffs are not rendered by default.

Binary file added assets/online.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"github.com/BurntSushi/toml"
log "github.com/sirupsen/logrus"
"os"
"sync"
)

type Config struct {
URL map[string]string
LoginData map[string]string
LogoutData map[string]string
Headers map[string]string
Cookie map[string]string
Options map[string]string
}

var instance *Config
var once sync.Once

func GetInstance() *Config {
once.Do(func() {
instance = &Config{}
err := instance.Load()
if err != nil {
notify.Send("加载配置文件失败", err.Error())
panic(err)
}
})
return instance
}

func (c *Config) Load() error {
_, err := toml.DecodeFile("config.toml", &c)
log.Info("加载配置文件")
// IO错误则创建一个新的配置文件
if err != nil {
if os.IsNotExist(err) {
// 创建一个新的配置文件
log.Info("配置文件不存在,创建一个新的配置文件")
err := CreateConfig()
if err != nil {
log.Error("创建配置文件失败", err)
return err
}

// 重新加载配置文件
_, err = toml.DecodeFile("config.toml", &c)
if err != nil {
log.Error("重新加载配置文件失败", err)
return err
}
} else {
return err
}
}
return nil
}

func CreateConfig() error {
// 创建一个新的配置文件
file, err := os.Create("config.toml")
if err != nil {
return err
}

defer func(file *os.File) {
err := file.Close()
if err != nil {
panic(err)
}
}(file)

// 使用默认的配置初始化文件
defaultConfig := GetDefaultConfig()
encoder := toml.NewEncoder(file)
err = encoder.Encode(defaultConfig)
if err != nil {
return err
}
return nil
}

func GetDefaultConfig() Config {
defaultConfig := Config{
URL: map[string]string{
"host": "http://127.0.0.1",
"login": "/eportal/InterFace.do?method=login",
"logout": "/eportal/InterFace.do?method=logout",
"check": "https://connect.rom.miui.com/generate_204",
},
LoginData: map[string]string{
"userId": "",
"password": "",
"service": "",
"queryString": "",
"operatorPwd": "",
"operatorUserId": "",
"validcode": "",
"passwordEncrypt": "",
},
LogoutData: map[string]string{},
Headers: map[string]string{},
Cookie: map[string]string{},
Options: map[string]string{},
}
return defaultConfig
}

func (c *Config) Save() error {
file, err := os.Create("config.toml")
if err != nil {
return err
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
panic(err)
}
}(file)

encoder := toml.NewEncoder(file)
return encoder.Encode(c)
}
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Auto-Ruijie-Tool

go 1.22

require (
github.com/BurntSushi/toml v1.3.2
github.com/sirupsen/logrus v1.9.3
)

require (
github.com/gen2brain/beeep v0.0.0-20240112042604-c7bb2cd88fea // indirect
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
golang.org/x/sys v0.17.0 // indirect
)
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gen2brain/beeep v0.0.0-20240112042604-c7bb2cd88fea h1:oWUHxzaBvwkRWiINbBOY39XIF+n9b4RJEPHdQ8waJUo=
github.com/gen2brain/beeep v0.0.0-20240112042604-c7bb2cd88fea/go.mod h1:0W7dI87PvXJ1Sjs0QPvWXKcQmNERY77e8l7GFhZB/s4=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4 h1:qZNfIGkIANxGv/OqtnntR4DfOY2+BgwR60cAcu/i3SE=
github.com/go-toast/toast v0.0.0-20190211030409-01e6764cf0a4/go.mod h1:kW3HQ4UdaAyrUCSSDR4xUzBKW6O2iA4uHhk7AtyYp10=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d h1:VhgPp6v9qf9Agr/56bj7Y/xa04UccTW04VP0Qed4vnQ=
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d/go.mod h1:YUTz3bUH2ZwIWBy3CJBeOBEugqcmXREj14T+iG/4k4U=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
26 changes: 26 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"os"

log "github.com/sirupsen/logrus"
)

func InitLog() {
// 创建一个日志文件
file, err := os.OpenFile("Auto-Ruijie-Tool.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}

// 设置 log 的输出到这个文件
log.SetOutput(file)

// 设置 log 的格式为默认的日志格式
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})

// 设置 log 的级别为 InfoLevel
log.SetLevel(log.InfoLevel)
}
150 changes: 150 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package main

import (
"encoding/json"
log "github.com/sirupsen/logrus"
"io"
"net/http"
"net/url"
"strings"
)

var config *Config
var notify *Notify

// Login 函数用于执行登录操作
func Login() bool {
data := url.Values{}
// 遍历配置中的登录数据,将其添加到url.Values对象中
for key, value := range config.LoginData {
data.Set(key, value)
}

client := &http.Client{}
// 创建一个新的HTTP请求,方法是POST,URL是配置中的主机地址加上登录路径,请求体是编码后的登录数据
req, err := http.NewRequest("POST", config.URL["host"]+config.URL["login"], strings.NewReader(data.Encode()))
// 如果在创建请求时发生错误,返回nil和错误
if err != nil {
log.Error("创建请求时发生错误", err)
return false
}

// 遍历配置中的头部数据,将其添加到请求的头部
for key, value := range config.Headers {
req.Header.Add(key, value)
}
// 使用客户端发送请求,获取响应
resp, err := client.Do(req)
if err != nil {
log.Error("发送请求时发生错误", err)
return false
}

// 读取响应体
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Error("读取响应体时发生错误", err)
return false
}
defer resp.Body.Close()

// 解析响应体为一个 map
var result map[string]interface{}
err = json.Unmarshal(bodyBytes, &result)
if err != nil {
log.Error("解析响应体时发生错误", err)
return false
}

// 获取 result 字段
res, _ := result["result"]

return res == "success"
}

//func Logout() (*http.Response, error) {
// client := &http.Client{}
// req, err := http.NewRequest("POST", config.URL["host"]+config.URL["logout"], nil)
// if err != nil {
// return nil, err
// }
//
// for key, value := range config.Headers {
// req.Header.Add(key, value)
// }
// resp, err := client.Do(req)
// if err != nil {
// return nil, err
// }
//
// return resp, nil
//}

func TestNet(url string) bool {
resp, err := http.Get(url)
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
return true
}
if err != nil {
log.Error("检测校园网环境出错", err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Error("关闭Body出错", err)
}
}(resp.Body)

return false
}

func main() {
InitLog()
log.Info("程序启动--------------------------------------")
notify = GetInstanceNotify()
config = GetInstance()

// 检测校园网环境,状态码200-299表示正常
log.Info("检测校园网环境")
res := TestNet(config.URL["host"])
if !res {
log.Info("校园网环境异常,循环检测60秒")
notify.Send("校园网环境异常", "循环检测60秒")
for i := 1; i <= 60; i++ {
res = TestNet(config.URL["host"])
if res {
break
} else {
log.Info("进行第", i, "次检测")
}
}
if !res {
log.Fatal("当前不处于校园网环境,退出程序")
notify.Send("校园网环境异常", "当前不处于校园网环境,退出程序")
return
}
}
log.Info("当前处于校园网环境")

// 检测是否已经登录
if TestNet(config.URL["check"]) {
log.Info("已经登录")
notify.Send("已经登录", "网络已连接")

// TODO: 是否需要退出登录
return
}

// 登录
if res = Login(); res {
log.Info("登录成功")
if TestNet(config.URL["check"]) {
notify.Send("登录成功", "网络已连接")
} else {
notify.Send("登录成功", "网络未连接")
}
} else {
log.Error("登录失败")
notify.Send("登录失败", "请检查日志")
}
}
Loading

0 comments on commit 0931835

Please sign in to comment.