Align Remnawave config with official docs and fix admin health check
This commit is contained in:
@@ -10,24 +10,32 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Client обращается к Remnawave Panel API:
|
||||
// - Base: REMNAWAVE_PANEL_URL (например https://panel.example.com)
|
||||
// - Auth: Authorization: Bearer REMNAWAVE_API_TOKEN
|
||||
// - Опционально: X-Api-Key: CADDY_AUTH_API_TOKEN
|
||||
// Документация: https://docs.rw/docs/install/subscription-page/bundled
|
||||
|
||||
type Client struct {
|
||||
baseURL string
|
||||
panelURL string
|
||||
token string
|
||||
caddyToken string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func NewClient(baseURL, apiToken, caddyToken string) *Client {
|
||||
func NewClient(panelURL, apiToken, caddyAuthToken string) *Client {
|
||||
return &Client{
|
||||
baseURL: strings.TrimRight(baseURL, "/"),
|
||||
panelURL: strings.TrimRight(panelURL, "/"),
|
||||
token: apiToken,
|
||||
caddyToken: caddyToken,
|
||||
caddyToken: caddyAuthToken,
|
||||
http: &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) PanelURL() string { return c.panelURL }
|
||||
|
||||
func (c *Client) countFromEndpoint(ctx context.Context, path, arrayKey string) (int, error) {
|
||||
resp, body, err := c.get(ctx, path)
|
||||
if err != nil {
|
||||
@@ -40,13 +48,23 @@ func (c *Client) countFromEndpoint(ctx context.Context, path, arrayKey string) (
|
||||
}
|
||||
|
||||
func (c *Client) get(ctx context.Context, path string) (*http.Response, []byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+path, nil)
|
||||
return c.doRequest(ctx, c.panelURL+path, true)
|
||||
}
|
||||
|
||||
func (c *Client) getPublic(ctx context.Context, path string) (*http.Response, []byte, error) {
|
||||
return c.doRequest(ctx, c.panelURL+path, false)
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(ctx context.Context, url string, withBearer bool) (*http.Response, []byte, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Accept", "application/json, text/html, */*")
|
||||
if withBearer {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
if c.caddyToken != "" {
|
||||
req.Header.Set("X-Api-Key", c.caddyToken)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user