@@ -63,6 +63,7 @@ import (
6363var (
6464 _ adapter.OutboundWithPreferredRoutes = (* Endpoint )(nil )
6565 _ adapter.DirectRouteOutbound = (* Endpoint )(nil )
66+ _ dialer.PacketDialerWithDestination = (* Endpoint )(nil )
6667)
6768
6869func init () {
@@ -518,19 +519,7 @@ func (t *Endpoint) DialContext(ctx context.Context, network string, destination
518519 }
519520}
520521
521- func (t * Endpoint ) ListenPacket (ctx context.Context , destination M.Socksaddr ) (net.PacketConn , error ) {
522- t .logger .InfoContext (ctx , "outbound packet connection to " , destination )
523- if destination .IsFqdn () {
524- destinationAddresses , err := t .dnsRouter .Lookup (ctx , destination .Fqdn , adapter.DNSQueryOptions {})
525- if err != nil {
526- return nil , err
527- }
528- packetConn , _ , err := N .ListenSerial (ctx , t , destination , destinationAddresses )
529- if err != nil {
530- return nil , err
531- }
532- return packetConn , err
533- }
522+ func (t * Endpoint ) listenPacketWithAddress (ctx context.Context , destination M.Socksaddr ) (net.PacketConn , error ) {
534523 addr4 , addr6 := t .server .TailscaleIPs ()
535524 bind := tcpip.FullAddress {
536525 NIC : 1 ,
@@ -556,6 +545,44 @@ func (t *Endpoint) ListenPacket(ctx context.Context, destination M.Socksaddr) (n
556545 return udpConn , nil
557546}
558547
548+ func (t * Endpoint ) ListenPacketWithDestination (ctx context.Context , destination M.Socksaddr ) (net.PacketConn , netip.Addr , error ) {
549+ t .logger .InfoContext (ctx , "outbound packet connection to " , destination )
550+ if destination .IsFqdn () {
551+ destinationAddresses , err := t .dnsRouter .Lookup (ctx , destination .Fqdn , adapter.DNSQueryOptions {})
552+ if err != nil {
553+ return nil , netip.Addr {}, err
554+ }
555+ var errors []error
556+ for _ , address := range destinationAddresses {
557+ packetConn , packetErr := t .listenPacketWithAddress (ctx , M .SocksaddrFrom (address , destination .Port ))
558+ if packetErr == nil {
559+ return packetConn , address , nil
560+ }
561+ errors = append (errors , packetErr )
562+ }
563+ return nil , netip.Addr {}, E .Errors (errors ... )
564+ }
565+ packetConn , err := t .listenPacketWithAddress (ctx , destination )
566+ if err != nil {
567+ return nil , netip.Addr {}, err
568+ }
569+ if destination .IsIP () {
570+ return packetConn , destination .Addr , nil
571+ }
572+ return packetConn , netip.Addr {}, nil
573+ }
574+
575+ func (t * Endpoint ) ListenPacket (ctx context.Context , destination M.Socksaddr ) (net.PacketConn , error ) {
576+ packetConn , destinationAddress , err := t .ListenPacketWithDestination (ctx , destination )
577+ if err != nil {
578+ return nil , err
579+ }
580+ if destinationAddress .IsValid () && destination != M .SocksaddrFrom (destinationAddress , destination .Port ) {
581+ return bufio .NewNATPacketConn (bufio .NewPacketConn (packetConn ), M .SocksaddrFrom (destinationAddress , destination .Port ), destination ), nil
582+ }
583+ return packetConn , nil
584+ }
585+
559586func (t * Endpoint ) PrepareConnection (network string , source M.Socksaddr , destination M.Socksaddr , routeContext tun.DirectRouteContext , timeout time.Duration ) (tun.DirectRouteDestination , error ) {
560587 tsFilter := t .filter .Load ()
561588 if tsFilter != nil {
0 commit comments