Skip to content

Commit 692b26a

Browse files
committed
Update system stats processing logic with also supporting legacy method https://github.com/MHSanaei/3x-ui/blob/40b6d7707a8d7856eb97e00ea7c86052859ce162/web/controller/server.go#L41
1 parent 49cf378 commit 692b26a

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

.github/workflows/dockerhub-publish.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
platforms: linux/amd64,linux/arm64
2828
push: true
2929
build-args: |
30-
GIT_TAG=v1.3.2
30+
GIT_TAG=v1.4.0
3131
tags: |
3232
hteppl/x-ui-exporter:latest
33-
hteppl/x-ui-exporter:v1.3.2
33+
hteppl/x-ui-exporter:v1.4.0

api/api.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"crypto/tls"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"io"
910
"net/http"
@@ -176,9 +177,25 @@ func (a *APIClient) FetchServerStatus(cookie *http.Cookie) error {
176177
// Clear old version metric to avoid accumulating obsolete label values
177178
metrics.XrayVersion.Reset()
178179

179-
body, err := a.sendRequest("/server/status", http.MethodPost, cookie)
180+
var (
181+
body []byte
182+
err error
183+
)
184+
185+
// Try GET first
186+
body, err = a.sendRequest("/server/status", http.MethodGet, cookie)
180187
if err != nil {
181-
return fmt.Errorf("system stats: %w", err)
188+
// Check if error is due to HTTP method not allowed or endpoint not found
189+
var httpErr *HTTPError
190+
if errors.As(err, &httpErr) && (httpErr.StatusCode == http.StatusMethodNotAllowed || httpErr.StatusCode == http.StatusNotFound) {
191+
// Retry with POST
192+
body, err = a.sendRequest("/server/status", http.MethodPost, cookie)
193+
if err != nil {
194+
return fmt.Errorf("system stats (POST fallback): %w", err)
195+
}
196+
} else {
197+
return fmt.Errorf("system stats (GET): %w", err)
198+
}
182199
}
183200

184201
response := serverStatusPool.Get().(*client3xui.ServerStatusResponse)
@@ -289,6 +306,15 @@ func (a *APIClient) createRequest(method, path string, cookie *http.Cookie) (*ht
289306
return req, nil
290307
}
291308

309+
type HTTPError struct {
310+
StatusCode int
311+
Body []byte
312+
}
313+
314+
func (e *HTTPError) Error() string {
315+
return fmt.Sprintf("HTTP request failed with status %d", e.StatusCode)
316+
}
317+
292318
func (a *APIClient) sendRequest(path, method string, cookie *http.Cookie) ([]byte, error) {
293319
req, err := a.createRequest(method, path, cookie)
294320
if err != nil {
@@ -303,5 +329,17 @@ func (a *APIClient) sendRequest(path, method string, cookie *http.Cookie) ([]byt
303329
_ = resp.Body.Close()
304330
}()
305331

306-
return io.ReadAll(resp.Body)
332+
body, err := io.ReadAll(resp.Body)
333+
if err != nil {
334+
return nil, err
335+
}
336+
337+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
338+
return nil, &HTTPError{
339+
StatusCode: resp.StatusCode,
340+
Body: body,
341+
}
342+
}
343+
344+
return body, nil
307345
}

0 commit comments

Comments
 (0)