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

feat: 增加基础配置

parent 50b5aa95
......@@ -20,4 +20,6 @@ var (
MonitorLogUnModDuration = 600 * time.Second
BinDir = WorkDir + "/bin"
SelfOperatedAndroid = "/data/smallp"
)
......@@ -7,10 +7,10 @@ import (
"agent/module"
"agent/module/plugin"
"agent/module/resources"
iTime "agent/pkg/time"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
......@@ -19,7 +19,6 @@ import (
"linkfog.com/public/lib/monitor_daemon"
"github.com/judwhite/go-svc"
"github.com/shirou/gopsutil/host"
)
var AgentSvc = &Agent{}
......@@ -120,36 +119,31 @@ func genVersion() string {
}
func initSvcCfg() error {
global.HostInfoTail = &global.HostInfo{
Platform: global.PlatformNameAndroid,
}
// 获取平台和内核版本
hostInfo, err := host.Info()
global.GetPlatformInfo()
// 设置工作目录
err := global.SetWorkDir()
if err != nil {
l.Warnf("get host info err:%v", 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
return err
}
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
}
......
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 (
ResourceModuleName = "resources"
PluginModuleName = "pluginMgr"
......@@ -7,6 +22,8 @@ const (
PlatformNameAndroid = "android"
PlatformNameX86 = "x86"
PlatformNameArm64 = "arm"
AndroidVersion4 = "19"
)
var HostInfoTail *HostInfo
......@@ -15,4 +32,101 @@ type HostInfo struct {
Platform string
OSVersion 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
replace linkfog.com/pluginx => web.lueluesay.top/git/lil/pluginx v0.0.0-20241014064823-a6e286ccb5cd
require (
github.com/beevik/ntp v1.4.3
github.com/judwhite/go-svc v1.2.1
github.com/olekukonko/tablewriter v0.0.5
github.com/prometheus/procfs v0.15.1
......@@ -16,11 +17,9 @@ require (
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/golang/protobuf v1.5.4 // 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/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
......@@ -31,5 +30,4 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
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