From de83d8cb03a7026b2e85602426aa686cd1c653e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=8E=E6=89=87=E6=BB=91=E7=BF=94=E7=BF=BC?= Date: Mon, 24 Feb 2025 00:41:38 +0800 Subject: [PATCH] Update dialer.go --- transport/internet/dialer.go | 92 +++++++++++++++++++++++------------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/transport/internet/dialer.go b/transport/internet/dialer.go index 086ae9a5..dccc2d30 100644 --- a/transport/internet/dialer.go +++ b/transport/internet/dialer.go @@ -143,49 +143,79 @@ func redirect(ctx context.Context, dst net.Destination, obt string) net.Conn { return nil } +// SrvPortOnly = 1; +// SrvAddressOnly = 2; +// SrvPortAndAddress = 3; +// TxtPortOnly = 4; +// TxtAddressOnly = 5; +// TxtPortAndAddress = 6; func checkDestinationStrategy(ctx context.Context, dest net.Destination, sockopt *SocketConfig) (*net.Destination, error) { - if !dest.Address.Family().IsDomain() || sockopt.DestinationStrategy == DestinationStrategy_TxtPortOnly { + if sockopt.DestinationStrategy == DestinationStrategy_None { + return nil, nil + } + newDest := dest + var OverridePort, OverrideAddress bool + var OverrideBy string + switch sockopt.DestinationStrategy { + case DestinationStrategy_SrvPortOnly: + OverridePort = true + OverrideAddress = false + OverrideBy = "srv" + case DestinationStrategy_SrvAddressOnly: + OverridePort = false + OverrideAddress = true + OverrideBy = "srv" + case DestinationStrategy_SrvPortAndAddress: + OverridePort = true + OverrideAddress = true + OverrideBy = "srv" + case DestinationStrategy_TxtPortOnly: + OverridePort = true + OverrideAddress = false + OverrideBy = "txt" + case DestinationStrategy_TxtAddressOnly: + OverridePort = false + OverrideAddress = true + OverrideBy = "txt" + case DestinationStrategy_TxtPortAndAddress: + OverridePort = true + OverrideAddress = true + OverrideBy = "txt" + default: + return nil, errors.New("unknown DestinationStrategy") + } + // sockopt.DestinationStrategy == DestinationStrategy_TxtPortOnly and skip this ???? + // if !dest.Address.Family().IsDomain() || sockopt.DestinationStrategy == DestinationStrategy_TxtPortOnly { + + if !dest.Address.Family().IsDomain() { return nil, nil } - if sockopt.DestinationStrategy == DestinationStrategy_SrvPortOnly || - sockopt.DestinationStrategy == DestinationStrategy_SrvAddressOnly || - sockopt.DestinationStrategy == DestinationStrategy_SrvPortAndAddress { - + if OverrideBy == "srv" { errors.LogDebug(ctx, "query SRV record for "+dest.Address.String()) parts := strings.SplitN(dest.Address.String(), ".", 3) if len(parts) != 3 { - errors.LogError(ctx, "invalid address format: "+dest.Address.String()) - return nil, errors.New("invalid address format") + return nil, errors.New("invalid address format", dest.Address.String()) } _, srvRecords, err := gonet.DefaultResolver.LookupSRV(context.Background(), parts[0][1:], parts[1][1:], parts[2]) if err != nil { - errors.LogError(ctx, "failed to lookup SRV record: "+err.Error()) - return nil, err + return nil, errors.New("failed to lookup SRV record").Base(err) } - for _, srvRecord := range srvRecords { - errors.LogDebug(ctx, "SRV record: "+fmt.Sprintf("addr=%s, port=%d, priority=%d, weight=%d", srvRecord.Target, srvRecord.Port, srvRecord.Priority, srvRecord.Weight)) - newDest := dest - if sockopt.DestinationStrategy == DestinationStrategy_SrvPortOnly { - newDest.Port = net.Port(srvRecord.Port) - } else if sockopt.DestinationStrategy == DestinationStrategy_SrvAddressOnly { - newDest.Address = net.ParseAddress(srvRecord.Target) - } else if sockopt.DestinationStrategy == DestinationStrategy_SrvPortAndAddress { - newDest.Address = net.ParseAddress(srvRecord.Target) - newDest.Port = net.Port(srvRecord.Port) - } - - return &newDest, nil + errors.LogDebug(ctx, "SRV record: "+fmt.Sprintf("addr=%s, port=%d, priority=%d, weight=%d", srvRecords[0].Target, srvRecords[0].Port, srvRecords[0].Priority, srvRecords[0].Weight)) + if OverridePort { + newDest.Port = net.Port(srvRecords[0].Port) } - } else if sockopt.DestinationStrategy == DestinationStrategy_TxtPortOnly || - sockopt.DestinationStrategy == DestinationStrategy_TxtAddressOnly || - sockopt.DestinationStrategy == DestinationStrategy_TxtPortAndAddress { - + if OverrideAddress { + newDest.Address = net.ParseAddress(srvRecords[0].Target) + } + return &newDest, nil + } + if OverrideBy == "txt" { errors.LogDebug(ctx, "query TXT record for "+dest.Address.String()) txtRecords, err := gonet.DefaultResolver.LookupTXT(ctx, dest.Address.String()) if err != nil { errors.LogError(ctx, "failed to lookup SRV record: "+err.Error()) - return nil, err + return nil, errors.New("failed to lookup SRV record").Base(err) } for _, txtRecord := range txtRecords { errors.LogDebug(ctx, "TXT record: "+txtRecord) @@ -197,13 +227,11 @@ func checkDestinationStrategy(ctx context.Context, dest net.Destination, sockopt } newDest := dest - if sockopt.DestinationStrategy == DestinationStrategy_TxtPortOnly { + if OverridePort { newDest.Port = port - } else if sockopt.DestinationStrategy == DestinationStrategy_TxtAddressOnly { + } + if OverrideAddress { newDest.Address = addr - } else if sockopt.DestinationStrategy == DestinationStrategy_TxtPortAndAddress { - newDest.Address = addr - newDest.Port = port } return &newDest, nil }