Commit 29dfbe5b authored by Lei Li's avatar Lei Li
Browse files

feat: 增加基础配置

parent 50b5aa95
...@@ -20,4 +20,6 @@ var ( ...@@ -20,4 +20,6 @@ var (
MonitorLogUnModDuration = 600 * time.Second MonitorLogUnModDuration = 600 * time.Second
BinDir = WorkDir + "/bin" BinDir = WorkDir + "/bin"
SelfOperatedAndroid = "/data/smallp"
) )
...@@ -7,10 +7,10 @@ import ( ...@@ -7,10 +7,10 @@ import (
"agent/module" "agent/module"
"agent/module/plugin" "agent/module/plugin"
"agent/module/resources" "agent/module/resources"
iTime "agent/pkg/time"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"syscall" "syscall"
"time" "time"
...@@ -19,7 +19,6 @@ import ( ...@@ -19,7 +19,6 @@ import (
"linkfog.com/public/lib/monitor_daemon" "linkfog.com/public/lib/monitor_daemon"
"github.com/judwhite/go-svc" "github.com/judwhite/go-svc"
"github.com/shirou/gopsutil/host"
) )
var AgentSvc = &Agent{} var AgentSvc = &Agent{}
...@@ -120,36 +119,31 @@ func genVersion() string { ...@@ -120,36 +119,31 @@ func genVersion() string {
} }
func initSvcCfg() error { func initSvcCfg() error {
global.HostInfoTail = &global.HostInfo{
Platform: global.PlatformNameAndroid,
}
// 获取平台和内核版本 // 获取平台和内核版本
hostInfo, err := host.Info() global.GetPlatformInfo()
// 设置工作目录
err := global.SetWorkDir()
if err != nil { if err != nil {
l.Warnf("get host info err:%v", err) return err
} else {
if strings.Contains(hostInfo.KernelArch, "armv7") || strings.Contains(hostInfo.KernelArch, "armv8") {
global.HostInfoTail.Platform = global.PlatformNameAndroid
} else if strings.Contains(hostInfo.KernelArch, "x86") {
global.HostInfoTail.Platform = global.PlatformNameX86
} else if strings.Contains(hostInfo.KernelArch, "aarch") {
global.HostInfoTail.Platform = global.PlatformNameArm64
} }
global.HostInfoTail.KernelVersion = hostInfo.KernelVersion // 异常情况处理
if _, err := os.Stat(config.SelfOperatedAndroid); os.IsExist(err) {
l.Infof("the host is a self operated Android device")
for {
time.Sleep(24 * time.Hour)
} }
// 设置工作目录
l.Infof("the architecture(GOARCH) type is %s", runtime.GOARCH)
if strings.Contains(runtime.GOARCH, "amd64") || strings.Contains(runtime.GOARCH, "arm64") ||
strings.Contains(runtime.GOARCH, "aarch64") {
config.WorkDir = "/linkfog/agent/"
} else if strings.Contains(runtime.GOARCH, "android") || strings.Contains(runtime.GOARCH, "arm") {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err == nil {
config.WorkDir = filepath.Join(strings.Replace(dir, "\\", "/", -1), "agent")
} }
// TODO 序列号设置
// 时间修正
if !iTime.SynchronizeSystemTime() {
return fmt.Errorf("synchronization system time failed")
} }
// 获取安卓信息
global.GetAndroidInfo()
return nil return nil
} }
......
package global package global
import (
"agent/cmd/agent/config"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"runtime"
"strings"
"linkfog.com/public/lib/l"
"github.com/shirou/gopsutil/host"
)
const ( const (
ResourceModuleName = "resources" ResourceModuleName = "resources"
PluginModuleName = "pluginMgr" PluginModuleName = "pluginMgr"
...@@ -7,6 +22,8 @@ const ( ...@@ -7,6 +22,8 @@ const (
PlatformNameAndroid = "android" PlatformNameAndroid = "android"
PlatformNameX86 = "x86" PlatformNameX86 = "x86"
PlatformNameArm64 = "arm" PlatformNameArm64 = "arm"
AndroidVersion4 = "19"
) )
var HostInfoTail *HostInfo var HostInfoTail *HostInfo
...@@ -15,4 +32,101 @@ type HostInfo struct { ...@@ -15,4 +32,101 @@ type HostInfo struct {
Platform string Platform string
OSVersion string OSVersion string
KernelVersion string KernelVersion string
AndroidVersion string
UID string
SDKVersion string
AndroidVersion4 bool
}
func GetPlatformInfo() {
HostInfoTail = &HostInfo{
Platform: PlatformNameAndroid,
}
hostInfo, err := host.Info()
if err != nil {
l.Warnf("get host info err:%v", err)
} else {
if strings.Contains(hostInfo.KernelArch, "armv7") || strings.Contains(hostInfo.KernelArch, "armv8") {
HostInfoTail.Platform = PlatformNameAndroid
} else if strings.Contains(hostInfo.KernelArch, "x86") {
HostInfoTail.Platform = PlatformNameX86
} else if strings.Contains(hostInfo.KernelArch, "aarch") {
HostInfoTail.Platform = PlatformNameArm64
}
HostInfoTail.KernelVersion = hostInfo.KernelVersion
}
}
func SetWorkDir() error {
l.Infof("the architecture(GOARCH) type is %s", runtime.GOARCH)
if strings.Contains(runtime.GOARCH, "amd64") || strings.Contains(runtime.GOARCH, "arm64") ||
strings.Contains(runtime.GOARCH, "aarch64") {
config.WorkDir = "/linkfog/agent/"
} else if strings.Contains(runtime.GOARCH, "android") || strings.Contains(runtime.GOARCH, "arm") {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err == nil {
config.WorkDir = filepath.Join(strings.Replace(dir, "\\", "/", -1), "lw_agent")
}
}
err := os.MkdirAll(path.Join(config.WorkDir), 0777)
if err != nil {
return err
}
err = os.MkdirAll(path.Join(config.LogDir), 0644)
if err != nil {
return err
}
err = os.MkdirAll(path.Join(config.BinDir), 0644)
if err != nil {
return err
}
return nil
}
func GetAndroidInfo() {
if HostInfoTail.Platform == PlatformNameAndroid {
getAndroidVersion()
getUID()
getSDKVersion()
}
}
func getAndroidVersion() {
cmd := exec.Command("getprop", "ro.build.version.release")
output, err := cmd.CombinedOutput()
if err != nil {
l.Error("Error executing command:", err)
return
}
HostInfoTail.AndroidVersion = strings.TrimSpace(string(output))
//mlog.Logln("CPU Type:", AndroidVersion)
return
}
func getUID() {
currentUser, err := user.Current()
if err != nil {
l.Error("Error:", err)
return
}
HostInfoTail.UID = currentUser.Uid
//mlog.Logln("User ID:", Uid)
return
}
func getSDKVersion() {
cmd := exec.Command("getprop", "ro.build.version.sdk")
output, err := cmd.CombinedOutput()
if err != nil {
l.Error("Error executing command:", err)
return
}
HostInfoTail.SDKVersion = strings.TrimSpace(string(output))
if HostInfoTail.SDKVersion == AndroidVersion4 {
HostInfoTail.AndroidVersion4 = true
}
return
} }
...@@ -7,6 +7,7 @@ replace linkfog.com/public => web.lueluesay.top/git/lil/public v0.0.0-2024101503 ...@@ -7,6 +7,7 @@ replace linkfog.com/public => web.lueluesay.top/git/lil/public v0.0.0-2024101503
replace linkfog.com/pluginx => web.lueluesay.top/git/lil/pluginx v0.0.0-20241014064823-a6e286ccb5cd replace linkfog.com/pluginx => web.lueluesay.top/git/lil/pluginx v0.0.0-20241014064823-a6e286ccb5cd
require ( require (
github.com/beevik/ntp v1.4.3
github.com/judwhite/go-svc v1.2.1 github.com/judwhite/go-svc v1.2.1
github.com/olekukonko/tablewriter v0.0.5 github.com/olekukonko/tablewriter v0.0.5
github.com/prometheus/procfs v0.15.1 github.com/prometheus/procfs v0.15.1
...@@ -16,11 +17,9 @@ require ( ...@@ -16,11 +17,9 @@ require (
) )
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-ole/go-ole v1.2.6 // indirect
github.com/golang/protobuf v1.5.4 // indirect github.com/golang/protobuf v1.5.4 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect github.com/satori/go.uuid v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect github.com/tklauser/numcpus v0.8.0 // indirect
...@@ -31,5 +30,4 @@ require ( ...@@ -31,5 +30,4 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.67.1 // indirect google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )
github.com/beevik/ntp v1.4.3 h1:PlbTvE5NNy4QHmA4Mg57n7mcFTmr1W1j3gcK7L1lqho=
github.com/beevik/ntp v1.4.3/go.mod h1:Unr8Zg+2dRn7d8bHFuehIMSvvUYssHMxW3Q5Nx4RW5Q=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
...@@ -46,7 +48,6 @@ google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E= ...@@ -46,7 +48,6 @@ google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA= google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
......
package time
import (
"syscall"
"time"
"linkfog.com/public/lib/l"
"github.com/beevik/ntp"
)
func SynchronizeSystemTime() bool {
l.Info("the current device start check time sync...")
defer l.Info("sync system time ends")
go func() {
pool := make([]string, 0)
pool = append(pool, "pool.ntp.org")
pool = append(pool, "cn.pool.ntp.org")
pool = append(pool, "ntp1.aliyun.com")
pool = append(pool, "ntp.ntsc.ac.cn")
pool = append(pool, "time1.cloud.tencent.com")
pool = append(pool, "time.ustc.edu.cn")
for _, v := range pool {
response, err := ntp.Query(v)
if err != nil {
l.Warn("Failed to get time from NTP:", err)
continue
}
ntpTime := time.Now().Add(response.ClockOffset)
tv := syscall.NsecToTimeval(ntpTime.UnixNano())
err = syscall.Settimeofday(&tv)
if err != nil {
l.Warnf("Failed to set system time: %v", err)
continue
} else {
l.Info("set system time successfully")
break
}
}
}()
for i := 30; i > 0; i-- {
ts := time.Now().Unix()
//2024-04-01 00:00:00
if ts > 1711900800 {
l.Info("time sync success...", ts)
return true
} else {
l.Warn("time does not sync...", ts)
}
time.Sleep(10 * time.Second)
}
return false
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment