experimental.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package adapter
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/binary"
  6. "io"
  7. "time"
  8. "github.com/sagernet/sing/common/observable"
  9. "github.com/sagernet/sing/common/varbin"
  10. )
  11. type ClashServer interface {
  12. LifecycleService
  13. ConnectionTracker
  14. Mode() string
  15. ModeList() []string
  16. SetModeUpdateHook(hook *observable.Subscriber[struct{}])
  17. HistoryStorage() URLTestHistoryStorage
  18. }
  19. type URLTestHistory struct {
  20. Time time.Time `json:"time"`
  21. Delay uint16 `json:"delay"`
  22. }
  23. type URLTestHistoryStorage interface {
  24. SetHook(hook *observable.Subscriber[struct{}])
  25. LoadURLTestHistory(tag string) *URLTestHistory
  26. DeleteURLTestHistory(tag string)
  27. StoreURLTestHistory(tag string, history *URLTestHistory)
  28. Close() error
  29. }
  30. type V2RayServer interface {
  31. LifecycleService
  32. StatsService() ConnectionTracker
  33. }
  34. type CacheFile interface {
  35. LifecycleService
  36. StoreFakeIP() bool
  37. FakeIPStorage
  38. StoreRDRC() bool
  39. RDRCStore
  40. LoadMode() string
  41. StoreMode(mode string) error
  42. LoadSelected(group string) string
  43. StoreSelected(group string, selected string) error
  44. LoadGroupExpand(group string) (isExpand bool, loaded bool)
  45. StoreGroupExpand(group string, expand bool) error
  46. LoadRuleSet(tag string) *SavedBinary
  47. SaveRuleSet(tag string, set *SavedBinary) error
  48. }
  49. type SavedBinary struct {
  50. Content []byte
  51. LastUpdated time.Time
  52. LastEtag string
  53. }
  54. func (s *SavedBinary) MarshalBinary() ([]byte, error) {
  55. var buffer bytes.Buffer
  56. err := binary.Write(&buffer, binary.BigEndian, uint8(1))
  57. if err != nil {
  58. return nil, err
  59. }
  60. _, err = varbin.WriteUvarint(&buffer, uint64(len(s.Content)))
  61. if err != nil {
  62. return nil, err
  63. }
  64. _, err = buffer.Write(s.Content)
  65. if err != nil {
  66. return nil, err
  67. }
  68. err = binary.Write(&buffer, binary.BigEndian, s.LastUpdated.Unix())
  69. if err != nil {
  70. return nil, err
  71. }
  72. _, err = varbin.WriteUvarint(&buffer, uint64(len(s.LastEtag)))
  73. if err != nil {
  74. return nil, err
  75. }
  76. _, err = buffer.WriteString(s.LastEtag)
  77. if err != nil {
  78. return nil, err
  79. }
  80. return buffer.Bytes(), nil
  81. }
  82. func (s *SavedBinary) UnmarshalBinary(data []byte) error {
  83. reader := bytes.NewReader(data)
  84. var version uint8
  85. err := binary.Read(reader, binary.BigEndian, &version)
  86. if err != nil {
  87. return err
  88. }
  89. contentLength, err := binary.ReadUvarint(reader)
  90. if err != nil {
  91. return err
  92. }
  93. s.Content = make([]byte, contentLength)
  94. _, err = io.ReadFull(reader, s.Content)
  95. if err != nil {
  96. return err
  97. }
  98. var lastUpdated int64
  99. err = binary.Read(reader, binary.BigEndian, &lastUpdated)
  100. if err != nil {
  101. return err
  102. }
  103. s.LastUpdated = time.Unix(lastUpdated, 0)
  104. etagLength, err := binary.ReadUvarint(reader)
  105. if err != nil {
  106. return err
  107. }
  108. etagBytes := make([]byte, etagLength)
  109. _, err = io.ReadFull(reader, etagBytes)
  110. if err != nil {
  111. return err
  112. }
  113. s.LastEtag = string(etagBytes)
  114. return nil
  115. }
  116. type OutboundGroup interface {
  117. Outbound
  118. Now() string
  119. All() []string
  120. }
  121. type URLTestGroup interface {
  122. OutboundGroup
  123. URLTest(ctx context.Context) (map[string]uint16, error)
  124. }
  125. func OutboundTag(detour Outbound) string {
  126. if group, isGroup := detour.(OutboundGroup); isGroup {
  127. return group.Now()
  128. }
  129. return detour.Tag()
  130. }