feat: 自动发布realease

This commit is contained in:
Akizon77
2025-04-29 20:57:23 +08:00
parent f21001c71e
commit 79d1760163
13 changed files with 324 additions and 238 deletions

View File

@@ -7,12 +7,13 @@ import (
)
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"`
Endpoint string `json:"endpoint"`
Token string `json:"token"`
Terminal bool `json:"terminal"`
MaxRetries int `json:"maxRetries"`
ReconnectInterval int `json:"reconnectInterval"`
IgnoreUnsafeCert bool `json:"ignoreUnsafeCert"`
Interval float64 `json:"interval"`
}
func LoadConfig() (LocalConfig, error) {
@@ -24,7 +25,8 @@ func LoadConfig() (LocalConfig, error) {
path string
maxRetries int
reconnectInterval int
ignoreUnsafeCert bool
ignoreUnsafeCert bool
interval float64
)
flag.StringVar(&endpoint, "e", "", "The endpoint URL")
@@ -33,7 +35,8 @@ func LoadConfig() (LocalConfig, error) {
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.Float64Var(&interval, "interval", 1.1, "Interval in seconds for sending data to the server")
flag.BoolVar(&ignoreUnsafeCert, "ignoreUnsafeCert", false, "Ignore unsafe certificate errors")
flag.Parse()
// Ensure -c cannot coexist with other flags
@@ -63,6 +66,7 @@ func LoadConfig() (LocalConfig, error) {
Terminal: terminal,
MaxRetries: maxRetries,
ReconnectInterval: reconnectInterval,
IgnoreUnsafeCert: ignoreUnsafeCert,
IgnoreUnsafeCert: ignoreUnsafeCert,
Interval: interval,
}, nil
}

View File

@@ -1,70 +0,0 @@
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/clients/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
}