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+
292318func (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