This commit is contained in:
Akizon77
2025-04-11 17:26:34 +08:00
commit c2a9148d4c
14 changed files with 705 additions and 0 deletions

68
config/local.go Normal file
View File

@@ -0,0 +1,68 @@
package config
import (
"encoding/json"
"flag"
"os"
)
type LocalConfig struct {
Endpoint string `json:"endpoint"`
Token string `json:"token"`
Terminal bool `json:"terminal"`
MaxRetries int `json:"maxRetries"`
ReconnectInterval int `json:"reconnectInterval"`
IgnoreUnsafeCert bool `json:"ignoreUnsafeCert"`
}
func LoadConfig() (LocalConfig, error) {
var (
endpoint string
token string
terminal bool
path string
maxRetries int
reconnectInterval int
ignoreUnsafeCert bool
)
flag.StringVar(&endpoint, "e", "", "The endpoint URL")
flag.StringVar(&token, "token", "", "The authentication token")
flag.BoolVar(&terminal, "terminal", false, "Enable or disable terminal (default: false)")
flag.StringVar(&path, "c", "agent.json", "Path to the configuration file")
flag.IntVar(&maxRetries, "maxRetries", 10, "Maximum number of retries for WebSocket connection")
flag.IntVar(&reconnectInterval, "reconnectInterval", 5, "Reconnect interval in seconds")
flag.BoolVar(&ignoreUnsafeCert,"ignoreUnsafeCert", false, "Ignore unsafe certificate errors")
flag.Parse()
// Ensure -c cannot coexist with other flags
if path != "agent.json" && (endpoint != "" || token != "" || !terminal) {
return LocalConfig{}, flag.ErrHelp
}
// 必填项 Endpoint、Token 没有读取配置文件
if endpoint == "" || token == "" {
file, err := os.Open(path)
if err != nil {
return LocalConfig{}, err
}
defer file.Close()
var localConfig LocalConfig
if err := json.NewDecoder(file).Decode(&localConfig); err != nil {
return LocalConfig{}, err
}
return localConfig, nil
}
return LocalConfig{
Endpoint: endpoint,
Token: token,
Terminal: terminal,
MaxRetries: maxRetries,
ReconnectInterval: reconnectInterval,
IgnoreUnsafeCert: ignoreUnsafeCert,
}, nil
}

70
config/remote.go Normal file
View File

@@ -0,0 +1,70 @@
package config
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
type RemoteConfig struct {
Cpu bool `json:"cpu"`
Gpu bool `json:"gpu"`
Ram bool `json:"ram"`
Swap bool `json:"swap"`
Load bool `json:"load"`
Uptime bool `json:"uptime"`
Temperature bool `json:"temperature"`
Os bool `json:"os"`
Disk bool `json:"disk"`
Network bool `json:"network"`
Process bool `json:"process"`
Interval int `json:"interval"`
Connections bool `json:"connections"`
}
// 使用HTTP GET请求远程配置
//
// GET /api/getRemoteConfig
//
// Request the remote configuration
func LoadRemoteConfig(endpoint string, token string) (RemoteConfig, error) {
const maxRetry = 3
endpoint = strings.TrimSuffix(endpoint, "/") + "/api/getRemoteConfig" + "?token=" + token
var resp *http.Response
var err error
for attempt := 1; attempt <= maxRetry; attempt++ {
resp, err = http.Get(endpoint,)
if err == nil && resp.StatusCode == http.StatusOK {
break
}
if resp != nil {
resp.Body.Close()
}
if attempt == maxRetry {
if err != nil {
return RemoteConfig{}, fmt.Errorf("failed to fetchafter %d attempts: %v", maxRetry, err)
}
return RemoteConfig{}, fmt.Errorf("failed to fetch after %d attempts: %s", maxRetry, resp.Status)
}
time.Sleep(time.Second * time.Duration(attempt)) // Exponential backoff
}
defer resp.Body.Close()
response, err := io.ReadAll(resp.Body)
if err != nil {
return RemoteConfig{}, err
}
var remoteConfig RemoteConfig
if err := json.Unmarshal(response, &remoteConfig); err != nil {
return RemoteConfig{}, err
}
return remoteConfig, nil
}