urltest.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.notifyUpdated()
  43. s.access.Unlock()
  44. }
  45. func (s *HistoryStorage) StoreURLTestHistory(tag string, history *adapter.URLTestHistory) {
  46. s.access.Lock()
  47. s.delayHistory[tag] = history
  48. s.notifyUpdated()
  49. s.access.Unlock()
  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.access.Lock()
  62. defer s.access.Unlock()
  63. s.updateHook = nil
  64. return nil
  65. }
  66. func URLTest(ctx context.Context, link string, detour N.Dialer) (t uint16, err error) {
  67. if link == "" {
  68. link = "https://www.gstatic.com/generate_204"
  69. }
  70. linkURL, err := url.Parse(link)
  71. if err != nil {
  72. return
  73. }
  74. hostname := linkURL.Hostname()
  75. port := linkURL.Port()
  76. if port == "" {
  77. switch linkURL.Scheme {
  78. case "http":
  79. port = "80"
  80. case "https":
  81. port = "443"
  82. }
  83. }
  84. start := time.Now()
  85. instance, err := detour.DialContext(ctx, "tcp", M.ParseSocksaddrHostPortStr(hostname, port))
  86. if err != nil {
  87. return
  88. }
  89. defer instance.Close()
  90. if earlyConn, isEarlyConn := common.Cast[N.EarlyConn](instance); isEarlyConn && earlyConn.NeedHandshake() {
  91. start = time.Now()
  92. }
  93. req, err := http.NewRequest(http.MethodHead, link, nil)
  94. if err != nil {
  95. return
  96. }
  97. client := http.Client{
  98. Transport: &http.Transport{
  99. DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
  100. return instance, nil
  101. },
  102. TLSClientConfig: &tls.Config{
  103. Time: ntp.TimeFuncFromContext(ctx),
  104. RootCAs: adapter.RootPoolFromContext(ctx),
  105. },
  106. },
  107. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  108. return http.ErrUseLastResponse
  109. },
  110. Timeout: C.TCPTimeout,
  111. }
  112. defer client.CloseIdleConnections()
  113. resp, err := client.Do(req.WithContext(ctx))
  114. if err != nil {
  115. return
  116. }
  117. resp.Body.Close()
  118. t = uint16(time.Since(start) / time.Millisecond)
  119. return
  120. }