mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-27 02:45:41 +08:00
95 lines
1.9 KiB
Go
95 lines
1.9 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"
|
|
|
|
mDNS "github.com/miekg/dns"
|
|
)
|
|
|
|
var (
|
|
_ dns.Transport = (*Transport)(nil)
|
|
_ adapter.FakeIPTransport = (*Transport)(nil)
|
|
)
|
|
|
|
func init() {
|
|
dns.RegisterTransport([]string{"fakeip"}, func(options dns.TransportOptions) (dns.Transport, error) {
|
|
return NewTransport(options)
|
|
})
|
|
}
|
|
|
|
type Transport struct {
|
|
name string
|
|
router adapter.Router
|
|
store adapter.FakeIPStore
|
|
logger logger.ContextLogger
|
|
}
|
|
|
|
func NewTransport(options dns.TransportOptions) (*Transport, error) {
|
|
router := adapter.RouterFromContext(options.Context)
|
|
if router == nil {
|
|
return nil, E.New("missing router in context")
|
|
}
|
|
return &Transport{
|
|
name: options.Name,
|
|
router: router,
|
|
logger: options.Logger,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Transport) Name() string {
|
|
return s.name
|
|
}
|
|
|
|
func (s *Transport) Start() error {
|
|
s.store = s.router.FakeIPStore()
|
|
if s.store == nil {
|
|
return E.New("fakeip not enabled")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Transport) Reset() {
|
|
}
|
|
|
|
func (s *Transport) Close() error {
|
|
return nil
|
|
}
|
|
|
|
func (s *Transport) Raw() bool {
|
|
return false
|
|
}
|
|
|
|
func (s *Transport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
|
return nil, os.ErrInvalid
|
|
}
|
|
|
|
func (s *Transport) 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, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
addresses = append(addresses, inet4Address)
|
|
}
|
|
if strategy != dns.DomainStrategyUseIPv4 {
|
|
inet6Address, err := s.store.Create(domain, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
addresses = append(addresses, inet6Address)
|
|
}
|
|
return addresses, nil
|
|
}
|
|
|
|
func (s *Transport) Store() adapter.FakeIPStore {
|
|
return s.store
|
|
}
|