| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 | 
							- package adapter
 
- import (
 
- 	"bytes"
 
- 	"context"
 
- 	"encoding/binary"
 
- 	"time"
 
- 	"github.com/sagernet/sing/common/observable"
 
- 	"github.com/sagernet/sing/common/varbin"
 
- )
 
- type ClashServer interface {
 
- 	LifecycleService
 
- 	ConnectionTracker
 
- 	Mode() string
 
- 	ModeList() []string
 
- 	SetModeUpdateHook(hook *observable.Subscriber[struct{}])
 
- 	HistoryStorage() URLTestHistoryStorage
 
- }
 
- type URLTestHistory struct {
 
- 	Time  time.Time `json:"time"`
 
- 	Delay uint16    `json:"delay"`
 
- }
 
- type URLTestHistoryStorage interface {
 
- 	SetHook(hook *observable.Subscriber[struct{}])
 
- 	LoadURLTestHistory(tag string) *URLTestHistory
 
- 	DeleteURLTestHistory(tag string)
 
- 	StoreURLTestHistory(tag string, history *URLTestHistory)
 
- 	Close() error
 
- }
 
- type V2RayServer interface {
 
- 	LifecycleService
 
- 	StatsService() ConnectionTracker
 
- }
 
- type CacheFile interface {
 
- 	LifecycleService
 
- 	StoreFakeIP() bool
 
- 	FakeIPStorage
 
- 	StoreRDRC() bool
 
- 	RDRCStore
 
- 	LoadMode() string
 
- 	StoreMode(mode string) error
 
- 	LoadSelected(group string) string
 
- 	StoreSelected(group string, selected string) error
 
- 	LoadGroupExpand(group string) (isExpand bool, loaded bool)
 
- 	StoreGroupExpand(group string, expand bool) error
 
- 	LoadRuleSet(tag string) *SavedBinary
 
- 	SaveRuleSet(tag string, set *SavedBinary) error
 
- }
 
- type SavedBinary struct {
 
- 	Content     []byte
 
- 	LastUpdated time.Time
 
- 	LastEtag    string
 
- }
 
- func (s *SavedBinary) MarshalBinary() ([]byte, error) {
 
- 	var buffer bytes.Buffer
 
- 	err := binary.Write(&buffer, binary.BigEndian, uint8(1))
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	err = varbin.Write(&buffer, binary.BigEndian, s.Content)
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	err = binary.Write(&buffer, binary.BigEndian, s.LastUpdated.Unix())
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	err = varbin.Write(&buffer, binary.BigEndian, s.LastEtag)
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	return buffer.Bytes(), nil
 
- }
 
- func (s *SavedBinary) UnmarshalBinary(data []byte) error {
 
- 	reader := bytes.NewReader(data)
 
- 	var version uint8
 
- 	err := binary.Read(reader, binary.BigEndian, &version)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	err = varbin.Read(reader, binary.BigEndian, &s.Content)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	var lastUpdated int64
 
- 	err = binary.Read(reader, binary.BigEndian, &lastUpdated)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	s.LastUpdated = time.Unix(lastUpdated, 0)
 
- 	err = varbin.Read(reader, binary.BigEndian, &s.LastEtag)
 
- 	if err != nil {
 
- 		return err
 
- 	}
 
- 	return nil
 
- }
 
- type OutboundGroup interface {
 
- 	Outbound
 
- 	Now() string
 
- 	All() []string
 
- }
 
- type URLTestGroup interface {
 
- 	OutboundGroup
 
- 	URLTest(ctx context.Context) (map[string]uint16, error)
 
- }
 
- func OutboundTag(detour Outbound) string {
 
- 	if group, isGroup := detour.(OutboundGroup); isGroup {
 
- 		return group.Now()
 
- 	}
 
- 	return detour.Tag()
 
- }
 
 
  |