| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 | 
							- package wireguard
 
- import (
 
- 	"context"
 
- 	"net"
 
- 	"net/netip"
 
- 	"time"
 
- 	"github.com/sagernet/sing-box/adapter"
 
- 	"github.com/sagernet/sing-box/adapter/endpoint"
 
- 	"github.com/sagernet/sing-box/common/dialer"
 
- 	C "github.com/sagernet/sing-box/constant"
 
- 	"github.com/sagernet/sing-box/log"
 
- 	"github.com/sagernet/sing-box/option"
 
- 	"github.com/sagernet/sing-box/route/rule"
 
- 	"github.com/sagernet/sing-box/transport/wireguard"
 
- 	"github.com/sagernet/sing-tun"
 
- 	"github.com/sagernet/sing/common"
 
- 	"github.com/sagernet/sing/common/bufio"
 
- 	E "github.com/sagernet/sing/common/exceptions"
 
- 	"github.com/sagernet/sing/common/logger"
 
- 	M "github.com/sagernet/sing/common/metadata"
 
- 	N "github.com/sagernet/sing/common/network"
 
- 	"github.com/sagernet/sing/service"
 
- )
 
- var _ adapter.OutboundWithPreferredRoutes = (*Endpoint)(nil)
 
- func RegisterEndpoint(registry *endpoint.Registry) {
 
- 	endpoint.Register[option.WireGuardEndpointOptions](registry, C.TypeWireGuard, NewEndpoint)
 
- }
 
- type Endpoint struct {
 
- 	endpoint.Adapter
 
- 	ctx            context.Context
 
- 	router         adapter.Router
 
- 	dnsRouter      adapter.DNSRouter
 
- 	logger         logger.ContextLogger
 
- 	localAddresses []netip.Prefix
 
- 	endpoint       *wireguard.Endpoint
 
- }
 
- func NewEndpoint(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.WireGuardEndpointOptions) (adapter.Endpoint, error) {
 
- 	ep := &Endpoint{
 
- 		Adapter:        endpoint.NewAdapterWithDialerOptions(C.TypeWireGuard, tag, []string{N.NetworkTCP, N.NetworkUDP, N.NetworkICMP}, options.DialerOptions),
 
- 		ctx:            ctx,
 
- 		router:         router,
 
- 		dnsRouter:      service.FromContext[adapter.DNSRouter](ctx),
 
- 		logger:         logger,
 
- 		localAddresses: options.Address,
 
- 	}
 
- 	if options.Detour != "" && options.ListenPort != 0 {
 
- 		return nil, E.New("`listen_port` is conflict with `detour`")
 
- 	}
 
- 	outboundDialer, err := dialer.NewWithOptions(dialer.Options{
 
- 		Context: ctx,
 
- 		Options: options.DialerOptions,
 
- 		RemoteIsDomain: common.Any(options.Peers, func(it option.WireGuardPeer) bool {
 
- 			return !M.ParseAddr(it.Address).IsValid()
 
- 		}),
 
- 		ResolverOnDetour: true,
 
- 	})
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	var udpTimeout time.Duration
 
- 	if options.UDPTimeout != 0 {
 
- 		udpTimeout = time.Duration(options.UDPTimeout)
 
- 	} else {
 
- 		udpTimeout = C.UDPTimeout
 
- 	}
 
- 	wgEndpoint, err := wireguard.NewEndpoint(wireguard.EndpointOptions{
 
- 		Context:    ctx,
 
- 		Logger:     logger,
 
- 		System:     options.System,
 
- 		Handler:    ep,
 
- 		UDPTimeout: udpTimeout,
 
- 		Dialer:     outboundDialer,
 
- 		CreateDialer: func(interfaceName string) N.Dialer {
 
- 			return common.Must1(dialer.NewDefault(ctx, option.DialerOptions{
 
- 				BindInterface: interfaceName,
 
- 			}))
 
- 		},
 
- 		Name:       options.Name,
 
- 		MTU:        options.MTU,
 
- 		Address:    options.Address,
 
- 		PrivateKey: options.PrivateKey,
 
- 		ListenPort: options.ListenPort,
 
- 		ResolvePeer: func(domain string) (netip.Addr, error) {
 
- 			endpointAddresses, lookupErr := ep.dnsRouter.Lookup(ctx, domain, outboundDialer.(dialer.ResolveDialer).QueryOptions())
 
- 			if lookupErr != nil {
 
- 				return netip.Addr{}, lookupErr
 
- 			}
 
- 			return endpointAddresses[0], nil
 
- 		},
 
- 		Peers: common.Map(options.Peers, func(it option.WireGuardPeer) wireguard.PeerOptions {
 
- 			return wireguard.PeerOptions{
 
- 				Endpoint:                    M.ParseSocksaddrHostPort(it.Address, it.Port),
 
- 				PublicKey:                   it.PublicKey,
 
- 				PreSharedKey:                it.PreSharedKey,
 
- 				AllowedIPs:                  it.AllowedIPs,
 
- 				PersistentKeepaliveInterval: it.PersistentKeepaliveInterval,
 
- 				Reserved:                    it.Reserved,
 
- 			}
 
- 		}),
 
- 		Workers: options.Workers,
 
- 	})
 
- 	if err != nil {
 
- 		return nil, err
 
- 	}
 
- 	ep.endpoint = wgEndpoint
 
- 	return ep, nil
 
- }
 
