Add fakeip support
This commit is contained in:
44
transport/fakeip/memory.go
Normal file
44
transport/fakeip/memory.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package fakeip
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing/common/cache"
|
||||
)
|
||||
|
||||
var _ adapter.FakeIPStorage = (*MemoryStorage)(nil)
|
||||
|
||||
type MemoryStorage struct {
|
||||
metadata *adapter.FakeIPMetadata
|
||||
domainCache *cache.LruCache[netip.Addr, string]
|
||||
}
|
||||
|
||||
func NewMemoryStorage() *MemoryStorage {
|
||||
return &MemoryStorage{
|
||||
domainCache: cache.New[netip.Addr, string](),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPMetadata() *adapter.FakeIPMetadata {
|
||||
return s.metadata
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPSaveMetadata(metadata *adapter.FakeIPMetadata) error {
|
||||
s.metadata = metadata
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPStore(address netip.Addr, domain string) error {
|
||||
s.domainCache.Store(address, domain)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPLoad(address netip.Addr) (string, bool) {
|
||||
return s.domainCache.Load(address)
|
||||
}
|
||||
|
||||
func (s *MemoryStorage) FakeIPReset() error {
|
||||
s.domainCache = cache.New[netip.Addr, string]()
|
||||
return nil
|
||||
}
|
||||
55
transport/fakeip/packet.go
Normal file
55
transport/fakeip/packet.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package fakeip
|
||||
|
||||
import (
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
var _ N.PacketConn = (*NATPacketConn)(nil)
|
||||
|
||||
type NATPacketConn struct {
|
||||
N.PacketConn
|
||||
origin M.Socksaddr
|
||||
destination M.Socksaddr
|
||||
}
|
||||
|
||||
func NewNATPacketConn(conn N.PacketConn, origin M.Socksaddr, destination M.Socksaddr) *NATPacketConn {
|
||||
return &NATPacketConn{
|
||||
PacketConn: conn,
|
||||
origin: socksaddrWithoutPort(origin),
|
||||
destination: socksaddrWithoutPort(destination),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *NATPacketConn) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, err error) {
|
||||
destination, err = c.PacketConn.ReadPacket(buffer)
|
||||
if socksaddrWithoutPort(destination) == c.origin {
|
||||
destination = M.Socksaddr{
|
||||
Addr: c.destination.Addr,
|
||||
Fqdn: c.destination.Fqdn,
|
||||
Port: destination.Port,
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *NATPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||
if socksaddrWithoutPort(destination) == c.destination {
|
||||
destination = M.Socksaddr{
|
||||
Addr: c.origin.Addr,
|
||||
Fqdn: c.origin.Fqdn,
|
||||
Port: destination.Port,
|
||||
}
|
||||
}
|
||||
return c.PacketConn.WritePacket(buffer, destination)
|
||||
}
|
||||
|
||||
func (c *NATPacketConn) Upstream() any {
|
||||
return c.PacketConn
|
||||
}
|
||||
|
||||
func socksaddrWithoutPort(destination M.Socksaddr) M.Socksaddr {
|
||||
destination.Port = 0
|
||||
return destination
|
||||
}
|
||||
83
transport/fakeip/server.go
Normal file
83
transport/fakeip/server.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package fakeip
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"os"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-dns"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
"github.com/sagernet/sing/common/logger"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
|
||||
mDNS "github.com/miekg/dns"
|
||||
)
|
||||
|
||||
var _ dns.Transport = (*Server)(nil)
|
||||
|
||||
func init() {
|
||||
dns.RegisterTransport([]string{"fakeip"}, NewTransport)
|
||||
}
|
||||
|
||||
type Server struct {
|
||||
name string
|
||||
router adapter.Router
|
||||
store adapter.FakeIPStore
|
||||
logger logger.ContextLogger
|
||||
}
|
||||
|
||||
func NewTransport(name string, ctx context.Context, logger logger.ContextLogger, dialer N.Dialer, link string) (dns.Transport, error) {
|
||||
router := adapter.RouterFromContext(ctx)
|
||||
if router == nil {
|
||||
return nil, E.New("missing router in context")
|
||||
}
|
||||
return &Server{
|
||||
name: name,
|
||||
router: router,
|
||||
logger: logger,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) Name() string {
|
||||
return s.name
|
||||
}
|
||||
|
||||
func (s *Server) Start() error {
|
||||
s.store = s.router.FakeIPStore()
|
||||
if s.store == nil {
|
||||
return E.New("fakeip not enabled")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) Raw() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Server) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
||||
return nil, os.ErrInvalid
|
||||
}
|
||||
|
||||
func (s *Server) Lookup(ctx context.Context, domain string, strategy dns.DomainStrategy) ([]netip.Addr, error) {
|
||||
var addresses []netip.Addr
|
||||
if strategy != dns.DomainStrategyUseIPv6 {
|
||||
inet4Address, err := s.store.Create(domain, dns.DomainStrategyUseIPv4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addresses = append(addresses, inet4Address)
|
||||
}
|
||||
if strategy != dns.DomainStrategyUseIPv4 {
|
||||
inet6Address, err := s.store.Create(domain, dns.DomainStrategyUseIPv6)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
addresses = append(addresses, inet6Address)
|
||||
}
|
||||
return addresses, nil
|
||||
}
|
||||
108
transport/fakeip/store.go
Normal file
108
transport/fakeip/store.go
Normal file
@@ -0,0 +1,108 @@
|
||||
package fakeip
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-dns"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
)
|
||||
|
||||
var _ adapter.FakeIPStore = (*Store)(nil)
|
||||
|
||||
type Store struct {
|
||||
router adapter.Router
|
||||
inet4Range netip.Prefix
|
||||
inet6Range netip.Prefix
|
||||
storage adapter.FakeIPStorage
|
||||
inet4Current netip.Addr
|
||||
inet6Current netip.Addr
|
||||
}
|
||||
|
||||
func NewStore(router adapter.Router, inet4Range netip.Prefix, inet6Range netip.Prefix) *Store {
|
||||
return &Store{
|
||||
router: router,
|
||||
inet4Range: inet4Range,
|
||||
inet6Range: inet6Range,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Store) Start() error {
|
||||
var storage adapter.FakeIPStorage
|
||||
if clashServer := s.router.ClashServer(); clashServer != nil && clashServer.StoreFakeIP() {
|
||||
if cacheFile := clashServer.CacheFile(); cacheFile != nil {
|
||||
storage = cacheFile
|
||||
}
|
||||
}
|
||||
if storage == nil {
|
||||
storage = NewMemoryStorage()
|
||||
}
|
||||
metadata := storage.FakeIPMetadata()
|
||||
if metadata != nil && metadata.Inet4Range == s.inet4Range && metadata.Inet6Range == s.inet6Range {
|
||||
s.inet4Current = metadata.Inet4Current
|
||||
s.inet6Current = metadata.Inet6Current
|
||||
} else {
|
||||
if s.inet4Range.IsValid() {
|
||||
s.inet4Current = s.inet4Range.Addr().Next().Next()
|
||||
}
|
||||
if s.inet6Range.IsValid() {
|
||||
s.inet6Current = s.inet6Range.Addr().Next().Next()
|
||||
}
|
||||
}
|
||||
s.storage = storage
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Contains(address netip.Addr) bool {
|
||||
return s.inet4Range.Contains(address) || s.inet6Range.Contains(address)
|
||||
}
|
||||
|
||||
func (s *Store) Close() error {
|
||||
if s.storage == nil {
|
||||
return nil
|
||||
}
|
||||
return s.storage.FakeIPSaveMetadata(&adapter.FakeIPMetadata{
|
||||
Inet4Range: s.inet4Range,
|
||||
Inet6Range: s.inet6Range,
|
||||
Inet4Current: s.inet4Current,
|
||||
Inet6Current: s.inet6Current,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) Create(domain string, strategy dns.DomainStrategy) (netip.Addr, error) {
|
||||
var address netip.Addr
|
||||
if strategy == dns.DomainStrategyUseIPv4 {
|
||||
if !s.inet4Current.IsValid() {
|
||||
return netip.Addr{}, E.New("missing IPv4 fakeip address range")
|
||||
}
|
||||
nextAddress := s.inet4Current.Next()
|
||||
if !s.inet4Range.Contains(nextAddress) {
|
||||
nextAddress = s.inet4Range.Addr().Next().Next()
|
||||
}
|
||||
s.inet4Current = nextAddress
|
||||
address = nextAddress
|
||||
} else {
|
||||
if !s.inet6Current.IsValid() {
|
||||
return netip.Addr{}, E.New("missing IPv6 fakeip address range")
|
||||
}
|
||||
nextAddress := s.inet6Current.Next()
|
||||
if !s.inet6Range.Contains(nextAddress) {
|
||||
nextAddress = s.inet6Range.Addr().Next().Next()
|
||||
}
|
||||
s.inet6Current = nextAddress
|
||||
address = nextAddress
|
||||
}
|
||||
err := s.storage.FakeIPStore(address, domain)
|
||||
if err != nil {
|
||||
return netip.Addr{}, err
|
||||
}
|
||||
return address, nil
|
||||
}
|
||||
|
||||
func (s *Store) Lookup(address netip.Addr) (string, bool) {
|
||||
return s.storage.FakeIPLoad(address)
|
||||
}
|
||||
|
||||
func (s *Store) Reset() error {
|
||||
return s.storage.FakeIPReset()
|
||||
}
|
||||
Reference in New Issue
Block a user