Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying the targets as hostnames. #6

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {

func listen(ctx context.Context, listenerNum int, parentLogger *slog.Logger, listenErrors chan<- error) {
logger := parentLogger.With(slog.Int("listenerNum", listenerNum),
slog.String("protocol", protocolStr), slog.String("listenAdr", opts.ListenAddr.String()))
slog.String("protocol", protocolStr), slog.String("listenAddr", opts.ListenAddr.String()))

listenConfig := net.ListenConfig{}
if listeners > 1 {
Expand Down Expand Up @@ -132,12 +132,12 @@ func main() {
}

var err error
if opts.ListenAddr, err = utils.ParseHostPort(listenAddrStr); err != nil {
if opts.ListenAddr, err = utils.ParseHostPort(listenAddrStr, 0); err != nil {
logger.Error("listen address is malformed", "error", err)
os.Exit(1)
}

if opts.TargetAddr4, err = netip.ParseAddrPort(targetAddr4Str); err != nil {
if opts.TargetAddr4, err = utils.ParseHostPort(targetAddr4Str, 4); err != nil {
logger.Error("ipv4 target address is malformed", "error", err)
os.Exit(1)
}
Expand All @@ -146,7 +146,7 @@ func main() {
os.Exit(1)
}

if opts.TargetAddr6, err = netip.ParseAddrPort(targetAddr6Str); err != nil {
if opts.TargetAddr6, err = utils.ParseHostPort(targetAddr6Str, 6); err != nil {
logger.Error("ipv6 target address is malformed", "error", err)
os.Exit(1)
}
Expand Down
16 changes: 12 additions & 4 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func CheckOriginAllowed(remoteIP netip.Addr, allowedSubnets []netip.Prefix) bool
return false
}

func ParseHostPort(hostport string) (netip.AddrPort, error) {
func ParseHostPort(hostport string, ipVersion int) (netip.AddrPort, error) {
host, portStr, err := net.SplitHostPort(hostport)
if err != nil {
return netip.AddrPort{}, fmt.Errorf("failed to parse host and port: %w", err)
Expand All @@ -56,7 +56,16 @@ func ParseHostPort(hostport string) (netip.AddrPort, error) {
if err != nil {
return netip.AddrPort{}, fmt.Errorf("failed to lookup IP addresses: %w", err)
}
if len(ips) == 0 {

filteredIPs := make([]netip.Addr, 0, len(ips))
for _, stdip := range ips {
ip := netip.MustParseAddr(stdip.String())
if ipVersion == 0 || (ip.Is4() && ipVersion == 4) || (ip.Is6() && ipVersion == 6) {
filteredIPs = append(filteredIPs, ip)
}
}

if len(filteredIPs) == 0 {
return netip.AddrPort{}, fmt.Errorf("no IP addresses found")
}

Expand All @@ -65,8 +74,7 @@ func ParseHostPort(hostport string) (netip.AddrPort, error) {
return netip.AddrPort{}, fmt.Errorf("failed to parse port: %w", err)
}

ip, _ := netip.AddrFromSlice(ips[0])
return netip.AddrPortFrom(ip, uint16(port)), nil
return netip.AddrPortFrom(filteredIPs[0], uint16(port)), nil
}

func DialUpstreamControl(sport uint16, protocol Protocol, mark int) func(string, string, syscall.RawConn) error {
Expand Down
Loading