experimental.go 2.7 KB

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