experimental.go 2.3 KB

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