fix: correct the logic of converting SocksAddr into net.Destination.

This commit is contained in:
cty123 2023-07-19 00:28:51 +02:00 committed by yuhan6665
parent 7aeca33729
commit b68a43f4fc
2 changed files with 13 additions and 2 deletions

View File

@ -18,19 +18,25 @@ func ToNetwork(network string) net.Network {
} }
func ToDestination(socksaddr M.Socksaddr, network net.Network) net.Destination { func ToDestination(socksaddr M.Socksaddr, network net.Network) net.Destination {
// IsFqdn() implicitly checks if the domain name is valid
if socksaddr.IsFqdn() { if socksaddr.IsFqdn() {
return net.Destination{ return net.Destination{
Network: network, Network: network,
Address: net.DomainAddress(socksaddr.Fqdn), Address: net.DomainAddress(socksaddr.Fqdn),
Port: net.Port(socksaddr.Port), Port: net.Port(socksaddr.Port),
} }
} else { }
// IsIP() implicitly checks if the IP address is valid
if socksaddr.IsIP() {
return net.Destination{ return net.Destination{
Network: network, Network: network,
Address: net.IPAddress(socksaddr.Addr.AsSlice()), Address: net.IPAddress(socksaddr.Addr.AsSlice()),
Port: net.Port(socksaddr.Port), Port: net.Port(socksaddr.Port),
} }
} }
return net.Destination{}
} }
func ToSocksaddr(destination net.Destination) M.Socksaddr { func ToSocksaddr(destination net.Destination) M.Socksaddr {

View File

@ -204,7 +204,12 @@ func (i *MultiUserInbound) NewConnection(ctx context.Context, conn net.Conn, met
}) })
newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx)) newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
dispatcher := session.DispatcherFromContext(ctx) dispatcher := session.DispatcherFromContext(ctx)
link, err := dispatcher.Dispatch(ctx, singbridge.ToDestination(metadata.Destination, net.Network_TCP)) destination := singbridge.ToDestination(metadata.Destination, net.Network_TCP)
if !destination.IsValid() {
return newError("invalid destination")
}
link, err := dispatcher.Dispatch(ctx, destination)
if err != nil { if err != nil {
return err return err
} }