mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-10 11:03:13 +00:00
84 lines
1.8 KiB
Go
84 lines
1.8 KiB
Go
|
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
|
||
|
}
|