remote.go 20 KB

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