Commit 04fd4500 authored by Lei Li's avatar Lei Li Committed by “李磊”
Browse files

feat:第一版支持拉取

parent 1cd203b9
lib/
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/agent-shell.iml" filepath="$PROJECT_DIR$/.idea/agent-shell.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
.pre-build:
mkdir -p lib/
SRC=./*.go
DST=lib/libagent.so
x86:
export GOARCH=amd64 GOOS=linux GO111MODULE=on CGO_ENABLED=1 && go build -buildmode=c-shared -o $(DST) $(SRC)
arm:
export GOARCH=arm GOOS=linux GO111MODULE=on CGO_ENABLED=1 CC=arm-linux-gnueabi-gcc && go build -buildmode=c-shared -o $(DST) $(SRC)
android:
export GOARCH=arm GOOS=android GO111MODULE=on CGO_ENABLED=1 CC=/usr/local/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi19-clang && go build -buildmode=c-shared -o $(DST) $(SRC)
.PHONY: clean
clean:
rm -f lib/*
package main
import "C"
import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"syscall"
"time"
"linkfog.com/public/lib/file"
"linkfog.com/public/lib/l"
)
//export start
func start() {
RootDir = GetCurrentDirectory()
for {
if file.Exists(filepath.Join(RootDir, agentFileName)) {
go startAgent()
break
} else {
err := DownloadFile(agentVersionReportFileName, agentVersionURL)
if err != nil {
l.Errorf("DownloadFile err: %v", err)
time.Sleep(5 * time.Second)
continue
}
content, err := os.ReadFile(path.Join(RootDir, agentVersionReportFileName))
if err != nil {
l.Errorf("ReadFile err: %v", err)
time.Sleep(5 * time.Second)
continue
}
data := make(map[string]string)
err = json.Unmarshal(content, &data)
if err != nil {
time.Sleep(5 * time.Second)
l.Error("unmarshal VersionReport content failed...")
return
}
l.Info("look up new version proc,updating...")
newFileURL := data["url"]
newExecFile := fmt.Sprintf("%s-new", agentFileName)
err = DownloadFile(newExecFile, newFileURL)
if err != nil {
time.Sleep(5 * time.Second)
l.Error("download Agent failed...")
continue
}
err = os.Rename(filepath.Join(RootDir, newExecFile), filepath.Join(RootDir, agentFileName))
if err != nil {
l.Errorf("copy Agent failed...err:%v", err)
time.Sleep(5 * time.Second)
continue
}
err = os.Chmod(filepath.Join(RootDir, agentFileName), 0755)
if err != nil {
l.Errorf("chmod Agent failed...err:%v", err)
time.Sleep(5 * time.Second)
continue
}
md5, err := file.GetFileMD5(filepath.Join(RootDir, agentFileName))
if err != nil {
l.Warnf("GetFileMD5 err: %v", err)
}
if md5 != data["md5"] {
time.Sleep(5 * time.Second)
l.Error("update new agent failed, check md5sum err!")
continue
}
os.Remove(filepath.Join(RootDir, newExecFile))
os.Remove(filepath.Join(RootDir, agentVersionReportFileName))
go startAgent()
break
}
}
l.Info("start")
}
//export stop
func stop() {
err := syscall.Kill(childPID, syscall.SIGTERM)
if err != nil {
l.Error(err)
}
l.Info("stop")
}
func main() {}
package main
import (
"os"
"path/filepath"
)
var (
RootDir = ""
agentFileName = "lw_android_agent"
agentVersionURL = "https://cdn2.bkdomain.cn/android-agent/VersionReport"
agentVersionReportFileName = ".versionReport"
childPID int
)
func GetCurrentDirectory() (ret string) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
//fmt.Println("fail to get workdir:",err)
ret = "/writable/agent-new"
} else {
ret = dir
}
return
}
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"time"
"linkfog.com/public/lib/l"
)
func DownloadFile(file, url string) error {
// URL of the file to be downloaded
defer l.Info("exited download file")
versionFile := path.Join(RootDir, file)
// Send HTTP GET request
response, err := HttpGet(url, nil)
if err != nil {
l.Error("get method failed:", err)
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("url:%s, response status code: %d", url, response.StatusCode)
}
// Create the file
out, err := os.Create(versionFile)
if err != nil {
l.Error("create new file failed:", err)
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, response.Body)
if err != nil {
l.Error("copy new file failed:", err)
return err
}
l.Info("download new file success!!!")
return nil
}
func HttpGet(url string, body []byte) (*http.Response, error) {
client := http.Client{}
req, _ := http.NewRequest("GET", url, bytes.NewBuffer(body))
//req.Header.Set("Content-Type", "application/json")
return client.Do(req)
}
func startAgent() {
for {
agent := exec.Command(filepath.Join(RootDir, agentFileName), "-vendor=test")
// 启动子进程
err := agent.Start()
if err != nil {
l.Errorf("agent start failed: %v", err)
time.Sleep(10 * time.Second)
continue
}
childPID = agent.Process.Pid
// 等待子进程结束
err = agent.Wait()
if err != nil {
l.Errorf("agent wait failed: %v", err)
}
time.Sleep(10 * time.Second)
}
}
module agent-shell
go 1.23.2
replace linkfog.com/public => web.lueluesay.top/git/lil/public v0.0.0-20241017054747-f08bac907b6c
require linkfog.com/public v0.0.0-00010101000000-000000000000
require golang.org/x/sys v0.26.0 // indirect
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