experimental.go 2.3 KB

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