45 lines
1007 B
Go
45 lines
1007 B
Go
package nodeclient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/orohi/vpn-panel/internal/models"
|
|
)
|
|
|
|
type HealthResponse struct {
|
|
OK bool `json:"ok"`
|
|
Name string `json:"name"`
|
|
Version string `json:"version"`
|
|
Uptime int64 `json:"uptime_sec"`
|
|
Protocols []string `json:"protocols"`
|
|
}
|
|
|
|
func Health(n *models.Node) (*HealthResponse, error) {
|
|
url := fmt.Sprintf("http://%s:%d/health", n.Host, n.NodePort)
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer "+n.SecretKey)
|
|
|
|
client := &http.Client{Timeout: 8 * time.Second}
|
|
res, err := client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer res.Body.Close()
|
|
body, _ := io.ReadAll(res.Body)
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("status %d: %s", res.StatusCode, string(body))
|
|
}
|
|
var hr HealthResponse
|
|
if err := json.Unmarshal(body, &hr); err != nil {
|
|
return nil, err
|
|
}
|
|
return &hr, nil
|
|
}
|