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(root string) { dir, err := os.Getwd() if err != nil { LogError("MainActivity", dir) } LogInfo("MainActivity", dir) exePath, err := os.Executable() if err != nil { LogError("MainActivity", exePath) } LogInfo("MainActivity", exePath) RootDir = GetCurrentDirectory() if RootDir == "/" { RootDir = root } LogInfo("MainActivity", RootDir) msg := "" 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) msg = fmt.Sprintf("DownloadFile err:%s", err.Error()) LogError("MainActivity", msg) time.Sleep(5 * time.Second) continue } content, err := os.ReadFile(path.Join(RootDir, agentVersionReportFileName)) if err != nil { l.Errorf("ReadFile err: %v", err) msg = fmt.Sprintf("ReadFile err:%s", err.Error()) LogError("MainActivity", msg) time.Sleep(5 * time.Second) continue } data := make(map[string]string) err = json.Unmarshal(content, &data) if err != nil { l.Error("unmarshal VersionReport content failed...") msg = fmt.Sprintf("unmarshal VersionReport content failed... err:%s", err.Error()) LogError("MainActivity", msg) time.Sleep(5 * time.Second) 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 { l.Error("download Agent failed...") msg = fmt.Sprintf("download Agent failed.... err:%s", err.Error()) LogError("MainActivity", msg) time.Sleep(5 * time.Second) continue } err = os.Rename(filepath.Join(RootDir, newExecFile), filepath.Join(RootDir, agentFileName)) if err != nil { l.Errorf("copy Agent failed...err:%v", err) msg = fmt.Sprintf("copy Agent failed.... err:%s", err.Error()) LogError("MainActivity", msg) 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) msg = fmt.Sprintf("chmod Agent failed.... err:%s", err.Error()) LogError("MainActivity", msg) time.Sleep(5 * time.Second) continue } md5, err := file.GetFileMD5(filepath.Join(RootDir, agentFileName)) if err != nil { l.Warnf("GetFileMD5 err: %v", err) msg = fmt.Sprintf("GetFileMD5 err:%s", err.Error()) LogWarn("MainActivity", msg) } if md5 != data["md5"] { l.Error("update new agent failed, check md5sum err!") msg = "update new agent failed, check md5sum err!" LogError("MainActivity", msg) time.Sleep(5 * time.Second) continue } os.Remove(filepath.Join(RootDir, newExecFile)) os.Remove(filepath.Join(RootDir, agentVersionReportFileName)) go startAgent() break } } l.Info("start") LogInfo("MainActivity", "start") } //export stop func stop() { err := syscall.Kill(childPID, syscall.SIGTERM) if err != nil { l.Error(err) } l.Info("stop") LogInfo("MainActivity", "stop") } func main() {}