Multiplex http and ssh ports

This commit is contained in:
Tom Wiesing 2023-03-08 11:27:19 +01:00
parent 668f1dd193
commit f0073a649f
No known key found for this signature in database
20 changed files with 188 additions and 29 deletions

33
internal/config/ports.go Normal file
View file

@ -0,0 +1,33 @@
package config
import (
"fmt"
"golang.org/x/exp/slices"
)
type ListenConfig struct {
// Ports are the public addresses to bind to.
// Each address is automatically multiplexed to serve http, https and ssh traffic.
// This should typically be port 80 and port 443.
Ports []uint16 `yaml:"ports" default:"80" validate:"ports"`
// AdvertisedSSHPort is the port that shows up as the ssh port in various places in the interface.
// It is automaticalled added to the ports to listen to.
AdvertisedSSHPort uint16 `yaml:"advertise_ssh" default:"80" validate:"port"`
}
// ComposePorts returns a list of ports to be used within a docker-compose.yml file.
// These can be used to forward all ports to the internal port.
func (lc ListenConfig) ComposePorts(internal string) []string {
// sort and uniquify ports
ports := append([]uint16{lc.AdvertisedSSHPort}, lc.Ports...)
slices.Sort(ports)
ports = slices.Compact(ports)
forwards := make([]string, len(ports))
for i, port := range ports {
forwards[i] = fmt.Sprintf("%d:%s", port, internal)
}
return forwards
}