Files
sing-box-extended-mirror/adapter/experimental.go
T

162 lines
3.3 KiB
Go
Raw Normal View History

2022-07-19 22:16:49 +08:00
package adapter
import (
2023-12-01 13:24:12 +08:00
"bytes"
2022-07-19 22:16:49 +08:00
"context"
2023-12-01 13:24:12 +08:00
"encoding/binary"
2026-01-28 18:22:28 +08:00
"io"
2023-12-01 13:24:12 +08:00
"time"
2022-07-19 22:16:49 +08:00
E "github.com/sagernet/sing/common/exceptions"
2024-06-24 09:49:15 +08:00
"github.com/sagernet/sing/common/varbin"
2022-07-19 22:16:49 +08:00
)
2025-01-08 10:34:45 +08:00
type URLTestHistory struct {
Time time.Time `json:"time"`
Delay uint16 `json:"delay"`
}
2024-11-10 16:46:59 +08:00
type V2RayServer interface {
LifecycleService
StatsService() ConnectionTracker
2022-07-20 07:12:40 +08:00
}
2023-11-28 12:00:28 +08:00
type CacheFile interface {
2024-11-10 16:46:59 +08:00
LifecycleService
2023-11-28 12:00:28 +08:00
2026-07-22 22:03:58 +08:00
CacheID() string
2023-11-28 12:00:28 +08:00
StoreFakeIP() bool
2023-12-01 13:24:12 +08:00
FakeIPStorage
2023-11-28 12:00:28 +08:00
2024-02-14 20:42:58 +08:00
StoreRDRC() bool
2025-03-16 14:50:44 +08:00
RDRCStore
2024-02-14 20:42:58 +08:00
2026-04-11 12:10:52 +08:00
StoreDNS() bool
DNSCacheStore
SetDisableExpire(disableExpire bool)
SetOptimisticTimeout(timeout time.Duration)
LoadMode() string
StoreMode(mode string) error
2022-09-10 14:40:16 +08:00
LoadSelected(group string) string
StoreSelected(group string, selected string) error
2023-08-12 19:33:43 +08:00
LoadGroupExpand(group string) (isExpand bool, loaded bool)
StoreGroupExpand(group string, expand bool) error
2025-02-01 19:42:21 +08:00
LoadRuleSet(tag string) *SavedBinary
SaveRuleSet(tag string, set *SavedBinary) error
2023-12-01 13:24:12 +08:00
}
2025-02-01 19:42:21 +08:00
type SavedBinary struct {
2023-12-01 13:24:12 +08:00
Content []byte
LastUpdated time.Time
LastEtag string
URLHash []byte
2023-12-01 13:24:12 +08:00
}
2025-02-01 19:42:21 +08:00
func (s *SavedBinary) MarshalBinary() ([]byte, error) {
2023-12-01 13:24:12 +08:00
var buffer bytes.Buffer
err := binary.Write(&buffer, binary.BigEndian, uint8(2))
2023-12-01 13:24:12 +08:00
if err != nil {
return nil, err
}
2026-01-28 18:22:28 +08:00
_, err = varbin.WriteUvarint(&buffer, uint64(len(s.Content)))
if err != nil {
return nil, err
}
_, err = buffer.Write(s.Content)
2023-12-01 13:24:12 +08:00
if err != nil {
return nil, err
}
err = binary.Write(&buffer, binary.BigEndian, s.LastUpdated.Unix())
if err != nil {
return nil, err
}
2026-01-28 18:22:28 +08:00
_, err = varbin.WriteUvarint(&buffer, uint64(len(s.LastEtag)))
if err != nil {
return nil, err
}
_, err = buffer.WriteString(s.LastEtag)
2023-12-01 13:24:12 +08:00
if err != nil {
return nil, err
}
_, err = varbin.WriteUvarint(&buffer, uint64(len(s.URLHash)))
if err != nil {
return nil, err
}
_, err = buffer.Write(s.URLHash)
if err != nil {
return nil, err
}
2023-12-01 13:24:12 +08:00
return buffer.Bytes(), nil
}
2025-02-01 19:42:21 +08:00
func (s *SavedBinary) UnmarshalBinary(data []byte) error {
2023-12-01 13:24:12 +08:00
reader := bytes.NewReader(data)
var version uint8
err := binary.Read(reader, binary.BigEndian, &version)
if err != nil {
return err
}
2026-01-28 18:22:28 +08:00
contentLength, err := binary.ReadUvarint(reader)
if err != nil {
return err
}
if contentLength > uint64(reader.Len()) {
return E.New("invalid content length: ", contentLength)
}
2026-01-28 18:22:28 +08:00
s.Content = make([]byte, contentLength)
_, err = io.ReadFull(reader, s.Content)
2023-12-01 13:24:12 +08: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)
2026-01-28 18:22:28 +08:00
etagLength, err := binary.ReadUvarint(reader)
if err != nil {
return err
}
if etagLength > uint64(reader.Len()) {
return E.New("invalid etag length: ", etagLength)
}
2026-01-28 18:22:28 +08:00
etagBytes := make([]byte, etagLength)
_, err = io.ReadFull(reader, etagBytes)
2023-12-01 13:24:12 +08:00
if err != nil {
return err
}
2026-01-28 18:22:28 +08:00
s.LastEtag = string(etagBytes)
if version < 2 {
return nil
}
urlHashLength, err := binary.ReadUvarint(reader)
if err != nil {
return err
}
if urlHashLength > uint64(reader.Len()) {
return E.New("invalid url hash length: ", urlHashLength)
}
s.URLHash = make([]byte, urlHashLength)
_, err = io.ReadFull(reader, s.URLHash)
if err != nil {
return err
}
2023-12-01 13:24:12 +08:00
return nil
2022-09-10 14:40:16 +08:00
}
2022-07-22 13:51:08 +08:00
type OutboundGroup interface {
2023-07-02 16:45:30 +08:00
Outbound
2022-07-22 13:51:08 +08:00
Now() string
All() []string
}
2022-07-28 16:36:31 +08:00
2023-04-11 16:43:45 +08:00
type URLTestGroup interface {
OutboundGroup
2023-12-02 17:47:57 +08:00
URLTest(ctx context.Context) (map[string]uint16, error)
PerformUpdateCheck()
2023-04-11 16:43:45 +08:00
}