- func (w *Endpoint) Start(stage adapter.StartStage) error {
 
- 	switch stage {
 
- 	case adapter.StartStateStart:
 
- 		return w.endpoint.Start(false)
 
- 	case adapter.StartStatePostStart:
 
- 		return w.endpoint.Start(true)
 
- 	}
 
- 	return nil
 
- }
 
- func (w *Endpoint) Close() error {
 
- 	return w.endpoint.Close()
 
- }
 
- func (w *Endpoint) PrepareConnection(network string, source M.Socksaddr, destination M.Socksaddr, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error) {
 
- 	var ipVersion uint8
 
- 	if !destination.IsIPv6() {
 
- 		ipVersion = 4
 
- 	} else {
 
- 		ipVersion = 6
 
- 	}
 
- 	routeDestination, err := w.router.PreMatch(adapter.InboundContext{
 
- 		Inbound:     w.Tag(),
 
- 		InboundType: w.Type(),
 
- 		IPVersion:   ipVersion,
 
- 		Network:     network,
 
- 		Source:      source,
 
- 		Destination: destination,
 
- 	}, routeContext, timeout)
 
- 	if err != nil {
 
- 		if !rule.IsRejected(err) {
 
- 			w.logger.Warn(E.Cause(err, "link ", network, " connection from ", source.AddrString(), " to ", destination.AddrString()))
 
- 		}
 
- 	}
 
- 	return routeDestination, err
 
- }
 
- func (w *Endpoint) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
 
- 	var metadata adapter.InboundContext
 
- 	metadata.Inbound = w.Tag()
 
- 	metadata.InboundType = w.Type()
 
- 	metadata.Source = source
 
- 	for _, localPrefix := range w.localAddresses {
 
- 		if localPrefix.Contains(destination.Addr) {
 
- 			metadata.OriginDestination = destination
 
- 			if destination.Addr.Is4() {
 
- 				destination.Addr = netip.AddrFrom4([4]uint8{127, 0, 0, 1})
 
- 			} else {
 
- 				destination.Addr = netip.IPv6Loopback()
 
- 			}
 
- 			break
 
- 		}
 
- 	}
 
- 	metadata.Destination = destination
 
- 	w.logger.InfoContext(ctx, "inbound connection from ", source)
 
- 	w.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
 
- 	w.router.RouteConnectionEx(ctx, conn, metadata, onClose)
 
- }
 
- func (w *Endpoint) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
 
- 	var metadata adapter.InboundContext
 
- 	metadata.Inbound = w.Tag()
 
- 	metadata.InboundType = w.Type()
 
- 	metadata.Source = source
 
- 	metadata.Destination = destination
 
- 	for _, localPrefix := range w.localAddresses {
 
- 		if localPrefix.Contains(destination.Addr) {
 
- 			metadata.OriginDestination = destination
 
- 			if destination.Addr.Is4() {
 
- 				metadata.Destination.Addr = netip.AddrFrom4([4]uint8{127, 0, 0, 1})
 
- 			} else {
 
- 				metadata.Destination.Addr = netip.IPv6Loopback()
 
- 			}
 
- 			conn = bufio.NewNATPacketConn(bufio.NewNetPacketConn(conn), metadata.OriginDestination, metadata.Destination)
 
- 		}
 
- 	}
 
- 	w.logger.InfoContext(ctx, "inbound packet connection from ", source)
 
- 	w.logger.InfoContext(ctx, "inbound packet connection to ", destination)
 
- 	w.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
 
- }
 
- func (w *Endpoint) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
 
- 	switch network {
 
- 	case N.NetworkTCP:
 
- 		w.logger.InfoContext(ctx, "outbound connection to ", destination)
 
- 	case N.NetworkUDP:
 
- 		w.logger.InfoContext(ctx, "outbound packet connection to ", destination)
 
- 	}
 
- 	if destination.IsFqdn() {
 
- 		destinationAddresses, err := w.dnsRouter.Lookup(ctx, destination.Fqdn, adapter.DNSQueryOptions{})
 
- 		if err != nil {
 
- 			return nil, err
 
- 		}
 
- 		return N.DialSerial(ctx, w.endpoint, network, destination, destinationAddresses)
 
- 	} else if !destination.Addr.IsValid() {
 
- 		return nil, E.New("invalid destination: ", destination)
 
- 	}
 
- 	return w.endpoint.DialContext(ctx, network, destination)
 
- }
 
- func (w *Endpoint) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
 
- 	w.logger.InfoContext(ctx, "outbound packet connection to ", destination)
 
- 	if destination.IsFqdn() {
 
- 		destinationAddresses, err := w.dnsRouter.Lookup(ctx, destination.Fqdn, adapter.DNSQueryOptions{})
 
- 		if err != nil {
 
- 			return nil, err
 
- 		}
 
- 		packetConn, _, err := N.ListenSerial(ctx, w.endpoint, destination, destinationAddresses)
 
- 		if err != nil {
 
- 			return nil, err
 
- 		}
 
- 		return packetConn, err
 
- 	}
 
- 	return w.endpoint.ListenPacket(ctx, destination)
 
- }
 
- func (w *Endpoint) PreferredDomain(domain string) bool {
 
- 	return false
 
- }
 
- func (w *Endpoint) PreferredAddress(address netip.Addr) bool {
 
- 	return w.endpoint.Lookup(address) != nil
 
- }
 
- func (w *Endpoint) NewDirectRouteConnection(metadata adapter.InboundContext, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error) {
 
- 	return w.endpoint.NewDirectRouteConnection(metadata, routeContext, timeout)
 
- }
 
 
  |