Skip to content

Commit

Permalink
Allow specifying hostname addresses in options.
Browse files Browse the repository at this point in the history
  • Loading branch information
kzemek committed Mar 23, 2024
1 parent 42950bf commit bf2bd5a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ func main() {
}

var err error
if Opts.ListenAddr, err = netip.ParseAddrPort(Opts.ListenAddrStr); err != nil {
if Opts.ListenAddr, err = ParseHostPort(Opts.ListenAddrStr); err != nil {
Opts.Logger.Error("listen address is malformed", "error", err)
os.Exit(1)
}

if Opts.TargetAddr4, err = netip.ParseAddrPort(Opts.TargetAddr4Str); err != nil {
if Opts.TargetAddr4, err = ParseHostPort(Opts.TargetAddr4Str); err != nil {
Opts.Logger.Error("ipv4 target address is malformed", "error", err)
os.Exit(1)
}
Expand All @@ -144,7 +144,7 @@ func main() {
os.Exit(1)
}

if Opts.TargetAddr6, err = netip.ParseAddrPort(Opts.TargetAddr6Str); err != nil {
if Opts.TargetAddr6, err = ParseHostPort(Opts.TargetAddr6Str); err != nil {
Opts.Logger.Error("ipv6 target address is malformed", "error", err)
os.Exit(1)
}
Expand Down
25 changes: 25 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package main
import (
"fmt"
"net"
"net/netip"
"strconv"
"syscall"
)

Expand All @@ -30,6 +32,29 @@ func CheckOriginAllowed(remoteIP net.IP) bool {
return false
}

func ParseHostPort(hostport string) (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)
}

ips, err := net.LookupIP(host)
if err != nil {
return netip.AddrPort{}, fmt.Errorf("failed to lookup IP addresses: %w", err)
}
if len(ips) == 0 {
return netip.AddrPort{}, fmt.Errorf("no IP addresses found")
}

port, err := strconv.ParseUint(portStr, 10, 16)
if err != nil {
return netip.AddrPort{}, fmt.Errorf("failed to parse port: %w", err)
}

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

func DialUpstreamControl(sport int) func(string, string, syscall.RawConn) error {
return func(network, address string, c syscall.RawConn) error {
var syscallErr error
Expand Down

0 comments on commit bf2bd5a

Please sign in to comment.