1package domain
2
3import (
4 "fmt"
5 "net"
6 "strconv"
7 "strings"
8)
9
10// EntryPointListener is the canonical public bind of one installation
11// entrypoint. An app L4 publish declaration must be exactly compatible
12// with the entrypoint that will actually bind it: the runtime binds the
13// entrypoint address, so a narrower publish (for example loopback) would
14// silently be exposed on the entrypoint listener instead.
15type EntryPointListener struct {
16 // Address is the configured listen address, "host:port" or ":port".
17 Address string
18 // Protocol is the entrypoint transport.
19 Protocol EntryPointProtocol
20}
21
22// CanonicalBindHost normalizes a bind host: an empty host means "all
23// interfaces" and is reported as 0.0.0.0 so an unset host and an explicit
24// wildcard compare equal.
25func CanonicalBindHost(host string) string {
26 host = strings.TrimSpace(host)
27 if host == "" {
28 return "0.0.0.0"
29 }
30 return host
31}
32
33// EntryPointCarriesTransport reports whether an entrypoint protocol can
34// carry the given app interface transport.
35func EntryPointCarriesTransport(protocol EntryPointProtocol, transport NetworkProtocol) bool {
36 switch transport {
37 case NetworkProtocolTCP:
38 switch protocol {
39 case EntryPointProtocolTCP, EntryPointProtocolTLSMux, EntryPointProtocolSmartTCP:
40 return true
41 }
42 return false
43 case NetworkProtocolUDP:
44 return protocol == EntryPointProtocolUDP
45 default:
46 return false
47 }
48}
49
50// ValidatePublishForListener requires a declared publish bind to equal the
51// entrypoint listener address and the declared transport to be supported
52// by the entrypoint protocol. Any mismatch is rejected before persistence
53// or container effects; a caller must never substitute the entrypoint
54// address for a narrower declared bind.
55func ValidatePublishForListener(publish string, transport NetworkProtocol, listener EntryPointListener, entrypointName string) error {
56 host, port, err := ParsePublish(publish)
57 if err != nil {
58 return err
59 }
60 listenerHost, listenerPort, err := SplitListenerAddress(listener.Address)
61 if err != nil {
62 return fmt.Errorf("%w: entrypoint %q listener address %q is invalid: %s", ErrInvalidAppSpec, entrypointName, listener.Address, err)
63 }
64 if port != listenerPort || CanonicalBindHost(host) != CanonicalBindHost(listenerHost) {
65 return fmt.Errorf(
66 "%w: publish %q for entrypoint %q must match the listener bind %q exactly",
67 ErrInvalidAppSpec, publish, entrypointName, listener.Address,
68 )
69 }
70 if !EntryPointCarriesTransport(listener.Protocol, transport) {
71 return fmt.Errorf(
72 "%w: entrypoint %q protocol %q cannot carry %s traffic",
73 ErrInvalidAppSpec, entrypointName, listener.Protocol, transport,
74 )
75 }
76 return nil
77}
78
79// SplitListenerAddress parses a configured listen address into its
80// canonical host and port. An address without a port is invalid: the
81// runtime always binds a concrete port.
82func SplitListenerAddress(address string) (string, int, error) {
83 address = strings.TrimSpace(address)
84 if address == "" {
85 return "", 0, fmt.Errorf("empty listen address")
86 }
87 host, portText, err := net.SplitHostPort(address)
88 if err != nil {
89 return "", 0, err
90 }
91 port, err := strconv.Atoi(portText)
92 if err != nil || port < 1 || port > 65535 {
93 return "", 0, fmt.Errorf("port %q must be 1-65535", portText)
94 }
95 return host, port, nil
96}