1
0

node.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/mhsanaei/3x-ui/v3/internal/database"
  17. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  18. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  19. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  20. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  21. "github.com/mhsanaei/3x-ui/v3/internal/util/netsafe"
  22. "github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
  23. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  24. )
  25. type HeartbeatPatch struct {
  26. Status string
  27. LastHeartbeat int64
  28. LatencyMs int
  29. XrayVersion string
  30. PanelVersion string
  31. Guid string
  32. CpuPct float64
  33. MemPct float64
  34. UptimeSecs uint64
  35. LastError string
  36. // XrayState and XrayError come from the remote /panel/api/server/status when the
  37. // panel API is reachable. They allow distinguishing panel connectivity from
  38. // Xray core health on the node.
  39. XrayState string
  40. XrayError string
  41. }
  42. type NodeService struct{}
  43. // FetchCertFingerprint connects to the node over HTTPS without verifying the
  44. // certificate and returns the leaf certificate's SHA-256 as base64, so the UI
  45. // can offer a "fetch and pin current certificate" action.
  46. func (s *NodeService) FetchCertFingerprint(ctx context.Context, n *model.Node) (string, error) {
  47. addr, err := netsafe.NormalizeHost(n.Address)
  48. if err != nil {
  49. return "", err
  50. }
  51. scheme := n.Scheme
  52. if scheme != "http" && scheme != "https" {
  53. scheme = "https"
  54. }
  55. if scheme != "https" {
  56. return "", common.NewError("certificate pinning is only available for https nodes")
  57. }
  58. if n.Port <= 0 || n.Port > 65535 {
  59. return "", common.NewError("node port must be 1-65535")
  60. }
  61. probeURL := &url.URL{
  62. Scheme: scheme,
  63. Host: net.JoinHostPort(addr, strconv.Itoa(n.Port)),
  64. Path: normalizeBasePath(n.BasePath) + "panel/api/server/status",
  65. }
  66. req, err := http.NewRequestWithContext(
  67. netsafe.ContextWithAllowPrivate(ctx, n.AllowPrivateAddress),
  68. http.MethodGet, probeURL.String(), nil)
  69. if err != nil {
  70. return "", err
  71. }
  72. client := &http.Client{
  73. Transport: &http.Transport{
  74. DialContext: netsafe.SSRFGuardedDialContext,
  75. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // lgtm[go/disabled-certificate-check]
  76. },
  77. }
  78. resp, err := client.Do(req)
  79. if err != nil {
  80. return "", err
  81. }
  82. defer resp.Body.Close()
  83. if resp.TLS == nil || len(resp.TLS.PeerCertificates) == 0 {
  84. return "", common.NewError("node did not present a TLS certificate")
  85. }
  86. sum := sha256.Sum256(resp.TLS.PeerCertificates[0].Raw)
  87. return base64.StdEncoding.EncodeToString(sum[:]), nil
  88. }
  89. func (s *NodeService) GetAll() ([]*model.Node, error) {
  90. db := database.GetDB()
  91. var nodes []*model.Node
  92. err := db.Model(model.Node{}).Order("id asc").Find(&nodes).Error
  93. if err != nil || len(nodes) == 0 {
  94. return nodes, err
  95. }
  96. type inboundRow struct {
  97. Id int
  98. NodeID int `gorm:"column:node_id"`
  99. }
  100. var inboundRows []inboundRow
  101. if err := db.Table("inbounds").
  102. Select("id, node_id").
  103. Where("node_id IS NOT NULL").
  104. Scan(&inboundRows).Error; err != nil {
  105. return nodes, nil
  106. }
  107. if len(inboundRows) == 0 {
  108. return nodes, nil
  109. }
  110. inboundsByNode := make(map[int][]int, len(nodes))
  111. nodeByInbound := make(map[int]int, len(inboundRows))
  112. for _, row := range inboundRows {
  113. inboundsByNode[row.NodeID] = append(inboundsByNode[row.NodeID], row.Id)
  114. nodeByInbound[row.Id] = row.NodeID
  115. }
  116. type clientCountRow struct {
  117. NodeID int `gorm:"column:node_id"`
  118. Count int `gorm:"column:count"`
  119. }
  120. var clientCounts []clientCountRow
  121. if err := db.Raw(`
  122. SELECT inbounds.node_id AS node_id, COUNT(DISTINCT client_inbounds.client_id) AS count
  123. FROM inbounds
  124. JOIN client_inbounds ON client_inbounds.inbound_id = inbounds.id
  125. WHERE inbounds.node_id IS NOT NULL
  126. GROUP BY inbounds.node_id
  127. `).Scan(&clientCounts).Error; err == nil {
  128. for _, row := range clientCounts {
  129. for _, n := range nodes {
  130. if n.Id == row.NodeID {
  131. n.ClientCount = row.Count
  132. break
  133. }
  134. }
  135. }
  136. }
  137. now := time.Now().UnixMilli()
  138. type trafficRow struct {
  139. InboundID int `gorm:"column:inbound_id"`
  140. Email string
  141. Enable bool
  142. Total int64
  143. Up int64
  144. Down int64
  145. ExpiryTime int64 `gorm:"column:expiry_time"`
  146. }
  147. var trafficRows []trafficRow
  148. inboundIDs := make([]int, 0, len(nodeByInbound))
  149. for id := range nodeByInbound {
  150. inboundIDs = append(inboundIDs, id)
  151. }
  152. // Chunk the IN clause to avoid "too many SQL variables" on SQLite
  153. // when there are many node-owned inbounds (common with many nodes).
  154. // sqliteMaxVars is defined in this package (inbound.go).
  155. for _, batch := range chunkInts(inboundIDs, sqliteMaxVars) {
  156. var page []trafficRow
  157. if err := db.Table("client_traffics").
  158. Select("inbound_id, email, enable, total, up, down, expiry_time").
  159. Where("inbound_id IN ?", batch).
  160. Scan(&page).Error; err == nil {
  161. trafficRows = append(trafficRows, page...)
  162. }
  163. }
  164. depletedByNode := make(map[int]int)
  165. if len(trafficRows) > 0 {
  166. for _, row := range trafficRows {
  167. nodeID, ok := nodeByInbound[row.InboundID]
  168. if !ok {
  169. continue
  170. }
  171. expired := row.ExpiryTime > 0 && row.ExpiryTime <= now
  172. exhausted := row.Total > 0 && row.Up+row.Down >= row.Total
  173. if expired || exhausted || !row.Enable {
  174. depletedByNode[nodeID]++
  175. }
  176. }
  177. }
  178. onlineByGuid := s.onlineEmailsByGuid()
  179. for _, n := range nodes {
  180. n.InboundCount = len(inboundsByNode[n.Id])
  181. n.DepletedCount = depletedByNode[n.Id]
  182. // Online is attributed to the node that physically hosts the client
  183. // (by GUID): a client on a sub-node counts under the sub-node, not
  184. // the intermediate node it syncs through (#4983).
  185. n.OnlineCount = len(onlineByGuid[effectiveNodeGuid(n)])
  186. }
  187. return nodes, nil
  188. }
  189. func (s *NodeService) onlineEmailsByGuid() map[string]map[string]struct{} {
  190. svc := InboundService{}
  191. byGuid := svc.GetOnlineClientsByGuid()
  192. out := make(map[string]map[string]struct{}, len(byGuid))
  193. for guid, emails := range byGuid {
  194. set := make(map[string]struct{}, len(emails))
  195. for _, email := range emails {
  196. set[email] = struct{}{}
  197. }
  198. out[guid] = set
  199. }
  200. return out
  201. }
  202. // effectiveNodeGuid is a node's stable online-attribution key: its reported
  203. // panelGuid, or a master-local synthetic id when the node is an old build that
  204. // hasn't reported one yet (#4983).
  205. func effectiveNodeGuid(n *model.Node) string {
  206. if n.Guid != "" {
  207. return n.Guid
  208. }
  209. return synthNodeGuid(n.Id)
  210. }
  211. func (s *NodeService) GetById(id int) (*model.Node, error) {
  212. db := database.GetDB()
  213. n := &model.Node{}
  214. if err := db.Model(model.Node{}).Where("id = ?", id).First(n).Error; err != nil {
  215. return nil, err
  216. }
  217. return n, nil
  218. }
  219. // NodeExists reports whether a node with the given id exists on this panel.
  220. // Used to drop stale, cross-panel node references on inbound import. A Count
  221. // query distinguishes "no such node" (count 0, no error) from a real DB error.
  222. func (s *NodeService) NodeExists(id int) (bool, error) {
  223. if id <= 0 {
  224. return false, nil
  225. }
  226. var count int64
  227. if err := database.GetDB().Model(model.Node{}).Where("id = ?", id).Count(&count).Error; err != nil {
  228. return false, err
  229. }
  230. return count > 0, nil
  231. }
  232. func normalizeBasePath(p string) string {
  233. p = strings.TrimSpace(p)
  234. if p == "" {
  235. return "/"
  236. }
  237. if !strings.HasPrefix(p, "/") {
  238. p = "/" + p
  239. }
  240. if !strings.HasSuffix(p, "/") {
  241. p = p + "/"
  242. }
  243. return p
  244. }
  245. func (s *NodeService) normalize(n *model.Node) error {
  246. n.Name = strings.TrimSpace(n.Name)
  247. n.ApiToken = strings.TrimSpace(n.ApiToken)
  248. if n.Name == "" {
  249. return common.NewError("node name is required")
  250. }
  251. addr, err := netsafe.NormalizeHost(n.Address)
  252. if err != nil {
  253. return common.NewError(err.Error())
  254. }
  255. n.Address = addr
  256. if n.Port <= 0 || n.Port > 65535 {
  257. return common.NewError("node port must be 1-65535")
  258. }
  259. if n.Scheme != "http" && n.Scheme != "https" {
  260. n.Scheme = "https"
  261. }
  262. if n.TlsVerifyMode != "skip" && n.TlsVerifyMode != "pin" {
  263. n.TlsVerifyMode = "verify"
  264. }
  265. n.PinnedCertSha256 = strings.TrimSpace(n.PinnedCertSha256)
  266. if n.InboundSyncMode != "selected" {
  267. n.InboundSyncMode = "all"
  268. n.InboundTags = nil
  269. } else {
  270. seen := make(map[string]struct{}, len(n.InboundTags))
  271. tags := make([]string, 0, len(n.InboundTags))
  272. for _, tag := range n.InboundTags {
  273. tag = strings.TrimSpace(tag)
  274. if tag == "" {
  275. continue
  276. }
  277. if _, ok := seen[tag]; ok {
  278. continue
  279. }
  280. seen[tag] = struct{}{}
  281. tags = append(tags, tag)
  282. }
  283. n.InboundTags = tags
  284. }
  285. if n.TlsVerifyMode == "pin" {
  286. if _, err := runtime.DecodeCertPin(n.PinnedCertSha256); err != nil {
  287. return common.NewError(err.Error())
  288. }
  289. }
  290. n.BasePath = normalizeBasePath(n.BasePath)
  291. return nil
  292. }
  293. func (s *NodeService) Create(n *model.Node) error {
  294. if err := s.normalize(n); err != nil {
  295. return err
  296. }
  297. db := database.GetDB()
  298. return db.Create(n).Error
  299. }
  300. func (s *NodeService) Update(id int, in *model.Node) error {
  301. if err := s.normalize(in); err != nil {
  302. return err
  303. }
  304. inboundTagsJSON, err := json.Marshal(in.InboundTags)
  305. if err != nil {
  306. return err
  307. }
  308. db := database.GetDB()
  309. existing := &model.Node{}
  310. if err := db.Where("id = ?", id).First(existing).Error; err != nil {
  311. return err
  312. }
  313. updates := map[string]any{
  314. "name": in.Name,
  315. "remark": in.Remark,
  316. "scheme": in.Scheme,
  317. "address": in.Address,
  318. "port": in.Port,
  319. "base_path": in.BasePath,
  320. "api_token": in.ApiToken,
  321. "enable": in.Enable,
  322. "allow_private_address": in.AllowPrivateAddress,
  323. "tls_verify_mode": in.TlsVerifyMode,
  324. "pinned_cert_sha256": in.PinnedCertSha256,
  325. "inbound_sync_mode": in.InboundSyncMode,
  326. "inbound_tags": string(inboundTagsJSON),
  327. "outbound_tag": in.OutboundTag,
  328. }
  329. if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
  330. return err
  331. }
  332. if mgr := runtime.GetManager(); mgr != nil {
  333. mgr.InvalidateNode(id)
  334. }
  335. return nil
  336. }
  337. func (s *NodeService) GetRemoteInboundOptions(ctx context.Context, n *model.Node) ([]runtime.RemoteInboundOption, error) {
  338. if err := s.normalize(n); err != nil {
  339. return nil, err
  340. }
  341. return runtime.NewRemote(n, nil).ListInboundOptions(ctx)
  342. }
  343. // EnsureInboundTagAllowed adds a panel-managed inbound's tag to the node's
  344. // selection when the node syncs in "selected" mode. Without it, the next
  345. // traffic sync would filter the tag out of the snapshot and the orphan sweep
  346. // would silently delete the central row the panel just created or renamed.
  347. // Tags are only ever added (never removed): on a rename the node may keep
  348. // reporting the old tag until the remote update lands, and a leftover entry
  349. // that matches nothing is harmless.
  350. func (s *NodeService) EnsureInboundTagAllowed(nodeID int, tag string) error {
  351. tag = strings.TrimSpace(tag)
  352. if nodeID <= 0 || tag == "" {
  353. return nil
  354. }
  355. db := database.GetDB()
  356. node := &model.Node{}
  357. if err := db.Where("id = ?", nodeID).First(node).Error; err != nil {
  358. return err
  359. }
  360. if node.InboundSyncMode != "selected" {
  361. return nil
  362. }
  363. for _, t := range node.InboundTags {
  364. if t == tag {
  365. return nil
  366. }
  367. }
  368. buf, err := json.Marshal(append(node.InboundTags, tag))
  369. if err != nil {
  370. return err
  371. }
  372. return db.Model(model.Node{}).Where("id = ?", nodeID).
  373. Updates(map[string]any{"inbound_tags": string(buf)}).Error
  374. }
  375. func FilterNodeSnapshot(n *model.Node, snap *runtime.TrafficSnapshot) {
  376. if n == nil || snap == nil || n.InboundSyncMode != "selected" {
  377. return
  378. }
  379. allowed := make(map[string]struct{}, len(n.InboundTags))
  380. for _, tag := range n.InboundTags {
  381. allowed[tag] = struct{}{}
  382. }
  383. filtered := make([]*model.Inbound, 0, len(snap.Inbounds))
  384. for _, inbound := range snap.Inbounds {
  385. if inbound == nil {
  386. continue
  387. }
  388. if _, ok := allowed[inbound.Tag]; ok {
  389. filtered = append(filtered, inbound)
  390. }
  391. }
  392. snap.Inbounds = filtered
  393. }
  394. func (s *NodeService) Delete(id int) error {
  395. db := database.GetDB()
  396. if err := db.Where("id = ?", id).Delete(model.Node{}).Error; err != nil {
  397. return err
  398. }
  399. if err := db.Where("node_id = ?", id).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  400. return err
  401. }
  402. if mgr := runtime.GetManager(); mgr != nil {
  403. mgr.InvalidateNode(id)
  404. }
  405. nodeMetrics.drop(nodeMetricKey(id, "cpu"))
  406. nodeMetrics.drop(nodeMetricKey(id, "mem"))
  407. return nil
  408. }
  409. func (s *NodeService) SetEnable(id int, enable bool) error {
  410. db := database.GetDB()
  411. if err := db.Model(model.Node{}).Where("id = ?", id).Update("enable", enable).Error; err != nil {
  412. return err
  413. }
  414. if mgr := runtime.GetManager(); mgr != nil {
  415. mgr.InvalidateNode(id)
  416. }
  417. return nil
  418. }
  419. // GetWebCertFiles asks a node for its own web TLS certificate/key file paths,
  420. // used by "Set Cert from Panel" so a node-assigned inbound gets paths that
  421. // exist on the node rather than the central panel. See issue #4854.
  422. func (s *NodeService) GetWebCertFiles(id int) (*runtime.WebCertFiles, error) {
  423. n, err := s.GetById(id)
  424. if err != nil || n == nil {
  425. return nil, fmt.Errorf("node not found")
  426. }
  427. if !n.Enable {
  428. return nil, fmt.Errorf("node is disabled")
  429. }
  430. mgr := runtime.GetManager()
  431. if mgr == nil {
  432. return nil, fmt.Errorf("runtime manager unavailable")
  433. }
  434. remote, err := mgr.RemoteFor(n)
  435. if err != nil {
  436. return nil, err
  437. }
  438. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  439. defer cancel()
  440. return remote.GetWebCertFiles(ctx)
  441. }
  442. // NodeUpdateResult reports the outcome of triggering a panel self-update on one
  443. // node so the UI can show per-node success/failure for a bulk request.
  444. type NodeUpdateResult struct {
  445. Id int `json:"id"`
  446. Name string `json:"name"`
  447. OK bool `json:"ok"`
  448. Error string `json:"error,omitempty"`
  449. }
  450. // UpdatePanels triggers the official self-updater on each given node. Only
  451. // enabled, online nodes are eligible — an offline node can't be reached, so it
  452. // is reported as skipped rather than silently dropped.
  453. func (s *NodeService) UpdatePanels(ids []int) ([]NodeUpdateResult, error) {
  454. mgr := runtime.GetManager()
  455. if mgr == nil {
  456. return nil, fmt.Errorf("runtime manager unavailable")
  457. }
  458. results := make([]NodeUpdateResult, 0, len(ids))
  459. for _, id := range ids {
  460. n, err := s.GetById(id)
  461. if err != nil || n == nil {
  462. results = append(results, NodeUpdateResult{Id: id, OK: false, Error: "node not found"})
  463. continue
  464. }
  465. res := NodeUpdateResult{Id: id, Name: n.Name}
  466. switch {
  467. case !n.Enable:
  468. res.Error = "node is disabled"
  469. case n.Status != "online":
  470. res.Error = "node is offline"
  471. default:
  472. remote, remoteErr := mgr.RemoteFor(n)
  473. if remoteErr != nil {
  474. res.Error = remoteErr.Error()
  475. break
  476. }
  477. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  478. updErr := remote.UpdatePanel(ctx)
  479. cancel()
  480. if updErr != nil {
  481. res.Error = updErr.Error()
  482. } else {
  483. res.OK = true
  484. }
  485. }
  486. results = append(results, res)
  487. }
  488. return results, nil
  489. }
  490. func (s *NodeService) UpdateHeartbeat(id int, p HeartbeatPatch) error {
  491. db := database.GetDB()
  492. updates := map[string]any{
  493. "status": p.Status,
  494. "last_heartbeat": p.LastHeartbeat,
  495. "latency_ms": p.LatencyMs,
  496. "xray_version": p.XrayVersion,
  497. "panel_version": p.PanelVersion,
  498. "cpu_pct": p.CpuPct,
  499. "mem_pct": p.MemPct,
  500. "uptime_secs": p.UptimeSecs,
  501. "last_error": p.LastError,
  502. "xray_state": p.XrayState,
  503. "xray_error": p.XrayError,
  504. }
  505. // Only learn the GUID; never clear a known one if an old-build node (or a
  506. // failed probe) reports none, so the stable identity survives blips.
  507. if p.Guid != "" {
  508. updates["guid"] = p.Guid
  509. }
  510. if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
  511. return err
  512. }
  513. if p.Status == "online" {
  514. now := time.Unix(p.LastHeartbeat, 0)
  515. nodeMetrics.append(nodeMetricKey(id, "cpu"), now, p.CpuPct)
  516. nodeMetrics.append(nodeMetricKey(id, "mem"), now, p.MemPct)
  517. }
  518. return nil
  519. }
  520. func (s *NodeService) MarkNodeDirty(id int) error {
  521. if id <= 0 {
  522. return nil
  523. }
  524. return database.GetDB().Model(model.Node{}).
  525. Where("id = ?", id).
  526. Updates(map[string]any{
  527. "config_dirty": true,
  528. "config_dirty_at": time.Now().UnixMilli(),
  529. }).Error
  530. }
  531. func (s *NodeService) ClearNodeDirty(id int, dirtyAt int64) error {
  532. if id <= 0 {
  533. return nil
  534. }
  535. return database.GetDB().Model(model.Node{}).
  536. Where("id = ? AND config_dirty_at = ?", id, dirtyAt).
  537. Update("config_dirty", false).Error
  538. }
  539. func (s *NodeService) NodeSyncState(id int) (enabled bool, status string, dirty bool, dirtyAt int64, err error) {
  540. if id <= 0 {
  541. return false, "", false, 0, errors.New("invalid node id")
  542. }
  543. var row model.Node
  544. err = database.GetDB().Model(model.Node{}).
  545. Select("enable", "status", "config_dirty", "config_dirty_at").
  546. Where("id = ?", id).
  547. First(&row).Error
  548. if err != nil {
  549. return false, "", false, 0, err
  550. }
  551. return row.Enable, row.Status, row.ConfigDirty, row.ConfigDirtyAt, nil
  552. }
  553. func (s *NodeService) IsNodePending(id int) bool {
  554. enabled, status, dirty, _, err := s.NodeSyncState(id)
  555. if err != nil {
  556. return false
  557. }
  558. return !enabled || status != "online" || dirty
  559. }
  560. func nodeMetricKey(id int, metric string) string {
  561. return "node:" + strconv.Itoa(id) + ":" + metric
  562. }
  563. func (s *NodeService) AggregateNodeMetric(id int, metric string, bucketSeconds int, maxPoints int) []map[string]any {
  564. return nodeMetrics.aggregate(nodeMetricKey(id, metric), bucketSeconds, maxPoints)
  565. }
  566. func (s *NodeService) Probe(ctx context.Context, n *model.Node) (HeartbeatPatch, error) {
  567. proxyURL := ""
  568. if n.OutboundTag != "" {
  569. if mgr := runtime.GetManager(); mgr != nil {
  570. proxyURL = mgr.NodeEgressProxyURL(n.Id)
  571. }
  572. }
  573. return s.probe(ctx, n, proxyURL)
  574. }
  575. func (s *NodeService) ProbeWithOutbound(ctx context.Context, n *model.Node, outboundTag string) (HeartbeatPatch, error) {
  576. if outboundTag == "" {
  577. return s.Probe(ctx, n)
  578. }
  579. proc := XrayProcess()
  580. if proc == nil || !proc.IsRunning() {
  581. return s.Probe(ctx, n)
  582. }
  583. apiPort := proc.GetAPIPort()
  584. if apiPort <= 0 {
  585. return s.Probe(ctx, n)
  586. }
  587. listener, err := net.Listen("tcp", "127.0.0.1:0")
  588. if err != nil {
  589. return s.Probe(ctx, n)
  590. }
  591. port := listener.Addr().(*net.TCPAddr).Port
  592. listener.Close()
  593. tag := fmt.Sprintf("node-test-%d-%d", n.Id, time.Now().UnixNano())
  594. proxyURL := fmt.Sprintf("socks5://127.0.0.1:%d", port)
  595. inboundJSON, err := json.Marshal(xray.InboundConfig{
  596. Listen: json_util.RawMessage(`"127.0.0.1"`),
  597. Port: port,
  598. Protocol: "socks",
  599. Settings: json_util.RawMessage(`{"auth":"noauth","udp":false}`),
  600. Tag: tag,
  601. })
  602. if err != nil {
  603. return s.Probe(ctx, n)
  604. }
  605. cfg := proc.GetConfig()
  606. routing := map[string]any{}
  607. if len(cfg.RouterConfig) > 0 {
  608. _ = json.Unmarshal(cfg.RouterConfig, &routing)
  609. }
  610. rules, _ := routing["rules"].([]any)
  611. rule := map[string]any{
  612. "type": "field",
  613. "inboundTag": []any{tag},
  614. }
  615. if routingTagIsBalancer(routing, outboundTag) {
  616. rule["balancerTag"] = outboundTag
  617. } else {
  618. rule["outboundTag"] = outboundTag
  619. }
  620. routing["rules"] = append([]any{rule}, rules...)
  621. routingJSON, err := json.Marshal(routing)
  622. if err != nil {
  623. return s.Probe(ctx, n)
  624. }
  625. originalRoutingJSON := cfg.RouterConfig
  626. api := xray.XrayAPI{}
  627. if err := api.Init(apiPort); err != nil {
  628. return s.Probe(ctx, n)
  629. }
  630. defer api.Close()
  631. if err := api.AddInbound(inboundJSON); err != nil {
  632. return s.Probe(ctx, n)
  633. }
  634. removed := false
  635. defer func() {
  636. if removed {
  637. return
  638. }
  639. if err := api.DelInbound(tag); err != nil {
  640. logger.Warning("remove temp node test inbound failed:", err)
  641. }
  642. }()
  643. if err := api.ApplyRoutingConfig(routingJSON); err != nil {
  644. return s.Probe(ctx, n)
  645. }
  646. defer func() {
  647. restore := originalRoutingJSON
  648. if len(restore) == 0 {
  649. restore = []byte("{}")
  650. }
  651. if err := api.ApplyRoutingConfig(restore); err != nil {
  652. logger.Warning("restore routing after node test failed:", err)
  653. }
  654. }()
  655. patch, err := s.probe(ctx, n, proxyURL)
  656. removed = true
  657. if delErr := api.DelInbound(tag); delErr != nil {
  658. logger.Warning("remove temp node test inbound failed:", delErr)
  659. }
  660. if err != nil {
  661. return patch, err
  662. }
  663. return patch, nil
  664. }
  665. func (s *NodeService) probe(ctx context.Context, n *model.Node, proxyURL string) (HeartbeatPatch, error) {
  666. patch := HeartbeatPatch{LastHeartbeat: time.Now().Unix()}
  667. addr, err := netsafe.NormalizeHost(n.Address)
  668. if err != nil {
  669. patch.LastError = err.Error()
  670. return patch, err
  671. }
  672. scheme := n.Scheme
  673. if scheme != "http" && scheme != "https" {
  674. scheme = "https"
  675. }
  676. if n.Port <= 0 || n.Port > 65535 {
  677. patch.LastError = "node port must be 1-65535"
  678. return patch, errors.New(patch.LastError)
  679. }
  680. probeURL := &url.URL{
  681. Scheme: scheme,
  682. Host: net.JoinHostPort(addr, strconv.Itoa(n.Port)),
  683. Path: normalizeBasePath(n.BasePath) + "panel/api/server/status",
  684. }
  685. req, err := http.NewRequestWithContext(
  686. netsafe.ContextWithAllowPrivate(ctx, n.AllowPrivateAddress),
  687. http.MethodGet, probeURL.String(), nil)
  688. if err != nil {
  689. patch.LastError = err.Error()
  690. return patch, err
  691. }
  692. if n.ApiToken != "" {
  693. req.Header.Set("Authorization", "Bearer "+n.ApiToken)
  694. }
  695. req.Header.Set("Accept", "application/json")
  696. client, err := runtime.HTTPClientForNode(n, proxyURL)
  697. if err != nil {
  698. patch.LastError = err.Error()
  699. return patch, err
  700. }
  701. start := time.Now()
  702. resp, err := client.Do(req)
  703. if err != nil {
  704. patch.LastError = err.Error()
  705. return patch, err
  706. }
  707. defer resp.Body.Close()
  708. patch.LatencyMs = int(time.Since(start) / time.Millisecond)
  709. if resp.StatusCode != http.StatusOK {
  710. patch.LastError = fmt.Sprintf("HTTP %d from remote panel", resp.StatusCode)
  711. return patch, errors.New(patch.LastError)
  712. }
  713. var envelope struct {
  714. Success bool `json:"success"`
  715. Msg string `json:"msg"`
  716. Obj *struct {
  717. CpuPct float64 `json:"cpu"`
  718. Mem struct {
  719. Current uint64 `json:"current"`
  720. Total uint64 `json:"total"`
  721. } `json:"mem"`
  722. Xray struct {
  723. Version string `json:"version"`
  724. State string `json:"state"`
  725. ErrorMsg string `json:"errorMsg"`
  726. } `json:"xray"`
  727. PanelVersion string `json:"panelVersion"`
  728. PanelGuid string `json:"panelGuid"`
  729. Uptime uint64 `json:"uptime"`
  730. } `json:"obj"`
  731. }
  732. if err := json.NewDecoder(resp.Body).Decode(&envelope); err != nil {
  733. patch.LastError = "decode response: " + err.Error()
  734. return patch, err
  735. }
  736. if !envelope.Success || envelope.Obj == nil {
  737. patch.LastError = "remote returned success=false: " + envelope.Msg
  738. return patch, errors.New(patch.LastError)
  739. }
  740. o := envelope.Obj
  741. patch.CpuPct = o.CpuPct
  742. if o.Mem.Total > 0 {
  743. patch.MemPct = float64(o.Mem.Current) * 100.0 / float64(o.Mem.Total)
  744. }
  745. patch.XrayVersion = o.Xray.Version
  746. patch.XrayState = o.Xray.State
  747. patch.XrayError = o.Xray.ErrorMsg
  748. patch.PanelVersion = o.PanelVersion
  749. patch.Guid = o.PanelGuid
  750. patch.UptimeSecs = o.Uptime
  751. return patch, nil
  752. }
  753. type ProbeResultUI struct {
  754. Status string `json:"status" example:"online"`
  755. LatencyMs int `json:"latencyMs" example:"42"`
  756. XrayVersion string `json:"xrayVersion" example:"25.10.31"`
  757. PanelVersion string `json:"panelVersion" example:"v3.x.x"`
  758. CpuPct float64 `json:"cpuPct" example:"12.5"`
  759. MemPct float64 `json:"memPct" example:"45.2"`
  760. UptimeSecs uint64 `json:"uptimeSecs" example:"86400"`
  761. Error string `json:"error"`
  762. // XrayState/XrayError are populated on successful probes even when the node's
  763. // Xray core is not healthy. The UI uses them for a distinct "panel ok, xray failed" indicator.
  764. XrayState string `json:"xrayState"`
  765. XrayError string `json:"xrayError"`
  766. }
  767. func (p HeartbeatPatch) ToUI(ok bool) ProbeResultUI {
  768. r := ProbeResultUI{
  769. LatencyMs: p.LatencyMs,
  770. XrayVersion: p.XrayVersion,
  771. PanelVersion: p.PanelVersion,
  772. CpuPct: p.CpuPct,
  773. MemPct: p.MemPct,
  774. UptimeSecs: p.UptimeSecs,
  775. Error: FriendlyProbeError(p.LastError),
  776. XrayState: p.XrayState,
  777. XrayError: p.XrayError,
  778. }
  779. if ok {
  780. r.Status = "online"
  781. } else {
  782. r.Status = "offline"
  783. }
  784. return r
  785. }
  786. func FriendlyProbeError(msg string) string {
  787. if strings.Contains(msg, "server gave HTTP response to HTTPS client") {
  788. return "the server speaks HTTP, not HTTPS; set the node scheme to http"
  789. }
  790. return msg
  791. }