agent.go 2.97 KB
Newer Older
Lei Li's avatar
Lei Li committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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()
Lei Li's avatar
Lei Li committed
20
	msg := ""
Lei Li's avatar
Lei Li committed
21
22
23
24
25
26
27
28
	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)
Lei Li's avatar
Lei Li committed
29
30
				msg = fmt.Sprintf("DownloadFile err:%s", err.Error())
				LogError("MainActivity", msg)
Lei Li's avatar
Lei Li committed
31
32
33
34
35
36
				time.Sleep(5 * time.Second)
				continue
			}
			content, err := os.ReadFile(path.Join(RootDir, agentVersionReportFileName))
			if err != nil {
				l.Errorf("ReadFile err: %v", err)
Lei Li's avatar
Lei Li committed
37
38
				msg = fmt.Sprintf("ReadFile err:%s", err.Error())
				LogError("MainActivity", msg)
Lei Li's avatar
Lei Li committed
39
40
41
42
43
44
45
				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...")
Lei Li's avatar
Lei Li committed
46
47
48
				msg = fmt.Sprintf("unmarshal VersionReport content failed... err:%s", err.Error())
				LogError("MainActivity", msg)
				time.Sleep(5 * time.Second)
Lei Li's avatar
Lei Li committed
49
50
51
52
53
54
55
56
57
				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...")
Lei Li's avatar
Lei Li committed
58
59
60
				msg = fmt.Sprintf("download Agent failed.... err:%s", err.Error())
				LogError("MainActivity", msg)
				time.Sleep(5 * time.Second)
Lei Li's avatar
Lei Li committed
61
62
63
64
65
66
				continue
			}

			err = os.Rename(filepath.Join(RootDir, newExecFile), filepath.Join(RootDir, agentFileName))
			if err != nil {
				l.Errorf("copy Agent failed...err:%v", err)
Lei Li's avatar
Lei Li committed
67
68
				msg = fmt.Sprintf("copy Agent failed.... err:%s", err.Error())
				LogError("MainActivity", msg)
Lei Li's avatar
Lei Li committed
69
70
71
72
73
74
				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)
Lei Li's avatar
Lei Li committed
75
76
				msg = fmt.Sprintf("chmod Agent failed.... err:%s", err.Error())
				LogError("MainActivity", msg)
Lei Li's avatar
Lei Li committed
77
78
79
80
81
82
				time.Sleep(5 * time.Second)
				continue
			}
			md5, err := file.GetFileMD5(filepath.Join(RootDir, agentFileName))
			if err != nil {
				l.Warnf("GetFileMD5 err: %v", err)
Lei Li's avatar
Lei Li committed
83
84
				msg = fmt.Sprintf("GetFileMD5 err:%s", err.Error())
				LogWarn("MainActivity", msg)
Lei Li's avatar
Lei Li committed
85
86
87
			}
			if md5 != data["md5"] {
				l.Error("update new agent failed, check md5sum err!")
Lei Li's avatar
Lei Li committed
88
89
90
				msg = "update new agent failed, check md5sum err!"
				LogError("MainActivity", msg)
				time.Sleep(5 * time.Second)
Lei Li's avatar
Lei Li committed
91
92
93
94
95
96
97
98
99
100
101
				continue
			}

			os.Remove(filepath.Join(RootDir, newExecFile))
			os.Remove(filepath.Join(RootDir, agentVersionReportFileName))

			go startAgent()
			break
		}
	}
	l.Info("start")
Lei Li's avatar
Lei Li committed
102
	LogInfo("MainActivity", "start")
Lei Li's avatar
Lei Li committed
103
104
105
106
107
108
109
110
111
}

//export stop
func stop() {
	err := syscall.Kill(childPID, syscall.SIGTERM)
	if err != nil {
		l.Error(err)
	}
	l.Info("stop")
Lei Li's avatar
Lei Li committed
112
	LogInfo("MainActivity", "stop")
Lei Li's avatar
Lei Li committed
113
114
115
}

func main() {}