node.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. package service
  2. import (
  3. "context"
  4. "crypto/sha256"
  5. "crypto/tls"
  6. "encoding/base64"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "net"
  11. "net/http"
  12. "net/url"
  13. "slices"
  14. "strconv"
  15. "strings"
  16. "sync"
  17. "time"
  18. "github.com/mhsanaei/3x-ui/v3/internal/database"
  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/common"
  22. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  23. "github.com/mhsanaei/3x-ui/v3/internal/util/netsafe"
  24. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  25. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  26. "gorm.io/gorm"
  27. )
  28. type HeartbeatPatch struct {
  29. Status string
  30. LastHeartbeat int64
  31. LatencyMs int
  32. XrayVersion string
  33. PanelVersion string
  34. Guid string
  35. CpuPct float64
  36. MemPct float64
  37. UptimeSecs uint64
  38. // NetUp/NetDown are the node's current interface throughput (bytes/sec),
  39. // summed over non-virtual interfaces, read from its status response.
  40. NetUp uint64
  41. NetDown uint64
  42. LastError string
  43. // XrayState and XrayError come from the remote /panel/api/server/status when the
  44. // panel API is reachable. They allow distinguishing panel connectivity from
  45. // Xray core health on the node.
  46. XrayState string
  47. XrayError string
  48. }
  49. type NodeService struct{}
  50. // FetchCertFingerprint connects to the node over HTTPS without verifying the
  51. // certificate and returns the leaf certificate's SHA-256 as base64, so the UI
  52. // can offer a "fetch and pin current certificate" action.
  53. func (s *NodeService) FetchCertFingerprint(ctx context.Context, n *model.Node) (string, error) {
  54. addr, err := netsafe.NormalizeHost(n.Address)
  55. if err != nil {
  56. return "", err
  57. }
  58. scheme := n.Scheme
  59. if scheme != "http" && scheme != "https" {
  60. scheme = "https"
  61. }
  62. if scheme != "https" {
  63. return "", common.NewError("certificate pinning is only available for https nodes")
  64. }
  65. if n.Port <= 0 || n.Port > 65535 {
  66. return "", common.NewError("node port must be 1-65535")
  67. }
  68. probeURL := &url.URL{
  69. Scheme: scheme,
  70. Host: net.JoinHostPort(addr, strconv.Itoa(n.Port)),
  71. Path: normalizeBasePath(n.BasePath) + "panel/api/server/status",
  72. }
  73. req, err := http.NewRequestWithContext(
  74. netsafe.ContextWithAllowPrivate(ctx, n.AllowPrivateAddress),
  75. http.MethodGet, probeURL.String(), nil)
  76. if err != nil {
  77. return "", err
  78. }
  79. client := &http.Client{
  80. Transport: &http.Transport{
  81. DialContext: netsafe.SSRFGuardedDialContext,
  82. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // lgtm[go/disabled-certificate-check]
  83. },
  84. }
  85. resp, err := client.Do(req)
  86. if err != nil {
  87. return "", err
  88. }
  89. defer resp.Body.Close()
  90. if resp.TLS == nil || len(resp.TLS.PeerCertificates) == 0 {
  91. return "", common.NewError("node did not present a TLS certificate")
  92. }
  93. sum := sha256.Sum256(resp.TLS.PeerCertificates[0].Raw)
  94. return base64.StdEncoding.EncodeToString(sum[:]), nil
  95. }
  96. func (s *NodeService) GetAll() ([]*model.Node, error) {
  97. db := database.GetDB()
  98. var nodes []*model.Node
  99. err := db.Model(model.Node{}).Order("id asc").Find(&nodes).Error
  100. if err != nil || len(nodes) == 0 {
  101. return nodes, err
  102. }
  103. type inboundRow struct {
  104. Id int
  105. NodeID int `gorm:"column:node_id"`
  106. }
  107. var inboundRows []inboundRow
  108. if err := db.Table("inbounds").
  109. Select("id, node_id").
  110. Where("node_id IS NOT NULL").
  111. Scan(&inboundRows).Error; err != nil {
  112. return nodes, nil
  113. }
  114. if len(inboundRows) == 0 {
  115. return nodes, nil
  116. }
  117. inboundsByNode := make(map[int][]int, len(nodes))
  118. for _, row := range inboundRows {
  119. inboundsByNode[row.NodeID] = append(inboundsByNode[row.NodeID], row.Id)
  120. }
  121. type clientCountRow struct {
  122. NodeID int `gorm:"column:node_id"`
  123. Count int `gorm:"column:count"`
  124. }
  125. var clientCounts []clientCountRow
  126. if err := db.Raw(`
  127. SELECT inbounds.node_id AS node_id, COUNT(DISTINCT client_inbounds.client_id) AS count
  128. FROM inbounds
  129. JOIN client_inbounds ON client_inbounds.inbound_id = inbounds.id
  130. WHERE inbounds.node_id IS NOT NULL
  131. GROUP BY inbounds.node_id
  132. `).Scan(&clientCounts).Error; err == nil {
  133. for _, row := range clientCounts {
  134. for _, n := range nodes {
  135. if n.Id == row.NodeID {
  136. n.ClientCount = row.Count
  137. break
  138. }
  139. }
  140. }
  141. }
  142. depletedByNode := make(map[int]int)
  143. disabledByNode := make(map[int]int)
  144. activeByNode := make(map[int]int)
  145. statuses, _ := s.nodeClientStatuses()
  146. seen := make(map[int]map[int]struct{}, len(nodes))
  147. for _, st := range statuses {
  148. clientsSeen := seen[st.NodeID]
  149. if clientsSeen == nil {
  150. clientsSeen = make(map[int]struct{})
  151. seen[st.NodeID] = clientsSeen
  152. }
  153. if _, dup := clientsSeen[st.ClientID]; dup {
  154. // A client attached to several inbounds of one node counts once,
  155. // matching the distinct ClientCount above.
  156. continue
  157. }
  158. clientsSeen[st.ClientID] = struct{}{}
  159. switch {
  160. case st.Depleted:
  161. depletedByNode[st.NodeID]++
  162. case st.Disabled:
  163. disabledByNode[st.NodeID]++
  164. default:
  165. activeByNode[st.NodeID]++
  166. }
  167. }
  168. onlineByGuid := s.onlineEmailsByGuid()
  169. selfGuid, _ := (&SettingService{}).GetPanelGuid()
  170. ambiguous := ambiguousNodeGuids(nodes, selfGuid)
  171. for _, n := range nodes {
  172. n.InboundCount = len(inboundsByNode[n.Id])
  173. n.DepletedCount = depletedByNode[n.Id]
  174. n.DisabledCount = disabledByNode[n.Id]
  175. n.ActiveCount = activeByNode[n.Id]
  176. // Online is attributed to the node that physically hosts the client
  177. // (by GUID): a client on a sub-node counts under the sub-node, not
  178. // the intermediate node it syncs through (#4983).
  179. n.OnlineCount = len(onlineByGuid[effectiveNodeGuid(n, ambiguous)])
  180. }
  181. return nodes, nil
  182. }
  183. // nodeClientStatus is one node-hosted client's classification, carrying enough
  184. // identity for callers to bucket it by node id or by attribution GUID.
  185. type nodeClientStatus struct {
  186. InboundID int
  187. NodeID int
  188. ClientID int
  189. Depleted bool
  190. Disabled bool
  191. }
  192. // nodeClientStatuses classifies every client attached to a node-hosted inbound as
  193. // depleted / disabled / active, matching client_traffics by EMAIL rather than by
  194. // inbound_id. client_traffics.inbound_id goes stale after an inbound is
  195. // delete+recreated, so filtering by it silently drops most rows; the
  196. // client_inbounds -> clients join is the reliable client set and the email join
  197. // pulls each client's live counters. Precedence matches the inbound page:
  198. // depleted (expired/exhausted) wins over disabled.
  199. func (s *NodeService) nodeClientStatuses() ([]nodeClientStatus, error) {
  200. type row struct {
  201. InboundID int `gorm:"column:inbound_id"`
  202. NodeID int `gorm:"column:node_id"`
  203. ClientID int `gorm:"column:client_id"`
  204. Enable bool `gorm:"column:enable"`
  205. Total int64 `gorm:"column:total"`
  206. Up int64 `gorm:"column:up"`
  207. Down int64 `gorm:"column:down"`
  208. ExpiryTime int64 `gorm:"column:expiry_time"`
  209. }
  210. var rows []row
  211. if err := database.GetDB().Table("inbounds").
  212. Select("inbounds.id AS inbound_id, inbounds.node_id AS node_id, clients.id AS client_id, " +
  213. "clients.enable AS enable, ct.total AS total, ct.up AS up, ct.down AS down, ct.expiry_time AS expiry_time").
  214. Joins("JOIN client_inbounds ON client_inbounds.inbound_id = inbounds.id").
  215. Joins("JOIN clients ON clients.id = client_inbounds.client_id").
  216. Joins("LEFT JOIN client_traffics ct ON ct.email = clients.email").
  217. Where("inbounds.node_id IS NOT NULL").
  218. Scan(&rows).Error; err != nil {
  219. return nil, err
  220. }
  221. now := time.Now().UnixMilli()
  222. out := make([]nodeClientStatus, 0, len(rows))
  223. for _, r := range rows {
  224. st := nodeClientStatus{InboundID: r.InboundID, NodeID: r.NodeID, ClientID: r.ClientID}
  225. expired := r.ExpiryTime > 0 && r.ExpiryTime <= now
  226. exhausted := r.Total > 0 && r.Up+r.Down >= r.Total
  227. switch {
  228. case expired || exhausted:
  229. st.Depleted = true
  230. case !r.Enable:
  231. st.Disabled = true
  232. }
  233. out = append(out, st)
  234. }
  235. return out, nil
  236. }
  237. func (s *NodeService) onlineEmailsByGuid() map[string]map[string]struct{} {
  238. svc := InboundService{}
  239. byGuid := svc.GetOnlineClientsByGuid()
  240. out := make(map[string]map[string]struct{}, len(byGuid))
  241. for guid, emails := range byGuid {
  242. set := make(map[string]struct{}, len(emails))
  243. for _, email := range emails {
  244. set[email] = struct{}{}
  245. }
  246. out[guid] = set
  247. }
  248. return out
  249. }
  250. // effectiveNodeGuid is a node's stable online/inbound attribution key: its
  251. // reported panelGuid, or a master-local synthetic node-id fallback when the node
  252. // has no GUID yet (old build) or its GUID is ambiguous. ambiguous comes from
  253. // ambiguousNodeGuids.
  254. func effectiveNodeGuid(n *model.Node, ambiguous map[string]struct{}) string {
  255. if n.Guid == "" {
  256. return synthNodeGuid(n.Id)
  257. }
  258. if n.Id > 0 {
  259. if _, bad := ambiguous[n.Guid]; bad {
  260. return synthNodeGuid(n.Id)
  261. }
  262. }
  263. return n.Guid
  264. }
  265. // ambiguousNodeGuids returns the panelGuids a node must not be attributed under
  266. // directly, because doing so would merge two distinct identities: a GUID
  267. // reported by more than one of this master's direct nodes (cloned node servers
  268. // ship the same panelGuid in their copied settings), or a GUID equal to the
  269. // master's own panelGuid (a node cloned from the master). A node holding such a
  270. // GUID falls back to its node-unique synthNodeGuid. Transitive sub-nodes (Id 0)
  271. // carry distinct descendant GUIDs by construction and are excluded.
  272. func ambiguousNodeGuids(nodes []*model.Node, selfGuid string) map[string]struct{} {
  273. counts := make(map[string]int, len(nodes))
  274. for _, n := range nodes {
  275. if n.Id > 0 && n.Guid != "" {
  276. counts[n.Guid]++
  277. }
  278. }
  279. ambiguous := make(map[string]struct{})
  280. for guid, c := range counts {
  281. if c > 1 {
  282. ambiguous[guid] = struct{}{}
  283. }
  284. }
  285. if selfGuid != "" {
  286. if _, ok := counts[selfGuid]; ok {
  287. ambiguous[selfGuid] = struct{}{}
  288. }
  289. }
  290. return ambiguous
  291. }
  292. // effectiveNodeKey returns one node's attribution key without a preloaded node
  293. // list — its panelGuid when that GUID uniquely identifies it among the master's
  294. // nodes and differs from the master's own, otherwise its node-unique
  295. // synthNodeGuid. Same rule as effectiveNodeGuid + ambiguousNodeGuids, for the
  296. // write paths that handle a single node (online tree, IP attribution).
  297. func effectiveNodeKey(node *model.Node) string {
  298. if node == nil {
  299. return ""
  300. }
  301. if node.Guid == "" {
  302. return synthNodeGuid(node.Id)
  303. }
  304. var sameGuid int64
  305. database.GetDB().Model(&model.Node{}).Where("guid = ?", node.Guid).Count(&sameGuid)
  306. masterGuid, _ := (&SettingService{}).GetPanelGuid()
  307. if sameGuid > 1 || node.Guid == masterGuid {
  308. return synthNodeGuid(node.Id)
  309. }
  310. return node.Guid
  311. }
  312. func (s *NodeService) GetById(id int) (*model.Node, error) {
  313. db := database.GetDB()
  314. n := &model.Node{}
  315. if err := db.Model(model.Node{}).Where("id = ?", id).First(n).Error; err != nil {
  316. return nil, err
  317. }
  318. return n, nil
  319. }
  320. // NodeExists reports whether a node with the given id exists on this panel.
  321. // Used to drop stale, cross-panel node references on inbound import. A Count
  322. // query distinguishes "no such node" (count 0, no error) from a real DB error.
  323. func (s *NodeService) NodeExists(id int) (bool, error) {
  324. if id <= 0 {
  325. return false, nil
  326. }
  327. var count int64
  328. if err := database.GetDB().Model(model.Node{}).Where("id = ?", id).Count(&count).Error; err != nil {
  329. return false, err
  330. }
  331. return count > 0, nil
  332. }
  333. func normalizeBasePath(p string) string {
  334. p = strings.TrimSpace(p)
  335. if p == "" {
  336. return "/"
  337. }
  338. if !strings.HasPrefix(p, "/") {
  339. p = "/" + p
  340. }
  341. if !strings.HasSuffix(p, "/") {
  342. p = p + "/"
  343. }
  344. return p
  345. }
  346. func (s *NodeService) normalize(n *model.Node) error {
  347. n.Name = strings.TrimSpace(n.Name)
  348. n.ApiToken = strings.TrimSpace(n.ApiToken)
  349. if n.Name == "" {
  350. return common.NewError("node name is required")
  351. }
  352. addr, err := netsafe.NormalizeHost(n.Address)
  353. if err != nil {
  354. return common.NewError(err.Error())
  355. }
  356. n.Address = addr
  357. if n.Port <= 0 || n.Port > 65535 {
  358. return common.NewError("node port must be 1-65535")
  359. }
  360. if n.Scheme != "http" && n.Scheme != "https" {
  361. n.Scheme = "https"
  362. }
  363. if n.TlsVerifyMode != "skip" && n.TlsVerifyMode != "pin" && n.TlsVerifyMode != "mtls" {
  364. n.TlsVerifyMode = "verify"
  365. }
  366. if n.TlsVerifyMode == "mtls" && n.Scheme != "https" {
  367. return common.NewError("mtls requires the node scheme to be https")
  368. }
  369. n.PinnedCertSha256 = strings.TrimSpace(n.PinnedCertSha256)
  370. if n.InboundSyncMode != "selected" {
  371. n.InboundSyncMode = "all"
  372. n.InboundTags = nil
  373. } else {
  374. seen := make(map[string]struct{}, len(n.InboundTags))
  375. tags := make([]string, 0, len(n.InboundTags))
  376. for _, tag := range n.InboundTags {
  377. tag = strings.TrimSpace(tag)
  378. if tag == "" {
  379. continue
  380. }
  381. if _, ok := seen[tag]; ok {
  382. continue
  383. }
  384. seen[tag] = struct{}{}
  385. tags = append(tags, tag)
  386. }
  387. n.InboundTags = tags
  388. }
  389. if n.TlsVerifyMode == "pin" {
  390. if _, err := runtime.DecodeCertPin(n.PinnedCertSha256); err != nil {
  391. return common.NewError(err.Error())
  392. }
  393. }
  394. n.BasePath = normalizeBasePath(n.BasePath)
  395. return nil
  396. }
  397. func (s *NodeService) Create(n *model.Node) error {
  398. if err := s.normalize(n); err != nil {
  399. return err
  400. }
  401. db := database.GetDB()
  402. return db.Create(n).Error
  403. }
  404. func (s *NodeService) Update(id int, in *model.Node) error {
  405. if err := s.normalize(in); err != nil {
  406. return err
  407. }
  408. inboundTagsJSON, err := json.Marshal(in.InboundTags)
  409. if err != nil {
  410. return err
  411. }
  412. db := database.GetDB()
  413. existing := &model.Node{}
  414. if err := db.Where("id = ?", id).First(existing).Error; err != nil {
  415. return err
  416. }
  417. updates := map[string]any{
  418. "name": in.Name,
  419. "remark": in.Remark,
  420. "scheme": in.Scheme,
  421. "address": in.Address,
  422. "port": in.Port,
  423. "base_path": in.BasePath,
  424. "api_token": in.ApiToken,
  425. "enable": in.Enable,
  426. "allow_private_address": in.AllowPrivateAddress,
  427. "tls_verify_mode": in.TlsVerifyMode,
  428. "pinned_cert_sha256": in.PinnedCertSha256,
  429. "inbound_sync_mode": in.InboundSyncMode,
  430. "inbound_tags": string(inboundTagsJSON),
  431. "outbound_tag": in.OutboundTag,
  432. }
  433. if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
  434. return err
  435. }
  436. if dErr := s.MarkNodeDirty(id); dErr != nil {
  437. logger.Warning("mark node dirty after update failed:", dErr)
  438. }
  439. if mgr := runtime.GetManager(); mgr != nil {
  440. mgr.InvalidateNode(id)
  441. }
  442. return nil
  443. }
  444. func (s *NodeService) GetRemoteInboundOptions(ctx context.Context, n *model.Node) ([]runtime.RemoteInboundOption, error) {
  445. if err := s.normalize(n); err != nil {
  446. return nil, err
  447. }
  448. if n.OutboundTag == "" {
  449. return runtime.NewRemote(n, nil).ListInboundOptions(ctx)
  450. }
  451. // Mirror ProbeWithOutbound: a node being added/edited has no persistent
  452. // egress bridge yet, so route the list call through a temporary one or the
  453. // remote panel stays unreachable and the request times out.
  454. var options []runtime.RemoteInboundOption
  455. var err error
  456. s.withOutboundBridge(n.Id, n.OutboundTag, func(proxyURL string) {
  457. options, err = runtime.NewRemote(n, staticEgressResolver(proxyURL)).ListInboundOptions(ctx)
  458. })
  459. return options, err
  460. }
  461. // staticEgressResolver hands a fixed proxy URL to runtime.NewRemote. An empty
  462. // string yields a direct connection, so it doubles as the graceful fallback
  463. // when a temporary bridge can't be built.
  464. type staticEgressResolver string
  465. func (r staticEgressResolver) NodeEgressProxyURL(int) string { return string(r) }
  466. // EnsureInboundTagAllowed adds a panel-managed inbound's tag to the node's
  467. // selection when the node syncs in "selected" mode. Without it, the next
  468. // traffic sync would filter the tag out of the snapshot and the orphan sweep
  469. // would silently delete the central row the panel just created or renamed.
  470. // Tags are only ever added (never removed): on a rename the node may keep
  471. // reporting the old tag until the remote update lands, and a leftover entry
  472. // that matches nothing is harmless.
  473. func (s *NodeService) EnsureInboundTagAllowed(nodeID int, tag string) error {
  474. tag = strings.TrimSpace(tag)
  475. if nodeID <= 0 || tag == "" {
  476. return nil
  477. }
  478. db := database.GetDB()
  479. node := &model.Node{}
  480. if err := db.Where("id = ?", nodeID).First(node).Error; err != nil {
  481. return err
  482. }
  483. if node.InboundSyncMode != "selected" {
  484. return nil
  485. }
  486. if slices.Contains(node.InboundTags, tag) {
  487. return nil
  488. }
  489. buf, err := json.Marshal(append(node.InboundTags, tag))
  490. if err != nil {
  491. return err
  492. }
  493. return db.Model(model.Node{}).Where("id = ?", nodeID).
  494. Updates(map[string]any{"inbound_tags": string(buf)}).Error
  495. }
  496. func FilterNodeSnapshot(n *model.Node, snap *runtime.TrafficSnapshot) {
  497. if n == nil || snap == nil || n.InboundSyncMode != "selected" {
  498. return
  499. }
  500. allowed := make(map[string]struct{}, len(n.InboundTags))
  501. for _, tag := range n.InboundTags {
  502. allowed[tag] = struct{}{}
  503. }
  504. filtered := make([]*model.Inbound, 0, len(snap.Inbounds))
  505. for _, inbound := range snap.Inbounds {
  506. if inbound == nil {
  507. continue
  508. }
  509. if _, ok := allowed[inbound.Tag]; ok {
  510. filtered = append(filtered, inbound)
  511. }
  512. }
  513. snap.Inbounds = filtered
  514. }
  515. func (s *NodeService) Delete(id int) error {
  516. db := database.GetDB()
  517. // Refuse to delete a node that still owns inbounds: dropping the node row
  518. // while inbounds keep its node_id leaves orphaned, dangling references that
  519. // confuse node sync, subscriptions and cleanup. The operator must detach or
  520. // remove those inbounds first. (DB-002)
  521. var attached int64
  522. if err := db.Model(&model.Inbound{}).Where("node_id = ?", id).Count(&attached).Error; err != nil {
  523. return err
  524. }
  525. if attached > 0 {
  526. return common.NewError(fmt.Sprintf("cannot delete node: %d inbound(s) still attached to it; detach or delete them first", attached))
  527. }
  528. // Capture the node's guid before deleting the row so we can drop its per-node
  529. // IP attribution. NodeClientIp is keyed by the node's attribution key, which
  530. // is its guid normally but its node-unique key for a cloned/ambiguous-guid
  531. // node (see effectiveNodeKey) — so we purge both below.
  532. var guid string
  533. var n model.Node
  534. if err := db.Select("guid").Where("id = ?", id).First(&n).Error; err == nil {
  535. guid = n.Guid
  536. }
  537. // Delete the node row and its per-node child rows atomically. Remove the
  538. // children (traffic baselines, IP attribution) before the parent node row so
  539. // the ordering already matches a future ON DELETE constraint. Delete stays
  540. // tolerant of a missing node row so it can still clean up orphaned baselines.
  541. if err := db.Transaction(func(tx *gorm.DB) error {
  542. if err := tx.Where("node_id = ?", id).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  543. return err
  544. }
  545. guids := []string{synthNodeGuid(id)}
  546. if guid != "" {
  547. guids = append(guids, guid)
  548. }
  549. if err := tx.Where("node_guid IN ?", guids).Delete(&model.NodeClientIp{}).Error; err != nil {
  550. return err
  551. }
  552. return tx.Where("id = ?", id).Delete(&model.Node{}).Error
  553. }); err != nil {
  554. return err
  555. }
  556. if mgr := runtime.GetManager(); mgr != nil {
  557. mgr.InvalidateNode(id)
  558. }
  559. nodeMetrics.drop(nodeMetricKey(id, "cpu"))
  560. nodeMetrics.drop(nodeMetricKey(id, "mem"))
  561. return nil
  562. }
  563. func (s *NodeService) SetEnable(id int, enable bool) error {
  564. db := database.GetDB()
  565. if err := db.Model(model.Node{}).Where("id = ?", id).Update("enable", enable).Error; err != nil {
  566. return err
  567. }
  568. if mgr := runtime.GetManager(); mgr != nil {
  569. mgr.InvalidateNode(id)
  570. }
  571. return nil
  572. }
  573. // GetWebCertFiles asks a node for its own web TLS certificate/key file paths,
  574. // used by "Set Cert from Panel" so a node-assigned inbound gets paths that
  575. // exist on the node rather than the central panel. See issue #4854.
  576. func (s *NodeService) GetWebCertFiles(id int) (*runtime.WebCertFiles, error) {
  577. n, err := s.GetById(id)
  578. if err != nil || n == nil {
  579. return nil, fmt.Errorf("node not found")
  580. }
  581. if !n.Enable {
  582. return nil, fmt.Errorf("node is disabled")
  583. }
  584. mgr := runtime.GetManager()
  585. if mgr == nil {
  586. return nil, fmt.Errorf("runtime manager unavailable")
  587. }
  588. remote, err := mgr.RemoteFor(n)
  589. if err != nil {
  590. return nil, err
  591. }
  592. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  593. defer cancel()
  594. return remote.GetWebCertFiles(ctx)
  595. }
  596. // NodeUpdateResult reports the outcome of triggering a panel self-update on one
  597. // node so the UI can show per-node success/failure for a bulk request.
  598. type NodeUpdateResult struct {
  599. Id int `json:"id"`
  600. Name string `json:"name"`
  601. OK bool `json:"ok"`
  602. Error string `json:"error,omitempty"`
  603. }
  604. // UpdatePanels triggers the official self-updater on each given node. Only
  605. // enabled, online nodes are eligible — an offline node can't be reached, so it
  606. // is reported as skipped rather than silently dropped.
  607. func (s *NodeService) UpdatePanels(ids []int) ([]NodeUpdateResult, error) {
  608. mgr := runtime.GetManager()
  609. if mgr == nil {
  610. return nil, fmt.Errorf("runtime manager unavailable")
  611. }
  612. results := make([]NodeUpdateResult, 0, len(ids))
  613. for _, id := range ids {
  614. n, err := s.GetById(id)
  615. if err != nil || n == nil {
  616. results = append(results, NodeUpdateResult{Id: id, OK: false, Error: "node not found"})
  617. continue
  618. }
  619. res := NodeUpdateResult{Id: id, Name: n.Name}
  620. switch {
  621. case !n.Enable:
  622. res.Error = "node is disabled"
  623. case n.Status != "online":
  624. res.Error = "node is offline"
  625. default:
  626. remote, remoteErr := mgr.RemoteFor(n)
  627. if remoteErr != nil {
  628. res.Error = remoteErr.Error()
  629. break
  630. }
  631. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  632. updErr := remote.UpdatePanel(ctx)
  633. cancel()
  634. if updErr != nil {
  635. res.Error = updErr.Error()
  636. } else {
  637. res.OK = true
  638. }
  639. }
  640. results = append(results, res)
  641. }
  642. return results, nil
  643. }
  644. func (s *NodeService) UpdateHeartbeat(id int, p HeartbeatPatch) error {
  645. db := database.GetDB()
  646. updates := map[string]any{
  647. "status": p.Status,
  648. "last_heartbeat": p.LastHeartbeat,
  649. "latency_ms": p.LatencyMs,
  650. "xray_version": p.XrayVersion,
  651. "panel_version": p.PanelVersion,
  652. "cpu_pct": p.CpuPct,
  653. "mem_pct": p.MemPct,
  654. "uptime_secs": p.UptimeSecs,
  655. "net_up": p.NetUp,
  656. "net_down": p.NetDown,
  657. "last_error": p.LastError,
  658. "xray_state": p.XrayState,
  659. "xray_error": p.XrayError,
  660. }
  661. // Only learn the GUID; never clear a known one if an old-build node (or a
  662. // failed probe) reports none, so the stable identity survives blips.
  663. if p.Guid != "" {
  664. updates["guid"] = p.Guid
  665. s.warnOnDuplicateGuid(id, p.Guid)
  666. }
  667. if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
  668. return err
  669. }
  670. if p.Status == "online" {
  671. now := time.Unix(p.LastHeartbeat, 0)
  672. nodeMetrics.append(nodeMetricKey(id, "cpu"), now, p.CpuPct)
  673. nodeMetrics.append(nodeMetricKey(id, "mem"), now, p.MemPct)
  674. nodeMetrics.append(nodeMetricKey(id, "netUp"), now, float64(p.NetUp))
  675. nodeMetrics.append(nodeMetricKey(id, "netDown"), now, float64(p.NetDown))
  676. }
  677. return nil
  678. }
  679. // warnedDupGuid remembers the (nodeID -> guid) pairs already warned about so a
  680. // cloned-server collision is logged once, not every heartbeat.
  681. var warnedDupGuid sync.Map
  682. // warnOnDuplicateGuid logs once when a node reports a panelGuid already held by
  683. // another node or by the master itself (the cloned-server footgun). Attribution
  684. // still works — it falls back to node-unique keys — but the operator should
  685. // regenerate the duplicate panelGuid to restore real identity and per-node IP
  686. // attribution. Re-arms if the collision later clears.
  687. func (s *NodeService) warnOnDuplicateGuid(id int, guid string) {
  688. var clash int64
  689. database.GetDB().Model(&model.Node{}).Where("guid = ? AND id <> ?", guid, id).Count(&clash)
  690. masterGuid, _ := (&SettingService{}).GetPanelGuid()
  691. if clash == 0 && guid != masterGuid {
  692. warnedDupGuid.Delete(id)
  693. return
  694. }
  695. if prev, ok := warnedDupGuid.Load(id); ok && prev == guid {
  696. return
  697. }
  698. warnedDupGuid.Store(id, guid)
  699. logger.Warningf("node %d reports panelGuid %s already used by another node or the master (cloned server?) — regenerate it on that node so online and IP attribution stay per-node", id, guid)
  700. }
  701. func (s *NodeService) MarkNodeDirty(id int) error {
  702. if id <= 0 {
  703. return nil
  704. }
  705. return database.GetDB().Model(model.Node{}).
  706. Where("id = ?", id).
  707. Updates(map[string]any{
  708. "config_dirty": true,
  709. "config_dirty_at": time.Now().UnixMilli(),
  710. }).Error
  711. }
  712. func (s *NodeService) ClearNodeDirty(id int, dirtyAt int64) error {
  713. if id <= 0 {
  714. return nil
  715. }
  716. return database.GetDB().Model(model.Node{}).
  717. Where("id = ? AND config_dirty_at = ?", id, dirtyAt).
  718. Update("config_dirty", false).Error
  719. }
  720. func (s *NodeService) NodeSyncState(id int) (enabled bool, status string, dirty bool, dirtyAt int64, err error) {
  721. if id <= 0 {
  722. return false, "", false, 0, errors.New("invalid node id")
  723. }
  724. var row model.Node
  725. err = database.GetDB().Model(model.Node{}).
  726. Select("enable", "status", "config_dirty", "config_dirty_at").
  727. Where("id = ?", id).
  728. First(&row).Error
  729. if err != nil {
  730. return false, "", false, 0, err
  731. }
  732. return row.Enable, row.Status, row.ConfigDirty, row.ConfigDirtyAt, nil
  733. }
  734. func (s *NodeService) IsNodePending(id int) bool {
  735. enabled, status, dirty, _, err := s.NodeSyncState(id)
  736. if err != nil {
  737. return false
  738. }
  739. return !enabled || status != "online" || dirty
  740. }
  741. func nodeMetricKey(id int, metric string) string {
  742. return "node:" + strconv.Itoa(id) + ":" + metric
  743. }
  744. func (s *NodeService) AggregateNodeMetric(id int, metric string, bucketSeconds int, maxPoints int) []map[string]any {
  745. return nodeMetrics.aggregate(nodeMetricKey(id, metric), bucketSeconds, maxPoints)
  746. }
  747. func (s *NodeService) Probe(ctx context.Context, n *model.Node) (HeartbeatPatch, error) {
  748. proxyURL := ""
  749. if n.OutboundTag != "" {
  750. if mgr := runtime.GetManager(); mgr != nil {
  751. proxyURL = mgr.NodeEgressProxyURL(n.Id)
  752. }
  753. }
  754. return s.probe(ctx, n, proxyURL)
  755. }
  756. func (s *NodeService) ProbeWithOutbound(ctx context.Context, n *model.Node, outboundTag string) (HeartbeatPatch, error) {
  757. if outboundTag == "" {
  758. return s.Probe(ctx, n)
  759. }
  760. var patch HeartbeatPatch
  761. var err error
  762. s.withOutboundBridge(n.Id, outboundTag, func(proxyURL string) {
  763. if proxyURL == "" {
  764. patch, err = s.Probe(ctx, n)
  765. return
  766. }
  767. patch, err = s.probe(ctx, n, proxyURL)
  768. })
  769. return patch, err
  770. }
  771. // withOutboundBridge stands up a temporary loopback SOCKS5 inbound in the
  772. // running Xray, routes it through outboundTag, and runs fn with the bridge's
  773. // proxy URL before tearing it down. It is used to reach a node through its
  774. // connection outbound before the persistent egress bridge has been injected
  775. // into the config (e.g. while the node is still being added or edited). When
  776. // Xray isn't running or the bridge can't be built, fn runs with an empty
  777. // proxyURL so callers fall back to a direct connection.
  778. func (s *NodeService) withOutboundBridge(nodeID int, outboundTag string, fn func(proxyURL string)) {
  779. proc := XrayProcess()
  780. if proc == nil || !proc.IsRunning() {
  781. fn("")
  782. return
  783. }
  784. apiPort := proc.GetAPIPort()
  785. if apiPort <= 0 {
  786. fn("")
  787. return
  788. }
  789. listener, err := net.Listen("tcp", "127.0.0.1:0")
  790. if err != nil {
  791. fn("")
  792. return
  793. }
  794. port := listener.Addr().(*net.TCPAddr).Port
  795. listener.Close()
  796. tag := fmt.Sprintf("node-test-%d-%d", nodeID, time.Now().UnixNano())
  797. proxyURL := fmt.Sprintf("socks5://127.0.0.1:%d", port)
  798. inboundJSON, err := json.Marshal(xray.InboundConfig{
  799. Listen: json_util.RawMessage(`"127.0.0.1"`),
  800. Port: port,
  801. Protocol: "socks",
  802. Settings: json_util.RawMessage(`{"auth":"noauth","udp":false}`),
  803. Tag: tag,
  804. })
  805. if err != nil {
  806. fn("")
  807. return
  808. }
  809. cfg := proc.GetConfig()
  810. routing := map[string]any{}
  811. if len(cfg.RouterConfig) > 0 {
  812. _ = json.Unmarshal(cfg.RouterConfig, &routing)
  813. }
  814. rules, _ := routing["rules"].([]any)
  815. rule := map[string]any{
  816. "type": "field",
  817. "inboundTag": []any{tag},
  818. }
  819. if routingTagIsBalancer(routing, outboundTag) {
  820. rule["balancerTag"] = outboundTag
  821. } else {
  822. rule["outboundTag"] = outboundTag
  823. }
  824. routing["rules"] = append([]any{rule}, rules...)
  825. routingJSON, err := json.Marshal(routing)
  826. if err != nil {
  827. fn("")
  828. return
  829. }
  830. originalRoutingJSON := cfg.RouterConfig
  831. api := xray.XrayAPI{}
  832. if err := api.Init(apiPort); err != nil {
  833. fn("")
  834. return
  835. }
  836. defer api.Close()
  837. if err := api.AddInbound(inboundJSON); err != nil {
  838. fn("")
  839. return
  840. }
  841. defer func() {
  842. if err := api.DelInbound(tag); err != nil {
  843. logger.Warning("remove temp node bridge inbound failed:", err)
  844. }
  845. }()
  846. if err := api.ApplyRoutingConfig(routingJSON); err != nil {
  847. fn("")
  848. return
  849. }
  850. defer func() {
  851. restore := originalRoutingJSON
  852. if len(restore) == 0 {
  853. restore = []byte("{}")
  854. }
  855. if err := api.ApplyRoutingConfig(restore); err != nil {
  856. logger.Warning("restore routing after node bridge failed:", err)
  857. }
  858. }()
  859. fn(proxyURL)
  860. }
  861. func (s *NodeService) probe(ctx context.Context, n *model.Node, proxyURL string) (HeartbeatPatch, error) {
  862. patch := HeartbeatPatch{LastHeartbeat: time.Now().Unix()}
  863. addr, err := netsafe.NormalizeHost(n.Address)
  864. if err != nil {
  865. patch.LastError = err.Error()
  866. return patch, err
  867. }
  868. scheme := n.Scheme
  869. if scheme != "http" && scheme != "https" {
  870. scheme = "https"
  871. }
  872. if n.Port <= 0 || n.Port > 65535 {
  873. patch.LastError = "node port must be 1-65535"
  874. return patch, errors.New(patch.LastError)
  875. }
  876. probeURL := &url.URL{
  877. Scheme: scheme,
  878. Host: net.JoinHostPort(addr, strconv.Itoa(n.Port)),
  879. Path: normalizeBasePath(n.BasePath) + "panel/api/server/status",
  880. }
  881. req, err := http.NewRequestWithContext(
  882. netsafe.ContextWithAllowPrivate(ctx, n.AllowPrivateAddress),
  883. http.MethodGet, probeURL.String(), nil)
  884. if err != nil {
  885. patch.LastError = err.Error()
  886. return patch, err
  887. }
  888. if n.ApiToken != "" {
  889. req.Header.Set("Authorization", "Bearer "+n.ApiToken)
  890. }
  891. req.Header.Set("Accept", "application/json")
  892. client, err := runtime.HTTPClientForNode(n, proxyURL)
  893. if err != nil {
  894. patch.LastError = err.Error()
  895. return patch, err
  896. }
  897. start := time.Now()
  898. resp, err := client.Do(req)
  899. if err != nil {
  900. patch.LastError = err.Error()
  901. return patch, err
  902. }
  903. defer resp.Body.Close()
  904. patch.LatencyMs = int(time.Since(start) / time.Millisecond)
  905. if resp.StatusCode != http.StatusOK {
  906. patch.LastError = fmt.Sprintf("HTTP %d from remote panel", resp.StatusCode)
  907. return patch, errors.New(patch.LastError)
  908. }
  909. var envelope struct {
  910. Success bool `json:"success"`
  911. Msg string `json:"msg"`
  912. Obj *struct {
  913. CpuPct float64 `json:"cpu"`
  914. Mem struct {
  915. Current uint64 `json:"current"`
  916. Total uint64 `json:"total"`
  917. } `json:"mem"`
  918. Xray struct {
  919. Version string `json:"version"`
  920. State string `json:"state"`
  921. ErrorMsg string `json:"errorMsg"`
  922. } `json:"xray"`
  923. PanelVersion string `json:"panelVersion"`
  924. PanelGuid string `json:"panelGuid"`
  925. Uptime uint64 `json:"uptime"`
  926. NetIO struct {
  927. Up uint64 `json:"up"`
  928. Down uint64 `json:"down"`
  929. } `json:"netIO"`
  930. } `json:"obj"`
  931. }
  932. if err := json.NewDecoder(resp.Body).Decode(&envelope); err != nil {
  933. patch.LastError = "decode response: " + err.Error()
  934. return patch, err
  935. }
  936. if !envelope.Success || envelope.Obj == nil {
  937. patch.LastError = "remote returned success=false: " + envelope.Msg
  938. return patch, errors.New(patch.LastError)
  939. }
  940. o := envelope.Obj
  941. patch.CpuPct = o.CpuPct
  942. if o.Mem.Total > 0 {
  943. patch.MemPct = float64(o.Mem.Current) * 100.0 / float64(o.Mem.Total)
  944. }
  945. patch.XrayVersion = o.Xray.Version
  946. patch.XrayState = o.Xray.State
  947. patch.XrayError = o.Xray.ErrorMsg
  948. patch.PanelVersion = o.PanelVersion
  949. patch.Guid = o.PanelGuid
  950. patch.UptimeSecs = o.Uptime
  951. patch.NetUp = o.NetIO.Up
  952. patch.NetDown = o.NetIO.Down
  953. return patch, nil
  954. }
  955. type ProbeResultUI struct {
  956. Status string `json:"status" example:"online"`
  957. LatencyMs int `json:"latencyMs" example:"42"`
  958. XrayVersion string `json:"xrayVersion" example:"25.10.31"`
  959. PanelVersion string `json:"panelVersion" example:"v3.x.x"`
  960. CpuPct float64 `json:"cpuPct" example:"12.5"`
  961. MemPct float64 `json:"memPct" example:"45.2"`
  962. UptimeSecs uint64 `json:"uptimeSecs" example:"86400"`
  963. Error string `json:"error"`
  964. // XrayState/XrayError are populated on successful probes even when the node's
  965. // Xray core is not healthy. The UI uses them for a distinct "panel ok, xray failed" indicator.
  966. XrayState string `json:"xrayState"`
  967. XrayError string `json:"xrayError"`
  968. }
  969. func (p HeartbeatPatch) ToUI(ok bool) ProbeResultUI {
  970. r := ProbeResultUI{
  971. LatencyMs: p.LatencyMs,
  972. XrayVersion: p.XrayVersion,
  973. PanelVersion: p.PanelVersion,
  974. CpuPct: p.CpuPct,
  975. MemPct: p.MemPct,
  976. UptimeSecs: p.UptimeSecs,
  977. Error: FriendlyProbeError(p.LastError),
  978. XrayState: p.XrayState,
  979. XrayError: p.XrayError,
  980. }
  981. if ok {
  982. r.Status = "online"
  983. } else {
  984. r.Status = "offline"
  985. }
  986. return r
  987. }
  988. func FriendlyProbeError(msg string) string {
  989. if strings.Contains(msg, "server gave HTTP response to HTTPS client") {
  990. return "the server speaks HTTP, not HTTPS; set the node scheme to http"
  991. }
  992. return msg
  993. }