1
0

remote.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. package runtime
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "time"
  16. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  17. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  18. "github.com/mhsanaei/3x-ui/v3/internal/util/netsafe"
  19. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  20. )
  21. const remoteHTTPTimeout = 10 * time.Second
  22. type envelope struct {
  23. Success bool `json:"success"`
  24. Msg string `json:"msg"`
  25. Obj json.RawMessage `json:"obj"`
  26. }
  27. type Remote struct {
  28. node *model.Node
  29. mu sync.RWMutex
  30. remoteIDByTag map[string]int
  31. // Per-node client honoring the TLS verify mode, built once and reused; a
  32. // node config change drops the cached Remote so the next one rebuilds it.
  33. clientOnce sync.Once
  34. client *http.Client
  35. clientErr error
  36. egressResolver NodeEgressResolver
  37. }
  38. type RemoteInboundOption struct {
  39. Tag string `json:"tag"`
  40. Remark string `json:"remark"`
  41. Protocol model.Protocol `json:"protocol"`
  42. Port int `json:"port"`
  43. }
  44. func NewRemote(n *model.Node, r NodeEgressResolver) *Remote {
  45. return &Remote{
  46. node: n,
  47. remoteIDByTag: make(map[string]int),
  48. egressResolver: r,
  49. }
  50. }
  51. func (r *Remote) Name() string { return "node:" + r.node.Name }
  52. // httpClient lazily builds and caches the per-node client honoring the TLS
  53. // verify mode, so Remote ops don't fall back to system CA on skip/pin (#5264).
  54. func (r *Remote) httpClient() (*http.Client, error) {
  55. r.clientOnce.Do(func() {
  56. proxyURL := ""
  57. if r.node.OutboundTag != "" && r.egressResolver != nil {
  58. proxyURL = r.egressResolver.NodeEgressProxyURL(r.node.Id)
  59. }
  60. r.client, r.clientErr = HTTPClientForNode(r.node, proxyURL)
  61. })
  62. return r.client, r.clientErr
  63. }
  64. func (r *Remote) baseURL() (string, error) {
  65. addr, err := netsafe.NormalizeHost(r.node.Address)
  66. if err != nil {
  67. return "", err
  68. }
  69. scheme := r.node.Scheme
  70. if scheme != "http" && scheme != "https" {
  71. scheme = "https"
  72. }
  73. if r.node.Port <= 0 || r.node.Port > 65535 {
  74. return "", fmt.Errorf("invalid node port %d", r.node.Port)
  75. }
  76. bp := r.node.BasePath
  77. if bp == "" {
  78. bp = "/"
  79. }
  80. if !strings.HasSuffix(bp, "/") {
  81. bp += "/"
  82. }
  83. u := &url.URL{
  84. Scheme: scheme,
  85. Host: net.JoinHostPort(addr, strconv.Itoa(r.node.Port)),
  86. Path: bp,
  87. }
  88. return u.String(), nil
  89. }
  90. func (r *Remote) do(ctx context.Context, method, path string, body any) (*envelope, error) {
  91. if r.node.ApiToken == "" {
  92. return nil, errors.New("node has no API token configured")
  93. }
  94. base, err := r.baseURL()
  95. if err != nil {
  96. return nil, err
  97. }
  98. target := base + strings.TrimPrefix(path, "/")
  99. var (
  100. reqBody io.Reader
  101. contentType string
  102. )
  103. switch b := body.(type) {
  104. case nil:
  105. case url.Values:
  106. reqBody = strings.NewReader(b.Encode())
  107. contentType = "application/x-www-form-urlencoded"
  108. default:
  109. buf, jerr := json.Marshal(b)
  110. if jerr != nil {
  111. return nil, fmt.Errorf("marshal body: %w", jerr)
  112. }
  113. reqBody = bytes.NewReader(buf)
  114. contentType = "application/json"
  115. }
  116. cctx, cancel := context.WithTimeout(netsafe.ContextWithAllowPrivate(ctx, r.node.AllowPrivateAddress), remoteHTTPTimeout)
  117. defer cancel()
  118. req, err := http.NewRequestWithContext(cctx, method, target, reqBody)
  119. if err != nil {
  120. return nil, err
  121. }
  122. req.Header.Set("Authorization", "Bearer "+r.node.ApiToken)
  123. req.Header.Set("Accept", "application/json")
  124. if contentType != "" {
  125. req.Header.Set("Content-Type", contentType)
  126. }
  127. client, err := r.httpClient()
  128. if err != nil {
  129. return nil, err
  130. }
  131. resp, err := client.Do(req)
  132. if err != nil {
  133. return nil, fmt.Errorf("%s %s: %w", method, path, err)
  134. }
  135. defer resp.Body.Close()
  136. raw, err := io.ReadAll(resp.Body)
  137. if err != nil {
  138. return nil, fmt.Errorf("read body: %w", err)
  139. }
  140. if resp.StatusCode != http.StatusOK {
  141. return nil, fmt.Errorf("%s %s: HTTP %d", method, path, resp.StatusCode)
  142. }
  143. var env envelope
  144. if err := json.Unmarshal(raw, &env); err != nil {
  145. return nil, fmt.Errorf("decode envelope: %w", err)
  146. }
  147. if !env.Success {
  148. return &env, fmt.Errorf("remote: %s", env.Msg)
  149. }
  150. return &env, nil
  151. }
  152. func (r *Remote) resolveRemoteID(ctx context.Context, tag string) (int, error) {
  153. if id, ok := r.cacheGetTag(tag); ok {
  154. return id, nil
  155. }
  156. if err := r.refreshRemoteIDs(ctx); err != nil {
  157. return 0, err
  158. }
  159. if id, ok := r.cacheGetTag(tag); ok {
  160. return id, nil
  161. }
  162. return 0, fmt.Errorf("remote inbound with tag %q not found on node %s", tag, r.node.Name)
  163. }
  164. // cacheGetTag looks up a remote inbound id by tag, tolerating an n<id>- prefix
  165. // that lives on only one of the two panels: the node may carry the bare tag
  166. // while the central panel stores the prefixed form, or vice versa.
  167. func (r *Remote) cacheGetTag(tag string) (int, bool) {
  168. if id, ok := r.cacheGet(tag); ok {
  169. return id, true
  170. }
  171. prefix := fmt.Sprintf("n%d-", r.node.Id)
  172. if stripped, found := strings.CutPrefix(tag, prefix); found {
  173. return r.cacheGet(stripped)
  174. }
  175. return r.cacheGet(prefix + tag)
  176. }
  177. func (r *Remote) cacheGet(tag string) (int, bool) {
  178. r.mu.RLock()
  179. defer r.mu.RUnlock()
  180. id, ok := r.remoteIDByTag[tag]
  181. return id, ok
  182. }
  183. func (r *Remote) cacheSet(tag string, id int) {
  184. r.mu.Lock()
  185. defer r.mu.Unlock()
  186. r.remoteIDByTag[tag] = id
  187. }
  188. func (r *Remote) cacheDel(tag string) {
  189. r.mu.Lock()
  190. defer r.mu.Unlock()
  191. delete(r.remoteIDByTag, tag)
  192. }
  193. func (r *Remote) ListRemoteTags(ctx context.Context) ([]string, error) {
  194. if err := r.refreshRemoteIDs(ctx); err != nil {
  195. return nil, err
  196. }
  197. r.mu.RLock()
  198. defer r.mu.RUnlock()
  199. tags := make([]string, 0, len(r.remoteIDByTag))
  200. for tag := range r.remoteIDByTag {
  201. tags = append(tags, tag)
  202. }
  203. return tags, nil
  204. }
  205. func (r *Remote) ListInboundOptions(ctx context.Context) ([]RemoteInboundOption, error) {
  206. env, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil)
  207. if err != nil {
  208. return nil, err
  209. }
  210. var list []RemoteInboundOption
  211. if err := json.Unmarshal(env.Obj, &list); err != nil {
  212. return nil, fmt.Errorf("decode inbound list: %w", err)
  213. }
  214. return list, nil
  215. }
  216. func (r *Remote) refreshRemoteIDs(ctx context.Context) error {
  217. env, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil)
  218. if err != nil {
  219. return err
  220. }
  221. var list []struct {
  222. Id int `json:"id"`
  223. Tag string `json:"tag"`
  224. }
  225. if err := json.Unmarshal(env.Obj, &list); err != nil {
  226. return fmt.Errorf("decode inbound list: %w", err)
  227. }
  228. next := make(map[string]int, len(list))
  229. for _, ib := range list {
  230. if ib.Tag == "" {
  231. continue
  232. }
  233. next[ib.Tag] = ib.Id
  234. }
  235. r.mu.Lock()
  236. r.remoteIDByTag = next
  237. r.mu.Unlock()
  238. return nil
  239. }
  240. func (r *Remote) AddInbound(ctx context.Context, ib *model.Inbound) error {
  241. payload := wireInbound(ib)
  242. env, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/add", payload)
  243. if err != nil {
  244. return err
  245. }
  246. var created struct {
  247. Id int `json:"id"`
  248. Tag string `json:"tag"`
  249. }
  250. if len(env.Obj) > 0 {
  251. if err := json.Unmarshal(env.Obj, &created); err == nil && created.Id > 0 && created.Tag != "" {
  252. r.cacheSet(created.Tag, created.Id)
  253. }
  254. }
  255. return nil
  256. }
  257. func (r *Remote) DelInbound(ctx context.Context, ib *model.Inbound) error {
  258. id, err := r.resolveRemoteID(ctx, ib.Tag)
  259. if err != nil {
  260. logger.Warning("remote DelInbound: tag", ib.Tag, "not found on", r.node.Name)
  261. return nil
  262. }
  263. if _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/del/"+strconv.Itoa(id), nil); err != nil {
  264. return err
  265. }
  266. r.cacheDel(ib.Tag)
  267. return nil
  268. }
  269. func (r *Remote) UpdateInbound(ctx context.Context, oldIb, newIb *model.Inbound) error {
  270. id, err := r.resolveRemoteID(ctx, oldIb.Tag)
  271. if err != nil {
  272. return r.AddInbound(ctx, newIb)
  273. }
  274. payload := wireInbound(newIb)
  275. if _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/update/"+strconv.Itoa(id), payload); err != nil {
  276. return err
  277. }
  278. if oldIb.Tag != newIb.Tag {
  279. r.cacheDel(oldIb.Tag)
  280. }
  281. r.cacheSet(newIb.Tag, id)
  282. return nil
  283. }
  284. func (r *Remote) AddUser(ctx context.Context, ib *model.Inbound, _ map[string]any) error {
  285. return r.UpdateInbound(ctx, ib, ib)
  286. }
  287. func (r *Remote) RemoveUser(ctx context.Context, ib *model.Inbound, _ string) error {
  288. return r.UpdateInbound(ctx, ib, ib)
  289. }
  290. func (r *Remote) AddClient(ctx context.Context, ib *model.Inbound, client model.Client) error {
  291. id, err := r.resolveRemoteID(ctx, ib.Tag)
  292. if err != nil {
  293. return fmt.Errorf("remote AddClient: resolve tag %q: %w", ib.Tag, err)
  294. }
  295. payload := map[string]any{
  296. "client": client,
  297. "inboundIds": []int{id},
  298. }
  299. if _, err := r.do(ctx, http.MethodPost, "panel/api/clients/add", payload); err != nil {
  300. return err
  301. }
  302. return nil
  303. }
  304. func (r *Remote) DeleteUser(ctx context.Context, ib *model.Inbound, email string) error {
  305. if email == "" {
  306. return nil
  307. }
  308. id, err := r.resolveRemoteID(ctx, ib.Tag)
  309. if err != nil {
  310. return nil
  311. }
  312. body := map[string]any{"inboundIds": []int{id}}
  313. _, err = r.do(ctx, http.MethodPost,
  314. "panel/api/clients/"+url.PathEscape(email)+"/detach", body)
  315. if err == nil {
  316. return nil
  317. }
  318. if strings.Contains(strings.ToLower(err.Error()), "not found") {
  319. return nil
  320. }
  321. return err
  322. }
  323. func (r *Remote) UpdateUser(ctx context.Context, ib *model.Inbound, oldEmail string, payload model.Client) error {
  324. if oldEmail == "" {
  325. oldEmail = payload.Email
  326. }
  327. id, err := r.resolveRemoteID(ctx, ib.Tag)
  328. if err != nil {
  329. return err
  330. }
  331. path := "panel/api/clients/update/" + url.PathEscape(oldEmail) +
  332. "?inboundIds=" + strconv.Itoa(id)
  333. if _, err := r.do(ctx, http.MethodPost, path, payload); err != nil {
  334. return err
  335. }
  336. return nil
  337. }
  338. func (r *Remote) RestartXray(ctx context.Context) error {
  339. _, err := r.do(ctx, http.MethodPost, "panel/api/server/restartXrayService", nil)
  340. return err
  341. }
  342. // UpdatePanel asks the node to run its own official self-updater (update.sh)
  343. // and restart onto the latest release. The node returns as soon as the job is
  344. // launched; the new version surfaces on the next heartbeat.
  345. func (r *Remote) UpdatePanel(ctx context.Context) error {
  346. _, err := r.do(ctx, http.MethodPost, "panel/api/server/updatePanel", nil)
  347. return err
  348. }
  349. // WebCertFiles holds a node's own web TLS certificate and key file paths.
  350. type WebCertFiles struct {
  351. WebCertFile string `json:"webCertFile"`
  352. WebKeyFile string `json:"webKeyFile"`
  353. }
  354. // GetWebCertFiles fetches the node's own web TLS certificate/key file paths so
  355. // the central panel can offer them as the "Set Cert from Panel" default for a
  356. // node-assigned inbound — those paths exist on the node, the central panel's
  357. // don't. See issue #4854.
  358. func (r *Remote) GetWebCertFiles(ctx context.Context) (*WebCertFiles, error) {
  359. env, err := r.do(ctx, http.MethodGet, "panel/api/server/getWebCertFiles", nil)
  360. if err != nil {
  361. return nil, err
  362. }
  363. var files WebCertFiles
  364. if err := json.Unmarshal(env.Obj, &files); err != nil {
  365. return nil, fmt.Errorf("decode web cert files: %w", err)
  366. }
  367. return &files, nil
  368. }
  369. // GetDescendants fetches the node's read-only summaries of the nodes IT
  370. // manages, so this panel can surface them as transitive sub-nodes in a chained
  371. // topology (#4983). Best-effort: an old-build node without the endpoint returns
  372. // an error the caller ignores.
  373. func (r *Remote) GetDescendants(ctx context.Context) ([]model.NodeSummary, error) {
  374. env, err := r.do(ctx, http.MethodGet, "panel/api/server/descendants", nil)
  375. if err != nil {
  376. return nil, err
  377. }
  378. var out []model.NodeSummary
  379. if len(env.Obj) > 0 {
  380. if err := json.Unmarshal(env.Obj, &out); err != nil {
  381. return nil, fmt.Errorf("decode descendants: %w", err)
  382. }
  383. }
  384. return out, nil
  385. }
  386. func (r *Remote) ResetClientTraffic(ctx context.Context, _ *model.Inbound, email string) error {
  387. _, err := r.do(ctx, http.MethodPost,
  388. "panel/api/clients/resetTraffic/"+url.PathEscape(email), nil)
  389. return err
  390. }
  391. func (r *Remote) ResetAllTraffics(ctx context.Context) error {
  392. _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/resetAllTraffics", nil)
  393. return err
  394. }
  395. func (r *Remote) ResetInboundTraffic(ctx context.Context, ib *model.Inbound) error {
  396. _, err := r.do(ctx, http.MethodPost, fmt.Sprintf("panel/api/inbounds/%d/resetTraffic", ib.Id), nil)
  397. return err
  398. }
  399. type TrafficSnapshot struct {
  400. Inbounds []*model.Inbound
  401. OnlineEmails []string
  402. // OnlineTree is the node's GUID-keyed online subtree (its own clients under
  403. // its panelGuid plus every descendant under theirs). Preferred over the flat
  404. // OnlineEmails so the master can attribute deeply nested clients to the real
  405. // node across a chain (#4983). Empty when the node is an old build without
  406. // the per-GUID endpoint — OnlineEmails is the fallback then.
  407. OnlineTree map[string][]string
  408. LastOnlineMap map[string]int64
  409. }
  410. func (r *Remote) FetchTrafficSnapshot(ctx context.Context) (*TrafficSnapshot, error) {
  411. snap := &TrafficSnapshot{LastOnlineMap: map[string]int64{}}
  412. envList, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil)
  413. if err != nil {
  414. return nil, err
  415. }
  416. if err := json.Unmarshal(envList.Obj, &snap.Inbounds); err != nil {
  417. return nil, fmt.Errorf("decode inbound list: %w", err)
  418. }
  419. // Prefer the GUID-keyed subtree; fall back to the flat list only when the
  420. // node is an old build without the per-GUID endpoint (#4983).
  421. envTree, err := r.do(ctx, http.MethodPost, "panel/api/clients/onlinesByGuid", nil)
  422. if err == nil && len(envTree.Obj) > 0 {
  423. _ = json.Unmarshal(envTree.Obj, &snap.OnlineTree)
  424. }
  425. if len(snap.OnlineTree) == 0 {
  426. envOnlines, err := r.do(ctx, http.MethodPost, "panel/api/clients/onlines", nil)
  427. if err != nil {
  428. logger.Warning("remote", r.node.Name, "onlines fetch failed:", err)
  429. } else if len(envOnlines.Obj) > 0 {
  430. _ = json.Unmarshal(envOnlines.Obj, &snap.OnlineEmails)
  431. }
  432. }
  433. envLastOnline, err := r.do(ctx, http.MethodPost, "panel/api/clients/lastOnline", nil)
  434. if err != nil {
  435. logger.Warning("remote", r.node.Name, "lastOnline fetch failed:", err)
  436. } else if len(envLastOnline.Obj) > 0 {
  437. _ = json.Unmarshal(envLastOnline.Obj, &snap.LastOnlineMap)
  438. }
  439. return snap, nil
  440. }
  441. // PushGlobalClientTraffics sends this panel's aggregated per-client usage to
  442. // the node, tagged with this panel's GUID so the node keeps one row per
  443. // pushing master. Display/enforcement input on the node only — the node never
  444. // folds these into the counters it reports back, so this panel's (and any
  445. // other master's) delta accounting over the node snapshot stays intact.
  446. func (r *Remote) PushGlobalClientTraffics(ctx context.Context, masterGuid string, traffics []*xray.ClientTraffic) error {
  447. payload := map[string]any{
  448. "masterGuid": masterGuid,
  449. "traffics": traffics,
  450. }
  451. _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/pushClientTraffics", payload)
  452. return err
  453. }
  454. func wireInbound(ib *model.Inbound) url.Values {
  455. v := url.Values{}
  456. v.Set("total", strconv.FormatInt(ib.Total, 10))
  457. v.Set("remark", ib.Remark)
  458. v.Set("subSortIndex", strconv.Itoa(ib.SubSortIndex))
  459. v.Set("enable", strconv.FormatBool(ib.Enable))
  460. v.Set("expiryTime", strconv.FormatInt(ib.ExpiryTime, 10))
  461. v.Set("listen", ib.Listen)
  462. v.Set("port", strconv.Itoa(ib.Port))
  463. v.Set("protocol", string(ib.Protocol))
  464. v.Set("settings", ib.Settings)
  465. v.Set("streamSettings", sanitizeStreamSettingsForRemote(ib.StreamSettings))
  466. v.Set("tag", ib.Tag)
  467. v.Set("sniffing", ib.Sniffing)
  468. shareAddrStrategy := strings.TrimSpace(ib.ShareAddrStrategy)
  469. switch shareAddrStrategy {
  470. case "listen", "custom":
  471. default:
  472. shareAddrStrategy = "node"
  473. }
  474. v.Set("shareAddrStrategy", shareAddrStrategy)
  475. v.Set("shareAddr", ib.ShareAddr)
  476. if ib.TrafficReset != "" {
  477. v.Set("trafficReset", ib.TrafficReset)
  478. }
  479. return v
  480. }
  481. // sanitizeStreamSettingsForRemote strips file-based TLS certificate paths
  482. // from the StreamSettings before sending to a remote node, but ONLY when
  483. // inline certificate content (certificate / key) is also present in the same
  484. // entry. In that case the file paths are redundant and stripping them avoids
  485. // confusion when the central panel's local paths don't exist on the remote.
  486. //
  487. // When a certificate entry contains ONLY file paths (no inline content) the
  488. // paths are left untouched: the user explicitly entered paths that exist on
  489. // the remote node's filesystem, and removing them would leave Xray with TLS
  490. // configured but no certificate, causing Xray to crash on the remote node.
  491. func sanitizeStreamSettingsForRemote(streamSettings string) string {
  492. if streamSettings == "" {
  493. return streamSettings
  494. }
  495. var stream map[string]any
  496. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  497. return streamSettings
  498. }
  499. tlsSettings, ok := stream["tlsSettings"].(map[string]any)
  500. if !ok {
  501. return streamSettings
  502. }
  503. certificates, ok := tlsSettings["certificates"].([]any)
  504. if !ok {
  505. return streamSettings
  506. }
  507. changed := false
  508. for _, cert := range certificates {
  509. c, ok := cert.(map[string]any)
  510. if !ok {
  511. continue
  512. }
  513. // Only strip file paths when inline content is present so that the
  514. // remote Xray still has a valid certificate to use.
  515. hasCertFile := c["certificateFile"] != nil && c["certificateFile"] != ""
  516. hasKeyFile := c["keyFile"] != nil && c["keyFile"] != ""
  517. hasCertInline := isNonEmptySlice(c["certificate"])
  518. hasKeyInline := isNonEmptySlice(c["key"])
  519. if hasCertFile && hasCertInline {
  520. delete(c, "certificateFile")
  521. changed = true
  522. }
  523. if hasKeyFile && hasKeyInline {
  524. delete(c, "keyFile")
  525. changed = true
  526. }
  527. }
  528. if !changed {
  529. return streamSettings
  530. }
  531. out, err := json.Marshal(stream)
  532. if err != nil {
  533. return streamSettings
  534. }
  535. return string(out)
  536. }
  537. // isNonEmptySlice reports whether v is a non-nil, non-empty JSON array value.
  538. func isNonEmptySlice(v any) bool {
  539. s, ok := v.([]any)
  540. return ok && len(s) > 0
  541. }
  542. func (r *Remote) FetchAllClientIps(ctx context.Context) ([]model.InboundClientIps, error) {
  543. env, err := r.do(ctx, http.MethodGet, "panel/api/server/clientIps", nil)
  544. if err != nil {
  545. return nil, err
  546. }
  547. var ips []model.InboundClientIps
  548. if len(env.Obj) > 0 {
  549. if err := json.Unmarshal(env.Obj, &ips); err != nil {
  550. return nil, fmt.Errorf("decode client ips: %w", err)
  551. }
  552. }
  553. return ips, nil
  554. }
  555. func (r *Remote) PushAllClientIps(ctx context.Context, ips []model.InboundClientIps) error {
  556. _, err := r.do(ctx, http.MethodPost, "panel/api/server/clientIps", ips)
  557. return err
  558. }