Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev #6

Merged
merged 6 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@



#### 获取key和iv
#### 获取key

> 自动获取:[Mirouterui/MiKVIVator](https://github.com/Mirouterui/MiKVIVator)
> ps:我在3个路由器上发现了一样的数值,已添加为默认值,如果无法登录再尝试更改吧
Expand All @@ -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 会在初次运行时自动下载
Expand Down
111 changes: 43 additions & 68 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,6 +35,7 @@ var (
routerName string
routerNames map[int]string
hardware string
hardwares map[int]string
tiny bool
routerunit bool
dev []config.Dev
Expand All @@ -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"`
Expand All @@ -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])
}
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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"})
Expand Down
6 changes: 3 additions & 3 deletions modules/config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions modules/download/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions modules/login/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
66 changes: 66 additions & 0 deletions modules/tp/base.go
Original file line number Diff line number Diff line change
@@ -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
}