1
0

xray.go 35 KB

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