process.go 22 KB

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