2022-07-19 14:16:49 +00:00
|
|
|
package adapter
|
|
|
|
|
|
|
|
import (
|
2023-12-01 05:24:12 +00:00
|
|
|
"bytes"
|
2022-07-19 14:16:49 +00:00
|
|
|
"context"
|
2023-12-01 05:24:12 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"time"
|
2022-07-19 14:16:49 +00:00
|
|
|
|
2022-09-15 07:22:08 +00:00
|
|
|
"github.com/sagernet/sing-box/common/urltest"
|
2024-02-14 12:42:58 +00:00
|
|
|
"github.com/sagernet/sing-dns"
|
2024-06-24 01:49:15 +00:00
|
|
|
"github.com/sagernet/sing/common/varbin"
|
2022-07-19 14:16:49 +00:00
|
|
|
)
|
|
|
|
|
2022-07-19 23:12:40 +00:00
|
|
|
type ClashServer interface {
|
2024-11-10 08:46:59 +00:00
|
|
|
LifecycleService
|
|
|
|
ConnectionTracker
|
2022-09-10 06:09:47 +00:00
|
|
|
Mode() string
|
2023-08-24 13:52:38 +00:00
|
|
|
ModeList() []string
|
2022-09-15 07:22:08 +00:00
|
|
|
HistoryStorage() *urltest.HistoryStorage
|
2024-11-10 08:46:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type V2RayServer interface {
|
|
|
|
LifecycleService
|
|
|
|
StatsService() ConnectionTracker
|
2022-07-19 23:12:40 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 04:00:28 +00:00
|
|
|
type CacheFile interface {
|
2024-11-10 08:46:59 +00:00
|
|
|
LifecycleService
|
2023-11-28 04:00:28 +00:00
|
|
|
|
|
|
|
StoreFakeIP() bool
|
2023-12-01 05:24:12 +00:00
|
|
|
FakeIPStorage
|
2023-11-28 04:00:28 +00:00
|
|
|
|
2024-02-14 12:42:58 +00:00
|
|
|
StoreRDRC() bool
|
|
|
|
dns.RDRCStore
|
|
|
|
|
2023-08-24 13:52:38 +00:00
|
|
|
LoadMode() string
|
|
|
|
StoreMode(mode string) error
|
2022-09-10 06:40:16 +00:00
|
|
|
LoadSelected(group string) string
|
|
|
|
StoreSelected(group string, selected string) error
|
2023-08-12 11:33:43 +00:00
|
|
|
LoadGroupExpand(group string) (isExpand bool, loaded bool)
|
|
|
|
StoreGroupExpand(group string, expand bool) error
|
2023-12-01 05:24:12 +00:00
|
|
|
LoadRuleSet(tag string) *SavedRuleSet
|
|
|
|
SaveRuleSet(tag string, set *SavedRuleSet) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type SavedRuleSet struct {
|
|
|
|
Content []byte
|
|
|
|
LastUpdated time.Time
|
|
|
|
LastEtag string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SavedRuleSet) MarshalBinary() ([]byte, error) {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
err := binary.Write(&buffer, binary.BigEndian, uint8(1))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-24 01:49:15 +00:00
|
|
|
err = varbin.Write(&buffer, binary.BigEndian, s.Content)
|
2023-12-01 05:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = binary.Write(&buffer, binary.BigEndian, s.LastUpdated.Unix())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-24 01:49:15 +00:00
|
|
|
err = varbin.Write(&buffer, binary.BigEndian, s.LastEtag)
|
2023-12-01 05:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buffer.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SavedRuleSet) UnmarshalBinary(data []byte) error {
|
|
|
|
reader := bytes.NewReader(data)
|
|
|
|
var version uint8
|
|
|
|
err := binary.Read(reader, binary.BigEndian, &version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-24 01:49:15 +00:00
|
|
|
err = varbin.Read(reader, binary.BigEndian, &s.Content)
|
2023-12-01 05:24:12 +00:00
|
|
|
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)
|
2024-06-24 01:49:15 +00:00
|
|
|
err = varbin.Read(reader, binary.BigEndian, &s.LastEtag)
|
2023-12-01 05:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-09-10 06:40:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 05:51:08 +00:00
|
|
|
type OutboundGroup interface {
|
2023-07-02 08:45:30 +00:00
|
|
|
Outbound
|
2022-07-22 05:51:08 +00:00
|
|
|
Now() string
|
|
|
|
All() []string
|
|
|
|
}
|
2022-07-28 08:36:31 +00:00
|
|
|
|
2023-04-11 08:43:45 +00:00
|
|
|
type URLTestGroup interface {
|
|
|
|
OutboundGroup
|
2023-12-02 09:47:57 +00:00
|
|
|
URLTest(ctx context.Context) (map[string]uint16, error)
|
2023-04-11 08:43:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-28 08:36:31 +00:00
|
|
|
func OutboundTag(detour Outbound) string {
|
|
|
|
if group, isGroup := detour.(OutboundGroup); isGroup {
|
|
|
|
return group.Now()
|
|
|
|
}
|
|
|
|
return detour.Tag()
|
|
|
|
}
|