diff options
| author | tjpcc <tjp@ctrl-c.club> | 2023-05-03 12:22:27 -0600 | 
|---|---|---|
| committer | tjpcc <tjp@ctrl-c.club> | 2023-05-03 12:22:27 -0600 | 
| commit | def2baade5a4af58e3444cf6c923b1e4de584329 (patch) | |
| tree | a07811cc1aa87be328b5a118fe024f9999ddfb1e /internal | |
| parent | c7f8a1292544c8981eb1c35394d5b50100fe1790 (diff) | |
add JoinDefaultPort for servers' configuration
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/server.go | 17 | 
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) +} | 
