From 42950bf8c91e52f49ebbbba7faf67dd2ac4be178 Mon Sep 17 00:00:00 2001 From: Konrad Zemek Date: Sat, 23 Mar 2024 18:20:14 +0100 Subject: [PATCH 1/2] Add a test for listening on a hostname --- .github/workflows/test-startup.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test-startup.yml b/.github/workflows/test-startup.yml index bb9ff91..a386b2b 100644 --- a/.github/workflows/test-startup.yml +++ b/.github/workflows/test-startup.yml @@ -29,7 +29,18 @@ jobs: sudo systemctl daemon-reload - name: Start go-mmproxy - run: sudo systemctl start go-mmproxy || sudo systemctl status go-mmproxy.service + run: sudo systemctl start go-mmproxy || sudo journalctl -xeu go-mmproxy.service + + - name: Check go-mmproxy status + run: systemctl is-active go-mmproxy + + - name: Replace listen address of go-mmproxy with a hostname + run: | + sudo sed -i 's/-l 0.0.0.0:1234/-l %H:1234/' /etc/systemd/system/go-mmproxy.service + sudo systemctl daemon-reload + + - name: Restart go-mmproxy + run: sudo systemctl restart go-mmproxy || sudo journalctl -xeu go-mmproxy.service - name: Check go-mmproxy status run: systemctl is-active go-mmproxy From 81a559351f4b254e671a0e4cf3aa667f130a5168 Mon Sep 17 00:00:00 2001 From: Konrad Zemek Date: Sat, 23 Mar 2024 18:51:16 +0100 Subject: [PATCH 2/2] Allow specifying hostname addresses in options. --- main.go | 2 +- utils.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index aa67bc5..c978aac 100644 --- a/main.go +++ b/main.go @@ -130,7 +130,7 @@ 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) } diff --git a/utils.go b/utils.go index 432c152..00b806d 100644 --- a/utils.go +++ b/utils.go @@ -7,6 +7,8 @@ package main import ( "fmt" "net" + "net/netip" + "strconv" "syscall" ) @@ -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