process.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. package xray
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "runtime"
  12. "sort"
  13. "strings"
  14. "sync"
  15. "sync/atomic"
  16. "syscall"
  17. "time"
  18. "github.com/mhsanaei/3x-ui/v3/internal/config"
  19. "github.com/mhsanaei/3x-ui/v3/internal/logger"
  20. "github.com/mhsanaei/3x-ui/v3/internal/util/common"
  21. )
  22. // GetBinaryName returns the Xray binary filename for the current OS and architecture.
  23. func GetBinaryName() string {
  24. arch := runtime.GOARCH
  25. if arch == "arm" {
  26. arch = "arm32"
  27. }
  28. return fmt.Sprintf("xray-%s-%s", runtime.GOOS, arch)
  29. }
  30. // GetBinaryPath returns the full path to the Xray binary executable.
  31. func GetBinaryPath() string {
  32. return config.GetBinFolderPath() + "/" + GetBinaryName()
  33. }
  34. // GetConfigPath returns the path to the Xray configuration file in the binary folder.
  35. func GetConfigPath() string {
  36. return config.GetBinFolderPath() + "/config.json"
  37. }
  38. // GetGeositePath returns the path to the geosite data file used by Xray.
  39. func GetGeositePath() string {
  40. return config.GetBinFolderPath() + "/geosite.dat"
  41. }
  42. // GetGeoipPath returns the path to the geoip data file used by Xray.
  43. func GetGeoipPath() string {
  44. return config.GetBinFolderPath() + "/geoip.dat"
  45. }
  46. // GetIPLimitLogPath returns the path to the IP limit log file.
  47. func GetIPLimitLogPath() string {
  48. return config.GetLogFolder() + "/3xipl.log"
  49. }
  50. // GetIPLimitBannedLogPath returns the path to the banned IP log file.
  51. func GetIPLimitBannedLogPath() string {
  52. return config.GetLogFolder() + "/3xipl-banned.log"
  53. }
  54. // GetIPLimitBannedPrevLogPath returns the path to the previous banned IP log file.
  55. func GetIPLimitBannedPrevLogPath() string {
  56. return config.GetLogFolder() + "/3xipl-banned.prev.log"
  57. }
  58. // GetAccessLogPath reads the Xray config and returns the access log file path.
  59. func GetAccessLogPath() (string, error) {
  60. config, err := os.ReadFile(GetConfigPath())
  61. if err != nil {
  62. logger.Warningf("Failed to read configuration file: %s", err)
  63. return "", err
  64. }
  65. jsonConfig := map[string]any{}
  66. err = json.Unmarshal(config, &jsonConfig)
  67. if err != nil {
  68. logger.Warningf("Failed to parse JSON configuration: %s", err)
  69. return "", err
  70. }
  71. if jsonConfig["log"] != nil {
  72. jsonLog := jsonConfig["log"].(map[string]any)
  73. if jsonLog["access"] != nil {
  74. accessLogPath := jsonLog["access"].(string)
  75. return accessLogPath, nil
  76. }
  77. }
  78. return "", err
  79. }
  80. // stopProcess calls Stop on the given Process instance.
  81. func stopProcess(p *Process) {
  82. _ = p.Stop()
  83. }
  84. // Process wraps an Xray process instance and provides management methods.
  85. type Process struct {
  86. *process
  87. }
  88. // NewProcess creates a new Xray process and sets up cleanup on garbage collection.
  89. func NewProcess(xrayConfig *Config) *Process {
  90. p := &Process{newProcess(xrayConfig)}
  91. runtime.SetFinalizer(p, stopProcess)
  92. return p
  93. }
  94. // NewTestProcess creates a new Xray process that uses a specific config file path.
  95. // Used for test runs (e.g. outbound test) so the main config.json is not overwritten.
  96. // The config file at configPath is removed when the process is stopped.
  97. func NewTestProcess(xrayConfig *Config, configPath string) *Process {
  98. p := &Process{newTestProcess(xrayConfig, configPath)}
  99. runtime.SetFinalizer(p, stopProcess)
  100. return p
  101. }
  102. type process struct {
  103. // mu guards the process lifecycle fields (cmd, done, exitErr) which are
  104. // written by Start/startCommand and the waitForCommand goroutine while being
  105. // read concurrently by IsRunning/GetErr/GetResult/Stop from other goroutines
  106. // (status endpoint, check-xray-running job). Snapshot under the lock, then do
  107. // any blocking syscall (Wait/Signal/Kill) on the local copy without holding it.
  108. mu sync.RWMutex
  109. cmd *exec.Cmd
  110. done chan struct{}
  111. version string
  112. apiPort int
  113. // onlineClients is the set of emails active on THIS panel's own xray
  114. // within the online grace window. It is derived only from local xray
  115. // traffic polls (see RefreshLocalOnline) — never from remote-node
  116. // snapshots — so a client connected solely to a remote node is not
  117. // reported online on local inbounds.
  118. onlineClients []string
  119. // localActiveInbounds is the set of THIS panel's inbound tags that
  120. // carried traffic within the same grace window. Xray's user>>>email
  121. // stat aggregates across every inbound a client is attached to, so an
  122. // online email alone can't say which inbound it actually used. Pairing
  123. // it with the inbound>>>tag stat lets the per-inbound view drop a
  124. // multi-inbound client from inbounds that saw no traffic this window.
  125. localActiveInbounds []string
  126. // localLastOnline records, per email, the last time this panel's own
  127. // xray reported traffic for it. RefreshLocalOnline rebuilds
  128. // onlineClients from this map each tick, keeping the local online set
  129. // independent of the shared client_traffics.last_online column — that
  130. // column is bumped by remote-node syncs too and would otherwise leak
  131. // remote-only clients into the local set.
  132. localLastOnline map[string]int64
  133. // localInboundLastActive mirrors localLastOnline for inbound tags: the
  134. // last tick this panel's xray reported traffic through each tag.
  135. // Rebuilt into localActiveInbounds under the same grace window so the
  136. // two signals stay aligned — an email within grace always has the
  137. // inbound it used within grace too.
  138. localInboundLastActive map[string]int64
  139. // nodeOnlineTrees holds, per direct remote node (keyed by that node's
  140. // panel-local id), the GUID-keyed online-emails subtree that node
  141. // reported — its own clients under its panelGuid plus every descendant
  142. // under theirs. Keying the stored value by GUID (not node id) lets the
  143. // master attribute a deeply nested client to the node that physically
  144. // hosts it across a chain (#4983); the outer node-id key is only so a
  145. // failed probe can drop that whole branch's contribution. NodeTrafficSyncJob
  146. // populates entries per cron tick and clears them when a probe fails. The
  147. // mutex guards this map, onlineClients, and localLastOnline above so the
  148. // online getters never see a torn read.
  149. nodeOnlineTrees map[int]map[string][]string
  150. onlineMu sync.RWMutex
  151. // onlineAPISupport caches whether the running core implements the
  152. // online-stats RPCs (GetUsersStats). A new process is created on every
  153. // restart/version switch, so the flag resets to Unknown and is re-probed
  154. // lazily by the first caller.
  155. onlineAPISupport atomic.Int32
  156. config *Config
  157. configPath string // if set, use this path instead of GetConfigPath() and remove on Stop
  158. logWriter *LogWriter
  159. exitErr error
  160. startTime time.Time
  161. intentionalStop atomic.Bool
  162. }
  163. // OnlineAPISupport describes whether the running Xray core implements the
  164. // online-stats API (statsUserOnline + GetUsersStats).
  165. type OnlineAPISupport int32
  166. const (
  167. // OnlineAPIUnknown means support has not been probed yet for this process.
  168. OnlineAPIUnknown OnlineAPISupport = iota
  169. // OnlineAPISupported means the core answered the online-stats RPC.
  170. OnlineAPISupported
  171. // OnlineAPIUnsupported means the core returned Unimplemented (older binary).
  172. OnlineAPIUnsupported
  173. )
  174. // OnlineAPISupport returns the cached online-stats capability of this process.
  175. func (p *process) OnlineAPISupport() OnlineAPISupport {
  176. return OnlineAPISupport(p.onlineAPISupport.Load())
  177. }
  178. // SetOnlineAPISupport records the probed online-stats capability of this process.
  179. func (p *process) SetOnlineAPISupport(v OnlineAPISupport) {
  180. p.onlineAPISupport.Store(int32(v))
  181. }
  182. var (
  183. xrayGracefulStopTimeout = 5 * time.Second
  184. xrayForceStopTimeout = 2 * time.Second
  185. // OnCrash is called when xray crashes unexpectedly. Set from web layer.
  186. OnCrash func(err error)
  187. )
  188. // newProcess creates a new internal process struct for Xray.
  189. func newProcess(config *Config) *process {
  190. return &process{
  191. version: "Unknown",
  192. config: config,
  193. logWriter: NewLogWriter(),
  194. startTime: time.Now(),
  195. }
  196. }
  197. // newTestProcess creates a process that writes and runs with a specific config path.
  198. func newTestProcess(config *Config, configPath string) *process {
  199. p := newProcess(config)
  200. p.configPath = configPath
  201. return p
  202. }
  203. // IsRunning returns true if the Xray process is currently running.
  204. func (p *process) IsRunning() bool {
  205. p.mu.RLock()
  206. cmd, done := p.cmd, p.done
  207. p.mu.RUnlock()
  208. if cmd == nil || cmd.Process == nil {
  209. return false
  210. }
  211. // done is closed by the waitForCommand goroutine exactly when cmd.Wait
  212. // returns, i.e. when the process has exited; it is the race-free signal here
  213. // (reading cmd.ProcessState would race with that Wait).
  214. if done != nil {
  215. select {
  216. case <-done:
  217. return false
  218. default:
  219. }
  220. }
  221. return true
  222. }
  223. // GetErr returns the last error encountered by the Xray process.
  224. func (p *process) GetErr() error {
  225. p.mu.RLock()
  226. defer p.mu.RUnlock()
  227. return p.exitErr
  228. }
  229. // GetResult returns the last log line or error from the Xray process.
  230. func (p *process) GetResult() string {
  231. p.mu.RLock()
  232. exitErr := p.exitErr
  233. p.mu.RUnlock()
  234. lastLine := p.logWriter.LastLine()
  235. if len(lastLine) == 0 && exitErr != nil {
  236. return exitErr.Error()
  237. }
  238. return lastLine
  239. }
  240. // GetXrayVersion returns the version string of the Xray process.
  241. func (p *process) GetXrayVersion() string {
  242. return p.version
  243. }
  244. // GetAPIPort returns the API port used by the Xray process.
  245. func (p *Process) GetAPIPort() int {
  246. return p.apiPort
  247. }
  248. // GetConfig returns the configuration used by the Xray process.
  249. func (p *Process) GetConfig() *Config {
  250. return p.config
  251. }
  252. // SetConfig replaces the stored configuration snapshot after the running
  253. // process has been reconciled with it through the gRPC API (hot apply), so
  254. // later change detection compares against what is actually running.
  255. func (p *Process) SetConfig(config *Config) {
  256. p.config = config
  257. }
  258. // GetOnlineClients returns the union of locally-online clients and
  259. // node-online clients from every registered remote panel. Dedupes by
  260. // email so a client connected to both a local and a node-managed inbound
  261. // surfaces once. Cheap allocation — typical online sets are small and
  262. // the union is recomputed on demand.
  263. func (p *Process) GetOnlineClients() []string {
  264. p.onlineMu.RLock()
  265. defer p.onlineMu.RUnlock()
  266. if len(p.nodeOnlineTrees) == 0 {
  267. // Hot path for single-panel deployments: avoid the map+dedupe
  268. // work entirely and return the local slice as-is.
  269. return p.onlineClients
  270. }
  271. seen := make(map[string]struct{}, len(p.onlineClients))
  272. out := make([]string, 0, len(p.onlineClients))
  273. add := func(emails []string) {
  274. for _, email := range emails {
  275. if _, dup := seen[email]; dup {
  276. continue
  277. }
  278. seen[email] = struct{}{}
  279. out = append(out, email)
  280. }
  281. }
  282. add(p.onlineClients)
  283. for _, tree := range p.nodeOnlineTrees {
  284. for _, emails := range tree {
  285. add(emails)
  286. }
  287. }
  288. return out
  289. }
  290. // GetLocalOnlineClients returns a copy of the emails online on THIS panel's own
  291. // xray within the grace window. The service layer keys these under the panel's
  292. // own GUID when assembling the per-node online view.
  293. func (p *Process) GetLocalOnlineClients() []string {
  294. p.onlineMu.RLock()
  295. defer p.onlineMu.RUnlock()
  296. if len(p.onlineClients) == 0 {
  297. return nil
  298. }
  299. out := make([]string, len(p.onlineClients))
  300. copy(out, p.onlineClients)
  301. return out
  302. }
  303. // GetMergedNodeTrees returns the union of every direct node's reported subtree,
  304. // keyed by the panelGuid of the node that physically hosts each client set.
  305. // Because each child already reports its descendants under their own GUIDs,
  306. // merging the direct children yields the whole tree at any depth (#4983), so a
  307. // client three hops down is attributed to its real node, not the intermediate
  308. // one. GUIDs are globally unique, but a set reported under the same GUID by more
  309. // than one path is deduped per key; empty sets are omitted.
  310. func (p *Process) GetMergedNodeTrees() map[string][]string {
  311. p.onlineMu.RLock()
  312. defer p.onlineMu.RUnlock()
  313. if len(p.nodeOnlineTrees) == 0 {
  314. return map[string][]string{}
  315. }
  316. out := make(map[string][]string)
  317. seen := make(map[string]map[string]struct{})
  318. for _, tree := range p.nodeOnlineTrees {
  319. for guid, emails := range tree {
  320. if guid == "" || len(emails) == 0 {
  321. continue
  322. }
  323. dedup := seen[guid]
  324. if dedup == nil {
  325. dedup = make(map[string]struct{}, len(emails))
  326. seen[guid] = dedup
  327. }
  328. for _, email := range emails {
  329. if _, ok := dedup[email]; ok {
  330. continue
  331. }
  332. dedup[email] = struct{}{}
  333. out[guid] = append(out[guid], email)
  334. }
  335. }
  336. }
  337. return out
  338. }
  339. // GetLocalActiveInbounds returns a copy of THIS panel's inbound tags that
  340. // carried traffic within the grace window. Only the local xray reports
  341. // per-inbound activity; remote-node snapshots don't carry it, so the service
  342. // layer keys these under the panel's own GUID and a node missing from the
  343. // active-inbounds map means "don't gate" (fall back to the email-only signal).
  344. func (p *Process) GetLocalActiveInbounds() []string {
  345. p.onlineMu.RLock()
  346. defer p.onlineMu.RUnlock()
  347. if len(p.localActiveInbounds) == 0 {
  348. return nil
  349. }
  350. out := make([]string, len(p.localActiveInbounds))
  351. copy(out, p.localActiveInbounds)
  352. return out
  353. }
  354. // RefreshLocalOnline records that each email in activeEmails and each tag in
  355. // activeInboundTags had local xray traffic at now, then rebuilds onlineClients
  356. // and localActiveInbounds from every entry seen within graceMs, pruning older
  357. // ones. Called by the local XrayTrafficJob after each xray gRPC stats poll.
  358. // Pass nil/empty slices to only prune — NodeTrafficSyncJob does this so a
  359. // stopped local xray's clients and inbounds still age out between local polls.
  360. func (p *Process) RefreshLocalOnline(activeEmails, activeInboundTags []string, now, graceMs int64) {
  361. p.onlineMu.Lock()
  362. defer p.onlineMu.Unlock()
  363. if p.localLastOnline == nil {
  364. p.localLastOnline = make(map[string]int64, len(activeEmails))
  365. }
  366. for _, email := range activeEmails {
  367. p.localLastOnline[email] = now
  368. }
  369. online := make([]string, 0, len(p.localLastOnline))
  370. for email, ts := range p.localLastOnline {
  371. if now-ts < graceMs {
  372. online = append(online, email)
  373. } else {
  374. delete(p.localLastOnline, email)
  375. }
  376. }
  377. p.onlineClients = online
  378. if p.localInboundLastActive == nil {
  379. p.localInboundLastActive = make(map[string]int64, len(activeInboundTags))
  380. }
  381. for _, tag := range activeInboundTags {
  382. p.localInboundLastActive[tag] = now
  383. }
  384. activeInbounds := make([]string, 0, len(p.localInboundLastActive))
  385. for tag, ts := range p.localInboundLastActive {
  386. if now-ts < graceMs {
  387. activeInbounds = append(activeInbounds, tag)
  388. } else {
  389. delete(p.localInboundLastActive, tag)
  390. }
  391. }
  392. p.localActiveInbounds = activeInbounds
  393. }
  394. // SetNodeOnlineTree records the GUID-keyed online subtree one direct remote
  395. // node reported (its own clients under its panelGuid plus every descendant
  396. // under theirs). Replaces any previous entry for that node — NodeTrafficSyncJob
  397. // always sends the full subtree per tick.
  398. func (p *Process) SetNodeOnlineTree(nodeID int, tree map[string][]string) {
  399. p.onlineMu.Lock()
  400. defer p.onlineMu.Unlock()
  401. if p.nodeOnlineTrees == nil {
  402. p.nodeOnlineTrees = map[int]map[string][]string{}
  403. }
  404. p.nodeOnlineTrees[nodeID] = tree
  405. }
  406. // ClearNodeOnlineClients drops a direct node's whole subtree contribution.
  407. // Called when a probe fails so a downed node — and everything behind it — doesn't
  408. // keep its clients listed as "online" until the next successful probe.
  409. func (p *Process) ClearNodeOnlineClients(nodeID int) {
  410. p.onlineMu.Lock()
  411. defer p.onlineMu.Unlock()
  412. delete(p.nodeOnlineTrees, nodeID)
  413. }
  414. // GetUptime returns the uptime of the Xray process in seconds.
  415. func (p *Process) GetUptime() uint64 {
  416. return uint64(time.Since(p.startTime).Seconds())
  417. }
  418. // refreshAPIPort updates the API port from the inbound configs.
  419. func (p *process) refreshAPIPort() {
  420. for _, inbound := range p.config.InboundConfigs {
  421. if inbound.Tag == "api" {
  422. p.apiPort = inbound.Port
  423. break
  424. }
  425. }
  426. }
  427. // refreshVersion updates the version string by running the Xray binary with -version.
  428. func (p *process) refreshVersion() {
  429. cmd := exec.CommandContext(context.Background(), GetBinaryPath(), "-version")
  430. data, err := cmd.Output()
  431. if err != nil {
  432. p.version = "Unknown"
  433. } else {
  434. datas := bytes.Split(data, []byte(" "))
  435. if len(datas) <= 1 {
  436. p.version = "Unknown"
  437. } else {
  438. p.version = string(datas[1])
  439. }
  440. }
  441. }
  442. // Start launches the Xray process with the current configuration.
  443. func (p *process) Start() (err error) {
  444. if p.IsRunning() {
  445. return errors.New("xray is already running")
  446. }
  447. defer func() {
  448. if err != nil {
  449. logger.Error("Failure in running xray-core process: ", err)
  450. p.setExitErr(err)
  451. }
  452. }()
  453. data, err := json.MarshalIndent(p.config, "", " ")
  454. if err != nil {
  455. return common.NewErrorf("Failed to generate XRAY configuration files: %v", err)
  456. }
  457. err = os.MkdirAll(config.GetLogFolder(), 0o770)
  458. if err != nil {
  459. logger.Warningf("Failed to create log folder: %s", err)
  460. }
  461. configPath := GetConfigPath()
  462. if p.configPath != "" {
  463. configPath = p.configPath
  464. }
  465. err = writeFileAtomic(configPath, data, 0o600)
  466. if err != nil {
  467. return common.NewErrorf("Failed to write configuration file: %v", err)
  468. }
  469. cmd := exec.CommandContext(context.Background(), GetBinaryPath(), "-c", configPath)
  470. cmd.Stdout = p.logWriter
  471. cmd.Stderr = p.logWriter
  472. err = p.startCommand(cmd)
  473. if err != nil {
  474. return err
  475. }
  476. p.refreshVersion()
  477. p.refreshAPIPort()
  478. return nil
  479. }
  480. // writeFileAtomic writes data to path via a same-directory temp file that is
  481. // permissioned, synced, and renamed into place, so a crash can never leave a
  482. // partial config; the config holds credentials, hence the 0600 perm. After the
  483. // rename the parent directory is fsynced to persist the directory entry. That
  484. // final step is skipped on Windows, where directory fsync is unsupported and
  485. // os.Rename already uses replace-existing semantics.
  486. func writeFileAtomic(path string, data []byte, perm os.FileMode) (err error) {
  487. dir := filepath.Dir(path)
  488. tmp, err := os.CreateTemp(dir, ".config-*.tmp")
  489. if err != nil {
  490. return err
  491. }
  492. tmpPath := tmp.Name()
  493. defer func() {
  494. _ = tmp.Close()
  495. if err != nil {
  496. _ = os.Remove(tmpPath)
  497. }
  498. }()
  499. if err = tmp.Chmod(perm); err != nil {
  500. return err
  501. }
  502. if _, err = tmp.Write(data); err != nil {
  503. return err
  504. }
  505. if err = tmp.Sync(); err != nil {
  506. return err
  507. }
  508. if err = tmp.Close(); err != nil {
  509. return err
  510. }
  511. if err = renameFile(tmpPath, path); err != nil {
  512. return err
  513. }
  514. if runtime.GOOS == "windows" {
  515. return nil
  516. }
  517. dirHandle, err := os.Open(dir)
  518. if err != nil {
  519. return err
  520. }
  521. err = dirHandle.Sync()
  522. _ = dirHandle.Close()
  523. return err
  524. }
  525. var renameFile = os.Rename
  526. func (p *process) startCommand(cmd *exec.Cmd) error {
  527. p.mu.Lock()
  528. p.cmd = cmd
  529. p.done = make(chan struct{})
  530. p.exitErr = nil
  531. done := p.done
  532. p.mu.Unlock()
  533. p.intentionalStop.Store(false)
  534. if err := cmd.Start(); err != nil {
  535. close(done)
  536. p.mu.Lock()
  537. p.cmd = nil
  538. p.mu.Unlock()
  539. return err
  540. }
  541. attachChildLifetime(cmd)
  542. go p.waitForCommand(cmd, done)
  543. return nil
  544. }
  545. func (p *process) setExitErr(err error) {
  546. p.mu.Lock()
  547. p.exitErr = err
  548. p.mu.Unlock()
  549. }
  550. func (p *process) waitForCommand(cmd *exec.Cmd, done chan struct{}) {
  551. defer close(done)
  552. err := cmd.Wait()
  553. if err == nil || p.intentionalStop.Load() {
  554. return
  555. }
  556. // On Windows, killing the process results in "exit status 1" which isn't an error for us.
  557. if runtime.GOOS == "windows" {
  558. errStr := strings.ToLower(err.Error())
  559. if strings.Contains(errStr, "exit status 1") {
  560. p.setExitErr(err)
  561. return
  562. }
  563. }
  564. logger.Error("Failure in running xray-core:", err)
  565. p.setExitErr(err)
  566. if OnCrash != nil {
  567. OnCrash(err)
  568. }
  569. }
  570. // Stop terminates the running Xray process.
  571. func (p *process) Stop() error {
  572. if !p.IsRunning() {
  573. return errors.New("xray is not running")
  574. }
  575. p.intentionalStop.Store(true)
  576. // Snapshot cmd once, then run the blocking Signal/Kill/Wait on the local copy
  577. // without holding the lock.
  578. p.mu.RLock()
  579. cmd := p.cmd
  580. p.mu.RUnlock()
  581. if cmd == nil || cmd.Process == nil {
  582. return errors.New("xray is not running")
  583. }
  584. // Remove temporary config file used for test runs so main config is never touched
  585. if p.configPath != "" {
  586. if p.configPath != GetConfigPath() {
  587. // Check if file exists before removing
  588. if _, err := os.Stat(p.configPath); err == nil {
  589. _ = os.Remove(p.configPath)
  590. }
  591. }
  592. }
  593. if runtime.GOOS == "windows" {
  594. if err := cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
  595. return err
  596. }
  597. return p.waitForExit(xrayForceStopTimeout)
  598. }
  599. if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
  600. if errors.Is(err, os.ErrProcessDone) {
  601. return p.waitForExit(xrayForceStopTimeout)
  602. }
  603. return err
  604. }
  605. if err := p.waitForExit(xrayGracefulStopTimeout); err == nil {
  606. return nil
  607. }
  608. logger.Warning("xray-core did not stop after SIGTERM, killing process")
  609. if err := cmd.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
  610. return err
  611. }
  612. return p.waitForExit(xrayForceStopTimeout)
  613. }
  614. func (p *process) waitForExit(timeout time.Duration) error {
  615. p.mu.RLock()
  616. done := p.done
  617. p.mu.RUnlock()
  618. if done == nil {
  619. return nil
  620. }
  621. timer := time.NewTimer(timeout)
  622. defer timer.Stop()
  623. select {
  624. case <-done:
  625. return nil
  626. case <-timer.C:
  627. return common.NewErrorf("timed out waiting for xray-core process to stop after %s", timeout)
  628. }
  629. }
  630. const (
  631. crashReportPrefix = "core_crash_"
  632. crashReportSuffix = ".log"
  633. maxCrashReports = 10
  634. )
  635. // writeCrashReport persists a captured xray crash chunk to the log folder
  636. // with nanosecond-precision filename so restart-loop bursts don't overwrite
  637. // each other, and prunes old reports to keep the folder bounded.
  638. func writeCrashReport(m []byte) error {
  639. dir := config.GetLogFolder()
  640. if err := os.MkdirAll(dir, 0o770); err != nil {
  641. return err
  642. }
  643. pruneOldCrashReports(dir, maxCrashReports-1)
  644. name := crashReportPrefix + time.Now().Format("20060102_150405_000000000") + crashReportSuffix
  645. return os.WriteFile(filepath.Join(dir, name), m, 0o640)
  646. }
  647. func pruneOldCrashReports(dir string, keep int) {
  648. entries, err := os.ReadDir(dir)
  649. if err != nil {
  650. return
  651. }
  652. var reports []string
  653. for _, e := range entries {
  654. n := e.Name()
  655. if !e.IsDir() && strings.HasPrefix(n, crashReportPrefix) && strings.HasSuffix(n, crashReportSuffix) {
  656. reports = append(reports, n)
  657. }
  658. }
  659. if len(reports) <= keep {
  660. return
  661. }
  662. sort.Strings(reports)
  663. for _, old := range reports[:len(reports)-keep] {
  664. _ = os.Remove(filepath.Join(dir, old))
  665. }
  666. }