Skip to content

Commit def9706

Browse files
author
piexlmax
committed
Package compatible 1.16 and 1.18
1 parent 4afd12f commit def9706

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

service/auxiliarygt18.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package service
5+
6+
import (
7+
"debug/buildinfo"
8+
"fmt"
9+
"github.com/HXSecurity/DongTai-agent-go/model/request"
10+
"github.com/HXSecurity/DongTai-agent-go/utils"
11+
"github.com/pkg/errors"
12+
"io/fs"
13+
"os"
14+
"os/exec"
15+
"path/filepath"
16+
"runtime"
17+
"strings"
18+
)
19+
20+
// GenAQLForGolang 为golang组件生成aql
21+
func GenAQLForGolang(packageName, version string) string {
22+
return fmt.Sprintf("golang:%s:%s:", packageName, version)
23+
}
24+
25+
// 获取包
26+
func GetMod() ([]request.Component, string) {
27+
//fmt.Println(getCurrentPath())
28+
path, _ := os.Executable()
29+
return scanFile(path, true)
30+
}
31+
32+
// 判断是否是exe文件
33+
func isExe(file string, info fs.FileInfo) bool {
34+
if runtime.GOOS == "windows" {
35+
return strings.HasSuffix(strings.ToLower(file), ".exe")
36+
}
37+
return info.Mode().IsRegular() && info.Mode()&0111 != 0
38+
}
39+
40+
// 从二进制文件读取包信息
41+
func scanFile(file string, mustPrint bool) (packages []request.Component, agentVersion string) {
42+
bi, err := buildinfo.ReadFile(file)
43+
if err != nil {
44+
if mustPrint {
45+
if pathErr := (*os.PathError)(nil); errors.As(err, &pathErr) && filepath.Clean(pathErr.Path) == filepath.Clean(file) {
46+
fmt.Fprintf(os.Stderr, "%v\n", file)
47+
} else {
48+
fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
49+
}
50+
}
51+
return packages, agentVersion
52+
}
53+
fmt.Printf("%s: %s\n", file, bi.GoVersion)
54+
bi.GoVersion = "" // suppress printing go version again
55+
mod := bi.String()
56+
57+
li := strings.Split(mod[:len(mod)-1], "\n")
58+
for i := range li {
59+
licl := strings.Split(li[i], "\t")
60+
if licl[0] == "dep" {
61+
fmt.Printf("依赖:%s\t版本:%s\n", licl[1], licl[2])
62+
aql := GenAQLForGolang(licl[1], licl[2])
63+
if licl[1] == "github.com/HXSecurity/DongTai-agent-go" {
64+
fmt.Println("当前探针版本为:" + licl[2])
65+
agentVersion = licl[2]
66+
}
67+
packages = append(packages, request.Component{
68+
PackageName: aql,
69+
PackageAlgorithm: "SHA-1",
70+
PackagePath: file,
71+
PackageVersion: licl[2],
72+
PackageSignature: utils.SHA1(aql),
73+
},
74+
)
75+
}
76+
}
77+
return packages, agentVersion
78+
}
79+
80+
// 获取服务信息
81+
func getServerInfo() (server *utils.Server, err error) {
82+
var s utils.Server
83+
s.Os = utils.InitOS()
84+
if s.Cpu, err = utils.InitCPU(); err != nil {
85+
fmt.Println(err.Error())
86+
return &s, err
87+
}
88+
if s.Rrm, err = utils.InitRAM(); err != nil {
89+
fmt.Println(err.Error())
90+
return &s, err
91+
}
92+
if s.Disk, err = utils.InitDisk(); err != nil {
93+
fmt.Println(err.Error())
94+
return &s, err
95+
}
96+
97+
return &s, nil
98+
}
99+
100+
func getCurrentPath() (string, error) {
101+
file, err := exec.LookPath(os.Args[0])
102+
if err != nil {
103+
return "", err
104+
}
105+
path, err := filepath.Abs(file)
106+
if err != nil {
107+
return "", err
108+
}
109+
i := strings.LastIndex(path, "/")
110+
if i < 0 {
111+
i = strings.LastIndex(path, "\\")
112+
}
113+
if i < 0 {
114+
return "", errors.New(`error: Can't find "/" or "\".`)
115+
}
116+
return string(path[0 : i+1]), nil
117+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !go1.18
2+
// +build !go1.18
3+
14
package service
25

36
import (

0 commit comments

Comments
 (0)