github.com/sagernet/sing-box@v1.9.0-rc.20/adapter/experimental.go (about) 1 package adapter 2 3 import ( 4 "bytes" 5 "context" 6 "encoding/binary" 7 "io" 8 "net" 9 "time" 10 11 "github.com/sagernet/sing-box/common/urltest" 12 "github.com/sagernet/sing-dns" 13 N "github.com/sagernet/sing/common/network" 14 "github.com/sagernet/sing/common/rw" 15 ) 16 17 type ClashServer interface { 18 Service 19 PreStarter 20 Mode() string 21 ModeList() []string 22 HistoryStorage() *urltest.HistoryStorage 23 RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) (net.Conn, Tracker) 24 RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule) (N.PacketConn, Tracker) 25 } 26 27 type CacheFile interface { 28 Service 29 PreStarter 30 31 StoreFakeIP() bool 32 FakeIPStorage 33 34 StoreRDRC() bool 35 dns.RDRCStore 36 37 LoadMode() string 38 StoreMode(mode string) error 39 LoadSelected(group string) string 40 StoreSelected(group string, selected string) error 41 LoadGroupExpand(group string) (isExpand bool, loaded bool) 42 StoreGroupExpand(group string, expand bool) error 43 LoadRuleSet(tag string) *SavedRuleSet 44 SaveRuleSet(tag string, set *SavedRuleSet) error 45 } 46 47 type SavedRuleSet struct { 48 Content []byte 49 LastUpdated time.Time 50 LastEtag string 51 } 52 53 func (s *SavedRuleSet) 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 = rw.WriteUVariant(&buffer, uint64(len(s.Content))) 60 if err != nil { 61 return nil, err 62 } 63 buffer.Write(s.Content) 64 err = binary.Write(&buffer, binary.BigEndian, s.LastUpdated.Unix()) 65 if err != nil { 66 return nil, err 67 } 68 err = rw.WriteVString(&buffer, s.LastEtag) 69 if err != nil { 70 return nil, err 71 } 72 return buffer.Bytes(), nil 73 } 74 75 func (s *SavedRuleSet) UnmarshalBinary(data []byte) error { 76 reader := bytes.NewReader(data) 77 var version uint8 78 err := binary.Read(reader, binary.BigEndian, &version) 79 if err != nil { 80 return err 81 } 82 contentLen, err := rw.ReadUVariant(reader) 83 if err != nil { 84 return err 85 } 86 s.Content = make([]byte, contentLen) 87 _, err = io.ReadFull(reader, s.Content) 88 if err != nil { 89 return err 90 } 91 var lastUpdated int64 92 err = binary.Read(reader, binary.BigEndian, &lastUpdated) 93 if err != nil { 94 return err 95 } 96 s.LastUpdated = time.Unix(lastUpdated, 0) 97 s.LastEtag, err = rw.ReadVString(reader) 98 if err != nil { 99 return err 100 } 101 return nil 102 } 103 104 type Tracker interface { 105 Leave() 106 } 107 108 type OutboundGroup interface { 109 Outbound 110 Now() string 111 All() []string 112 } 113 114 type URLTestGroup interface { 115 OutboundGroup 116 URLTest(ctx context.Context) (map[string]uint16, error) 117 } 118 119 func OutboundTag(detour Outbound) string { 120 if group, isGroup := detour.(OutboundGroup); isGroup { 121 return group.Now() 122 } 123 return detour.Tag() 124 } 125 126 type V2RayServer interface { 127 Service 128 StatsService() V2RayStatsService 129 } 130 131 type V2RayStatsService interface { 132 RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn 133 RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn 134 }