node.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. package service
  2. import (
  3. "context"
  4. "crypto/sha256"
  5. "crypto/subtle"
  6. "crypto/tls"
  7. "encoding/base64"
  8. "encoding/hex"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "net"
  13. "net/http"
  14. "net/url"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "github.com/mhsanaei/3x-ui/v3/database"
  19. "github.com/mhsanaei/3x-ui/v3/database/model"
  20. "github.com/mhsanaei/3x-ui/v3/util/common"
  21. "github.com/mhsanaei/3x-ui/v3/util/netsafe"
  22. "github.com/mhsanaei/3x-ui/v3/web/runtime"
  23. )
  24. type HeartbeatPatch struct {
  25. Status string
  26. LastHeartbeat int64
  27. LatencyMs int
  28. XrayVersion string
  29. PanelVersion string
  30. CpuPct float64
  31. MemPct float64
  32. UptimeSecs uint64
  33. LastError string
  34. }
  35. type NodeService struct{}
  36. var nodeHTTPClient = &http.Client{
  37. Transport: &http.Transport{
  38. MaxIdleConns: 64,
  39. MaxIdleConnsPerHost: 4,
  40. IdleConnTimeout: 60 * time.Second,
  41. DialContext: netsafe.SSRFGuardedDialContext,
  42. },
  43. }
  44. // nodeHTTPClientFor returns the HTTP client used to reach a node, honoring its
  45. // per-node TLS verification mode. "verify" (or any http node) uses the shared
  46. // client with default certificate validation. "skip" disables validation.
  47. // "pin" disables the default chain check but verifies the leaf certificate's
  48. // SHA-256 against the stored pin, keeping MITM protection for self-signed certs.
  49. func nodeHTTPClientFor(n *model.Node) (*http.Client, error) {
  50. mode := n.TlsVerifyMode
  51. if mode == "" {
  52. mode = "verify"
  53. }
  54. if mode == "verify" || n.Scheme == "http" {
  55. return nodeHTTPClient, nil
  56. }
  57. tlsCfg := &tls.Config{InsecureSkipVerify: true}
  58. if mode == "pin" {
  59. want, err := decodeCertPin(n.PinnedCertSha256)
  60. if err != nil {
  61. return nil, err
  62. }
  63. tlsCfg.VerifyConnection = func(cs tls.ConnectionState) error {
  64. if len(cs.PeerCertificates) == 0 {
  65. return common.NewError("node presented no certificate")
  66. }
  67. sum := sha256.Sum256(cs.PeerCertificates[0].Raw)
  68. if subtle.ConstantTimeCompare(sum[:], want) != 1 {
  69. return common.NewError("node certificate does not match pinned SHA-256")
  70. }
  71. return nil
  72. }
  73. }
  74. return &http.Client{
  75. Transport: &http.Transport{
  76. MaxIdleConns: 64,
  77. MaxIdleConnsPerHost: 4,
  78. IdleConnTimeout: 60 * time.Second,
  79. DialContext: netsafe.SSRFGuardedDialContext,
  80. TLSClientConfig: tlsCfg,
  81. },
  82. }, nil
  83. }
  84. // decodeCertPin accepts a SHA-256 certificate hash as base64 (the format used
  85. // by Xray's pinnedPeerCertSha256) or hex with optional colons (the openssl
  86. // -fingerprint style) and returns the 32 raw bytes.
  87. func decodeCertPin(s string) ([]byte, error) {
  88. s = strings.TrimSpace(s)
  89. if s == "" {
  90. return nil, common.NewError("certificate pin is empty")
  91. }
  92. if b, err := hex.DecodeString(strings.ReplaceAll(s, ":", "")); err == nil && len(b) == sha256.Size {
  93. return b, nil
  94. }
  95. for _, enc := range []*base64.Encoding{base64.StdEncoding, base64.RawStdEncoding, base64.URLEncoding, base64.RawURLEncoding} {
  96. if b, err := enc.DecodeString(s); err == nil && len(b) == sha256.Size {
  97. return b, nil
  98. }
  99. }
  100. return nil, common.NewError("certificate pin must be a SHA-256 hash (base64 or hex)")
  101. }
  102. // FetchCertFingerprint connects to the node over HTTPS without verifying the
  103. // certificate and returns the leaf certificate's SHA-256 as base64, so the UI
  104. // can offer a "fetch and pin current certificate" action.
  105. func (s *NodeService) FetchCertFingerprint(ctx context.Context, n *model.Node) (string, error) {
  106. addr, err := netsafe.NormalizeHost(n.Address)
  107. if err != nil {
  108. return "", err
  109. }
  110. scheme := n.Scheme
  111. if scheme != "http" && scheme != "https" {
  112. scheme = "https"
  113. }
  114. if scheme != "https" {
  115. return "", common.NewError("certificate pinning is only available for https nodes")
  116. }
  117. if n.Port <= 0 || n.Port > 65535 {
  118. return "", common.NewError("node port must be 1-65535")
  119. }
  120. probeURL := &url.URL{
  121. Scheme: scheme,
  122. Host: net.JoinHostPort(addr, strconv.Itoa(n.Port)),
  123. Path: normalizeBasePath(n.BasePath) + "panel/api/server/status",
  124. }
  125. req, err := http.NewRequestWithContext(
  126. netsafe.ContextWithAllowPrivate(ctx, n.AllowPrivateAddress),
  127. http.MethodGet, probeURL.String(), nil)
  128. if err != nil {
  129. return "", err
  130. }
  131. var fingerprint string
  132. client := &http.Client{
  133. Transport: &http.Transport{
  134. DialContext: netsafe.SSRFGuardedDialContext,
  135. TLSClientConfig: &tls.Config{
  136. InsecureSkipVerify: true,
  137. VerifyConnection: func(cs tls.ConnectionState) error {
  138. if len(cs.PeerCertificates) > 0 {
  139. sum := sha256.Sum256(cs.PeerCertificates[0].Raw)
  140. fingerprint = base64.StdEncoding.EncodeToString(sum[:])
  141. }
  142. return nil
  143. },
  144. },
  145. },
  146. }
  147. resp, err := client.Do(req)
  148. if err != nil {
  149. return "", err
  150. }
  151. defer resp.Body.Close()
  152. if fingerprint == "" {
  153. return "", common.NewError("node did not present a TLS certificate")
  154. }
  155. return fingerprint, nil
  156. }
  157. func (s *NodeService) GetAll() ([]*model.Node, error) {
  158. db := database.GetDB()
  159. var nodes []*model.Node
  160. err := db.Model(model.Node{}).Order("id asc").Find(&nodes).Error
  161. if err != nil || len(nodes) == 0 {
  162. return nodes, err
  163. }
  164. type inboundRow struct {
  165. Id int
  166. NodeID int `gorm:"column:node_id"`
  167. }
  168. var inboundRows []inboundRow
  169. if err := db.Table("inbounds").
  170. Select("id, node_id").
  171. Where("node_id IS NOT NULL").
  172. Scan(&inboundRows).Error; err != nil {
  173. return nodes, nil
  174. }
  175. if len(inboundRows) == 0 {
  176. return nodes, nil
  177. }
  178. inboundsByNode := make(map[int][]int, len(nodes))
  179. nodeByInbound := make(map[int]int, len(inboundRows))
  180. for _, row := range inboundRows {
  181. inboundsByNode[row.NodeID] = append(inboundsByNode[row.NodeID], row.Id)
  182. nodeByInbound[row.Id] = row.NodeID
  183. }
  184. type clientCountRow struct {
  185. NodeID int `gorm:"column:node_id"`
  186. Count int `gorm:"column:count"`
  187. }
  188. var clientCounts []clientCountRow
  189. if err := db.Raw(`
  190. SELECT inbounds.node_id AS node_id, COUNT(DISTINCT client_inbounds.client_id) AS count
  191. FROM inbounds
  192. JOIN client_inbounds ON client_inbounds.inbound_id = inbounds.id
  193. WHERE inbounds.node_id IS NOT NULL
  194. GROUP BY inbounds.node_id
  195. `).Scan(&clientCounts).Error; err == nil {
  196. for _, row := range clientCounts {
  197. for _, n := range nodes {
  198. if n.Id == row.NodeID {
  199. n.ClientCount = row.Count
  200. break
  201. }
  202. }
  203. }
  204. }
  205. now := time.Now().UnixMilli()
  206. type trafficRow struct {
  207. InboundID int `gorm:"column:inbound_id"`
  208. Email string
  209. Enable bool
  210. Total int64
  211. Up int64
  212. Down int64
  213. ExpiryTime int64 `gorm:"column:expiry_time"`
  214. }
  215. var trafficRows []trafficRow
  216. inboundIDs := make([]int, 0, len(nodeByInbound))
  217. for id := range nodeByInbound {
  218. inboundIDs = append(inboundIDs, id)
  219. }
  220. if err := db.Table("client_traffics").
  221. Select("inbound_id, email, enable, total, up, down, expiry_time").
  222. Where("inbound_id IN ?", inboundIDs).
  223. Scan(&trafficRows).Error; err == nil {
  224. online := make(map[string]struct{})
  225. for _, email := range s.onlineEmails() {
  226. online[email] = struct{}{}
  227. }
  228. depletedByNode := make(map[int]int)
  229. onlineByNode := make(map[int]int)
  230. for _, row := range trafficRows {
  231. nodeID, ok := nodeByInbound[row.InboundID]
  232. if !ok {
  233. continue
  234. }
  235. expired := row.ExpiryTime > 0 && row.ExpiryTime <= now
  236. exhausted := row.Total > 0 && row.Up+row.Down >= row.Total
  237. if expired || exhausted || !row.Enable {
  238. depletedByNode[nodeID]++
  239. }
  240. if _, ok := online[row.Email]; ok {
  241. onlineByNode[nodeID]++
  242. }
  243. }
  244. for _, n := range nodes {
  245. n.InboundCount = len(inboundsByNode[n.Id])
  246. n.DepletedCount = depletedByNode[n.Id]
  247. n.OnlineCount = onlineByNode[n.Id]
  248. }
  249. }
  250. return nodes, nil
  251. }
  252. func (s *NodeService) onlineEmails() []string {
  253. svc := InboundService{}
  254. return svc.GetOnlineClients()
  255. }
  256. func (s *NodeService) GetById(id int) (*model.Node, error) {
  257. db := database.GetDB()
  258. n := &model.Node{}
  259. if err := db.Model(model.Node{}).Where("id = ?", id).First(n).Error; err != nil {
  260. return nil, err
  261. }
  262. return n, nil
  263. }
  264. func normalizeBasePath(p string) string {
  265. p = strings.TrimSpace(p)
  266. if p == "" {
  267. return "/"
  268. }
  269. if !strings.HasPrefix(p, "/") {
  270. p = "/" + p
  271. }
  272. if !strings.HasSuffix(p, "/") {
  273. p = p + "/"
  274. }
  275. return p
  276. }
  277. func (s *NodeService) normalize(n *model.Node) error {
  278. n.Name = strings.TrimSpace(n.Name)
  279. n.ApiToken = strings.TrimSpace(n.ApiToken)
  280. if n.Name == "" {
  281. return common.NewError("node name is required")
  282. }
  283. addr, err := netsafe.NormalizeHost(n.Address)
  284. if err != nil {
  285. return common.NewError(err.Error())
  286. }
  287. n.Address = addr
  288. if n.Port <= 0 || n.Port > 65535 {
  289. return common.NewError("node port must be 1-65535")
  290. }
  291. if n.Scheme != "http" && n.Scheme != "https" {
  292. n.Scheme = "https"
  293. }
  294. if n.TlsVerifyMode != "skip" && n.TlsVerifyMode != "pin" {
  295. n.TlsVerifyMode = "verify"
  296. }
  297. n.PinnedCertSha256 = strings.TrimSpace(n.PinnedCertSha256)
  298. if n.TlsVerifyMode == "pin" {
  299. if _, err := decodeCertPin(n.PinnedCertSha256); err != nil {
  300. return common.NewError(err.Error())
  301. }
  302. }
  303. n.BasePath = normalizeBasePath(n.BasePath)
  304. return nil
  305. }
  306. func (s *NodeService) Create(n *model.Node) error {
  307. if err := s.normalize(n); err != nil {
  308. return err
  309. }
  310. db := database.GetDB()
  311. return db.Create(n).Error
  312. }
  313. func (s *NodeService) Update(id int, in *model.Node) error {
  314. if err := s.normalize(in); err != nil {
  315. return err
  316. }
  317. db := database.GetDB()
  318. existing := &model.Node{}
  319. if err := db.Where("id = ?", id).First(existing).Error; err != nil {
  320. return err
  321. }
  322. updates := map[string]any{
  323. "name": in.Name,
  324. "remark": in.Remark,
  325. "scheme": in.Scheme,
  326. "address": in.Address,
  327. "port": in.Port,
  328. "base_path": in.BasePath,
  329. "api_token": in.ApiToken,
  330. "enable": in.Enable,
  331. "allow_private_address": in.AllowPrivateAddress,
  332. "tls_verify_mode": in.TlsVerifyMode,
  333. "pinned_cert_sha256": in.PinnedCertSha256,
  334. }
  335. if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
  336. return err
  337. }
  338. if mgr := runtime.GetManager(); mgr != nil {
  339. mgr.InvalidateNode(id)
  340. }
  341. return nil
  342. }
  343. func (s *NodeService) Delete(id int) error {
  344. db := database.GetDB()
  345. if err := db.Where("id = ?", id).Delete(model.Node{}).Error; err != nil {
  346. return err
  347. }
  348. if err := db.Where("node_id = ?", id).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  349. return err
  350. }
  351. if mgr := runtime.GetManager(); mgr != nil {
  352. mgr.InvalidateNode(id)
  353. }
  354. nodeMetrics.drop(nodeMetricKey(id, "cpu"))
  355. nodeMetrics.drop(nodeMetricKey(id, "mem"))
  356. return nil
  357. }
  358. func (s *NodeService) SetEnable(id int, enable bool) error {
  359. db := database.GetDB()
  360. return db.Model(model.Node{}).Where("id = ?", id).Update("enable", enable).Error
  361. }
  362. // NodeUpdateResult reports the outcome of triggering a panel self-update on one
  363. // node so the UI can show per-node success/failure for a bulk request.
  364. type NodeUpdateResult struct {
  365. Id int `json:"id"`
  366. Name string `json:"name"`
  367. OK bool `json:"ok"`
  368. Error string `json:"error,omitempty"`
  369. }
  370. // UpdatePanels triggers the official self-updater on each given node. Only
  371. // enabled, online nodes are eligible — an offline node can't be reached, so it
  372. // is reported as skipped rather than silently dropped.
  373. func (s *NodeService) UpdatePanels(ids []int) ([]NodeUpdateResult, error) {
  374. mgr := runtime.GetManager()
  375. if mgr == nil {
  376. return nil, fmt.Errorf("runtime manager unavailable")
  377. }
  378. results := make([]NodeUpdateResult, 0, len(ids))
  379. for _, id := range ids {
  380. n, err := s.GetById(id)
  381. if err != nil || n == nil {
  382. results = append(results, NodeUpdateResult{Id: id, OK: false, Error: "node not found"})
  383. continue
  384. }
  385. res := NodeUpdateResult{Id: id, Name: n.Name}
  386. switch {
  387. case !n.Enable:
  388. res.Error = "node is disabled"
  389. case n.Status != "online":
  390. res.Error = "node is offline"
  391. default:
  392. remote, remoteErr := mgr.RemoteFor(n)
  393. if remoteErr != nil {
  394. res.Error = remoteErr.Error()
  395. break
  396. }
  397. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  398. updErr := remote.UpdatePanel(ctx)
  399. cancel()
  400. if updErr != nil {
  401. res.Error = updErr.Error()
  402. } else {
  403. res.OK = true
  404. }
  405. }
  406. results = append(results, res)
  407. }
  408. return results, nil
  409. }
  410. func (s *NodeService) UpdateHeartbeat(id int, p HeartbeatPatch) error {
  411. db := database.GetDB()
  412. updates := map[string]any{
  413. "status": p.Status,
  414. "last_heartbeat": p.LastHeartbeat,
  415. "latency_ms": p.LatencyMs,
  416. "xray_version": p.XrayVersion,
  417. "panel_version": p.PanelVersion,
  418. "cpu_pct": p.CpuPct,
  419. "mem_pct": p.MemPct,
  420. "uptime_secs": p.UptimeSecs,
  421. "last_error": p.LastError,
  422. }
  423. if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
  424. return err
  425. }
  426. if p.Status == "online" {
  427. now := time.Unix(p.LastHeartbeat, 0)
  428. nodeMetrics.append(nodeMetricKey(id, "cpu"), now, p.CpuPct)
  429. nodeMetrics.append(nodeMetricKey(id, "mem"), now, p.MemPct)
  430. }
  431. return nil
  432. }
  433. func nodeMetricKey(id int, metric string) string {
  434. return "node:" + strconv.Itoa(id) + ":" + metric
  435. }
  436. func (s *NodeService) AggregateNodeMetric(id int, metric string, bucketSeconds int, maxPoints int) []map[string]any {
  437. return nodeMetrics.aggregate(nodeMetricKey(id, metric), bucketSeconds, maxPoints)
  438. }
  439. func (s *NodeService) Probe(ctx context.Context, n *model.Node) (HeartbeatPatch, error) {
  440. patch := HeartbeatPatch{LastHeartbeat: time.Now().Unix()}
  441. addr, err := netsafe.NormalizeHost(n.Address)
  442. if err != nil {
  443. patch.LastError = err.Error()
  444. return patch, err
  445. }
  446. scheme := n.Scheme
  447. if scheme != "http" && scheme != "https" {
  448. scheme = "https"
  449. }
  450. if n.Port <= 0 || n.Port > 65535 {
  451. patch.LastError = "node port must be 1-65535"
  452. return patch, errors.New(patch.LastError)
  453. }
  454. probeURL := &url.URL{
  455. Scheme: scheme,
  456. Host: net.JoinHostPort(addr, strconv.Itoa(n.Port)),
  457. Path: normalizeBasePath(n.BasePath) + "panel/api/server/status",
  458. }
  459. req, err := http.NewRequestWithContext(
  460. netsafe.ContextWithAllowPrivate(ctx, n.AllowPrivateAddress),
  461. http.MethodGet, probeURL.String(), nil)
  462. if err != nil {
  463. patch.LastError = err.Error()
  464. return patch, err
  465. }
  466. if n.ApiToken != "" {
  467. req.Header.Set("Authorization", "Bearer "+n.ApiToken)
  468. }
  469. req.Header.Set("Accept", "application/json")
  470. client, err := nodeHTTPClientFor(n)
  471. if err != nil {
  472. patch.LastError = err.Error()
  473. return patch, err
  474. }
  475. start := time.Now()
  476. resp, err := client.Do(req)
  477. if err != nil {
  478. patch.LastError = err.Error()
  479. return patch, err
  480. }
  481. defer resp.Body.Close()
  482. patch.LatencyMs = int(time.Since(start) / time.Millisecond)
  483. if resp.StatusCode != http.StatusOK {
  484. patch.LastError = fmt.Sprintf("HTTP %d from remote panel", resp.StatusCode)
  485. return patch, errors.New(patch.LastError)
  486. }
  487. var envelope struct {
  488. Success bool `json:"success"`
  489. Msg string `json:"msg"`
  490. Obj *struct {
  491. CpuPct float64 `json:"cpu"`
  492. Mem struct {
  493. Current uint64 `json:"current"`
  494. Total uint64 `json:"total"`
  495. } `json:"mem"`
  496. Xray struct {
  497. Version string `json:"version"`
  498. } `json:"xray"`
  499. PanelVersion string `json:"panelVersion"`
  500. Uptime uint64 `json:"uptime"`
  501. } `json:"obj"`
  502. }
  503. if err := json.NewDecoder(resp.Body).Decode(&envelope); err != nil {
  504. patch.LastError = "decode response: " + err.Error()
  505. return patch, err
  506. }
  507. if !envelope.Success || envelope.Obj == nil {
  508. patch.LastError = "remote returned success=false: " + envelope.Msg
  509. return patch, errors.New(patch.LastError)
  510. }
  511. o := envelope.Obj
  512. patch.CpuPct = o.CpuPct
  513. if o.Mem.Total > 0 {
  514. patch.MemPct = float64(o.Mem.Current) * 100.0 / float64(o.Mem.Total)
  515. }
  516. patch.XrayVersion = o.Xray.Version
  517. patch.PanelVersion = o.PanelVersion
  518. patch.UptimeSecs = o.Uptime
  519. return patch, nil
  520. }
  521. type ProbeResultUI struct {
  522. Status string `json:"status"`
  523. LatencyMs int `json:"latencyMs"`
  524. XrayVersion string `json:"xrayVersion"`
  525. PanelVersion string `json:"panelVersion"`
  526. CpuPct float64 `json:"cpuPct"`
  527. MemPct float64 `json:"memPct"`
  528. UptimeSecs uint64 `json:"uptimeSecs"`
  529. Error string `json:"error"`
  530. }
  531. func (p HeartbeatPatch) ToUI(ok bool) ProbeResultUI {
  532. r := ProbeResultUI{
  533. LatencyMs: p.LatencyMs,
  534. XrayVersion: p.XrayVersion,
  535. PanelVersion: p.PanelVersion,
  536. CpuPct: p.CpuPct,
  537. MemPct: p.MemPct,
  538. UptimeSecs: p.UptimeSecs,
  539. Error: p.LastError,
  540. }
  541. if ok {
  542. r.Status = "online"
  543. } else {
  544. r.Status = "offline"
  545. }
  546. return r
  547. }