From 095ad06baac4bbc07131923ce8773f21a4afbbf7 Mon Sep 17 00:00:00 2001 From: hosswald <32925139+hosswald@users.noreply.github.com> Date: Wed, 7 Sep 2022 15:20:03 +0200 Subject: [PATCH] add a sanitized version of the hostname (replace any character matching [^a-zA-Z0-9\-.] with underscore) fixes txn2/kubefwd#240 --- pkg/fwdport/fwdport.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/fwdport/fwdport.go b/pkg/fwdport/fwdport.go index a0837c2b..b31f5023 100644 --- a/pkg/fwdport/fwdport.go +++ b/pkg/fwdport/fwdport.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "net/http" + "regexp" "strconv" "sync" "time" @@ -248,6 +249,13 @@ func (pfo *PortForwardOpts) addHost(host string) { // add host to /etc/hosts pfo.HostFile.Hosts.AddHost(pfo.LocalIp.String(), host) + + // replace illegal characters with underscore and add another entry if this changes the hostname + hostnameIllegalChars := regexp.MustCompile(`[^a-zA-Z0-9\-.]`) + sanitizedHost := hostnameIllegalChars.ReplaceAllString(host, `_`) + if host != sanitizedHost { + pfo.addHost(sanitizedHost) //should recurse only once + } } // AddHosts adds hostname entries to /etc/hosts