GetClientId 获取客户端id
()
| 57 | |
| 58 | // GetClientId 获取客户端id |
| 59 | func GetClientId() string { |
| 60 | // 默认id |
| 61 | id := "XXXXXXXXXXXXXXXX" |
| 62 | // 尝试读取.key文件 |
| 63 | excFilepath, err := os.Executable() |
| 64 | if err != nil { |
| 65 | logs.Error(err) |
| 66 | } |
| 67 | idFile := filepath.Join(filepath.Dir(excFilepath), ".key") |
| 68 | if _, err := os.Stat(idFile); err != nil { |
| 69 | // 文件不存在则生成随机ID并保存 |
| 70 | if f, err := os.Create(idFile); err != nil { |
| 71 | logs.Error(err) |
| 72 | } else { |
| 73 | defer f.Close() |
| 74 | const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 75 | idbyte := []byte(id) |
| 76 | for i := range idbyte { |
| 77 | idbyte[i] = chars[mrand.Intn(26)] |
| 78 | } |
| 79 | f.Write(idbyte) |
| 80 | id = string(idbyte) |
| 81 | } |
| 82 | } else { |
| 83 | // 文件存在则读取ID |
| 84 | idbyte, err := os.ReadFile(idFile) |
| 85 | if err != nil { |
| 86 | logs.Error(err) |
| 87 | } |
| 88 | if len(idbyte) == 16 { |
| 89 | if ok, err := regexp.Match(`[A-Z]{16}`, idbyte); ok && err == nil { |
| 90 | id = string(idbyte) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | return id |
| 95 | } |
| 96 | |
| 97 | // Detect 发送任务解析请求 |
| 98 | func Detect(dtype string, reqbody []byte) (repbody []byte, err error) { |