xray.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "path"
  7. "path/filepath"
  8. "runtime"
  9. "strings"
  10. "sync"
  11. "github.com/mhsanaei/3x-ui/v3/internal/config"
  12. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  13. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  14. "github.com/mhsanaei/3x-ui/v3/internal/util/json_util"
  15. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  16. "go.uber.org/atomic"
  17. )
  18. var (
  19. p *xray.Process
  20. lock sync.Mutex
  21. isNeedXrayRestart atomic.Bool // Indicates that restart was requested for Xray
  22. isManuallyStopped atomic.Bool // Indicates that Xray was stopped manually from the panel
  23. result string
  24. )
  25. // XrayService provides business logic for Xray process management.
  26. // It handles starting, stopping, restarting Xray, and managing its configuration.
  27. type XrayService struct {
  28. inboundService InboundService
  29. settingService SettingService
  30. nodeService NodeService
  31. xrayAPI xray.XrayAPI
  32. }
  33. // IsXrayRunning checks if the Xray process is currently running.
  34. func (s *XrayService) IsXrayRunning() bool {
  35. return p != nil && p.IsRunning()
  36. }
  37. // XrayProcess returns the current Xray process instance (may be nil when Xray
  38. // is not running). It exposes the package-level process to callers outside this
  39. // package (e.g. the tgbot subpackage) without changing access semantics.
  40. func XrayProcess() *xray.Process {
  41. return p
  42. }
  43. // GetXrayErr returns the error from the Xray process, if any.
  44. func (s *XrayService) GetXrayErr() error {
  45. if p == nil {
  46. return nil
  47. }
  48. err := p.GetErr()
  49. if err == nil {
  50. return nil
  51. }
  52. if runtime.GOOS == "windows" && err.Error() == "exit status 1" {
  53. // exit status 1 on Windows means that Xray process was killed
  54. // as we kill process to stop in on Windows, this is not an error
  55. return nil
  56. }
  57. return err
  58. }
  59. // GetXrayResult returns the result string from the Xray process.
  60. func (s *XrayService) GetXrayResult() string {
  61. if result != "" {
  62. return result
  63. }
  64. if s.IsXrayRunning() {
  65. return ""
  66. }
  67. if p == nil {
  68. return ""
  69. }
  70. result = p.GetResult()
  71. if runtime.GOOS == "windows" && result == "exit status 1" {
  72. // exit status 1 on Windows means that Xray process was killed
  73. // as we kill process to stop in on Windows, this is not an error
  74. return ""
  75. }
  76. return result
  77. }
  78. // GetXrayVersion returns the version of the running Xray process.
  79. func (s *XrayService) GetXrayVersion() string {
  80. if p == nil {
  81. return "Unknown"
  82. }
  83. return p.GetVersion()
  84. }
  85. // RemoveIndex removes an element at the specified index from a slice.
  86. // Returns a new slice with the element removed.
  87. func RemoveIndex(s []any, index int) []any {
  88. return append(s[:index], s[index+1:]...)
  89. }
  90. // GetXrayConfig retrieves and builds the Xray configuration from settings and inbounds.
  91. func (s *XrayService) GetXrayConfig() (*xray.Config, error) {
  92. templateConfig, err := s.settingService.GetXrayConfigTemplate()
  93. if err != nil {
  94. return nil, err
  95. }
  96. xrayConfig := &xray.Config{}
  97. err = json.Unmarshal([]byte(templateConfig), xrayConfig)
  98. if err != nil {
  99. return nil, err
  100. }
  101. xrayConfig.LogConfig = resolveXrayLogPaths(xrayConfig.LogConfig)
  102. xrayConfig.API = ensureAPIServices(xrayConfig.API)
  103. xrayConfig.Policy = ensureStatsPolicy(xrayConfig.Policy)
  104. _, _, _ = s.inboundService.AddTraffic(nil, nil)
  105. inbounds, err := s.inboundService.GetAllInbounds()
  106. if err != nil {
  107. return nil, err
  108. }
  109. for _, inbound := range inbounds {
  110. if !inbound.Enable {
  111. continue
  112. }
  113. if inbound.NodeID != nil {
  114. continue
  115. }
  116. if inbound.Protocol == model.MTProto {
  117. continue
  118. }
  119. settings := map[string]any{}
  120. json.Unmarshal([]byte(inbound.Settings), &settings)
  121. dbClients, listErr := s.inboundService.clientService.ListForInbound(nil, inbound.Id)
  122. if listErr != nil {
  123. return nil, listErr
  124. }
  125. clientStats := inbound.ClientStats
  126. enableMap := make(map[string]bool, len(clientStats))
  127. for _, clientTraffic := range clientStats {
  128. enableMap[clientTraffic.Email] = clientTraffic.Enable
  129. }
  130. var finalClients []any
  131. for i := range dbClients {
  132. c := dbClients[i]
  133. if enable, exists := enableMap[c.Email]; exists && !enable {
  134. logger.Infof("Remove Inbound User %s due to expiration or traffic limit", c.Email)
  135. continue
  136. }
  137. if !c.Enable {
  138. continue
  139. }
  140. flow := c.Flow
  141. if flow == "xtls-rprx-vision-udp443" {
  142. flow = "xtls-rprx-vision"
  143. }
  144. entry := map[string]any{"email": c.Email}
  145. switch inbound.Protocol {
  146. case model.VLESS:
  147. if c.ID != "" {
  148. entry["id"] = c.ID
  149. }
  150. if flow != "" {
  151. entry["flow"] = flow
  152. }
  153. if c.Reverse != nil {
  154. entry["reverse"] = c.Reverse
  155. }
  156. case model.VMESS:
  157. if c.ID != "" {
  158. entry["id"] = c.ID
  159. }
  160. if c.Security != "" {
  161. entry["security"] = c.Security
  162. }
  163. case model.Trojan:
  164. if c.Password != "" {
  165. entry["password"] = c.Password
  166. }
  167. if flow != "" {
  168. entry["flow"] = flow
  169. }
  170. case model.Shadowsocks:
  171. if c.Password != "" {
  172. entry["password"] = c.Password
  173. }
  174. case model.Hysteria:
  175. if c.Auth != "" {
  176. entry["auth"] = c.Auth
  177. }
  178. }
  179. finalClients = append(finalClients, entry)
  180. }
  181. _, hadClients := settings["clients"]
  182. mutated := hadClients || len(finalClients) > 0
  183. if mutated {
  184. settings["clients"] = finalClients
  185. }
  186. if inboundCanHostFallbacks(inbound) {
  187. fallbacks, fbErr := s.inboundService.fallbackService.BuildFallbacksJSON(nil, inbound.Id)
  188. if fbErr != nil {
  189. return nil, fbErr
  190. }
  191. if len(fallbacks) > 0 {
  192. generic := make([]any, 0, len(fallbacks))
  193. for _, f := range fallbacks {
  194. generic = append(generic, f)
  195. }
  196. settings["fallbacks"] = generic
  197. mutated = true
  198. }
  199. }
  200. if mutated {
  201. modifiedSettings, err := json.MarshalIndent(settings, "", " ")
  202. if err != nil {
  203. return nil, err
  204. }
  205. inbound.Settings = string(modifiedSettings)
  206. }
  207. if len(inbound.StreamSettings) > 0 {
  208. // Unmarshal stream JSON
  209. var stream map[string]any
  210. json.Unmarshal([]byte(inbound.StreamSettings), &stream)
  211. // Remove the "settings" field under "tlsSettings" and "realitySettings"
  212. tlsSettings, ok1 := stream["tlsSettings"].(map[string]any)
  213. realitySettings, ok2 := stream["realitySettings"].(map[string]any)
  214. if ok1 || ok2 {
  215. if ok1 {
  216. delete(tlsSettings, "settings")
  217. } else if ok2 {
  218. delete(realitySettings, "settings")
  219. }
  220. }
  221. delete(stream, "externalProxy")
  222. newStream, err := json.MarshalIndent(stream, "", " ")
  223. if err != nil {
  224. return nil, err
  225. }
  226. inbound.StreamSettings = string(newStream)
  227. }
  228. if inbound.Protocol == model.Shadowsocks {
  229. if healed, ok := model.HealShadowsocksClientMethods(inbound.Settings); ok {
  230. inbound.Settings = healed
  231. }
  232. }
  233. inboundConfig := inbound.GenXrayInboundConfig()
  234. xrayConfig.InboundConfigs = append(xrayConfig.InboundConfigs, *inboundConfig)
  235. }
  236. // Merge subscription-derived outbounds (if any) into the final outbounds array.
  237. // These are additive: each subscription is placed before or after the template
  238. // outbounds based on its Prepend flag, ordered by Priority. Tags assigned by the
  239. // subscription service are kept stable across refreshes so that balancers and
  240. // routing rules continue to work.
  241. subSvc := &OutboundSubscriptionService{}
  242. if prepend, appendList, err := subSvc.activeOutboundsSplit(); err == nil && (len(prepend) > 0 || len(appendList) > 0) {
  243. mergeSubscriptionOutbounds(xrayConfig, prepend, appendList)
  244. }
  245. // Route opted-in local mtproto inbounds through the core's router. Each one
  246. // gets a loopback SOCKS bridge — tagged with the inbound's own tag so it is
  247. // matchable in routing rules — that its mtg sidecar dials Telegram through.
  248. // Done after the subscription merge so a selected subscription outbound (or
  249. // balancer) is a valid rule target.
  250. for i := range inbounds {
  251. inbound := inbounds[i]
  252. if inbound.Protocol != model.MTProto || !inbound.Enable || inbound.NodeID != nil {
  253. continue
  254. }
  255. injectMtprotoEgress(xrayConfig, inbound)
  256. }
  257. // Wire the panel's own HTTP traffic through the configured outbound, after
  258. // the subscription merge so subscription outbound tags are valid targets.
  259. if egressTag, err := s.settingService.GetPanelOutbound(); err != nil {
  260. logger.Warning("read panelOutbound setting failed:", err)
  261. } else if egressTag != "" {
  262. injectPanelEgress(xrayConfig, egressTag)
  263. }
  264. nodes, err := s.nodeService.GetAll()
  265. if err != nil {
  266. logger.Warning("read nodes for egress injection failed:", err)
  267. } else {
  268. injectNodeEgresses(xrayConfig, nodes)
  269. }
  270. return xrayConfig, nil
  271. }
  272. // PanelEgressInboundTag is the tag of the loopback SOCKS inbound injected into
  273. // the generated config when a panel outbound is configured. The panel's own
  274. // HTTP clients dial through it to egress via the chosen outbound.
  275. const PanelEgressInboundTag = "panel-egress"
  276. // panelEgressBasePort is the first port tried for the egress bridge; ports
  277. // already taken by other inbounds in the generated config are skipped.
  278. const panelEgressBasePort = 62790
  279. // injectPanelEgress appends a loopback SOCKS inbound to the generated config
  280. // and prepends a routing rule sending it to outboundTag. Both live only in the
  281. // generated config — the stored template is never modified — and both are
  282. // hot-appliable, so changing the panel outbound never restarts the core.
  283. func injectPanelEgress(cfg *xray.Config, outboundTag string) {
  284. for i := range cfg.InboundConfigs {
  285. if cfg.InboundConfigs[i].Tag == PanelEgressInboundTag {
  286. logger.Warning("panel egress: inbound tag [", PanelEgressInboundTag, "] already exists, skipping injection")
  287. return
  288. }
  289. }
  290. // The rule must exist before the inbound takes traffic, otherwise the
  291. // bridge would silently egress through the default outbound instead.
  292. routing := map[string]any{}
  293. if len(cfg.RouterConfig) > 0 {
  294. if err := json.Unmarshal(cfg.RouterConfig, &routing); err != nil {
  295. logger.Warning("panel egress: routing section is unparsable, skipping injection:", err)
  296. return
  297. }
  298. }
  299. rules, _ := routing["rules"].([]any)
  300. rule := map[string]any{
  301. "type": "field",
  302. "inboundTag": []any{PanelEgressInboundTag},
  303. }
  304. // The configured tag may name a routing balancer instead of a concrete
  305. // outbound. A field rule can target either, so emit the matching key —
  306. // balancerTag load-balances the panel's own traffic across the balancer's
  307. // outbounds, while a plain outbound tag keeps the original behavior.
  308. if routingTagIsBalancer(routing, outboundTag) {
  309. rule["balancerTag"] = outboundTag
  310. } else {
  311. rule["outboundTag"] = outboundTag
  312. }
  313. routing["rules"] = append([]any{rule}, rules...)
  314. newRouting, err := json.Marshal(routing)
  315. if err != nil {
  316. logger.Warning("panel egress: failed to rebuild routing section, skipping injection:", err)
  317. return
  318. }
  319. cfg.RouterConfig = json_util.RawMessage(newRouting)
  320. used := make(map[int]struct{}, len(cfg.InboundConfigs))
  321. for i := range cfg.InboundConfigs {
  322. used[cfg.InboundConfigs[i].Port] = struct{}{}
  323. }
  324. port := panelEgressBasePort
  325. for {
  326. if _, taken := used[port]; !taken {
  327. break
  328. }
  329. port++
  330. }
  331. cfg.InboundConfigs = append(cfg.InboundConfigs, xray.InboundConfig{
  332. Listen: json_util.RawMessage(`"127.0.0.1"`),
  333. Port: port,
  334. Protocol: "socks",
  335. Settings: json_util.RawMessage(`{"auth":"noauth","udp":false}`),
  336. Tag: PanelEgressInboundTag,
  337. })
  338. }
  339. // NodeEgressInboundTag returns the loopback SOCKS inbound tag for a given node.
  340. func NodeEgressInboundTag(nodeID int) string {
  341. return fmt.Sprintf("node-egress-%d", nodeID)
  342. }
  343. // nodeEgressBasePort is the first port tried for node egress bridges.
  344. const nodeEgressBasePort = 62800
  345. // injectNodeEgresses appends a loopback SOCKS inbound per enabled node that has
  346. // an OutboundTag, and prepends a routing rule sending that inbound's traffic to
  347. // the selected outbound tag. These bridges are hot-appliable.
  348. func injectNodeEgresses(cfg *xray.Config, nodes []*model.Node) {
  349. routing := map[string]any{}
  350. if len(cfg.RouterConfig) > 0 {
  351. if err := json.Unmarshal(cfg.RouterConfig, &routing); err != nil {
  352. logger.Warning("node egress: routing section is unparsable, skipping injection:", err)
  353. return
  354. }
  355. }
  356. used := make(map[int]struct{}, len(cfg.InboundConfigs))
  357. usedTags := make(map[string]struct{}, len(cfg.InboundConfigs))
  358. for i := range cfg.InboundConfigs {
  359. used[cfg.InboundConfigs[i].Port] = struct{}{}
  360. usedTags[cfg.InboundConfigs[i].Tag] = struct{}{}
  361. }
  362. rules, _ := routing["rules"].([]any)
  363. newRules := make([]any, 0)
  364. for _, n := range nodes {
  365. if !n.Enable || n.OutboundTag == "" {
  366. continue
  367. }
  368. tag := NodeEgressInboundTag(n.Id)
  369. if _, exists := usedTags[tag]; exists {
  370. logger.Warning("node egress: inbound tag [", tag, "] already exists, skipping")
  371. continue
  372. }
  373. usedTags[tag] = struct{}{}
  374. rule := map[string]any{
  375. "type": "field",
  376. "inboundTag": []any{tag},
  377. }
  378. if routingTagIsBalancer(routing, n.OutboundTag) {
  379. rule["balancerTag"] = n.OutboundTag
  380. } else {
  381. rule["outboundTag"] = n.OutboundTag
  382. }
  383. newRules = append(newRules, rule)
  384. port := nodeEgressBasePort + n.Id
  385. for {
  386. if _, taken := used[port]; !taken {
  387. break
  388. }
  389. port++
  390. }
  391. used[port] = struct{}{}
  392. cfg.InboundConfigs = append(cfg.InboundConfigs, xray.InboundConfig{
  393. Listen: json_util.RawMessage(`"127.0.0.1"`),
  394. Port: port,
  395. Protocol: "socks",
  396. Settings: json_util.RawMessage(`{"auth":"noauth","udp":false}`),
  397. Tag: tag,
  398. })
  399. }
  400. if len(newRules) == 0 {
  401. return
  402. }
  403. routing["rules"] = append(newRules, rules...)
  404. newRouting, err := json.Marshal(routing)
  405. if err != nil {
  406. logger.Warning("node egress: failed to rebuild routing section, skipping injection:", err)
  407. return
  408. }
  409. cfg.RouterConfig = json_util.RawMessage(newRouting)
  410. }
  411. // routingTagIsBalancer reports whether tag names a balancer in the parsed
  412. // routing section. The panel-egress rule targets a balancer via balancerTag and
  413. // a concrete outbound via outboundTag, so the caller picks the key from this.
  414. func routingTagIsBalancer(routing map[string]any, tag string) bool {
  415. if tag == "" {
  416. return false
  417. }
  418. balancers, ok := routing["balancers"].([]any)
  419. if !ok {
  420. return false
  421. }
  422. for _, b := range balancers {
  423. bm, ok := b.(map[string]any)
  424. if !ok {
  425. continue
  426. }
  427. if t, ok := bm["tag"].(string); ok && t == tag {
  428. return true
  429. }
  430. }
  431. return false
  432. }
  433. // mtprotoEgressSocksSettings is the loopback SOCKS server a routed mtproto
  434. // inbound exposes for its mtg sidecar to dial Telegram through. mtg makes plain
  435. // TCP connections, so UDP is left off (matching the panel egress bridge).
  436. const mtprotoEgressSocksSettings = `{"auth":"noauth","udp":false}`
  437. // injectMtprotoEgress wires one routed mtproto inbound into the generated
  438. // config: it appends a loopback SOCKS inbound (tagged with the inbound's own tag,
  439. // on the egress port persisted in settings) and, when an outbound is selected,
  440. // prepends a routing rule sending that tag to it. Both live only in the generated
  441. // config — the stored template is untouched — and both are hot-appliable, so
  442. // toggling routing never forces a full Xray restart. Mirrors injectPanelEgress.
  443. func injectMtprotoEgress(cfg *xray.Config, inbound *model.Inbound) {
  444. var parsed struct {
  445. RouteThroughXray bool `json:"routeThroughXray"`
  446. RouteXrayPort int `json:"routeXrayPort"`
  447. OutboundTag string `json:"outboundTag"`
  448. }
  449. if err := json.Unmarshal([]byte(inbound.Settings), &parsed); err != nil {
  450. return
  451. }
  452. if !parsed.RouteThroughXray || parsed.RouteXrayPort <= 0 || inbound.Tag == "" {
  453. return
  454. }
  455. tag := inbound.Tag
  456. for i := range cfg.InboundConfigs {
  457. if cfg.InboundConfigs[i].Tag == tag {
  458. logger.Warning("mtproto egress: inbound tag [", tag, "] already present in generated config, skipping bridge")
  459. return
  460. }
  461. }
  462. if parsed.OutboundTag != "" {
  463. routing := map[string]any{}
  464. parseOK := true
  465. if len(cfg.RouterConfig) > 0 {
  466. if err := json.Unmarshal(cfg.RouterConfig, &routing); err != nil {
  467. logger.Warning("mtproto egress: routing section is unparsable, skipping rule:", err)
  468. parseOK = false
  469. }
  470. }
  471. if parseOK {
  472. rules, _ := routing["rules"].([]any)
  473. rule := map[string]any{
  474. "type": "field",
  475. "inboundTag": []any{tag},
  476. }
  477. if routingTagIsBalancer(routing, parsed.OutboundTag) {
  478. rule["balancerTag"] = parsed.OutboundTag
  479. } else {
  480. rule["outboundTag"] = parsed.OutboundTag
  481. }
  482. routing["rules"] = append([]any{rule}, rules...)
  483. if newRouting, err := json.Marshal(routing); err == nil {
  484. cfg.RouterConfig = json_util.RawMessage(newRouting)
  485. } else {
  486. logger.Warning("mtproto egress: failed to rebuild routing section, skipping rule:", err)
  487. }
  488. }
  489. }
  490. cfg.InboundConfigs = append(cfg.InboundConfigs, xray.InboundConfig{
  491. Listen: json_util.RawMessage(`"127.0.0.1"`),
  492. Port: parsed.RouteXrayPort,
  493. Protocol: "socks",
  494. Settings: json_util.RawMessage(mtprotoEgressSocksSettings),
  495. Tag: tag,
  496. })
  497. }
  498. // mergeSubscriptionOutbounds appends the subscription outbounds to the
  499. // OutboundConfigs array of the xray config. It works on the already-unmarshaled
  500. // template so that manually configured outbounds are never overwritten.
  501. //
  502. // Safety: if we cannot parse the template's outbounds array, we leave
  503. // OutboundConfigs exactly as it came from the template (we do not inject
  504. // subscription outbounds). This prevents us from accidentally dropping the
  505. // user's manually configured outbounds when the template is in a weird state.
  506. func mergeSubscriptionOutbounds(cfg *xray.Config, prepend, appendList []any) {
  507. if len(prepend) == 0 && len(appendList) == 0 {
  508. return
  509. }
  510. var templateOutbounds []any
  511. if len(cfg.OutboundConfigs) > 0 {
  512. if err := json.Unmarshal(cfg.OutboundConfigs, &templateOutbounds); err != nil {
  513. // Corrupt template outbounds — do not touch the field at all.
  514. // The user will see problems on Xray start / next save.
  515. return
  516. }
  517. }
  518. merged := make([]any, 0, len(prepend)+len(templateOutbounds)+len(appendList))
  519. merged = append(merged, prepend...)
  520. merged = append(merged, templateOutbounds...)
  521. merged = append(merged, appendList...)
  522. combined, err := json.MarshalIndent(merged, "", " ")
  523. if err != nil {
  524. return
  525. }
  526. cfg.OutboundConfigs = json_util.RawMessage(combined)
  527. }
  528. // ensureAPIServices guarantees the gRPC services the panel depends on are
  529. // listed in the generated config's api block: HandlerService and StatsService
  530. // have always been required for inbound/user management and traffic polling,
  531. // and RoutingService enables hot routing reload on templates saved before it
  532. // was added to the default template. The stored template itself is not
  533. // modified — only the generated runtime config.
  534. func ensureAPIServices(api json_util.RawMessage) json_util.RawMessage {
  535. if len(api) == 0 {
  536. // No api block means the panel's API integration is deliberately
  537. // disabled; don't resurrect it behind the user's back.
  538. return api
  539. }
  540. var parsed map[string]any
  541. if err := json.Unmarshal(api, &parsed); err != nil {
  542. return api
  543. }
  544. services, _ := parsed["services"].([]any)
  545. have := make(map[string]bool, len(services))
  546. for _, svc := range services {
  547. if name, ok := svc.(string); ok {
  548. have[name] = true
  549. }
  550. }
  551. added := false
  552. for _, name := range []string{"HandlerService", "StatsService", "RoutingService"} {
  553. if !have[name] {
  554. services = append(services, name)
  555. added = true
  556. }
  557. }
  558. if !added {
  559. return api
  560. }
  561. parsed["services"] = services
  562. out, err := json.Marshal(parsed)
  563. if err != nil {
  564. return api
  565. }
  566. return out
  567. }
  568. // ensureStatsPolicy guarantees every policy level in the generated config has
  569. // statsUserOnline enabled, so the core tracks per-email online IPs for the
  570. // panel's online view and access-log-free IP limiting. Generated clients carry
  571. // no explicit level, so level "0" is created when absent. The flag is panel
  572. // infrastructure and is forced on even over an explicit false in the template,
  573. // same as the api services above. An entirely missing or unparsable policy
  574. // block is left alone; the stored template itself is never modified — only the
  575. // generated runtime config.
  576. func ensureStatsPolicy(policy json_util.RawMessage) json_util.RawMessage {
  577. if len(policy) == 0 {
  578. return policy
  579. }
  580. var parsed map[string]any
  581. if err := json.Unmarshal(policy, &parsed); err != nil {
  582. return policy
  583. }
  584. levels, _ := parsed["levels"].(map[string]any)
  585. if levels == nil {
  586. levels = make(map[string]any)
  587. }
  588. if _, ok := levels["0"]; !ok {
  589. levels["0"] = map[string]any{}
  590. }
  591. changed := false
  592. for _, raw := range levels {
  593. level, ok := raw.(map[string]any)
  594. if !ok {
  595. continue
  596. }
  597. if enabled, ok := level["statsUserOnline"].(bool); !ok || !enabled {
  598. level["statsUserOnline"] = true
  599. changed = true
  600. }
  601. }
  602. if !changed {
  603. return policy
  604. }
  605. parsed["levels"] = levels
  606. out, err := json.Marshal(parsed)
  607. if err != nil {
  608. return policy
  609. }
  610. return out
  611. }
  612. func resolveXrayLogPaths(logCfg json_util.RawMessage) json_util.RawMessage {
  613. if len(logCfg) == 0 {
  614. return logCfg
  615. }
  616. var parsed map[string]any
  617. if err := json.Unmarshal(logCfg, &parsed); err != nil {
  618. return logCfg
  619. }
  620. changed := false
  621. for _, key := range []string{"access", "error"} {
  622. v, ok := parsed[key].(string)
  623. if !ok {
  624. continue
  625. }
  626. trimmed := strings.TrimSpace(v)
  627. if trimmed == "" || strings.EqualFold(trimmed, "none") {
  628. continue
  629. }
  630. base := path.Base(filepath.ToSlash(trimmed))
  631. if base == "" || base == "." || base == ".." || base == "/" {
  632. continue
  633. }
  634. confined := filepath.Join(config.GetLogFolder(), base)
  635. if confined == trimmed {
  636. continue
  637. }
  638. parsed[key] = confined
  639. changed = true
  640. }
  641. if !changed {
  642. return logCfg
  643. }
  644. out, err := json.Marshal(parsed)
  645. if err != nil {
  646. return logCfg
  647. }
  648. return out
  649. }
  650. // GetXrayTraffic fetches the current traffic statistics from the running Xray process.
  651. func (s *XrayService) GetXrayTraffic() ([]*xray.Traffic, []*xray.ClientTraffic, error) {
  652. if !s.IsXrayRunning() {
  653. err := errors.New("xray is not running")
  654. logger.Debug("Attempted to fetch Xray traffic, but Xray is not running:", err)
  655. return nil, nil, err
  656. }
  657. apiPort := p.GetAPIPort()
  658. if err := s.xrayAPI.Init(apiPort); err != nil {
  659. logger.Debug("Failed to initialize Xray API:", err)
  660. return nil, nil, err
  661. }
  662. defer s.xrayAPI.Close()
  663. traffic, clientTraffic, err := s.xrayAPI.GetTraffic()
  664. if err != nil {
  665. logger.Debug("Failed to fetch Xray traffic:", err)
  666. return nil, nil, err
  667. }
  668. return traffic, clientTraffic, nil
  669. }
  670. // GetOnlineUsers returns connection-based online users (email + source IPs)
  671. // from the running core's online-stats API. ok=false means the API is not
  672. // available — xray isn't running or the core predates the online-stats RPCs —
  673. // and callers must use the legacy traffic-delta / access-log paths. The
  674. // capability is probed lazily per process: an Unimplemented answer pins this
  675. // core as unsupported until the next restart, while transient errors leave the
  676. // capability undecided so a flaky poll can't lock in legacy mode.
  677. func (s *XrayService) GetOnlineUsers() ([]xray.OnlineUser, bool, error) {
  678. if !s.IsXrayRunning() {
  679. return nil, false, nil
  680. }
  681. if p.OnlineAPISupport() == xray.OnlineAPIUnsupported {
  682. return nil, false, nil
  683. }
  684. if err := s.xrayAPI.Init(p.GetAPIPort()); err != nil {
  685. logger.Debug("Failed to initialize Xray API:", err)
  686. return nil, false, err
  687. }
  688. defer s.xrayAPI.Close()
  689. users, err := s.xrayAPI.GetOnlineUsers()
  690. if err != nil {
  691. if xray.IsUnimplementedErr(err) {
  692. p.SetOnlineAPISupport(xray.OnlineAPIUnsupported)
  693. logger.Info("xray core does not support the online-stats API; falling back to traffic-delta onlines and access-log IP limit")
  694. return nil, false, nil
  695. }
  696. logger.Debug("Failed to fetch Xray online users:", err)
  697. return nil, false, err
  698. }
  699. if p.OnlineAPISupport() == xray.OnlineAPIUnknown {
  700. p.SetOnlineAPISupport(xray.OnlineAPISupported)
  701. logger.Info("xray core supports the online-stats API; using connection-based onlines and access-log-free IP limit")
  702. }
  703. return users, true, nil
  704. }
  705. // BalancerStatus is the live view of one balancer for the panel UI. Running
  706. // is false when the balancer isn't present in the running core (e.g. xray is
  707. // stopped or the balancer hasn't been saved/applied yet).
  708. type BalancerStatus struct {
  709. Tag string `json:"tag"`
  710. Running bool `json:"running"`
  711. Override string `json:"override"`
  712. Selected []string `json:"selected"`
  713. }
  714. // GetBalancersStatus queries the running core for the live state of the
  715. // given balancer tags. Per-tag failures are reported as Running=false rather
  716. // than failing the whole call, so the UI can render saved-but-not-applied
  717. // balancers alongside live ones.
  718. func (s *XrayService) GetBalancersStatus(tags []string) ([]BalancerStatus, error) {
  719. statuses := make([]BalancerStatus, 0, len(tags))
  720. if !s.IsXrayRunning() {
  721. for _, tag := range tags {
  722. statuses = append(statuses, BalancerStatus{Tag: tag})
  723. }
  724. return statuses, nil
  725. }
  726. if err := s.xrayAPI.Init(p.GetAPIPort()); err != nil {
  727. return nil, err
  728. }
  729. defer s.xrayAPI.Close()
  730. for _, tag := range tags {
  731. info, err := s.xrayAPI.GetBalancerInfo(tag)
  732. if err != nil {
  733. logger.Debug("get balancer info [", tag, "] failed:", err)
  734. statuses = append(statuses, BalancerStatus{Tag: tag})
  735. continue
  736. }
  737. statuses = append(statuses, BalancerStatus{
  738. Tag: tag,
  739. Running: true,
  740. Override: info.Override,
  741. Selected: info.Selected,
  742. })
  743. }
  744. return statuses, nil
  745. }
  746. // OverrideBalancer forces a balancer in the running core to use the given
  747. // outbound tag; an empty target clears the override.
  748. func (s *XrayService) OverrideBalancer(tag, target string) error {
  749. if !s.IsXrayRunning() {
  750. return errors.New("xray is not running")
  751. }
  752. if err := s.xrayAPI.Init(p.GetAPIPort()); err != nil {
  753. return err
  754. }
  755. defer s.xrayAPI.Close()
  756. return s.xrayAPI.SetBalancerTarget(tag, target)
  757. }
  758. // TestRoute asks the running core which outbound its router picks for the
  759. // described connection.
  760. func (s *XrayService) TestRoute(req xray.RouteTestRequest) (*xray.RouteTestResult, error) {
  761. if !s.IsXrayRunning() {
  762. return nil, errors.New("xray is not running")
  763. }
  764. if err := s.xrayAPI.Init(p.GetAPIPort()); err != nil {
  765. return nil, err
  766. }
  767. defer s.xrayAPI.Close()
  768. return s.xrayAPI.TestRoute(req)
  769. }
  770. // RestartXray reconciles the running Xray process with the current desired
  771. // config. When isForce is false it first tries to apply the changes through
  772. // the Xray gRPC API without restarting the process (inbounds, outbounds and
  773. // routing rules/balancers are hot-reloadable); only changes the core cannot
  774. // take at runtime — or a force request — stop and restart the process.
  775. func (s *XrayService) RestartXray(isForce bool) error {
  776. lock.Lock()
  777. defer lock.Unlock()
  778. logger.Debug("restart Xray, force:", isForce)
  779. isManuallyStopped.Store(false)
  780. xrayConfig, err := s.GetXrayConfig()
  781. if err != nil {
  782. return err
  783. }
  784. if s.IsXrayRunning() {
  785. configUnchanged := p.GetConfig().Equals(xrayConfig)
  786. if !isForce && configUnchanged && !isNeedXrayRestart.Load() {
  787. logger.Debug("It does not need to restart Xray")
  788. return nil
  789. }
  790. if !isForce && !configUnchanged && s.tryHotApply(xrayConfig) {
  791. logger.Info("Xray config changes applied through the core API, no restart needed")
  792. return nil
  793. }
  794. p.Stop()
  795. }
  796. p = xray.NewProcess(xrayConfig)
  797. result = ""
  798. s.xrayAPI.StatsLastValues = nil
  799. err = p.Start()
  800. if err != nil {
  801. return err
  802. }
  803. return nil
  804. }
  805. // tryHotApply attempts to reconcile the running Xray instance with newCfg
  806. // through the core gRPC API (HandlerService for inbounds/outbounds,
  807. // RoutingService for rules/balancers). It returns true when the running
  808. // instance now matches newCfg; on any failure it returns false and the
  809. // caller falls back to a full process restart, which cleans up whatever was
  810. // partially applied. Callers must hold the package-level lock.
  811. func (s *XrayService) tryHotApply(newCfg *xray.Config) bool {
  812. oldCfg := p.GetConfig()
  813. diff, ok := xray.ComputeHotDiff(oldCfg, newCfg)
  814. if !ok {
  815. logger.Debug("hot apply: config change is not API-applicable, falling back to restart")
  816. return false
  817. }
  818. if diff.Empty() {
  819. p.SetConfig(newCfg)
  820. return true
  821. }
  822. apiPort := p.GetAPIPort()
  823. if apiPort <= 0 {
  824. return false
  825. }
  826. // A dedicated client: s.xrayAPI may be in use by traffic polling on other
  827. // service instances and is reset around restarts.
  828. hotAPI := xray.XrayAPI{}
  829. if err := hotAPI.Init(apiPort); err != nil {
  830. logger.Debug("hot apply: failed to init xray api:", err)
  831. return false
  832. }
  833. defer hotAPI.Close()
  834. // Removals first so changed handlers and port swaps never collide with
  835. // the additions that follow.
  836. for _, tag := range diff.RemovedInboundTags {
  837. if err := hotAPI.DelInbound(tag); err != nil && !xray.IsMissingHandlerErr(err) {
  838. logger.Info("hot apply: remove inbound [", tag, "] failed:", err)
  839. return false
  840. }
  841. }
  842. for _, tag := range diff.RemovedOutboundTags {
  843. if err := hotAPI.DelOutbound(tag); err != nil && !xray.IsMissingHandlerErr(err) {
  844. logger.Info("hot apply: remove outbound [", tag, "] failed:", err)
  845. return false
  846. }
  847. }
  848. for _, ob := range diff.AddedOutbounds {
  849. if err := addOutboundReconciling(&hotAPI, ob); err != nil {
  850. logger.Info("hot apply: add outbound failed:", err)
  851. return false
  852. }
  853. }
  854. for _, ib := range diff.AddedInbounds {
  855. if err := addInboundReconciling(&hotAPI, ib); err != nil {
  856. logger.Info("hot apply: add inbound failed:", err)
  857. return false
  858. }
  859. }
  860. if diff.RoutingConfig != nil {
  861. if err := hotAPI.ApplyRoutingConfig(diff.RoutingConfig); err != nil {
  862. logger.Info("hot apply: apply routing config failed:", err)
  863. return false
  864. }
  865. }
  866. p.SetConfig(newCfg)
  867. return true
  868. }
  869. // addInboundReconciling adds an inbound, and on a tag conflict (the handler
  870. // was already created through the runtime API while the stored snapshot was
  871. // stale) replaces the existing handler instead.
  872. func addInboundReconciling(api *xray.XrayAPI, inbound []byte) error {
  873. err := api.AddInbound(inbound)
  874. if err == nil || !xray.IsExistingTagErr(err) {
  875. return err
  876. }
  877. var meta struct {
  878. Tag string `json:"tag"`
  879. }
  880. if jsonErr := json.Unmarshal(inbound, &meta); jsonErr != nil || meta.Tag == "" {
  881. return err
  882. }
  883. if delErr := api.DelInbound(meta.Tag); delErr != nil && !xray.IsMissingHandlerErr(delErr) {
  884. return delErr
  885. }
  886. return api.AddInbound(inbound)
  887. }
  888. // addOutboundReconciling mirrors addInboundReconciling for outbounds.
  889. func addOutboundReconciling(api *xray.XrayAPI, outbound []byte) error {
  890. err := api.AddOutbound(outbound)
  891. if err == nil || !xray.IsExistingTagErr(err) {
  892. return err
  893. }
  894. var meta struct {
  895. Tag string `json:"tag"`
  896. }
  897. if jsonErr := json.Unmarshal(outbound, &meta); jsonErr != nil || meta.Tag == "" {
  898. return err
  899. }
  900. if delErr := api.DelOutbound(meta.Tag); delErr != nil && !xray.IsMissingHandlerErr(delErr) {
  901. return delErr
  902. }
  903. return api.AddOutbound(outbound)
  904. }
  905. // StopXray stops the running Xray process.
  906. func (s *XrayService) StopXray() error {
  907. lock.Lock()
  908. defer lock.Unlock()
  909. isManuallyStopped.Store(true)
  910. logger.Debug("Attempting to stop Xray...")
  911. if s.IsXrayRunning() {
  912. return p.Stop()
  913. }
  914. return errors.New("xray is not running")
  915. }
  916. // SetToNeedRestart marks that Xray needs to be restarted.
  917. func (s *XrayService) SetToNeedRestart() {
  918. isNeedXrayRestart.Store(true)
  919. }
  920. // GetXrayAPIPort returns the port the local xray process is listening on
  921. // for its gRPC HandlerService, or 0 when xray isn't currently running.
  922. // Exposed for the runtime package's LocalRuntime adapter — runtime can't
  923. // reach into the package-level `p` directly without a service-package
  924. // import cycle.
  925. func (s *XrayService) GetXrayAPIPort() int {
  926. if p == nil || !p.IsRunning() {
  927. return 0
  928. }
  929. return p.GetAPIPort()
  930. }
  931. // IsNeedRestartAndSetFalse checks if restart is needed and resets the flag to false.
  932. func (s *XrayService) IsNeedRestartAndSetFalse() bool {
  933. return isNeedXrayRestart.CompareAndSwap(true, false)
  934. }
  935. // DidXrayCrash checks if Xray crashed by verifying it's not running and wasn't manually stopped.
  936. func (s *XrayService) DidXrayCrash() bool {
  937. return !s.IsXrayRunning() && !isManuallyStopped.Load()
  938. }