remote.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. package runtime
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/sha256"
  6. "encoding/hex"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "net"
  12. "net/http"
  13. "net/url"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  19. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  20. "github.com/mhsanaei/3x-ui/v3/internal/util/netsafe"
  21. "github.com/mhsanaei/3x-ui/v3/internal/util/wirecodec"
  22. "github.com/mhsanaei/3x-ui/v3/internal/web/entity"
  23. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  24. )
  25. const remoteHTTPTimeout = 10 * time.Second
  26. // zstdMinBodyBytes is the smallest body worth compressing; below it the framing
  27. // overhead can outweigh the savings.
  28. const zstdMinBodyBytes = 1024
  29. // maxRemoteResponseBytes caps a single node RPC's response body. It bounds the
  30. // wire/decompressed size of one response — the real guard against a broken or
  31. // hostile node streaming an unbounded body. It is NOT a process-wide memory
  32. // bound: concurrent RPCs and the decoded JSON can each exceed it, so
  33. // endpoint-specific caps and a concurrency budget remain follow-ups. Node
  34. // responses (traffic snapshots, client-IP lists, inbound options) are JSON and
  35. // stay well under it.
  36. const maxRemoteResponseBytes = 64 << 20 // 64 MiB
  37. // errBodyDiagBytes bounds how much of a non-OK error body we read for a
  38. // diagnostic snippet (and to let small-error connections be reused) without
  39. // buffering a potentially huge or hostile error payload.
  40. const errBodyDiagBytes = 8 << 10 // 8 KiB
  41. // errRemoteResponseTooLarge is returned when a node response exceeds the cap.
  42. var errRemoteResponseTooLarge = errors.New("remote response exceeds size limit")
  43. // readCappedBody reads all of r but rejects bodies larger than limit, returning
  44. // errRemoteResponseTooLarge. It reads at most limit+1 bytes so a body of exactly
  45. // limit is accepted and the first oversize byte is detected without buffering
  46. // more.
  47. func readCappedBody(r io.Reader, limit int64) ([]byte, error) {
  48. raw, err := io.ReadAll(io.LimitReader(r, limit+1))
  49. if err != nil {
  50. return nil, err
  51. }
  52. if int64(len(raw)) > limit {
  53. return nil, errRemoteResponseTooLarge
  54. }
  55. return raw, nil
  56. }
  57. type envelope struct {
  58. Success bool `json:"success"`
  59. Msg string `json:"msg"`
  60. Obj json.RawMessage `json:"obj"`
  61. }
  62. // remoteAPIError is a node-panel envelope failure (HTTP 200, success=false),
  63. // distinct from transport/HTTP-status errors so callers can trust its message.
  64. type remoteAPIError struct{ msg string }
  65. func (e *remoteAPIError) Error() string { return "remote: " + e.msg }
  66. type Remote struct {
  67. node *model.Node
  68. mu sync.RWMutex
  69. remoteIDByTag map[string]int
  70. adoptedAliases map[string]string
  71. // pushedFP holds the fingerprint of the last inbound wire payload successfully
  72. // pushed, keyed by panel-side tag, so reconcile can skip re-sending an
  73. // unchanged inbound. Guarded by mu; dropped with the Remote on node config change.
  74. pushedFP map[string]string
  75. // supportsZstd is learned from the node's X-3x-Node-Caps response header; once
  76. // seen, config pushes to this node are zstd-compressed. Old nodes never set
  77. // it, so they keep receiving plain bodies (mixed-version safe).
  78. supportsZstd bool
  79. // Per-node client honoring the TLS verify mode, built once and reused; a
  80. // node config change drops the cached Remote so the next one rebuilds it.
  81. clientOnce sync.Once
  82. client *http.Client
  83. clientErr error
  84. egressResolver NodeEgressResolver
  85. }
  86. type RemoteInboundOption struct {
  87. Id int `json:"id"`
  88. Tag string `json:"tag"`
  89. Remark string `json:"remark"`
  90. Listen string `json:"listen"`
  91. Protocol model.Protocol `json:"protocol"`
  92. Port int `json:"port"`
  93. }
  94. func NewRemote(n *model.Node, r NodeEgressResolver) *Remote {
  95. return &Remote{
  96. node: n,
  97. remoteIDByTag: make(map[string]int),
  98. adoptedAliases: make(map[string]string),
  99. pushedFP: make(map[string]string),
  100. egressResolver: r,
  101. }
  102. }
  103. func (r *Remote) Name() string { return "node:" + r.node.Name }
  104. func (r *Remote) nodeSupportsZstd() bool {
  105. r.mu.RLock()
  106. defer r.mu.RUnlock()
  107. return r.supportsZstd
  108. }
  109. // recordCaps learns the node's capabilities from a response header so later
  110. // pushes can use the negotiated envelope.
  111. func (r *Remote) recordCaps(h http.Header) {
  112. if !strings.Contains(h.Get(wirecodec.CapsHeader), wirecodec.CapZstd) {
  113. return
  114. }
  115. r.mu.Lock()
  116. r.supportsZstd = true
  117. r.mu.Unlock()
  118. }
  119. // httpClient lazily builds and caches the per-node client honoring the TLS
  120. // verify mode, so Remote ops don't fall back to system CA on skip/pin (#5264).
  121. func (r *Remote) httpClient() (*http.Client, error) {
  122. r.clientOnce.Do(func() {
  123. proxyURL := ""
  124. if r.node.OutboundTag != "" && r.egressResolver != nil {
  125. proxyURL = r.egressResolver.NodeEgressProxyURL(r.node.Id)
  126. }
  127. r.client, r.clientErr = HTTPClientForNode(r.node, proxyURL)
  128. })
  129. return r.client, r.clientErr
  130. }
  131. func (r *Remote) baseURL() (string, error) {
  132. addr, err := netsafe.NormalizeHost(r.node.Address)
  133. if err != nil {
  134. return "", err
  135. }
  136. scheme := r.node.Scheme
  137. if scheme != "http" && scheme != "https" {
  138. scheme = "https"
  139. }
  140. if r.node.Port <= 0 || r.node.Port > 65535 {
  141. return "", fmt.Errorf("invalid node port %d", r.node.Port)
  142. }
  143. bp := r.node.BasePath
  144. if !strings.HasSuffix(bp, "/") {
  145. bp += "/"
  146. }
  147. u := &url.URL{
  148. Scheme: scheme,
  149. Host: net.JoinHostPort(addr, strconv.Itoa(r.node.Port)),
  150. Path: bp,
  151. }
  152. return u.String(), nil
  153. }
  154. func (r *Remote) do(ctx context.Context, method, path string, body any) (*envelope, error) {
  155. // mtls nodes authenticate via the client certificate, so a bearer token is
  156. // optional for them; every other mode still requires one.
  157. if r.node.ApiToken == "" && r.node.TlsVerifyMode != "mtls" {
  158. return nil, errors.New("node has no API token configured")
  159. }
  160. base, err := r.baseURL()
  161. if err != nil {
  162. return nil, err
  163. }
  164. target := base + strings.TrimPrefix(path, "/")
  165. var (
  166. bodyBytes []byte
  167. contentType string
  168. )
  169. switch b := body.(type) {
  170. case nil:
  171. case url.Values:
  172. bodyBytes = []byte(b.Encode())
  173. contentType = "application/x-www-form-urlencoded"
  174. default:
  175. buf, jerr := json.Marshal(b)
  176. if jerr != nil {
  177. return nil, fmt.Errorf("marshal body: %w", jerr)
  178. }
  179. bodyBytes = buf
  180. contentType = "application/json"
  181. }
  182. // Attach the integrity hash of the uncompressed body unconditionally (a new
  183. // node verifies it, an old one ignores it), and zstd-compress only when the
  184. // node advertised support and the body is worth it.
  185. var (
  186. reqBody io.Reader
  187. hashHex string
  188. zstdEncoded bool
  189. )
  190. if bodyBytes != nil {
  191. hashHex = wirecodec.Sha256Hex(bodyBytes)
  192. if len(bodyBytes) >= zstdMinBodyBytes && r.nodeSupportsZstd() {
  193. bodyBytes = wirecodec.Compress(bodyBytes)
  194. zstdEncoded = true
  195. }
  196. reqBody = bytes.NewReader(bodyBytes)
  197. }
  198. cctx, cancel := context.WithTimeout(netsafe.ContextWithAllowPrivate(ctx, r.node.AllowPrivateAddress), remoteHTTPTimeout)
  199. defer cancel()
  200. req, err := http.NewRequestWithContext(cctx, method, target, reqBody)
  201. if err != nil {
  202. return nil, err
  203. }
  204. if r.node.ApiToken != "" {
  205. req.Header.Set("Authorization", "Bearer "+r.node.ApiToken)
  206. }
  207. req.Header.Set("Accept", "application/json")
  208. if contentType != "" {
  209. req.Header.Set("Content-Type", contentType)
  210. }
  211. if hashHex != "" {
  212. req.Header.Set(wirecodec.HashHeader, hashHex)
  213. }
  214. if zstdEncoded {
  215. req.Header.Set("Content-Encoding", wirecodec.EncodingZstd)
  216. }
  217. client, err := r.httpClient()
  218. if err != nil {
  219. return nil, err
  220. }
  221. resp, err := client.Do(req)
  222. if err != nil {
  223. return nil, fmt.Errorf("%s %s: %w", method, path, err)
  224. }
  225. defer resp.Body.Close()
  226. r.recordCaps(resp.Header)
  227. // Validate status before reading a success payload: a non-OK response's
  228. // body is never used beyond a short diagnostic, so don't let a node force us
  229. // to buffer a large body just to return an HTTP error.
  230. if resp.StatusCode != http.StatusOK {
  231. snippet, _ := io.ReadAll(io.LimitReader(resp.Body, errBodyDiagBytes))
  232. if msg := bytes.TrimSpace(snippet); len(msg) > 0 {
  233. // %q quotes/escapes the untrusted node body so control characters or
  234. // newlines in it can't garble or inject into the error/log output.
  235. return nil, fmt.Errorf("%s %s: HTTP %d: %q", method, path, resp.StatusCode, msg)
  236. }
  237. return nil, fmt.Errorf("%s %s: HTTP %d", method, path, resp.StatusCode)
  238. }
  239. // Fast-fail on an honestly-declared oversize body; the LimitReader below is
  240. // the real guard since Content-Length is untrusted, may be absent, or is -1
  241. // under transparent decompression.
  242. if resp.ContentLength > maxRemoteResponseBytes {
  243. return nil, fmt.Errorf("%s %s: %w (content-length %d, cap %d)", method, path, errRemoteResponseTooLarge, resp.ContentLength, maxRemoteResponseBytes)
  244. }
  245. raw, err := readCappedBody(resp.Body, maxRemoteResponseBytes)
  246. if err != nil {
  247. if errors.Is(err, errRemoteResponseTooLarge) {
  248. return nil, fmt.Errorf("%s %s: %w (cap %d bytes)", method, path, err, maxRemoteResponseBytes)
  249. }
  250. return nil, fmt.Errorf("read body: %w", err)
  251. }
  252. var env envelope
  253. if err := json.Unmarshal(raw, &env); err != nil {
  254. return nil, fmt.Errorf("decode envelope: %w", err)
  255. }
  256. if !env.Success {
  257. return &env, &remoteAPIError{msg: env.Msg}
  258. }
  259. return &env, nil
  260. }
  261. func (r *Remote) resolveRemoteID(ctx context.Context, tag string) (int, error) {
  262. if id, ok := r.cacheGetTag(tag); ok {
  263. return id, nil
  264. }
  265. if err := r.refreshRemoteIDs(ctx); err != nil {
  266. return 0, err
  267. }
  268. if id, ok := r.cacheGetTag(tag); ok {
  269. return id, nil
  270. }
  271. return 0, fmt.Errorf("remote inbound with tag %q not found on node %s", tag, r.node.Name)
  272. }
  273. // nodeInboundTagPrefix is the central-panel alias for an inbound on nodeID.
  274. // Kept in sync with service.nodeTagPrefix (port_conflict.go); duplicated here
  275. // so runtime does not import service.
  276. func nodeInboundTagPrefix(nodeID int) string {
  277. return fmt.Sprintf("n%d-", nodeID)
  278. }
  279. // stripNodeInboundTagPrefix removes the central-only n<id>- prefix before
  280. // pushing an inbound to the node so Xray keeps its original tag and routing.
  281. func stripNodeInboundTagPrefix(nodeID int, tag string) string {
  282. if stripped, ok := strings.CutPrefix(tag, nodeInboundTagPrefix(nodeID)); ok {
  283. return stripped
  284. }
  285. return tag
  286. }
  287. // cacheGetTag looks up a remote inbound id by tag, tolerating an n<id>- prefix
  288. // that lives on only one of the two panels: the node may carry the bare tag
  289. // while the central panel stores the prefixed form, or vice versa.
  290. func (r *Remote) cacheGetTag(tag string) (int, bool) {
  291. if id, ok := r.cacheGet(tag); ok {
  292. return id, true
  293. }
  294. prefix := nodeInboundTagPrefix(r.node.Id)
  295. if stripped, found := strings.CutPrefix(tag, prefix); found {
  296. return r.cacheGet(stripped)
  297. }
  298. return r.cacheGet(prefix + tag)
  299. }
  300. func (r *Remote) cacheGet(tag string) (int, bool) {
  301. r.mu.RLock()
  302. defer r.mu.RUnlock()
  303. id, ok := r.remoteIDByTag[tag]
  304. return id, ok
  305. }
  306. func (r *Remote) cacheSet(tag string, id int) {
  307. r.mu.Lock()
  308. defer r.mu.Unlock()
  309. r.remoteIDByTag[tag] = id
  310. }
  311. func (r *Remote) cacheDel(tag string) {
  312. r.mu.Lock()
  313. defer r.mu.Unlock()
  314. delete(r.remoteIDByTag, tag)
  315. delete(r.pushedFP, tag)
  316. }
  317. func (r *Remote) ListRemoteTags(ctx context.Context) ([]string, error) {
  318. if err := r.refreshRemoteIDs(ctx); err != nil {
  319. return nil, err
  320. }
  321. r.mu.RLock()
  322. defer r.mu.RUnlock()
  323. tags := make([]string, 0, len(r.remoteIDByTag))
  324. for tag := range r.remoteIDByTag {
  325. tags = append(tags, tag)
  326. }
  327. return tags, nil
  328. }
  329. func (r *Remote) ListInboundOptions(ctx context.Context) ([]RemoteInboundOption, error) {
  330. env, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil)
  331. if err != nil {
  332. return nil, err
  333. }
  334. var list []RemoteInboundOption
  335. if err := json.Unmarshal(env.Obj, &list); err != nil {
  336. return nil, fmt.Errorf("decode inbound list: %w", err)
  337. }
  338. return list, nil
  339. }
  340. func (r *Remote) refreshRemoteIDs(ctx context.Context) error {
  341. env, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil)
  342. if err != nil {
  343. return err
  344. }
  345. var list []struct {
  346. Id int `json:"id"`
  347. Tag string `json:"tag"`
  348. }
  349. if err := json.Unmarshal(env.Obj, &list); err != nil {
  350. return fmt.Errorf("decode inbound list: %w", err)
  351. }
  352. next := make(map[string]int, len(list))
  353. for _, ib := range list {
  354. if ib.Tag == "" {
  355. continue
  356. }
  357. next[ib.Tag] = ib.Id
  358. }
  359. r.mu.Lock()
  360. r.remoteIDByTag = next
  361. r.mu.Unlock()
  362. return nil
  363. }
  364. func (r *Remote) AddInbound(ctx context.Context, ib *model.Inbound) error {
  365. payload := wireInbound(ib, r.node.Id)
  366. env, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/add", payload)
  367. if err != nil {
  368. return err
  369. }
  370. var created struct {
  371. Id int `json:"id"`
  372. Tag string `json:"tag"`
  373. }
  374. if len(env.Obj) > 0 {
  375. if err := json.Unmarshal(env.Obj, &created); err == nil && created.Id > 0 && created.Tag != "" {
  376. r.cacheSet(created.Tag, created.Id)
  377. }
  378. }
  379. r.recordPushedInbound(ib)
  380. return nil
  381. }
  382. func (r *Remote) DelInbound(ctx context.Context, ib *model.Inbound) error {
  383. id, err := r.resolveRemoteID(ctx, ib.Tag)
  384. if err != nil {
  385. logger.Warning("remote DelInbound: tag", ib.Tag, "not found on", r.node.Name)
  386. return nil
  387. }
  388. if _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/del/"+strconv.Itoa(id), nil); err != nil {
  389. return err
  390. }
  391. r.cacheDel(ib.Tag)
  392. return nil
  393. }
  394. func (r *Remote) UpdateInbound(ctx context.Context, oldIb, newIb *model.Inbound) error {
  395. id, err := r.resolveRemoteID(ctx, oldIb.Tag)
  396. if err != nil {
  397. return r.AddInbound(ctx, newIb)
  398. }
  399. payload := wireInbound(newIb, r.node.Id)
  400. if _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/update/"+strconv.Itoa(id), payload); err != nil {
  401. return err
  402. }
  403. if oldIb.Tag != newIb.Tag {
  404. r.cacheDel(oldIb.Tag)
  405. }
  406. r.cacheSet(newIb.Tag, id)
  407. r.recordPushedInbound(newIb)
  408. return nil
  409. }
  410. // ReconcileInbound pushes ib only when its wire payload differs from the last
  411. // successful push, or when the node no longer reports the tag (existsOnNode
  412. // false) — a node that dropped/restarted must still be re-seeded. Returns
  413. // whether a push actually happened. This turns a full-fleet reconcile from "send
  414. // every inbound's full settings" into "send only what changed".
  415. func (r *Remote) ReconcileInbound(ctx context.Context, ib *model.Inbound, existsOnNode bool) (bool, error) {
  416. fp := wireFingerprint(wireInbound(ib, r.node.Id))
  417. if existsOnNode {
  418. r.mu.RLock()
  419. prev, ok := r.pushedFP[ib.Tag]
  420. r.mu.RUnlock()
  421. if ok && prev == fp {
  422. return false, nil
  423. }
  424. }
  425. if err := r.UpdateInbound(ctx, ib, ib); err != nil {
  426. return false, err
  427. }
  428. return true, nil
  429. }
  430. // recordPushedInbound stamps the fingerprint after a full-payload push — the
  431. // only operation that proves the node holds the entire wire payload.
  432. func (r *Remote) recordPushedInbound(ib *model.Inbound) {
  433. fp := wireFingerprint(wireInbound(ib, r.node.Id))
  434. r.mu.Lock()
  435. r.pushedFP[ib.Tag] = fp
  436. r.mu.Unlock()
  437. }
  438. // RecordAdoptedInbound stamps the exact payload fingerprint after the master
  439. // adopts a node's settings serialization.
  440. func (r *Remote) RecordAdoptedInbound(ib *model.Inbound) {
  441. r.recordPushedInbound(ib)
  442. }
  443. // AdoptInboundAlias records a deployed alias without mutating either panel.
  444. // The runtime association is rediscovered after a master restart.
  445. func (r *Remote) AdoptInboundAlias(ib *model.Inbound, remote RemoteInboundOption) {
  446. r.mu.Lock()
  447. r.remoteIDByTag[remote.Tag] = remote.Id
  448. r.remoteIDByTag[ib.Tag] = remote.Id
  449. r.adoptedAliases[ib.Tag] = remote.Tag
  450. r.pushedFP[ib.Tag] = wireFingerprint(wireInbound(ib, r.node.Id))
  451. r.mu.Unlock()
  452. }
  453. func (r *Remote) AdoptedInboundAliases() []string {
  454. r.mu.RLock()
  455. defer r.mu.RUnlock()
  456. aliases := make([]string, 0, len(r.adoptedAliases))
  457. for _, alias := range r.adoptedAliases {
  458. aliases = append(aliases, alias)
  459. }
  460. return aliases
  461. }
  462. // AdvancePushedInbound moves the reconcile-skip fingerprint from an inbound's
  463. // pre-edit payload to its post-edit payload once every per-client push for the
  464. // edit succeeded. It advances only when the recorded fingerprint proves the
  465. // node held the exact pre-edit state; otherwise the stale fingerprint stays and
  466. // the next reconcile re-sends the full inbound.
  467. func (r *Remote) AdvancePushedInbound(prevIb, ib *model.Inbound) {
  468. prevFP := wireFingerprint(wireInbound(prevIb, r.node.Id))
  469. nextFP := wireFingerprint(wireInbound(ib, r.node.Id))
  470. r.mu.Lock()
  471. if r.pushedFP[ib.Tag] == prevFP {
  472. r.pushedFP[ib.Tag] = nextFP
  473. }
  474. r.mu.Unlock()
  475. }
  476. // wireFingerprint hashes a wire payload so an unchanged inbound is cheap to detect.
  477. func wireFingerprint(v url.Values) string {
  478. sum := sha256.Sum256([]byte(v.Encode()))
  479. return hex.EncodeToString(sum[:])
  480. }
  481. func (r *Remote) AddUser(ctx context.Context, ib *model.Inbound, _ map[string]any) error {
  482. return r.UpdateInbound(ctx, ib, ib)
  483. }
  484. func (r *Remote) RemoveUser(ctx context.Context, ib *model.Inbound, _ string) error {
  485. return r.UpdateInbound(ctx, ib, ib)
  486. }
  487. func (r *Remote) AddClient(ctx context.Context, ib *model.Inbound, client model.Client) error {
  488. id, err := r.resolveRemoteID(ctx, ib.Tag)
  489. if err != nil {
  490. return fmt.Errorf("remote AddClient: resolve tag %q: %w", ib.Tag, err)
  491. }
  492. payload := map[string]any{
  493. "client": client,
  494. "inboundIds": []int{id},
  495. }
  496. if _, err := r.do(ctx, http.MethodPost, "panel/api/clients/add", payload); err != nil {
  497. return err
  498. }
  499. return nil
  500. }
  501. func (r *Remote) DeleteUser(ctx context.Context, ib *model.Inbound, email string) error {
  502. if email == "" {
  503. return nil
  504. }
  505. id, err := r.resolveRemoteID(ctx, ib.Tag)
  506. if err != nil {
  507. // Can't confirm the delete reached the node — surface it so the caller
  508. // marks the node dirty and a reconcile converges, instead of silently
  509. // dropping the delete and letting the next snapshot resurrect the client.
  510. return fmt.Errorf("remote DeleteUser: resolve tag %q: %w", ib.Tag, err)
  511. }
  512. body := map[string]any{"inboundIds": []int{id}}
  513. _, err = r.do(ctx, http.MethodPost,
  514. "panel/api/clients/"+url.PathEscape(email)+"/detach", body)
  515. if err == nil {
  516. return nil
  517. }
  518. var apiErr *remoteAPIError
  519. if errors.As(err, &apiErr) && strings.Contains(strings.ToLower(apiErr.msg), "not found") {
  520. return nil
  521. }
  522. return err
  523. }
  524. func (r *Remote) DeleteClient(ctx context.Context, email string) error {
  525. if email == "" {
  526. return nil
  527. }
  528. _, err := r.do(ctx, http.MethodPost,
  529. "panel/api/clients/del/"+url.PathEscape(email), nil)
  530. if err == nil {
  531. return nil
  532. }
  533. var apiErr *remoteAPIError
  534. if errors.As(err, &apiErr) && strings.Contains(strings.ToLower(apiErr.msg), "not found") {
  535. return nil
  536. }
  537. return err
  538. }
  539. func (r *Remote) UpdateUser(ctx context.Context, ib *model.Inbound, oldEmail string, payload model.Client) error {
  540. if oldEmail == "" {
  541. oldEmail = payload.Email
  542. }
  543. id, err := r.resolveRemoteID(ctx, ib.Tag)
  544. if err != nil {
  545. return err
  546. }
  547. path := "panel/api/clients/update/" + url.PathEscape(oldEmail) +
  548. "?inboundIds=" + strconv.Itoa(id)
  549. if _, err := r.do(ctx, http.MethodPost, path, payload); err != nil {
  550. return err
  551. }
  552. return nil
  553. }
  554. func (r *Remote) RestartXray(ctx context.Context) error {
  555. _, err := r.do(ctx, http.MethodPost, "panel/api/server/restartXrayService", nil)
  556. return err
  557. }
  558. // UpdatePanel asks the node to run its own official self-updater (update.sh)
  559. // and restart onto the latest release. The node returns as soon as the job is
  560. // launched; the new version surfaces on the next heartbeat. When dev is true the
  561. // node is moved to the rolling dev channel instead of the latest stable release.
  562. func (r *Remote) UpdatePanel(ctx context.Context, dev bool) error {
  563. var body any
  564. if dev {
  565. body = url.Values{"dev": {"true"}}
  566. }
  567. _, err := r.do(ctx, http.MethodPost, "panel/api/server/updatePanel", body)
  568. return err
  569. }
  570. // WebCertFiles holds a node's own web TLS certificate and key file paths.
  571. type WebCertFiles struct {
  572. WebCertFile string `json:"webCertFile"`
  573. WebKeyFile string `json:"webKeyFile"`
  574. }
  575. // GetWebCertFiles fetches the node's own web TLS certificate/key file paths so
  576. // the central panel can offer them as the "Set Cert from Panel" default for a
  577. // node-assigned inbound — those paths exist on the node, the central panel's
  578. // don't. See issue #4854.
  579. func (r *Remote) GetWebCertFiles(ctx context.Context) (*WebCertFiles, error) {
  580. env, err := r.do(ctx, http.MethodGet, "panel/api/server/getWebCertFiles", nil)
  581. if err != nil {
  582. return nil, err
  583. }
  584. var files WebCertFiles
  585. if err := json.Unmarshal(env.Obj, &files); err != nil {
  586. return nil, fmt.Errorf("decode web cert files: %w", err)
  587. }
  588. return &files, nil
  589. }
  590. // GetDescendants fetches the node's read-only summaries of the nodes IT
  591. // manages, so this panel can surface them as transitive sub-nodes in a chained
  592. // topology (#4983). Best-effort: an old-build node without the endpoint returns
  593. // an error the caller ignores.
  594. func (r *Remote) GetDescendants(ctx context.Context) ([]model.NodeSummary, error) {
  595. env, err := r.do(ctx, http.MethodGet, "panel/api/server/descendants", nil)
  596. if err != nil {
  597. return nil, err
  598. }
  599. var out []model.NodeSummary
  600. if len(env.Obj) > 0 {
  601. if err := json.Unmarshal(env.Obj, &out); err != nil {
  602. return nil, fmt.Errorf("decode descendants: %w", err)
  603. }
  604. }
  605. return out, nil
  606. }
  607. func (r *Remote) ResetClientTraffic(ctx context.Context, _ *model.Inbound, email string) error {
  608. _, err := r.do(ctx, http.MethodPost,
  609. "panel/api/clients/resetTraffic/"+url.PathEscape(email), nil)
  610. return err
  611. }
  612. func (r *Remote) ResetAllTraffics(ctx context.Context) error {
  613. _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/resetAllTraffics", nil)
  614. return err
  615. }
  616. func (r *Remote) ResetInboundTraffic(ctx context.Context, ib *model.Inbound) error {
  617. _, err := r.do(ctx, http.MethodPost, fmt.Sprintf("panel/api/inbounds/%d/resetTraffic", ib.Id), nil)
  618. return err
  619. }
  620. type TrafficSnapshot struct {
  621. Inbounds []*model.Inbound
  622. OnlineEmails []string
  623. ManagedAliases []string
  624. // OnlineTree is the node's GUID-keyed online subtree (its own clients under
  625. // its panelGuid plus every descendant under theirs). Preferred over the flat
  626. // OnlineEmails so the master can attribute deeply nested clients to the real
  627. // node across a chain (#4983). Empty when the node is an old build without
  628. // the per-GUID endpoint — OnlineEmails is the fallback then.
  629. OnlineTree map[string][]string
  630. LastOnlineMap map[string]int64
  631. // HostGroups carries the node's per-inbound host overrides (TLS/SNI/
  632. // fingerprint), fetched only when the snapshot holds a not-yet-adopted tag.
  633. HostGroups []*entity.HostGroup
  634. }
  635. // FetchHostGroups pulls the node's host overrides so a freshly adopted inbound
  636. // keeps its subscription TLS/SNI/fingerprint settings on the master.
  637. func (r *Remote) FetchHostGroups(ctx context.Context) ([]*entity.HostGroup, error) {
  638. env, err := r.do(ctx, http.MethodGet, "panel/api/hosts/list", nil)
  639. if err != nil {
  640. return nil, err
  641. }
  642. var groups []*entity.HostGroup
  643. if len(env.Obj) > 0 {
  644. if err := json.Unmarshal(env.Obj, &groups); err != nil {
  645. return nil, fmt.Errorf("decode host groups: %w", err)
  646. }
  647. }
  648. return groups, nil
  649. }
  650. func (r *Remote) FetchTrafficSnapshot(ctx context.Context) (*TrafficSnapshot, error) {
  651. snap := &TrafficSnapshot{LastOnlineMap: map[string]int64{}}
  652. envList, err := r.do(ctx, http.MethodGet, "panel/api/inbounds/list", nil)
  653. if err != nil {
  654. return nil, err
  655. }
  656. if err := json.Unmarshal(envList.Obj, &snap.Inbounds); err != nil {
  657. return nil, fmt.Errorf("decode inbound list: %w", err)
  658. }
  659. // Prefer the GUID-keyed subtree; fall back to the flat list only when the
  660. // node is an old build without the per-GUID endpoint (#4983).
  661. envTree, err := r.do(ctx, http.MethodPost, "panel/api/clients/onlinesByGuid", nil)
  662. if err == nil && len(envTree.Obj) > 0 {
  663. _ = json.Unmarshal(envTree.Obj, &snap.OnlineTree)
  664. }
  665. if len(snap.OnlineTree) == 0 {
  666. envOnlines, err := r.do(ctx, http.MethodPost, "panel/api/clients/onlines", nil)
  667. if err != nil {
  668. logger.Warning("remote", r.node.Name, "onlines fetch failed:", err)
  669. } else if len(envOnlines.Obj) > 0 {
  670. _ = json.Unmarshal(envOnlines.Obj, &snap.OnlineEmails)
  671. }
  672. }
  673. envLastOnline, err := r.do(ctx, http.MethodPost, "panel/api/clients/lastOnline", nil)
  674. if err != nil {
  675. logger.Warning("remote", r.node.Name, "lastOnline fetch failed:", err)
  676. } else if len(envLastOnline.Obj) > 0 {
  677. _ = json.Unmarshal(envLastOnline.Obj, &snap.LastOnlineMap)
  678. }
  679. return snap, nil
  680. }
  681. // PushGlobalClientTraffics sends this panel's aggregated per-client usage to
  682. // the node, tagged with this panel's GUID so the node keeps one row per
  683. // pushing master. Display/enforcement input on the node only — the node never
  684. // folds these into the counters it reports back, so this panel's (and any
  685. // other master's) delta accounting over the node snapshot stays intact.
  686. func (r *Remote) PushGlobalClientTraffics(ctx context.Context, masterGuid string, traffics []*xray.ClientTraffic) error {
  687. payload := map[string]any{
  688. "masterGuid": masterGuid,
  689. "traffics": traffics,
  690. }
  691. _, err := r.do(ctx, http.MethodPost, "panel/api/inbounds/pushClientTraffics", payload)
  692. return err
  693. }
  694. func wireInbound(ib *model.Inbound, remoteNodeID int) url.Values {
  695. v := url.Values{}
  696. v.Set("total", strconv.FormatInt(ib.Total, 10))
  697. v.Set("remark", ib.Remark)
  698. v.Set("subSortIndex", strconv.Itoa(ib.SubSortIndex))
  699. v.Set("enable", strconv.FormatBool(ib.Enable))
  700. v.Set("expiryTime", strconv.FormatInt(ib.ExpiryTime, 10))
  701. v.Set("listen", ib.Listen)
  702. v.Set("port", strconv.Itoa(ib.Port))
  703. v.Set("protocol", string(ib.Protocol))
  704. v.Set("settings", ib.Settings)
  705. v.Set("streamSettings", sanitizeStreamSettingsForRemote(ib.StreamSettings))
  706. tag := ib.Tag
  707. if remoteNodeID > 0 {
  708. tag = stripNodeInboundTagPrefix(remoteNodeID, tag)
  709. }
  710. v.Set("tag", tag)
  711. v.Set("sniffing", ib.Sniffing)
  712. shareAddrStrategy := strings.TrimSpace(ib.ShareAddrStrategy)
  713. switch shareAddrStrategy {
  714. case "listen", "custom":
  715. default:
  716. shareAddrStrategy = "node"
  717. }
  718. v.Set("shareAddrStrategy", shareAddrStrategy)
  719. v.Set("shareAddr", ib.ShareAddr)
  720. if ib.TrafficReset != "" {
  721. v.Set("trafficReset", ib.TrafficReset)
  722. }
  723. if ib.TrafficResetDay > 0 {
  724. v.Set("trafficResetDay", strconv.Itoa(ib.TrafficResetDay))
  725. }
  726. return v
  727. }
  728. // sanitizeStreamSettingsForRemote strips file-based TLS certificate paths
  729. // from the StreamSettings before sending to a remote node, but ONLY when
  730. // inline certificate content (certificate / key) is also present in the same
  731. // entry. In that case the file paths are redundant and stripping them avoids
  732. // confusion when the central panel's local paths don't exist on the remote.
  733. //
  734. // When a certificate entry contains ONLY file paths (no inline content) the
  735. // paths are left untouched: the user explicitly entered paths that exist on
  736. // the remote node's filesystem, and removing them would leave Xray with TLS
  737. // configured but no certificate, causing Xray to crash on the remote node.
  738. func sanitizeStreamSettingsForRemote(streamSettings string) string {
  739. if streamSettings == "" {
  740. return streamSettings
  741. }
  742. var stream map[string]any
  743. if err := json.Unmarshal([]byte(streamSettings), &stream); err != nil {
  744. return streamSettings
  745. }
  746. tlsSettings, ok := stream["tlsSettings"].(map[string]any)
  747. if !ok {
  748. return streamSettings
  749. }
  750. certificates, ok := tlsSettings["certificates"].([]any)
  751. if !ok {
  752. return streamSettings
  753. }
  754. changed := false
  755. for _, cert := range certificates {
  756. c, ok := cert.(map[string]any)
  757. if !ok {
  758. continue
  759. }
  760. // Only strip file paths when inline content is present so that the
  761. // remote Xray still has a valid certificate to use.
  762. hasCertFile := c["certificateFile"] != nil && c["certificateFile"] != ""
  763. hasKeyFile := c["keyFile"] != nil && c["keyFile"] != ""
  764. hasCertInline := isNonEmptySlice(c["certificate"])
  765. hasKeyInline := isNonEmptySlice(c["key"])
  766. if hasCertFile && hasCertInline {
  767. delete(c, "certificateFile")
  768. changed = true
  769. }
  770. if hasKeyFile && hasKeyInline {
  771. delete(c, "keyFile")
  772. changed = true
  773. }
  774. }
  775. if !changed {
  776. return streamSettings
  777. }
  778. out, err := json.Marshal(stream)
  779. if err != nil {
  780. return streamSettings
  781. }
  782. return string(out)
  783. }
  784. // isNonEmptySlice reports whether v is a non-nil, non-empty JSON array value.
  785. func isNonEmptySlice(v any) bool {
  786. s, ok := v.([]any)
  787. return ok && len(s) > 0
  788. }
  789. func (r *Remote) FetchAllClientIps(ctx context.Context) ([]model.InboundClientIps, error) {
  790. env, err := r.do(ctx, http.MethodGet, "panel/api/server/clientIps", nil)
  791. if err != nil {
  792. return nil, err
  793. }
  794. var ips []model.InboundClientIps
  795. if len(env.Obj) > 0 {
  796. if err := json.Unmarshal(env.Obj, &ips); err != nil {
  797. return nil, fmt.Errorf("decode client ips: %w", err)
  798. }
  799. }
  800. return ips, nil
  801. }
  802. func (r *Remote) PushAllClientIps(ctx context.Context, ips []model.InboundClientIps) error {
  803. _, err := r.do(ctx, http.MethodPost, "panel/api/server/clientIps", ips)
  804. return err
  805. }
  806. // FetchClientIpsByGuid pulls the node's per-node IP attribution subtree
  807. // (guid -> email -> observed IPs). Unlike FetchAllClientIps (the flat union the
  808. // master also pushes back), this preserves which physical node each IP is on.
  809. // Returns an empty map for older nodes that lack the endpoint.
  810. func (r *Remote) FetchClientIpsByGuid(ctx context.Context) (map[string]map[string][]model.ClientIpEntry, error) {
  811. env, err := r.do(ctx, http.MethodPost, "panel/api/clients/clientIpsByGuid", nil)
  812. if err != nil {
  813. return nil, err
  814. }
  815. out := map[string]map[string][]model.ClientIpEntry{}
  816. if len(env.Obj) > 0 {
  817. if err := json.Unmarshal(env.Obj, &out); err != nil {
  818. return nil, fmt.Errorf("decode client ips by guid: %w", err)
  819. }
  820. }
  821. return out, nil
  822. }