urltest.go 2.8 KB

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