|
| 1 | +package utill |
| 2 | + |
| 3 | +import ( |
| 4 | + "agent/conf" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | +) |
| 13 | + |
| 14 | +var cMap = make(map[string]string) |
| 15 | +var mutex = sync.Mutex{} |
| 16 | + |
| 17 | +func GetRemoteFileBody(filePath string) string { |
| 18 | + |
| 19 | + log.Print("begin download file:", filePath) |
| 20 | + resp, err := http.Get(filePath) |
| 21 | + |
| 22 | + if err != nil { |
| 23 | + panic(err.Error()) |
| 24 | + } |
| 25 | + |
| 26 | + defer resp.Body.Close() |
| 27 | + data, _ := ioutil.ReadAll(resp.Body) |
| 28 | + |
| 29 | + fileBody := string(data) |
| 30 | + |
| 31 | + conMap := ParseAppConf() |
| 32 | + if conMap[conf.Encrypted] == "true" { |
| 33 | + |
| 34 | + fileBody = strings.Replace(fileBody, "\n", "", -1) |
| 35 | + fileBody = strings.Replace(fileBody, "\r", "", -1) |
| 36 | + fileBody, _ = DecryptAesData(fileBody) |
| 37 | + } |
| 38 | + |
| 39 | + return fileBody |
| 40 | +} |
| 41 | + |
| 42 | +func ParseAppConf() map[string]string { |
| 43 | + |
| 44 | + var configPath string |
| 45 | + |
| 46 | + mutex.Lock() |
| 47 | + defer mutex.Unlock() |
| 48 | + |
| 49 | + if len(cMap) != 0 { |
| 50 | + return cMap |
| 51 | + } |
| 52 | + |
| 53 | + home, _ := os.Getwd() |
| 54 | + configPath = filepath.Join(home, "src/agent/conf", "app.conf") |
| 55 | + |
| 56 | + if !FileExists(configPath) { |
| 57 | + configPath = filepath.Join(home, "conf", "app.conf") |
| 58 | + |
| 59 | + if !FileExists(configPath) { |
| 60 | + panic("app.conf not found!" + configPath) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + cFile, _ := os.Open(configPath) |
| 65 | + fileBytes, _ := ioutil.ReadAll(cFile) |
| 66 | + fileBody := string(fileBytes) |
| 67 | + |
| 68 | + fileBody = strings.Replace(fileBody, "\r", "", -1) |
| 69 | + bodyArr := strings.Split(fileBody, "\n") |
| 70 | + |
| 71 | + for _, body := range bodyArr { |
| 72 | + if body != "" && !strings.HasPrefix(body, "#") { |
| 73 | + arrV := strings.Split(body, "=") |
| 74 | + if len(arrV) == 2 { |
| 75 | + cMap[arrV[0]] = arrV[1] |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return cMap |
| 81 | +} |
| 82 | + |
| 83 | +func FileExists(name string) bool { |
| 84 | + if _, err := os.Stat(name); err != nil { |
| 85 | + if os.IsNotExist(err) { |
| 86 | + return false |
| 87 | + } |
| 88 | + } |
| 89 | + return true |
| 90 | +} |
0 commit comments