urltest.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package urltest
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "net/url"
  7. "sync"
  8. "time"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing/common"
  11. M "github.com/sagernet/sing/common/metadata"
  12. N "github.com/sagernet/sing/common/network"
  13. )
  14. type History struct {
  15. Time time.Time `json:"time"`
  16. Delay uint16 `json:"delay"`
  17. }
  18. type HistoryStorage struct {
  19. access sync.RWMutex
  20. delayHistory map[string]*History
  21. updateHook chan<- struct{}
  22. }
  23. func NewHistoryStorage() *HistoryStorage {
  24. return &HistoryStorage{
  25. delayHistory: make(map[string]*History),
  26. }
  27. }
  28. func (s *HistoryStorage) SetHook(hook chan<- struct{}) {
  29. s.updateHook = hook
  30. }
  31. func (s *HistoryStorage) LoadURLTestHistory(tag string) *History {
  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 *History) {
  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. },
  101. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  102. return http.ErrUseLastResponse
  103. },
  104. Timeout: C.TCPTimeout,
  105. }
  106. defer client.CloseIdleConnections()
  107. resp, err := client.Do(req.WithContext(ctx))
  108. if err != nil {
  109. return
  110. }
  111. resp.Body.Close()
  112. t = uint16(time.Since(start) / time.Millisecond)
  113. return
  114. }