sing-box/adapter/experimental.go

135 lines
2.9 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"
"io"
2022-07-19 22:16:49 +08:00
"net"
2023-12-01 13:24:12 +08:00
"time"
2022-07-19 22:16:49 +08:00
2022-09-15 15:22:08 +08:00
"github.com/sagernet/sing-box/common/urltest"
"github.com/sagernet/sing-dns"
2022-07-19 22:16:49 +08:00
N "github.com/sagernet/sing/common/network"
2023-12-01 13:24:12 +08:00
"github.com/sagernet/sing/common/rw"
2022-07-19 22:16:49 +08:00
)
2022-07-20 07:12:40 +08:00
type ClashServer interface {
Service
2023-03-18 20:26:58 +08:00
PreStarter
2022-09-10 14:09:47 +08:00
Mode() string
ModeList() []string
2022-09-15 15:22:08 +08:00
HistoryStorage() *urltest.HistoryStorage
2022-09-10 14:09:47 +08:00
RoutedConnection(ctx context.Context, conn net.Conn, metadata InboundContext, matchedRule Rule) (net.Conn, Tracker)
RoutedPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext, matchedRule Rule) (N.PacketConn, Tracker)
2022-07-20 07:12:40 +08:00
}
2023-11-28 12:00:28 +08:00
type CacheFile interface {
Service
PreStarter
StoreFakeIP() bool
2023-12-01 13:24:12 +08:00
FakeIPStorage
2023-11-28 12:00:28 +08:00
StoreRDRC() bool
dns.RDRCStore
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
2023-12-01 13:24:12 +08: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
}
err = rw.WriteUVariant(&buffer, uint64(len(s.Content)))
if err != nil {
return nil, err
}
buffer.Write(s.Content)
err = binary.Write(&buffer, binary.BigEndian, s.LastUpdated.Unix())
if err != nil {
return nil, err
}
err = rw.WriteVString(&buffer, s.LastEtag)
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
}
contentLen, err := rw.ReadUVariant(reader)
if err != nil {
return err
}
s.Content = make([]byte, contentLen)
_, err = io.ReadFull(reader, 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)
s.LastEtag, err = rw.ReadVString(reader)
if err != nil {
return err
}
return nil
2022-09-10 14:40:16 +08:00
}
2022-07-26 06:56:13 +08:00
type Tracker interface {
Leave()
}
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)
2023-04-11 16:43:45 +08:00
}
2022-07-28 16:36:31 +08:00
func OutboundTag(detour Outbound) string {
if group, isGroup := detour.(OutboundGroup); isGroup {
return group.Now()
}
return detour.Tag()
}
2022-09-26 19:37:06 +08:00
type V2RayServer interface {
Service
StatsService() V2RayStatsService
}
type V2RayStatsService interface {
2023-02-08 16:50:15 +08:00
RoutedConnection(inbound string, outbound string, user string, conn net.Conn) net.Conn
RoutedPacketConnection(inbound string, outbound string, user string, conn N.PacketConn) N.PacketConn
2022-09-26 19:37:06 +08:00
}