node.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. client := &http.Client{
  132. Transport: &http.Transport{
  133. DialContext: netsafe.SSRFGuardedDialContext,
  134. TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // lgtm[go/disabled-certificate-check]
  135. },
  136. }
  137. resp, err := client.Do(req)
  138. if err != nil {
  139. return "", err
  140. }
  141. defer resp.Body.Close()
  142. if resp.TLS == nil || len(resp.TLS.PeerCertificates) == 0 {
  143. return "", common.NewError("node did not present a TLS certificate")
  144. }
  145. sum := sha256.Sum256(resp.TLS.PeerCertificates[0].Raw)
  146. return base64.StdEncoding.EncodeToString(sum[:]), nil
  147. }
  148. func (s *NodeService) GetAll() ([]*model.Node, error) {
  149. db := database.GetDB()
  150. var nodes []*model.Node
  151. err := db.Model(model.Node{}).Order("id asc").Find(&nodes).Error
  152. if err != nil || len(nodes) == 0 {
  153. return nodes, err
  154. }
  155. type inboundRow struct {
  156. Id int
  157. NodeID int `gorm:"column:node_id"`
  158. }
  159. var inboundRows []inboundRow
  160. if err := db.Table("inbounds").
  161. Select("id, node_id").
  162. Where("node_id IS NOT NULL").
  163. Scan(&inboundRows).Error; err != nil {
  164. return nodes, nil
  165. }
  166. if len(inboundRows) == 0 {
  167. return nodes, nil
  168. }
  169. inboundsByNode := make(map[int][]int, len(nodes))
  170. nodeByInbound := make(map[int]int, len(inboundRows))
  171. for _, row := range inboundRows {
  172. inboundsByNode[row.NodeID] = append(inboundsByNode[row.NodeID], row.Id)
  173. nodeByInbound[row.Id] = row.NodeID
  174. }
  175. type clientCountRow struct {
  176. NodeID int `gorm:"column:node_id"`
  177. Count int `gorm:"column:count"`
  178. }
  179. var clientCounts []clientCountRow
  180. if err := db.Raw(`
  181. SELECT inbounds.node_id AS node_id, COUNT(DISTINCT client_inbounds.client_id) AS count
  182. FROM inbounds
  183. JOIN client_inbounds ON client_inbounds.inbound_id = inbounds.id
  184. WHERE inbounds.node_id IS NOT NULL
  185. GROUP BY inbounds.node_id
  186. `).Scan(&clientCounts).Error; err == nil {
  187. for _, row := range clientCounts {
  188. for _, n := range nodes {
  189. if n.Id == row.NodeID {
  190. n.ClientCount = row.Count
  191. break
  192. }
  193. }
  194. }
  195. }
  196. now := time.Now().UnixMilli()
  197. type trafficRow struct {
  198. InboundID int `gorm:"column:inbound_id"`
  199. Email string
  200. Enable bool
  201. Total int64
  202. Up int64
  203. Down int64
  204. ExpiryTime int64 `gorm:"column:expiry_time"`
  205. }
  206. var trafficRows []trafficRow
  207. inboundIDs := make([]int, 0, len(nodeByInbound))
  208. for id := range nodeByInbound {
  209. inboundIDs = append(inboundIDs, id)
  210. }
  211. if err := db.Table("client_traffics").
  212. Select("inbound_id, email, enable, total, up, down, expiry_time").
  213. Where("inbound_id IN ?", inboundIDs).
  214. Scan(&trafficRows).Error; err == nil {
  215. online := make(map[string]struct{})
  216. for _, email := range s.onlineEmails() {
  217. online[email] = struct{}{}
  218. }
  219. depletedByNode := make(map[int]int)
  220. onlineByNode := make(map[int]int)
  221. for _, row := range trafficRows {
  222. nodeID, ok := nodeByInbound[row.InboundID]
  223. if !ok {
  224. continue
  225. }
  226. expired := row.ExpiryTime > 0 && row.ExpiryTime <= now
  227. exhausted := row.Total > 0 && row.Up+row.Down >= row.Total
  228. if expired || exhausted || !row.Enable {
  229. depletedByNode[nodeID]++
  230. }
  231. if _, ok := online[row.Email]; ok {
  232. onlineByNode[nodeID]++
  233. }
  234. }
  235. for _, n := range nodes {
  236. n.InboundCount = len(inboundsByNode[n.Id])
  237. n.DepletedCount = depletedByNode[n.Id]
  238. n.OnlineCount = onlineByNode[n.Id]
  239. }
  240. }
  241. return nodes, nil
  242. }
  243. func (s *NodeService) onlineEmails() []string {
  244. svc := InboundService{}
  245. return svc.GetOnlineClients()
  246. }
  247. func (s *NodeService) GetById(id int) (*model.Node, error) {
  248. db := database.GetDB()
  249. n := &model.Node{}
  250. if err := db.Model(model.Node{}).Where("id = ?", id).First(n).Error; err != nil {
  251. return nil, err
  252. }
  253. return n, nil
  254. }
  255. func normalizeBasePath(p string) string {
  256. p = strings.TrimSpace(p)
  257. if p == "" {
  258. return "/"
  259. }
  260. if !strings.HasPrefix(p, "/") {
  261. p = "/" + p
  262. }
  263. if !strings.HasSuffix(p, "/") {
  264. p = p + "/"
  265. }
  266. return p
  267. }
  268. func (s *NodeService) normalize(n *model.Node) error {
  269. n.Name = strings.TrimSpace(n.Name)
  270. n.ApiToken = strings.TrimSpace(n.ApiToken)
  271. if n.Name == "" {
  272. return common.NewError("node name is required")
  273. }
  274. addr, err := netsafe.NormalizeHost(n.Address)
  275. if err != nil {
  276. return common.NewError(err.Error())
  277. }
  278. n.Address = addr
  279. if n.Port <= 0 || n.Port > 65535 {
  280. return common.NewError("node port must be 1-65535")
  281. }
  282. if n.Scheme != "http" && n.Scheme != "https" {
  283. n.Scheme = "https"
  284. }
  285. if n.TlsVerifyMode != "skip" && n.TlsVerifyMode != "pin" {
  286. n.TlsVerifyMode = "verify"
  287. }
  288. n.PinnedCertSha256 = strings.TrimSpace(n.PinnedCertSha256)
  289. if n.TlsVerifyMode == "pin" {
  290. if _, err := decodeCertPin(n.PinnedCertSha256); err != nil {
  291. return common.NewError(err.Error())
  292. }
  293. }
  294. n.BasePath = normalizeBasePath(n.BasePath)
  295. return nil
  296. }
  297. func (s *NodeService) Create(n *model.Node) error {
  298. if err := s.normalize(n); err != nil {
  299. return err
  300. }
  301. db := database.GetDB()
  302. return db.Create(n).Error
  303. }
  304. func (s *NodeService) Update(id int, in *model.Node) error {
  305. if err := s.normalize(in); 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. }
  326. if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
  327. return err
  328. }
  329. if mgr := runtime.GetManager(); mgr != nil {
  330. mgr.InvalidateNode(id)
  331. }
  332. return nil
  333. }
  334. func (s *NodeService) Delete(id int) error {
  335. db := database.GetDB()
  336. if err := db.Where("id = ?", id).Delete(model.Node{}).Error; err != nil {
  337. return err
  338. }
  339. if err := db.Where("node_id = ?", id).Delete(&model.NodeClientTraffic{}).Error; err != nil {
  340. return err
  341. }
  342. if mgr := runtime.GetManager(); mgr != nil {
  343. mgr.InvalidateNode(id)
  344. }
  345. nodeMetrics.drop(nodeMetricKey(id, "cpu"))
  346. nodeMetrics.drop(nodeMetricKey(id, "mem"))
  347. return nil
  348. }
  349. func (s *NodeService) SetEnable(id int, enable bool) error {
  350. db := database.GetDB()
  351. return db.Model(model.Node{}).Where("id = ?", id).Update("enable", enable).Error
  352. }
  353. // NodeUpdateResult reports the outcome of triggering a panel self-update on one
  354. // node so the UI can show per-node success/failure for a bulk request.
  355. type NodeUpdateResult struct {
  356. Id int `json:"id"`
  357. Name string `json:"name"`
  358. OK bool `json:"ok"`
  359. Error string `json:"error,omitempty"`
  360. }
  361. // UpdatePanels triggers the official self-updater on each given node. Only
  362. // enabled, online nodes are eligible — an offline node can't be reached, so it
  363. // is reported as skipped rather than silently dropped.
  364. func (s *NodeService) UpdatePanels(ids []int) ([]NodeUpdateResult, error) {
  365. mgr := runtime.GetManager()
  366. if mgr == nil {
  367. return nil, fmt.Errorf("runtime manager unavailable")
  368. }
  369. results := make([]NodeUpdateResult, 0, len(ids))
  370. for _, id := range ids {
  371. n, err := s.GetById(id)
  372. if err != nil || n == nil {
  373. results = append(results, NodeUpdateResult{Id: id, OK: false, Error: "node not found"})
  374. continue
  375. }
  376. res := NodeUpdateResult{Id: id, Name: n.Name}
  377. switch {
  378. case !n.Enable:
  379. res.Error = "node is disabled"
  380. case n.Status != "online":
  381. res.Error = "node is offline"
  382. default:
  383. remote, remoteErr := mgr.RemoteFor(n)
  384. if remoteErr != nil {
  385. res.Error = remoteErr.Error()
  386. break
  387. }
  388. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  389. updErr := remote.UpdatePanel(ctx)
  390. cancel()
  391. if updErr != nil {
  392. res.Error = updErr.Error()
  393. } else {
  394. res.OK = true
  395. }
  396. }
  397. results = append(results, res)
  398. }
  399. return results, nil
  400. }
  401. func (s *NodeService) UpdateHeartbeat(id int, p HeartbeatPatch) error {
  402. db := database.GetDB()
  403. updates := map[string]any{
  404. "status": p.Status,
  405. "last_heartbeat": p.LastHeartbeat,
  406. "latency_ms": p.LatencyMs,
  407. "xray_version": p.XrayVersion,
  408. "panel_version": p.PanelVersion,
  409. "cpu_pct": p.CpuPct,
  410. "mem_pct": p.MemPct,
  411. "uptime_secs": p.UptimeSecs,
  412. "last_error": p.LastError,
  413. }
  414. if err := db.Model(model.Node{}).Where("id = ?", id).Updates(updates).Error; err != nil {
  415. return err
  416. }
  417. if p.Status == "online" {
  418. now := time.Unix(p.LastHeartbeat, 0)
  419. nodeMetrics.append(nodeMetricKey(id, "cpu"), now, p.CpuPct)
  420. nodeMetrics.append(nodeMetricKey(id, "mem"), now, p.MemPct)
  421. }
  422. return nil
  423. }
  424. func nodeMetricKey(id int, metric string) string {
  425. return "node:" + strconv.Itoa(id) + ":" + metric
  426. }
  427. func (s *NodeService) AggregateNodeMetric(id int, metric string, bucketSeconds int, maxPoints int) []map[string]any {
  428. return nodeMetrics.aggregate(nodeMetricKey(id, metric), bucketSeconds, maxPoints)
  429. }
  430. func (s *NodeService) Probe(ctx context.Context, n *model.Node) (HeartbeatPatch, error) {
  431. patch := HeartbeatPatch{LastHeartbeat: time.Now().Unix()}
  432. addr, err := netsafe.NormalizeHost(n.Address)
  433. if err != nil {
  434. patch.LastError = err.Error()
  435. return patch, err
  436. }
  437. scheme := n.Scheme
  438. if scheme != "http" && scheme != "https" {
  439. scheme = "https"
  440. }
  441. if n.Port <= 0 || n.Port > 65535 {
  442. patch.LastError = "node port must be 1-65535"
  443. return patch, errors.New(patch.LastError)
  444. }
  445. probeURL := &url.URL{
  446. Scheme: scheme,
  447. Host: net.JoinHostPort(addr, strconv.Itoa(n.Port)),
  448. Path: normalizeBasePath(n.BasePath) + "panel/api/server/status",
  449. }
  450. req, err := http.NewRequestWithContext(
  451. netsafe.ContextWithAllowPrivate(ctx, n.AllowPrivateAddress),
  452. http.MethodGet, probeURL.String(), nil)
  453. if err != nil {
  454. patch.LastError = err.Error()
  455. return patch, err
  456. }
  457. if n.ApiToken != "" {
  458. req.Header.Set("Authorization", "Bearer "+n.ApiToken)
  459. }
  460. req.Header.Set("Accept", "application/json")
  461. client, err := nodeHTTPClientFor(n)
  462. if err != nil {
  463. patch.LastError = err.Error()
  464. return patch, err
  465. }
  466. start := time.Now()
  467. resp, err := client.Do(req)
  468. if err != nil {
  469. patch.LastError = err.Error()
  470. return patch, err
  471. }
  472. defer resp.Body.Close()
  473. patch.LatencyMs = int(time.Since(start) / time.Millisecond)
  474. if resp.StatusCode != http.StatusOK {
  475. patch.LastError = fmt.Sprintf("HTTP %d from remote panel", resp.StatusCode)
  476. return patch, errors.New(patch.LastError)
  477. }
  478. var envelope struct {
  479. Success bool `json:"success"`
  480. Msg string `json:"msg"`
  481. Obj *struct {
  482. CpuPct float64 `json:"cpu"`
  483. Mem struct {
  484. Current uint64 `json:"current"`
  485. Total uint64 `json:"total"`
  486. } `json:"mem"`
  487. Xray struct {
  488. Version string `json:"version"`
  489. } `json:"xray"`
  490. PanelVersion string `json:"panelVersion"`
  491. Uptime uint64 `json:"uptime"`
  492. } `json:"obj"`
  493. }
  494. if err := json.NewDecoder(resp.Body).Decode(&envelope); err != nil {
  495. patch.LastError = "decode response: " + err.Error()
  496. return patch, err
  497. }
  498. if !envelope.Success || envelope.Obj == nil {
  499. patch.LastError = "remote returned success=false: " + envelope.Msg
  500. return patch, errors.New(patch.LastError)
  501. }
  502. o := envelope.Obj
  503. patch.CpuPct = o.CpuPct
  504. if o.Mem.Total > 0 {
  505. patch.MemPct = float64(o.Mem.Current) * 100.0 / float64(o.Mem.Total)
  506. }
  507. patch.XrayVersion = o.Xray.Version
  508. patch.PanelVersion = o.PanelVersion
  509. patch.UptimeSecs = o.Uptime
  510. return patch, nil
  511. }
  512. type ProbeResultUI struct {
  513. Status string `json:"status"`
  514. LatencyMs int `json:"latencyMs"`
  515. XrayVersion string `json:"xrayVersion"`
  516. PanelVersion string `json:"panelVersion"`
  517. CpuPct float64 `json:"cpuPct"`
  518. MemPct float64 `json:"memPct"`
  519. UptimeSecs uint64 `json:"uptimeSecs"`
  520. Error string `json:"error"`
  521. }
  522. func (p HeartbeatPatch) ToUI(ok bool) ProbeResultUI {
  523. r := ProbeResultUI{
  524. LatencyMs: p.LatencyMs,
  525. XrayVersion: p.XrayVersion,
  526. PanelVersion: p.PanelVersion,
  527. CpuPct: p.CpuPct,
  528. MemPct: p.MemPct,
  529. UptimeSecs: p.UptimeSecs,
  530. Error: p.LastError,
  531. }
  532. if ok {
  533. r.Status = "online"
  534. } else {
  535. r.Status = "offline"
  536. }
  537. return r
  538. }