1
0

outbound.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. package service
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "net"
  9. "net/http"
  10. "net/http/httptrace"
  11. "net/url"
  12. "os"
  13. "strconv"
  14. "sync"
  15. "time"
  16. "github.com/mhsanaei/3x-ui/v3/config"
  17. "github.com/mhsanaei/3x-ui/v3/database"
  18. "github.com/mhsanaei/3x-ui/v3/database/model"
  19. "github.com/mhsanaei/3x-ui/v3/logger"
  20. "github.com/mhsanaei/3x-ui/v3/util/json_util"
  21. "github.com/mhsanaei/3x-ui/v3/xray"
  22. "gorm.io/gorm"
  23. )
  24. // OutboundService provides business logic for managing Xray outbound configurations.
  25. // It handles outbound traffic monitoring and statistics.
  26. type OutboundService struct{}
  27. // httpTestSemaphore serialises HTTP-mode probes (each one spawns a temp xray
  28. // instance, which is too expensive to run in parallel). TCP-mode probes are
  29. // dial-only and don't need the semaphore.
  30. var httpTestSemaphore sync.Mutex
  31. func (s *OutboundService) AddTraffic(traffics []*xray.Traffic, clientTraffics []*xray.ClientTraffic) (error, bool) {
  32. var err error
  33. db := database.GetDB()
  34. tx := db.Begin()
  35. defer func() {
  36. if err != nil {
  37. tx.Rollback()
  38. } else {
  39. tx.Commit()
  40. }
  41. }()
  42. err = s.addOutboundTraffic(tx, traffics)
  43. if err != nil {
  44. return err, false
  45. }
  46. return nil, false
  47. }
  48. func (s *OutboundService) addOutboundTraffic(tx *gorm.DB, traffics []*xray.Traffic) error {
  49. if len(traffics) == 0 {
  50. return nil
  51. }
  52. var err error
  53. for _, traffic := range traffics {
  54. if traffic.IsOutbound {
  55. var outbound model.OutboundTraffics
  56. err = tx.Model(&model.OutboundTraffics{}).Where("tag = ?", traffic.Tag).
  57. FirstOrCreate(&outbound).Error
  58. if err != nil {
  59. return err
  60. }
  61. outbound.Tag = traffic.Tag
  62. outbound.Up = outbound.Up + traffic.Up
  63. outbound.Down = outbound.Down + traffic.Down
  64. outbound.Total = outbound.Up + outbound.Down
  65. err = tx.Save(&outbound).Error
  66. if err != nil {
  67. return err
  68. }
  69. }
  70. }
  71. return nil
  72. }
  73. func (s *OutboundService) GetOutboundsTraffic() ([]*model.OutboundTraffics, error) {
  74. db := database.GetDB()
  75. var traffics []*model.OutboundTraffics
  76. err := db.Model(model.OutboundTraffics{}).Find(&traffics).Error
  77. if err != nil {
  78. logger.Warning("Error retrieving OutboundTraffics: ", err)
  79. return nil, err
  80. }
  81. return traffics, nil
  82. }
  83. func (s *OutboundService) ResetOutboundTraffic(tag string) error {
  84. db := database.GetDB()
  85. whereText := "tag "
  86. if tag == "-alltags-" {
  87. whereText += " <> ?"
  88. } else {
  89. whereText += " = ?"
  90. }
  91. result := db.Model(model.OutboundTraffics{}).
  92. Where(whereText, tag).
  93. Updates(map[string]any{"up": 0, "down": 0, "total": 0})
  94. err := result.Error
  95. if err != nil {
  96. return err
  97. }
  98. return nil
  99. }
  100. // TestOutboundResult represents the result of testing an outbound.
  101. // Delay/timing fields are in milliseconds. Endpoints is only populated for
  102. // TCP-mode probes; the HTTP-mode timing breakdown lives in DNSMs/ConnectMs/
  103. // TLSMs/TTFBMs (any of these can be 0 if the underlying step was skipped —
  104. // e.g. a non-TLS target leaves TLSMs at 0).
  105. type TestOutboundResult struct {
  106. Success bool `json:"success"`
  107. Delay int64 `json:"delay"`
  108. Error string `json:"error,omitempty"`
  109. StatusCode int `json:"statusCode,omitempty"`
  110. Mode string `json:"mode,omitempty"`
  111. DNSMs int64 `json:"dnsMs,omitempty"`
  112. ConnectMs int64 `json:"connectMs,omitempty"`
  113. TLSMs int64 `json:"tlsMs,omitempty"`
  114. TTFBMs int64 `json:"ttfbMs,omitempty"`
  115. Endpoints []TestEndpointResult `json:"endpoints,omitempty"`
  116. }
  117. // TestEndpointResult is one entry in a TCP-mode probe — the per-endpoint
  118. // dial outcome for outbounds that expose multiple servers/peers.
  119. type TestEndpointResult struct {
  120. Address string `json:"address"`
  121. Success bool `json:"success"`
  122. Delay int64 `json:"delay"`
  123. Error string `json:"error,omitempty"`
  124. }
  125. // TestOutbound dispatches to the chosen probe mode:
  126. // - mode="tcp": dial the outbound's host:port directly. No xray spin-up,
  127. // parallel-safe, ~100ms per endpoint. Doesn't validate the proxy
  128. // protocol — only that the remote is reachable on TCP.
  129. // - mode="" or "http": spin a temp xray instance, route a real HTTP
  130. // request through it, return delay + a DNS/Connect/TLS/TTFB breakdown.
  131. // Authoritative but expensive and serialised by httpTestSemaphore.
  132. //
  133. // allOutboundsJSON is only consulted in HTTP mode (it backs
  134. // sockopt.dialerProxy chains during test).
  135. func (s *OutboundService) TestOutbound(outboundJSON string, testURL string, allOutboundsJSON string, mode string) (*TestOutboundResult, error) {
  136. if mode == "tcp" {
  137. return s.testOutboundTCP(outboundJSON)
  138. }
  139. return s.testOutboundHTTP(outboundJSON, testURL, allOutboundsJSON)
  140. }
  141. func (s *OutboundService) testOutboundTCP(outboundJSON string) (*TestOutboundResult, error) {
  142. var ob map[string]any
  143. if err := json.Unmarshal([]byte(outboundJSON), &ob); err != nil {
  144. return &TestOutboundResult{Mode: "tcp", Success: false, Error: fmt.Sprintf("Invalid outbound JSON: %v", err)}, nil
  145. }
  146. tag, _ := ob["tag"].(string)
  147. protocol, _ := ob["protocol"].(string)
  148. if protocol == "blackhole" || protocol == "freedom" || tag == "blocked" {
  149. return &TestOutboundResult{Mode: "tcp", Success: false, Error: "Outbound has no testable endpoint"}, nil
  150. }
  151. endpoints := extractOutboundEndpoints(ob)
  152. if len(endpoints) == 0 {
  153. return &TestOutboundResult{Mode: "tcp", Success: false, Error: "No testable endpoint"}, nil
  154. }
  155. results := make([]TestEndpointResult, len(endpoints))
  156. var wg sync.WaitGroup
  157. for i := range endpoints {
  158. wg.Add(1)
  159. go func(i int) {
  160. defer wg.Done()
  161. results[i] = probeTCPEndpoint(endpoints[i], 5*time.Second)
  162. }(i)
  163. }
  164. wg.Wait()
  165. var bestDelay int64 = -1
  166. var firstErr string
  167. for _, r := range results {
  168. if r.Success {
  169. if bestDelay < 0 || r.Delay < bestDelay {
  170. bestDelay = r.Delay
  171. }
  172. } else if firstErr == "" {
  173. firstErr = r.Error
  174. }
  175. }
  176. out := &TestOutboundResult{Mode: "tcp", Endpoints: results}
  177. if bestDelay >= 0 {
  178. out.Success = true
  179. out.Delay = bestDelay
  180. } else {
  181. out.Error = firstErr
  182. if out.Error == "" {
  183. out.Error = "All endpoints unreachable"
  184. }
  185. }
  186. return out, nil
  187. }
  188. func probeTCPEndpoint(endpoint string, timeout time.Duration) TestEndpointResult {
  189. r := TestEndpointResult{Address: endpoint}
  190. start := time.Now()
  191. conn, err := net.DialTimeout("tcp", endpoint, timeout)
  192. r.Delay = time.Since(start).Milliseconds()
  193. if err != nil {
  194. r.Error = err.Error()
  195. return r
  196. }
  197. conn.Close()
  198. r.Success = true
  199. return r
  200. }
  201. func extractOutboundEndpoints(ob map[string]any) []string {
  202. protocol, _ := ob["protocol"].(string)
  203. settings, _ := ob["settings"].(map[string]any)
  204. if settings == nil {
  205. return nil
  206. }
  207. var out []string
  208. addServer := func(addr any, port any) {
  209. host, _ := addr.(string)
  210. p := numAsInt(port)
  211. if host != "" && p > 0 {
  212. out = append(out, fmt.Sprintf("%s:%d", host, p))
  213. }
  214. }
  215. switch protocol {
  216. case "vmess":
  217. if vnext, ok := settings["vnext"].([]any); ok {
  218. for _, v := range vnext {
  219. if vm, ok := v.(map[string]any); ok {
  220. addServer(vm["address"], vm["port"])
  221. }
  222. }
  223. }
  224. case "vless":
  225. addServer(settings["address"], settings["port"])
  226. case "trojan", "shadowsocks", "http", "socks":
  227. if servers, ok := settings["servers"].([]any); ok {
  228. for _, sv := range servers {
  229. if sm, ok := sv.(map[string]any); ok {
  230. addServer(sm["address"], sm["port"])
  231. }
  232. }
  233. }
  234. case "wireguard":
  235. if peers, ok := settings["peers"].([]any); ok {
  236. for _, p := range peers {
  237. if pm, ok := p.(map[string]any); ok {
  238. if ep, _ := pm["endpoint"].(string); ep != "" {
  239. out = append(out, ep)
  240. }
  241. }
  242. }
  243. }
  244. }
  245. return out
  246. }
  247. func numAsInt(v any) int {
  248. switch n := v.(type) {
  249. case float64:
  250. return int(n)
  251. case int:
  252. return n
  253. case int64:
  254. return int(n)
  255. case string:
  256. if i, err := strconv.Atoi(n); err == nil {
  257. return i
  258. }
  259. }
  260. return 0
  261. }
  262. func (s *OutboundService) testOutboundHTTP(outboundJSON string, testURL string, allOutboundsJSON string) (*TestOutboundResult, error) {
  263. if testURL == "" {
  264. testURL = "https://www.google.com/generate_204"
  265. }
  266. if !httpTestSemaphore.TryLock() {
  267. return &TestOutboundResult{
  268. Mode: "http",
  269. Success: false,
  270. Error: "Another outbound test is already running, please wait",
  271. }, nil
  272. }
  273. defer httpTestSemaphore.Unlock()
  274. var testOutbound map[string]any
  275. if err := json.Unmarshal([]byte(outboundJSON), &testOutbound); err != nil {
  276. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Invalid outbound JSON: %v", err)}, nil
  277. }
  278. outboundTag, _ := testOutbound["tag"].(string)
  279. if outboundTag == "" {
  280. return &TestOutboundResult{Mode: "http", Success: false, Error: "Outbound has no tag"}, nil
  281. }
  282. if protocol, _ := testOutbound["protocol"].(string); protocol == "blackhole" || outboundTag == "blocked" {
  283. return &TestOutboundResult{Mode: "http", Success: false, Error: "Blocked/blackhole outbound cannot be tested"}, nil
  284. }
  285. var allOutbounds []any
  286. if allOutboundsJSON != "" {
  287. if err := json.Unmarshal([]byte(allOutboundsJSON), &allOutbounds); err != nil {
  288. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Invalid allOutbounds JSON: %v", err)}, nil
  289. }
  290. }
  291. if len(allOutbounds) == 0 {
  292. allOutbounds = []any{testOutbound}
  293. }
  294. testPort, err := findAvailablePort()
  295. if err != nil {
  296. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Failed to find available port: %v", err)}, nil
  297. }
  298. testConfig := s.createTestConfig(outboundTag, allOutbounds, testPort)
  299. testConfigPath, err := createTestConfigPath()
  300. if err != nil {
  301. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Failed to create test config path: %v", err)}, nil
  302. }
  303. defer os.Remove(testConfigPath)
  304. testProcess := xray.NewTestProcess(testConfig, testConfigPath)
  305. defer func() {
  306. if testProcess.IsRunning() {
  307. testProcess.Stop()
  308. }
  309. }()
  310. if err := testProcess.Start(); err != nil {
  311. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Failed to start test xray instance: %v", err)}, nil
  312. }
  313. if err := waitForPort(testPort, 3*time.Second); err != nil {
  314. if !testProcess.IsRunning() {
  315. result := testProcess.GetResult()
  316. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Xray process exited: %s", result)}, nil
  317. }
  318. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Xray failed to start listening: %v", err)}, nil
  319. }
  320. if !testProcess.IsRunning() {
  321. result := testProcess.GetResult()
  322. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Xray process exited: %s", result)}, nil
  323. }
  324. return s.testConnection(testPort, testURL)
  325. }
  326. // createTestConfig creates a test config by copying all outbounds unchanged and adding
  327. // only the test inbound (SOCKS) and a route rule that sends traffic to the given outbound tag.
  328. func (s *OutboundService) createTestConfig(outboundTag string, allOutbounds []any, testPort int) *xray.Config {
  329. // Test inbound (SOCKS proxy) - only addition to inbounds
  330. testInbound := xray.InboundConfig{
  331. Tag: "test-inbound",
  332. Listen: json_util.RawMessage(`"127.0.0.1"`),
  333. Port: testPort,
  334. Protocol: "socks",
  335. Settings: json_util.RawMessage(`{"auth":"noauth","udp":true}`),
  336. }
  337. // Outbounds: copy all, but set noKernelTun=true for WireGuard outbounds
  338. processedOutbounds := make([]any, len(allOutbounds))
  339. for i, ob := range allOutbounds {
  340. outbound, ok := ob.(map[string]any)
  341. if !ok {
  342. processedOutbounds[i] = ob
  343. continue
  344. }
  345. if protocol, ok := outbound["protocol"].(string); ok && protocol == "wireguard" {
  346. // Set noKernelTun to true for WireGuard outbounds
  347. if settings, ok := outbound["settings"].(map[string]any); ok {
  348. settings["noKernelTun"] = true
  349. } else {
  350. // Create settings if it doesn't exist
  351. outbound["settings"] = map[string]any{
  352. "noKernelTun": true,
  353. }
  354. }
  355. }
  356. processedOutbounds[i] = outbound
  357. }
  358. outboundsJSON, _ := json.Marshal(processedOutbounds)
  359. // Create routing rule to route all traffic through test outbound
  360. routingRules := []map[string]any{
  361. {
  362. "type": "field",
  363. "outboundTag": outboundTag,
  364. "network": "tcp,udp",
  365. },
  366. }
  367. routingJSON, _ := json.Marshal(map[string]any{
  368. "domainStrategy": "AsIs",
  369. "rules": routingRules,
  370. })
  371. // Disable logging for test process to avoid creating orphaned log files
  372. logConfig := map[string]any{
  373. "loglevel": "warning",
  374. "access": "none",
  375. "error": "none",
  376. "dnsLog": false,
  377. }
  378. logJSON, _ := json.Marshal(logConfig)
  379. // Create minimal config
  380. cfg := &xray.Config{
  381. LogConfig: json_util.RawMessage(logJSON),
  382. InboundConfigs: []xray.InboundConfig{
  383. testInbound,
  384. },
  385. OutboundConfigs: json_util.RawMessage(string(outboundsJSON)),
  386. RouterConfig: json_util.RawMessage(string(routingJSON)),
  387. Policy: json_util.RawMessage(`{}`),
  388. Stats: json_util.RawMessage(`{}`),
  389. }
  390. return cfg
  391. }
  392. // testConnection runs the actual HTTP probe through the local SOCKS proxy.
  393. // A warmup request seeds xray's DNS cache / handshake; then a fresh
  394. // transport runs the measured request so httptrace sees a real cold
  395. // connection and reports DNS/Connect/TLS/TTFB. Note that DNS and Connect
  396. // reflect *client → SOCKS-on-loopback*, not the remote target — those
  397. // happen inside xray and aren't visible to net/http. TLS and TTFB are
  398. // the meaningful breakdown values for a SOCKS-proxied HTTPS probe.
  399. func (s *OutboundService) testConnection(proxyPort int, testURL string) (*TestOutboundResult, error) {
  400. proxyURLStr := fmt.Sprintf("socks5://127.0.0.1:%d", proxyPort)
  401. proxyURLParsed, err := url.Parse(proxyURLStr)
  402. if err != nil {
  403. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Invalid proxy URL: %v", err)}, nil
  404. }
  405. mkClient := func() *http.Client {
  406. return &http.Client{
  407. Timeout: 10 * time.Second,
  408. Transport: &http.Transport{
  409. Proxy: http.ProxyURL(proxyURLParsed),
  410. DialContext: (&net.Dialer{
  411. Timeout: 5 * time.Second,
  412. KeepAlive: 30 * time.Second,
  413. }).DialContext,
  414. MaxIdleConns: 1,
  415. IdleConnTimeout: 1 * time.Second,
  416. DisableCompression: true,
  417. },
  418. }
  419. }
  420. warmup := mkClient()
  421. warmupResp, err := warmup.Get(testURL)
  422. if err != nil {
  423. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Request failed: %v", err)}, nil
  424. }
  425. io.Copy(io.Discard, warmupResp.Body)
  426. warmupResp.Body.Close()
  427. warmup.CloseIdleConnections()
  428. var dnsStart, dnsDone, connectStart, connectDone, tlsStart, tlsDone, firstByte time.Time
  429. trace := &httptrace.ClientTrace{
  430. DNSStart: func(_ httptrace.DNSStartInfo) { dnsStart = time.Now() },
  431. DNSDone: func(_ httptrace.DNSDoneInfo) { dnsDone = time.Now() },
  432. ConnectStart: func(_, _ string) { connectStart = time.Now() },
  433. ConnectDone: func(_, _ string, _ error) { connectDone = time.Now() },
  434. TLSHandshakeStart: func() { tlsStart = time.Now() },
  435. TLSHandshakeDone: func(_ tls.ConnectionState, _ error) { tlsDone = time.Now() },
  436. GotFirstResponseByte: func() { firstByte = time.Now() },
  437. }
  438. client := mkClient()
  439. defer client.CloseIdleConnections()
  440. ctx := httptrace.WithClientTrace(context.Background(), trace)
  441. req, err := http.NewRequestWithContext(ctx, http.MethodGet, testURL, nil)
  442. if err != nil {
  443. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Request build failed: %v", err)}, nil
  444. }
  445. startTime := time.Now()
  446. resp, err := client.Do(req)
  447. delay := time.Since(startTime).Milliseconds()
  448. if err != nil {
  449. return &TestOutboundResult{Mode: "http", Success: false, Error: fmt.Sprintf("Request failed: %v", err)}, nil
  450. }
  451. io.Copy(io.Discard, resp.Body)
  452. resp.Body.Close()
  453. out := &TestOutboundResult{
  454. Mode: "http",
  455. Success: true,
  456. Delay: delay,
  457. StatusCode: resp.StatusCode,
  458. }
  459. if !dnsStart.IsZero() && !dnsDone.IsZero() {
  460. out.DNSMs = dnsDone.Sub(dnsStart).Milliseconds()
  461. }
  462. if !connectStart.IsZero() && !connectDone.IsZero() {
  463. out.ConnectMs = connectDone.Sub(connectStart).Milliseconds()
  464. }
  465. if !tlsStart.IsZero() && !tlsDone.IsZero() {
  466. out.TLSMs = tlsDone.Sub(tlsStart).Milliseconds()
  467. }
  468. if !firstByte.IsZero() {
  469. out.TTFBMs = firstByte.Sub(startTime).Milliseconds()
  470. }
  471. return out, nil
  472. }
  473. // waitForPort polls until the given TCP port is accepting connections or the timeout expires.
  474. func waitForPort(port int, timeout time.Duration) error {
  475. deadline := time.Now().Add(timeout)
  476. for time.Now().Before(deadline) {
  477. conn, err := net.DialTimeout("tcp", fmt.Sprintf("127.0.0.1:%d", port), 100*time.Millisecond)
  478. if err == nil {
  479. conn.Close()
  480. return nil
  481. }
  482. time.Sleep(50 * time.Millisecond)
  483. }
  484. return fmt.Errorf("port %d not ready after %v", port, timeout)
  485. }
  486. // findAvailablePort finds an available port for testing
  487. func findAvailablePort() (int, error) {
  488. listener, err := net.Listen("tcp", ":0")
  489. if err != nil {
  490. return 0, err
  491. }
  492. defer listener.Close()
  493. addr := listener.Addr().(*net.TCPAddr)
  494. return addr.Port, nil
  495. }
  496. // createTestConfigPath returns a unique path for a temporary xray config file in the bin folder.
  497. // The temp file is created and closed so the path is reserved; Start() will overwrite it.
  498. func createTestConfigPath() (string, error) {
  499. tmpFile, err := os.CreateTemp(config.GetBinFolderPath(), "xray_test_*.json")
  500. if err != nil {
  501. return "", err
  502. }
  503. path := tmpFile.Name()
  504. if err := tmpFile.Close(); err != nil {
  505. os.Remove(path)
  506. return "", err
  507. }
  508. return path, nil
  509. }