From 7d27f8cbd071a9c9d427211f8a58c50f7c46b9ed Mon Sep 17 00:00:00 2001 From: LinkLeong Date: Mon, 15 May 2023 03:34:32 +0100 Subject: [PATCH] Add find function --- go.mod | 2 +- main.go | 50 ++++++++++++++++++++++++++++++++++++++- model/zima.go | 1 + pkg/utils/ip_helper/ip.go | 7 +++--- service/system.go | 20 +++++++++++++++- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/go.mod b/go.mod index 6b2483b..f485592 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( go.uber.org/goleak v1.2.1 go.uber.org/zap v1.24.0 golang.org/x/crypto v0.8.0 + golang.org/x/net v0.9.0 golang.org/x/oauth2 v0.6.0 golang.org/x/sync v0.1.0 gorm.io/gorm v1.24.6 @@ -119,7 +120,6 @@ require ( golang.org/x/arch v0.3.0 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/image v0.6.0 // indirect - golang.org/x/net v0.9.0 // indirect golang.org/x/sys v0.7.0 // indirect golang.org/x/text v0.9.0 // indirect golang.org/x/time v0.3.0 // indirect diff --git a/main.go b/main.go index 2133fe1..ede1b43 100644 --- a/main.go +++ b/main.go @@ -5,22 +5,27 @@ package main import ( "context" _ "embed" + "encoding/base64" "encoding/json" "flag" "fmt" "net" "net/http" "path/filepath" + "strconv" "time" "github.com/IceWhaleTech/CasaOS-Common/model" "github.com/IceWhaleTech/CasaOS-Common/utils/constants" "github.com/IceWhaleTech/CasaOS-Common/utils/logger" + "github.com/tidwall/gjson" + "golang.org/x/net/websocket" util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http" "github.com/IceWhaleTech/CasaOS/codegen/message_bus" "github.com/IceWhaleTech/CasaOS/common" + model2 "github.com/IceWhaleTech/CasaOS/model" "github.com/IceWhaleTech/CasaOS/pkg/cache" "github.com/IceWhaleTech/CasaOS/pkg/config" "github.com/IceWhaleTech/CasaOS/pkg/sqlite" @@ -81,6 +86,7 @@ func init() { service.GetCPUThermalZone() route.InitFunction() + go SendToSocket(service.MyService.System().GetDeviceInfo()) service.MyService.System().GenreateSystemEntry() /// // service.MountLists = make(map[string]*mountlib.MountPoint) @@ -231,7 +237,8 @@ func main() { } } func Special(myservice service.Repository) { - http.HandleFunc("/v1/icewhale", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") m := myservice.System().GetDeviceInfo() jsonData, err := json.Marshal(m) if err != nil { @@ -248,3 +255,44 @@ func Special(myservice service.Repository) { } } + +func SendToSocket(m model2.DeviceInfo) { + if len(m.DeviceSN) == 0 { + //TODO:需要更换socket地址,需要放开sn的判断 + //return + } + by, _ := json.Marshal(m) + base64Str := base64.StdEncoding.EncodeToString(by) + var count int = 1 + for i := 0; i < 10; i++ { + wsURL := fmt.Sprintf("ws://%s/server/zima%s", "52.193.63.104:3060", "?device="+base64Str) + ws, err := websocket.Dial(wsURL, "", "http://localhost") + if err != nil { + logger.Error("connect websocket err"+strconv.Itoa(i), zap.Any("error", err)) + time.Sleep(time.Second * 1) + continue + } + defer ws.Close() + + logger.Info("subscribed to", zap.Any("url", wsURL)) + for { + msg := make([]byte, 1024) + n, err := ws.Read(msg) + if err != nil { + logger.Error("err", zap.Any("err", err.Error())) + break + } + message := msg[:n] + t := gjson.GetBytes(message, "type") + if t.Str == "ping" { + ws.Write([]byte(`{"type":"pong"}`)) + count++ + } + if count > 600 { + return + } + } + } + logger.Error("error when try to connect to message bus") + +} diff --git a/model/zima.go b/model/zima.go index a2454be..af21340 100644 --- a/model/zima.go +++ b/model/zima.go @@ -32,4 +32,5 @@ type DeviceInfo struct { DeviceSN string `json:"device_sn"` Initialized bool `json:"initialized"` OS_Version string `json:"os_version"` + Hash string `json:"hash"` } diff --git a/pkg/utils/ip_helper/ip.go b/pkg/utils/ip_helper/ip.go index 1fdbace..3023a05 100644 --- a/pkg/utils/ip_helper/ip.go +++ b/pkg/utils/ip_helper/ip.go @@ -57,8 +57,8 @@ func GetDeviceAllIP(port string) []string { } return address } -func GetDeviceAllIPv4() []string { - var address []string +func GetDeviceAllIPv4() map[string]string { + address := make(map[string]string) addrs, err := net.Interfaces() if err != nil { return address @@ -74,9 +74,8 @@ func GetDeviceAllIPv4() []string { } for _, addr := range addrs { - // 检查 IPv4 地址 if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil { - address = append(address, ipnet.IP.String()) + address[a.Name] = ipnet.IP.String() } } } diff --git a/service/system.go b/service/system.go index 4848c53..0d98259 100644 --- a/service/system.go +++ b/service/system.go @@ -1,6 +1,7 @@ package service import ( + "encoding/json" "errors" "fmt" "io/ioutil" @@ -86,12 +87,29 @@ func (c *systemService) GetDeviceInfo() model.DeviceInfo { } } } + allIpv4 := ip_helper.GetDeviceAllIPv4() + ip := []string{} + nets := MyService.System().GetNet(true) + for _, n := range nets { + if v, ok := allIpv4[n]; ok { + { + ip = append(ip, v) + } + } + } - m.LanIpv4 = ip_helper.GetDeviceAllIPv4() + m.LanIpv4 = ip h, err := host.Info() /* */ if err == nil { m.DeviceName = h.Hostname } + mb := model.BaseInfo{} + + err = json.Unmarshal(file.ReadFullFile(config.AppInfo.DBPath+"/baseinfo.conf"), &mb) + if err == nil { + m.Hash = mb.Hash + } + osRelease, _ := file.ReadOSRelease() m.DeviceModel = osRelease["MODEL"] m.DeviceSN = osRelease["SN"]