diff --git a/README.md b/README.md index 7064a9f..b9d807d 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ -#### 获取key和iv +#### 获取key > 自动获取:[Mirouterui/MiKVIVator](https://github.com/Mirouterui/MiKVIVator) > ps:我在3个路由器上发现了一样的数值,已添加为默认值,如果无法登录再尝试更改吧 @@ -72,7 +72,7 @@ 复制双引号里的内容粘贴到`config.json`对应栏目中,并填上密码(路由器后台密码) -![image](https://github.com/Mirouterui/mirouter-ui/assets/63234268/56edd993-2119-4979-bb2d-6f822f32059b) +![image](https://github.com/Mirouterui/mirouter-ui/assets/63234268/e2f1a583-e271-484d-9597-e4beecdfeff4) > config.json 会在初次运行时自动下载 diff --git a/main.go b/main.go index d8ce8f2..6cfd18a 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,9 @@ import ( "fmt" "io" "main/modules/config" + "main/modules/download" login "main/modules/login" + "main/modules/tp" "net/http" "os" "os/exec" @@ -33,6 +35,7 @@ var ( routerName string routerNames map[int]string hardware string + hardwares map[int]string tiny bool routerunit bool dev []config.Dev @@ -52,80 +55,16 @@ type Config struct { } func init() { - dev, debug, port, tiny = config.Getconfig() + dev, debug, port, tiny, basedirectory = config.Getconfig() tokens = make(map[int]string) routerNames = make(map[int]string) + hardwares = make(map[int]string) } func GetCpuPercent() float64 { percent, _ := cpu.Percent(time.Second, false) return percent[0] / 100 } -// 红米AX6专用 -func getTemperature(c echo.Context) error { - if routerunit == false { - return c.JSON(http.StatusOK, map[string]interface{}{ - "code": 1100, - "msg": "未开启routerunit模式", - }) - } - var cpu_out, w24g_out, w5g_out []byte - var err1, err2, err3 error - var cpu_tp, fanspeed, w24g_tp, w5g_tp string - switch hardware { - case "RA69": - cpu_cmd = exec.Command("cat", "/sys/class/thermal/thermal_zone0/temp") - w24g_cmd = exec.Command("cat", "/sys/class/ieee80211/phy0/device/net/wifi1/thermal/temp") - w5g_cmd = exec.Command("cat", "/sys/class/ieee80211/phy0/device/net/wifi0/thermal/temp") - cpu_out, err1 = cpu_cmd.Output() - w24g_out, err2 = w24g_cmd.Output() - w5g_out, err3 = w5g_cmd.Output() - - cpu_tp = string(cpu_out) - fanspeed = "-233" - w24g_tp = string(w24g_out) - w5g_tp = string(w5g_out) - case "R1D": - type Ubus_data struct { - Fanspeed string `json:"fanspeed"` - Temperature string `json:"temperature"` - } - cpu_cmd = exec.Command("ubus", "call", "rmonitor", "status") - cpu_out, err1 = cpu_cmd.Output() - var data Ubus_data - err := json.Unmarshal(cpu_out, &data) - if err != nil { - return c.JSON(http.StatusOK, map[string]interface{}{ - "code": 1100, - "msg": "JSON解析错误," + err.Error(), - }) - } - cpu_tp = data.Temperature - fanspeed = data.Fanspeed - w24g_tp = "-233" - w5g_tp = "-233" - default: - return c.JSON(http.StatusOK, map[string]interface{}{ - "code": 1101, - "msg": "设备不支持", - }) - } - - if err1 != nil || err2 != nil || err3 != nil { - return c.JSON(http.StatusOK, map[string]interface{}{ - "code": 1100, - "msg": "获取温度失败,报错信息为" + err1.Error() + err2.Error() + err3.Error(), - }) - } - - return c.JSON(http.StatusOK, map[string]interface{}{ - "code": 0, - "cpu_temperature": cpu_tp, - "fanspeed": fanspeed, - "w24g_temperature": w24g_tp, - "w5g_temperature": w5g_tp, - }) -} func getconfig(c echo.Context) error { type DevNoPassword struct { Key string `json:"key"` @@ -149,14 +88,16 @@ func getconfig(c echo.Context) error { "debug": debug, // "token": token, "dev": devsNoPassword, + "ver": Version, }) } func gettoken(dev []config.Dev) { for i, d := range dev { - token, routerName := login.GetToken(d.Password, d.Key, d.IP) + token, routerName, hardware := login.GetToken(d.Password, d.Key, d.IP) tokens[i] = token routerNames[i] = routerName + hardwares[i] = hardware print(tokens[i]) } } @@ -216,7 +157,27 @@ func main() { }) } }) - e.GET("/_api/gettemperature", getTemperature) + e.GET("/:devnum/_api/gettemperature", func(c echo.Context) error { + devnum, err := strconv.Atoi(c.Param("devnum")) + logrus.Debug(tokens) + if err != nil { + return c.JSON(http.StatusOK, map[string]interface{}{"code": 1100, "msg": "参数错误"}) + } + status, cpu_tp, fanspeed, w24g_tp, w5g_tp := tp.GetTemperature(c, devnum, hardwares[devnum]) + if status { + return c.JSON(http.StatusOK, map[string]interface{}{ + "code": 0, + "cpu": cpu_tp, + "fanspeed": fanspeed, + "w24g": w24g_tp, + "w5g": w5g_tp, + }) + } + return c.JSON(http.StatusOK, map[string]interface{}{ + "code": 1103, + "msg": "不支持该设备", + }) + }) e.GET("/_api/getconfig", getconfig) e.GET("/_api/quit", func(c echo.Context) error { go func() { @@ -228,6 +189,20 @@ func main() { "msg": "正在关闭", }) }) + e.GET("/_api/flushstatic", func(c echo.Context) error { + err := download.DownloadStatic(basedirectory, true) + if err != nil { + return c.JSON(http.StatusOK, map[string]interface{}{ + "code": 1101, + "msg": err, + }) + } + logrus.Debugln("执行完成") + return c.JSON(http.StatusOK, map[string]interface{}{ + "code": 0, + "msg": "执行完成", + }) + }) // var contentHandler = echo.WrapHandler(http.FileServer(http.FS(static))) // var contentRewrite = middleware.Rewrite(map[string]string{"/*": "/static/$1"}) diff --git a/modules/config/base.go b/modules/config/base.go index 876cf95..4a99612 100644 --- a/modules/config/base.go +++ b/modules/config/base.go @@ -40,7 +40,7 @@ type Config struct { Tiny bool `json:"tiny"` } -func Getconfig() (dev []Dev, debug bool, port int, tiny bool) { +func Getconfig() (dev []Dev, debug bool, port int, tiny bool, basedirectory string) { flag.StringVar(&configPath, "config", "", "配置文件路径") flag.StringVar(&basedirectory, "basedirectory", "", "基础目录路径") flag.Parse() @@ -81,7 +81,7 @@ func Getconfig() (dev []Dev, debug bool, port int, tiny bool) { // logrus.Info(password) // logrus.Info(key) if tiny == false { - CheckAndDownloadStatic(basedirectory) + DownloadStatic(basedirectory, false) } if debug == true { logrus.SetLevel(logrus.DebugLevel) @@ -95,7 +95,7 @@ func Getconfig() (dev []Dev, debug bool, port int, tiny bool) { time.Sleep(5 * time.Second) os.Exit(1) } - return dev, debug, port, tiny + return dev, debug, port, tiny, basedirectory } func checkErr(err error) { diff --git a/modules/download/base.go b/modules/download/base.go index ed58cd3..9a5a420 100644 --- a/modules/download/base.go +++ b/modules/download/base.go @@ -12,14 +12,18 @@ import ( "github.com/sirupsen/logrus" ) -func CheckAndDownloadStatic(basedirectory string) error { +func DownloadStatic(basedirectory string, force bool) error { directory := "static" if basedirectory != "" { directory = filepath.Join(basedirectory, "static") } + if force { + //删除 + os.RemoveAll(directory) + } _, err := os.Stat(directory) - if os.IsNotExist(err) { + if os.IsNotExist(err) || force { logrus.Info("正从'Mirouterui/static'下载静态资源") resp, err := http.Get("http://mrui-api.hzchu.top/downloadstatic") checkErr(err) diff --git a/modules/login/base.go b/modules/login/base.go index 4e5e206..949ac60 100644 --- a/modules/login/base.go +++ b/modules/login/base.go @@ -57,26 +57,26 @@ func newhashPassword(pwd string, nonce string, key string) string { return noncePwdKeyHashStr } -func getrouterinfo(ip string) (bool, string) { +func getrouterinfo(ip string) (bool, string, string) { // 发送 GET 请求 ourl := fmt.Sprintf("http://%s/cgi-bin/luci/api/xqsystem/init_info", ip) response, err := http.Get(ourl) if err != nil { - return false, routername + return false, "", "" } defer response.Body.Close() // 读取响应内容 body, err := io.ReadAll(response.Body) if err != nil { - return false, routername + return false, "", "" } // 解析 JSON var data map[string]interface{} err = json.Unmarshal(body, &data) if err != nil { - return false, routername + return false, "", "" } //提取routername routername = data["routername"].(string) @@ -87,19 +87,19 @@ func getrouterinfo(ip string) (bool, string) { newEncryptMode, ok := data["newEncryptMode"].(float64) if !ok { logrus.Debug("使用旧加密模式") - return false, routername + return false, routername, hardware } if newEncryptMode != 0 { logrus.Debug("使用新加密模式") logrus.Info("当前路由器可能无法正常获取某些数据!") - return true, routername + return true, routername, hardware } - return false, routername + return false, routername, hardware } -func GetToken(password string, key string, ip string) (string, string) { +func GetToken(password string, key string, ip string) (string, string, string) { logrus.Debug("获取路由器信息...") - newEncryptMode, routername := getrouterinfo(ip) + newEncryptMode, routername, hardware := getrouterinfo(ip) logrus.Info("更新token...") nonce := createNonce() var hashedPassword string @@ -139,5 +139,5 @@ func GetToken(password string, key string, ip string) (string, string) { time.Sleep(5 * time.Second) os.Exit(1) } - return token, routername + return token, routername, hardware } diff --git a/modules/tp/base.go b/modules/tp/base.go new file mode 100644 index 0000000..803afb3 --- /dev/null +++ b/modules/tp/base.go @@ -0,0 +1,66 @@ +package tp + +import ( + "encoding/json" + "main/modules/config" + "os/exec" + + "github.com/labstack/echo/v4" + "github.com/sirupsen/logrus" +) + +var ( + hardware string + dev []config.Dev + cpu_cmd *exec.Cmd + w24g_cmd *exec.Cmd + w5g_cmd *exec.Cmd +) + +// 获取温度 +func GetTemperature(c echo.Context, devnum int, hardware string) (bool, string, string, string, string) { + if dev[devnum].RouterUnit == false { + return false, "-233", "-233", "-233", "-233" + } + var cpu_out, w24g_out, w5g_out []byte + var err1, err2, err3 error + var cpu_tp, fanspeed, w24g_tp, w5g_tp string + switch hardware { + case "RA69": + cpu_cmd = exec.Command("cat", "/sys/class/thermal/thermal_zone0/temp") + w24g_cmd = exec.Command("cat", "/sys/class/ieee80211/phy0/device/net/wifi1/thermal/temp") + w5g_cmd = exec.Command("cat", "/sys/class/ieee80211/phy0/device/net/wifi0/thermal/temp") + cpu_out, err1 = cpu_cmd.Output() + w24g_out, err2 = w24g_cmd.Output() + w5g_out, err3 = w5g_cmd.Output() + + cpu_tp = string(cpu_out) + fanspeed = "-233" + w24g_tp = string(w24g_out) + w5g_tp = string(w5g_out) + case "R1D": + type Ubus_data struct { + Fanspeed string `json:"fanspeed"` + Temperature string `json:"temperature"` + } + cpu_cmd = exec.Command("ubus", "call", "rmonitor", "status") + cpu_out, err1 = cpu_cmd.Output() + var data Ubus_data + err := json.Unmarshal(cpu_out, &data) + if err != nil { + logrus.Error("获取温度失败,报错信息为" + err.Error()) + } + cpu_tp = data.Temperature + fanspeed = data.Fanspeed + w24g_tp = "-233" + w5g_tp = "-233" + default: + return false, "-233", "-233", "-233", "-233" + } + + if err1 != nil || err2 != nil || err3 != nil { + logrus.Error("获取温度失败,报错信息为" + err1.Error() + err2.Error() + err3.Error()) + } + + return true, cpu_tp, fanspeed, w24g_tp, w5g_tp +}