urltest.go 2.8 KB

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