outbound.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "os"
  8. "strconv"
  9. "sync"
  10. "time"
  11. "github.com/mhsanaei/3x-ui/v3/config"
  12. "github.com/mhsanaei/3x-ui/v3/database"
  13. "github.com/mhsanaei/3x-ui/v3/database/model"
  14. "github.com/mhsanaei/3x-ui/v3/logger"
  15. "github.com/mhsanaei/3x-ui/v3/util/json_util"
  16. "github.com/mhsanaei/3x-ui/v3/xray"
  17. "gorm.io/gorm"
  18. )
  19. // OutboundService provides business logic for managing Xray outbound configurations.
  20. // It handles outbound traffic monitoring and statistics.
  21. type OutboundService struct{}
  22. // httpTestSemaphore serialises HTTP-mode probes (each one spawns a temp xray
  23. // instance, which is too expensive to run in parallel). TCP-mode probes are
  24. // dial-only and don't need the semaphore.
  25. var httpTestSemaphore sync.Mutex
  26. func (s *OutboundService) AddTraffic(traffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (error, bool) {
  27. var err error
  28. db := database.GetDB()
  29. tx := db.Begin()
  30. defer func() {
  31. if err != nil {
  32. tx.Rollback()
  33. } else {
  34. tx.Commit()
  35. }
  36. }()
  37. err = s.addOutboundTraffic(tx, traffics)
  38. if err != nil {
  39. return err, false
  40. }
  41. return nil, false
  42. }
  43. func (s *OutboundService) addOutboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
  44. if len(traffics) == 0 {
  45. return nil
  46. }
  47. var err error
  48. for _, traffic := range traffics {
  49. if traffic.IsOutbound {
  50. var outbound model.OutboundTraffics
  51. err = tx.Model(&model.OutboundTraffics{}).Where("tag = ?", traffic.Tag).
  52. FirstOrCreate(&outbound).Error
  53. if err != nil {
  54. return err
  55. }
  56. outbound.Tag = traffic.Tag
  57. outbound.Up = outbound.Up + traffic.Up
  58. outbound.Down = outbound.Down + traffic.Down
  59. outbound.Total = outbound.Up + outbound.Down
  60. err = tx.Save(&outbound).Error
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. }
  66. return nil
  67. }
  68. func (s *OutboundService) GetOutboundsTraffic() ([]*model.OutboundTraffics, error) {
  69. db := database.GetDB()
  70. var traffics []*model.OutboundTraffics
  71. err := db.Model(model.OutboundTraffics{}).Find(&traffics).Error
  72. if err != nil {
  73. logger.Warning("Error retrieving OutboundTraffics: ", err)
  74. return nil, err
  75. }
  76. return traffics, nil
  77. }
  78. func (s *OutboundService) ResetOutboundTraffic(tag string) error {
  79. db := database.GetDB()
  80. whereText := "tag "
  81. if tag == "-alltags-" {
  82. whereText += " <> ?"
  83. } else {
  84. whereText += " = ?"
  85. }
  86. result := db.Model(model.OutboundTraffics{}).
  87. Where(whereText, tag).
  88. Updates(map[string]any{"up": 0, "down": 0, "total": 0})
  89. err := result.Error
  90. if err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. // TestOutboundResult represents the result of testing an outbound.
  96. // Delay is in milliseconds. Endpoints is only populated for TCP-mode
  97. // probes; HTTP mode reports the round-trip delay measured by xray's
  98. // burstObservatory probe.
  99. type TestOutboundResult struct {
  100. Success bool `json:"success"`
  101. Delay int64 `json:"delay"`
  102. Error string `json:"error,omitempty"`
  103. Mode string `json:"mode,omitempty"`
  104. Endpoints []TestEndpointResult `json:"endpoints,omitempty"`
  105. }
  106. // TestEndpointResult is one entry in a TCP-mode probe — the per-endpoint
  107. // dial outcome for outbounds that expose multiple servers/peers.
  108. type TestEndpointResult struct {
  109. Address string `json:"address"`
  110. Success bool `json:"success"`
  111. Delay int64 `json:"delay"`
  112. Error string `json:"error,omitempty"`
  113. }
  114. // TestOutbound dispatches to the chosen probe mode:
  115. // - mode="tcp": dial the outbound's host:port directly. No xray spin-up,
  116. // parallel-safe, ~100ms per endpoint. Doesn't validate the proxy
  117. // protocol — only that the remote is reachable on TCP.
  118. // - mode="" or "http": spin a temp xray instance, route a real HTTP
  119. // request through it, return delay + a DNS/Connect/TLS/TTFB breakdown.
  120. // Authoritative but expensive and serialised by httpTestSemaphore.
  121. //
  122. // allOutboundsJSON is only consulted in HTTP mode (it backs
  123. // sockopt.dialerProxy chains during test).
  124. func (s *OutboundService) TestOutbound(outboundJSON string, testURL string, allOutboundsJSON string, mode string) (*TestOutboundResult, error) {
  125. if mode == "tcp" {
  126. // A bare TCP dial only proves reachability for TCP-based proxies.
  127. // UDP protocols (wireguard, hysteria, kcp/quic transports) ignore
  128. // unauthenticated packets, so a raw dial can't tell "reachable" from
  129. // "dead" — route them through the authoritative xray handshake probe.
  130. var ob map[string]any
  131. if json.Unmarshal([]byte(outboundJSON), &ob) == nil && outboundTransportIsUDP(ob) {
  132. return s.testOutboundHTTP(outboundJSON, testURL, allOutboundsJSON)
  133. }
  134. return s.testOutboundTCP(outboundJSON)
  135. }
  136. return s.testOutboundHTTP(outboundJSON, testURL, allOutboundsJSON)
  137. }
  138. func (s *OutboundService) testOutboundTCP(outboundJSON string) (*TestOutboundResult, error) {
  139. var ob map[string]any
  140. if err := json.Unmarshal([]byte(outboundJSON), &ob); err != nil {
  141. return &TestOutboundResult{Mode: "tcp", Success: false, Error: fmt.Sprintf("Invalid outbound JSON: %v", err)}, nil
  142. }
  143. tag, _ := ob["tag"].(string)
  144. protocol, _ := ob["protocol"].(string)
  145. if protocol == "blackhole" || protocol == "freedom" || tag == "blocked" {
  146. return &TestOutboundResult{Mode: "tcp", Success: false, Error: "Outbound has no testable endpoint"}, nil
  147. }
  148. endpoints := extractOutboundEndpoints(ob)
  149. if len(endpoints) == 0 {
  150. return &TestOutboundResult{Mode: "tcp", Success: false, Error: "No testable endpoint"}, nil
  151. }
  152. results := make([]TestEndpointResult, len(endpoints))
  153. var wg sync.WaitGroup
  154. for i := range endpoints {
  155. wg.Add(1)
  156. go func(i int) {
  157. defer wg.Done()
  158. results[i] = probeTCPEndpoint(endpoints[i], 5*time.Second)
  159. }(i)
  160. }
  161. wg.Wait()
  162. var bestDelay int64 = -1
  163. var firstErr string
  164. for _, r := range results {
  165. if r.Success {
  166. if bestDelay < 0 || r.Delay < bestDelay {
  167. bestDelay = r.Delay
  168. }
  169. } else if firstErr == "" {
  170. firstErr = r.Error
  171. }
  172. }
  173. out := &TestOutboundResult{Mode: "tcp", Endpoints: results}
  174. if bestDelay >= 0 {
  175. out.Success = true
  176. out.Delay = bestDelay
  177. } else {
  178. out.Error = firstErr
  179. if out.Error == "" {
  180. out.Error = "All endpoints unreachable"
  181. }
  182. }
  183. return out, nil
  184. }
  185. func probeTCPEndpoint(endpoint string, timeout time.Duration) TestEndpointResult {
  186. r := TestEndpointResult{Address: endpoint}
  187. start := time.Now()
  188. conn, err := net.DialTimeout("tcp", endpoint, timeout)
  189. r.Delay = time.Since(start).Milliseconds()
  190. if err != nil {
  191. r.Error = err.Error()
  192. return r
  193. }
  194. conn.Close()
  195. r.Success = true
  196. return r
  197. }
  198. // outboundTransportIsUDP reports whether the outbound's proxy speaks UDP
  199. // (wireguard, hysteria, or a kcp/quic/hysteria stream transport). A bare
  200. // UDP dial can't probe these — they ignore unauthenticated packets, so a
  201. // dial neither proves reachability nor measures latency. Such outbounds
  202. // must go through the real xray handshake probe instead.
  203. func outboundTransportIsUDP(ob map[string]any) bool {
  204. if protocol, _ := ob["protocol"].(string); protocol == "hysteria" || protocol == "wireguard" {
  205. return true
  206. }
  207. if stream, ok := ob["streamSettings"].(map[string]any); ok {
  208. if n, _ := stream["network"].(string); n == "hysteria" || n == "kcp" || n == "quic" {
  209. return true
  210. }
  211. }
  212. return false
  213. }
  214. func extractOutboundEndpoints(ob map[string]any) []string {
  215. protocol, _ := ob["protocol"].(string)
  216. settings, _ := ob["settings"].(map[string]any)
  217. if settings == nil {
  218. return nil
  219. }
  220. var out []string
  221. addServer := func(addr any, port any) {
  222. host, _ := addr.(string)
  223. p := numAsInt(port)
  224. if host != "" && p > 0 {
  225. out = append(out, fmt.Sprintf("%s:%d", host, p))
  226. }
  227. }
  228. switch protocol {
  229. case "vmess":
  230. if vnext, ok := settings["vnext"].([]any); ok {
  231. for _, v := range vnext {
  232. if vm, ok := v.(map[string]any); ok {
  233. addServer(vm["address"], vm["port"])
  234. }
  235. }
  236. }
  237. case "vless":
  238. addServer(settings["address"], settings["port"])
  239. case "hysteria":
  240. addServer(settings["address"], settings["port"])
  241. case "trojan", "shadowsocks", "http", "socks":
  242. if servers, ok := settings["servers"].([]any); ok {
  243. for _, sv := range servers {
  244. if sm, ok := sv.(map[string]any); ok {
  245. addServer(sm["address"], sm["port"])
  246. }
  247. }
  248. }
  249. case "wireguard":
  250. if peers, ok := settings["peers"].([]any); ok {
  251. for _, p := range peers {
  252. if pm, ok := p.(map[string]any); ok {
  253. if ep, _ := pm["endpoint"].(string); ep != "" {
  254. out = append(out, ep)
  255. }
  256. }
  257. }
  258. }
  259. }
  260. return out
  261. }
  262. func numAsInt(v any) int {
  263. switch n := v.(type) {
  264. case float64:
  265. return int(n)
  266. case int:
  267. return n
  268. case int64:
  269. return int(n)
  270. case string:
  271. if i, err := strconv.Atoi(n); err == nil {
  272. return i
  273. }
  274. }
  275. return 0
  276. }
  277. // testOutboundHTTP spins up a temporary xray instance whose only job is
  278. // to run a burstObservatory probe against the target outbound, then polls
  279. // xray's metrics /debug/vars endpoint until that outbound is reported
  280. // alive (success) or the deadline expires (failure). The probe lives
  281. // inside xray, so the measured delay and any failure reason reflect what
  282. // xray itself sees over the real proxy chain — no SOCKS round-trip on
  283. // the client side.
  284. func (s *OutboundService) testOutboundHTTP(outboundJSON string, testURL string, allOutboundsJSON string) (*TestOutboundResult, error) {
  285. if testURL == "" {
  286. testURL = "https://www.google.com/generate_204"
  287. }
  288. if !httpTestSemaphore.TryLock() {
  289. return &TestOutboundResult{
  290. Mode: "http",
  291. Success: false,
  292. Error: "Another outbound test is already running, please wait",
  293. }, nil
  294. }
  295. defer httpTestSemaphore.Unlock()
  296. var testOutbound map[string]any
  297. if err := json.Unmarshal([]byte(outboundJSON), &testOutbound); err != nil {
  298. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Invalid outbound JSON: %v", err)}, nil
  299. }
  300. outboundTag, _ := testOutbound["tag"].(string)
  301. if outboundTag == "" {
  302. return &TestOutboundResult{Mode: "http", Success: false, Error: "Outbound has no tag"}, nil
  303. }
  304. if protocol, _ := testOutbound["protocol"].(string); protocol == "blackhole" || outboundTag == "blocked" {
  305. return &TestOutboundResult{Mode: "http", Success: false, Error: "Blocked/blackhole outbound cannot be tested"}, nil
  306. }
  307. var allOutbounds []any
  308. if allOutboundsJSON != "" {
  309. if err := json.Unmarshal([]byte(allOutboundsJSON), &allOutbounds); err != nil {
  310. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Invalid allOutbounds JSON: %v", err)}, nil
  311. }
  312. }
  313. if len(allOutbounds) == 0 {
  314. allOutbounds = []any{testOutbound}
  315. }
  316. metricsPort, err := findAvailablePort()
  317. if err != nil {
  318. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Failed to find available port: %v", err)}, nil
  319. }
  320. testConfig := s.createTestConfig(outboundTag, allOutbounds, metricsPort, testURL)
  321. testConfigPath, err := createTestConfigPath()
  322. if err != nil {
  323. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Failed to create test config path: %v", err)}, nil
  324. }
  325. defer os.Remove(testConfigPath)
  326. testProcess := xray.NewTestProcess(testConfig, testConfigPath)
  327. defer func() {
  328. if testProcess.IsRunning() {
  329. testProcess.Stop()
  330. }
  331. }()
  332. if err := testProcess.Start(); err != nil {
  333. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Failed to start test xray instance: %v", err)}, nil
  334. }
  335. if err := waitForPort(metricsPort, 5*time.Second); err != nil {
  336. if !testProcess.IsRunning() {
  337. result := testProcess.GetResult()
  338. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Xray process exited: %s", result)}, nil
  339. }
  340. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Xray failed to start metrics listener: %v", err)}, nil
  341. }
  342. if !testProcess.IsRunning() {
  343. result := testProcess.GetResult()
  344. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Xray process exited: %s", result)}, nil
  345. }
  346. return pollObservatoryResult(testProcess, metricsPort, outboundTag, 12*time.Second), nil
  347. }
  348. // createTestConfig builds a probe-only xray config: the original outbounds
  349. // are kept as-is so dialerProxy chains still resolve, a burstObservatory
  350. // is wired to probe the target tag, and a metrics listener exposes the
  351. // observatory snapshot via /debug/vars. No inbound or routing rules are
  352. // needed — burstObservatory issues the probe traffic itself.
  353. func (s *OutboundService) createTestConfig(outboundTag string, allOutbounds []any, metricsPort int, probeURL string) *xray.Config {
  354. processedOutbounds := make([]any, len(allOutbounds))
  355. for i, ob := range allOutbounds {
  356. outbound, ok := ob.(map[string]any)
  357. if !ok {
  358. processedOutbounds[i] = ob
  359. continue
  360. }
  361. if protocol, ok := outbound["protocol"].(string); ok && protocol == "wireguard" {
  362. if settings, ok := outbound["settings"].(map[string]any); ok {
  363. settings["noKernelTun"] = true
  364. } else {
  365. outbound["settings"] = map[string]any{"noKernelTun": true}
  366. }
  367. }
  368. processedOutbounds[i] = outbound
  369. }
  370. outboundsJSON, _ := json.Marshal(processedOutbounds)
  371. routingJSON, _ := json.Marshal(map[string]any{
  372. "domainStrategy": "AsIs",
  373. "rules": []any{},
  374. })
  375. burstObservatoryJSON, _ := json.Marshal(map[string]any{
  376. "subjectSelector": []string{outboundTag},
  377. "pingConfig": map[string]any{
  378. "destination": probeURL,
  379. "interval": "1s",
  380. "connectivity": "",
  381. "timeout": "5s",
  382. "samplingCount": 1,
  383. },
  384. })
  385. metricsJSON, _ := json.Marshal(map[string]any{
  386. "tag": "test-metrics",
  387. "listen": fmt.Sprintf("127.0.0.1:%d", metricsPort),
  388. })
  389. logConfig := map[string]any{
  390. "loglevel": "warning",
  391. "access": "none",
  392. "error": "none",
  393. "dnsLog": false,
  394. }
  395. logJSON, _ := json.Marshal(logConfig)
  396. cfg := &xray.Config{
  397. LogConfig: json_util.RawMessage(logJSON),
  398. InboundConfigs: []xray.InboundConfig{},
  399. OutboundConfigs: json_util.RawMessage(string(outboundsJSON)),
  400. RouterConfig: json_util.RawMessage(string(routingJSON)),
  401. Policy: json_util.RawMessage(`{}`),
  402. Stats: json_util.RawMessage(`{}`),
  403. BurstObservatory: json_util.RawMessage(string(burstObservatoryJSON)),
  404. Metrics: json_util.RawMessage(string(metricsJSON)),
  405. }
  406. return cfg
  407. }
  408. // observatoryEntry mirrors the per-outbound shape published by xray's
  409. // observatory under /debug/vars.
  410. type observatoryEntry struct {
  411. Alive bool `json:"alive"`
  412. Delay int64 `json:"delay"`
  413. LastSeenTime int64 `json:"last_seen_time"`
  414. LastTryTime int64 `json:"last_try_time"`
  415. OutboundTag string `json:"outbound_tag"`
  416. }
  417. // pollObservatoryResult repeatedly reads /debug/vars and returns as soon
  418. // as the target outbound reports alive=true. burstObservatory updates the
  419. // snapshot after each ping (interval=1s, timeout=5s), so a healthy
  420. // outbound usually surfaces within ~2s and the timeout caps the wait for
  421. // truly dead ones.
  422. func pollObservatoryResult(testProcess *xray.Process, metricsPort int, tag string, timeout time.Duration) *TestOutboundResult {
  423. url := fmt.Sprintf("http://127.0.0.1:%d/debug/vars", metricsPort)
  424. client := &http.Client{Timeout: 2 * time.Second}
  425. deadline := time.Now().Add(timeout)
  426. var lastEntry observatoryEntry
  427. var sawEntry bool
  428. for time.Now().Before(deadline) {
  429. if !testProcess.IsRunning() {
  430. result := testProcess.GetResult()
  431. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Xray process exited: %s", result)}
  432. }
  433. entry, ok := fetchObservatoryEntry(client, url, tag)
  434. if ok {
  435. if entry.Alive {
  436. delay := entry.Delay
  437. if delay <= 0 {
  438. delay = 1
  439. }
  440. return &TestOutboundResult{Mode: "http", Success: true, Delay: delay}
  441. }
  442. lastEntry = entry
  443. sawEntry = true
  444. }
  445. time.Sleep(400 * time.Millisecond)
  446. }
  447. msg := "Probe timed out — outbound did not become reachable"
  448. if sawEntry && lastEntry.LastTryTime > 0 {
  449. msg = fmt.Sprintf("All probes failed (last attempt %ds ago)", time.Now().Unix()-lastEntry.LastTryTime)
  450. }
  451. return &TestOutboundResult{Mode: "http", Success: false, Error: msg}
  452. }
  453. func fetchObservatoryEntry(client *http.Client, url, tag string) (observatoryEntry, bool) {
  454. resp, err := client.Get(url)
  455. if err != nil {
  456. return observatoryEntry{}, false
  457. }
  458. defer resp.Body.Close()
  459. if resp.StatusCode != http.StatusOK {
  460. return observatoryEntry{}, false
  461. }
  462. var payload struct {
  463. Observatory map[string]observatoryEntry `json:"observatory"`
  464. }
  465. if err := json.NewDecoder(resp.Body).Decode(&payload); err != nil {
  466. return observatoryEntry{}, false
  467. }
  468. if entry, ok := payload.Observatory[tag]; ok {
  469. return entry, true
  470. }
  471. for _, entry := range payload.Observatory {
  472. if entry.OutboundTag == tag {
  473. return entry, true
  474. }
  475. }
  476. return observatoryEntry{}, false
  477. }
  478. // waitForPort polls until the given TCP port is accepting connections or the timeout expires.
  479. func waitForPort(port int, timeout time.Duration) error {
  480. deadline := time.Now().Add(timeout)
  481. for time.Now().Before(deadline) {
  482. conn, err := net.DialTimeout("tcp", fmt.Sprintf("127.0.0.1:%d", port), 100*time.Millisecond)
  483. if err == nil {
  484. conn.Close()
  485. return nil
  486. }
  487. time.Sleep(50 * time.Millisecond)
  488. }
  489. return fmt.Errorf("port %d not ready after %v", port, timeout)
  490. }
  491. // findAvailablePort finds an available port for testing
  492. func findAvailablePort() (int, error) {
  493. listener, err := net.Listen("tcp", ":0")
  494. if err != nil {
  495. return 0, err
  496. }
  497. defer listener.Close()
  498. addr := listener.Addr().(*net.TCPAddr)
  499. return addr.Port, nil
  500. }
  501. // createTestConfigPath returns a unique path for a temporary xray config file in the bin folder.
  502. // The temp file is created and closed so the path is reserved; Start() will overwrite it.
  503. func createTestConfigPath() (string, error) {
  504. tmpFile, err := os.CreateTemp(config.GetBinFolderPath(), "xray_test_*.json")
  505. if err != nil {
  506. return "", err
  507. }
  508. path := tmpFile.Name()
  509. if err := tmpFile.Close(); err != nil {
  510. os.Remove(path)
  511. return "", err
  512. }
  513. return path, nil
  514. }