summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-05-03 12:22:27 -0600
committertjpcc <tjp@ctrl-c.club>2023-05-03 12:22:27 -0600
commitdef2baade5a4af58e3444cf6c923b1e4de584329 (patch)
treea07811cc1aa87be328b5a118fe024f9999ddfb1e /internal
parentc7f8a1292544c8981eb1c35394d5b50100fe1790 (diff)
add JoinDefaultPort for servers' configuration
Diffstat (limited to 'internal')
-rw-r--r--internal/server.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/internal/server.go b/internal/server.go
index 3efdf6e..e3fbfb2 100644
--- a/internal/server.go
+++ b/internal/server.go
@@ -3,6 +3,7 @@ package internal
import (
"context"
"net"
+ "strings"
"sync"
)
@@ -126,3 +127,19 @@ func (s *Server) propagateClose() {
_ = s.Listener.Close()
}()
}
+
+// JoinDefaultPort appends ":<port>" iff the address does not already contain a port.
+func JoinDefaultPort(address string, port string) string {
+ if address[0] == '[' {
+ hend := strings.LastIndexByte(address, ']')
+ if len(address) > hend+1 && address[hend+1] == ':' {
+ return address
+ }
+ return net.JoinHostPort(address[1:hend], port)
+ }
+
+ if strings.Contains(address, ":") {
+ return address
+ }
+ return net.JoinHostPort(address, port)
+}