urltest.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package urltest
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net"
  6. "net/http"
  7. "net/url"
  8. "sync"
  9. "time"
  10. "github.com/sagernet/sing-box/adapter"
  11. C "github.com/sagernet/sing-box/constant"
  12. "github.com/sagernet/sing/common"
  13. M "github.com/sagernet/sing/common/metadata"
  14. N "github.com/sagernet/sing/common/network"
  15. "github.com/sagernet/sing/common/ntp"
  16. )
  17. var _ adapter.URLTestHistoryStorage = (*HistoryStorage)(nil)
  18. type HistoryStorage struct {
  19. access sync.RWMutex
  20. delayHistory map[string]*adapter.URLTestHistory
  21. updateHook chan<- struct{}
  22. }
  23. func NewHistoryStorage() *HistoryStorage {
  24. return &HistoryStorage{
  25. delayHistory: make(map[string]*adapter.URLTestHistory),
  26. }
  27. }
  28. func (s *HistoryStorage) SetHook(hook chan<- struct{}) {
  29. s.updateHook = hook
  30. }
  31. func (s *HistoryStorage) LoadURLTestHistory(tag string) *adapter.URLTestHistory {
  32. if s == nil {
  33. return nil
  34. }
  35. s.access.RLock()
  36. defer s.access.RUnlock()
  37. return s.delayHistory[tag]
  38. }
  39. func (s *HistoryStorage) DeleteURLTestHistory(tag string) {
  40. s.access.Lock()
  41. delete(s.delayHistory, tag)
  42. s.access.Unlock()
  43. s.notifyUpdated()
  44. }
  45. func (s *HistoryStorage) StoreURLTestHistory(tag string, history *adapter.URLTestHistory) {
  46. s.access.Lock()
  47. s.delayHistory[tag] = history
  48. s.access.Unlock()
  49. s.notifyUpdated()
  50. }
  51. func (s *HistoryStorage) notifyUpdated() {
  52. updateHook := s.updateHook
  53. if updateHook != nil {
  54. select {
  55. case updateHook <- struct{}{}:
  56. default:
  57. }
  58. }
  59. }
  60. func (s *HistoryStorage) Close() error {
  61. s.updateHook = nil
  62. return nil
  63. }
  64. func URLTest(ctx context.Context, link string, detour N.Dialer) (t uint16, err error) {
  65. if link == "" {
  66. link = "https://www.gstatic.com/generate_204"
  67. }
  68. linkURL, err := url.Parse(link)
  69. if err != nil {
  70. return
  71. }
  72. hostname := linkURL.Hostname()
  73. port := linkURL.Port()
  74. if port == "" {
  75. switch linkURL.Scheme {
  76. case "http":
  77. port = "80"
  78. case "https":
  79. port = "443"
  80. }
  81. }
  82. start := time.Now()
  83. instance, err := detour.DialContext(ctx, "tcp", M.ParseSocksaddrHostPortStr(hostname, port))
  84. if err != nil {
  85. return
  86. }
  87. defer instance.Close()
  88. if earlyConn, isEarlyConn := common.Cast[N.EarlyConn](instance); isEarlyConn && earlyConn.NeedHandshake() {
  89. start = time.Now()
  90. }
  91. req, err := http.NewRequest(http.MethodHead, link, nil)
  92. if err != nil {
  93. return
  94. }
  95. client := http.Client{
  96. Transport: &http.Transport{
  97. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  98. return instance, nil
  99. },
  100. TLSClientConfig: &tls.Config{
  101. Time: ntp.TimeFuncFromContext(ctx),
  102. RootCAs: adapter.RootPoolFromContext(ctx),
  103. },
  104. },
  105. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  106. return http.ErrUseLastResponse
  107. },
  108. Timeout: C.TCPTimeout,
  109. }
  110. defer client.CloseIdleConnections()
  111. resp, err := client.Do(req.WithContext(ctx))
  112. if err != nil {
  113. return
  114. }
  115. resp.Body.Close()
  116. t = uint16(time.Since(start) / time.Millisecond)
  117. return
  118. }