remote.go 30 KB

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