Add find function

This commit is contained in:
LinkLeong 2023-05-15 03:34:32 +01:00
parent 00d3382ab4
commit 7d27f8cbd0
5 changed files with 73 additions and 7 deletions

2
go.mod
View File

@ -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

50
main.go
View File

@ -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")
}

View File

@ -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"`
}

View File

@ -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()
}
}
}

View File

@ -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"]