diff --git a/listeners.go b/listeners.go index b673c86e109..6623a57d1d5 100644 --- a/listeners.go +++ b/listeners.go @@ -38,10 +38,6 @@ import ( "github.com/caddyserver/caddy/v2/internal" ) -// listenFdsStart is the first file descriptor number for systemd socket activation. -// File descriptors 0, 1, 2 are reserved for stdin, stdout, stderr. -const listenFdsStart = 3 - // NetworkAddress represents one or more network addresses. // It contains the individual components for a parsed network // address of the form accepted by ParseNetworkAddress(). @@ -137,31 +133,29 @@ func (na NetworkAddress) ListenAll(ctx context.Context, config net.ListenConfig) // Listen synchronizes binds to unix domain sockets to avoid race conditions // while an existing socket is unlinked. func (na NetworkAddress) Listen(ctx context.Context, portOffset uint, config net.ListenConfig) (any, error) { - if na.IsUnixNetwork() { - unixSocketsMu.Lock() - defer unixSocketsMu.Unlock() - } + var ( + ln any + err error + ) - // check to see if plugin provides listener - if ln, err := getListenerFromPlugin(ctx, na.Network, na.Host, na.port(), portOffset, config); ln != nil || err != nil { + // check to see if network provides a listener + if ln, err = getListenerFromNetwork(ctx, na.Network, na.Host, na.port(), portOffset, config); ln != nil || err != nil { return ln, err } // create (or reuse) the listener ourselves - return na.listen(ctx, portOffset, config) -} - -func (na NetworkAddress) listen(ctx context.Context, portOffset uint, config net.ListenConfig) (any, error) { var ( - ln any - err error address string unixFileMode fs.FileMode ) + // lock other unix sockets from being bound and // split unix socket addr early so lnKey // is independent of permissions bits if na.IsUnixNetwork() { + unixSocketsMu.Lock() + defer unixSocketsMu.Unlock() + address, unixFileMode, err = internal.SplitUnixSocketPermissionsBits(na.Host) if err != nil { return nil, err @@ -172,7 +166,7 @@ func (na NetworkAddress) listen(ctx context.Context, portOffset uint, config net address = na.JoinHostPort(portOffset) } - if strings.HasPrefix(na.Network, "ip") { + if na.IsIpNetwork() { ln, err = config.ListenPacket(ctx, na.Network, address) } else { if na.IsUnixNetwork() { @@ -220,6 +214,12 @@ func (na NetworkAddress) IsFdNetwork() bool { return IsFdNetwork(na.Network) } +// IsIpNetwork returns true if na.Network starts with +// ip: ip4: or ip6: +func (na NetworkAddress) IsIpNetwork() bool { + return IsIpNetwork(na.Network) +} + // JoinHostPort is like net.JoinHostPort, but where the port // is StartPort + offset. func (na NetworkAddress) JoinHostPort(offset uint) string { @@ -299,74 +299,6 @@ func (na NetworkAddress) String() string { return JoinNetworkAddress(na.Network, na.Host, na.port()) } -// IsUnixNetwork returns true if the netw is a unix network. -func IsUnixNetwork(netw string) bool { - return strings.HasPrefix(netw, "unix") -} - -// IsFdNetwork returns true if the netw is a fd network. -func IsFdNetwork(netw string) bool { - return strings.HasPrefix(netw, "fd") -} - -// getFdByName returns the file descriptor number for the given -// socket name from systemd's LISTEN_FDNAMES environment variable. -// Socket names are provided by systemd via socket activation. -// -// The name can optionally include an index to handle multiple sockets -// with the same name: "web:0" for first, "web:1" for second, etc. -// If no index is specified, defaults to index 0 (first occurrence). -func getFdByName(nameWithIndex string) (int, error) { - if nameWithIndex == "" { - return 0, fmt.Errorf("socket name cannot be empty") - } - - fdNamesStr := os.Getenv("LISTEN_FDNAMES") - if fdNamesStr == "" { - return 0, fmt.Errorf("LISTEN_FDNAMES environment variable not set") - } - - // Parse name and optional index - parts := strings.Split(nameWithIndex, ":") - if len(parts) > 2 { - return 0, fmt.Errorf("invalid socket name format '%s': too many colons", nameWithIndex) - } - - name := parts[0] - targetIndex := 0 - - if len(parts) > 1 { - var err error - targetIndex, err = strconv.Atoi(parts[1]) - if err != nil { - return 0, fmt.Errorf("invalid socket index '%s': %v", parts[1], err) - } - if targetIndex < 0 { - return 0, fmt.Errorf("socket index cannot be negative: %d", targetIndex) - } - } - - // Parse the socket names - names := strings.Split(fdNamesStr, ":") - - // Find the Nth occurrence of the requested name - matchCount := 0 - for i, fdName := range names { - if fdName == name { - if matchCount == targetIndex { - return listenFdsStart + i, nil - } - matchCount++ - } - } - - if matchCount == 0 { - return 0, fmt.Errorf("socket name '%s' not found in LISTEN_FDNAMES", name) - } - - return 0, fmt.Errorf("socket name '%s' found %d times, but index %d requested", name, matchCount, targetIndex) -} - // ParseNetworkAddress parses addr into its individual // components. The input string is expected to be of // the form "network/host:port-range" where any part is @@ -398,27 +330,9 @@ func ParseNetworkAddressWithDefaults(addr, defaultNetwork string, defaultPort ui }, err } if IsFdNetwork(network) { - fdAddr := host - - // Handle named socket activation (fdname/name, fdgramname/name) - if strings.HasPrefix(network, "fdname") || strings.HasPrefix(network, "fdgramname") { - fdNum, err := getFdByName(host) - if err != nil { - return NetworkAddress{}, fmt.Errorf("named socket activation: %v", err) - } - fdAddr = strconv.Itoa(fdNum) - - // Normalize network to standard fd/fdgram - if strings.HasPrefix(network, "fdname") { - network = "fd" - } else { - network = "fdgram" - } - } - return NetworkAddress{ Network: network, - Host: fdAddr, + Host: host, }, nil } var start, end uint64 @@ -713,55 +627,12 @@ func (fcql *fakeCloseQuicListener) Close() error { return nil } -// RegisterNetwork registers a network type with Caddy so that if a listener is -// created for that network type, getListener will be invoked to get the listener. -// This should be called during init() and will panic if the network type is standard -// or reserved, or if it is already registered. EXPERIMENTAL and subject to change. -func RegisterNetwork(network string, getListener ListenerFunc) { - network = strings.TrimSpace(strings.ToLower(network)) - - if network == "tcp" || network == "tcp4" || network == "tcp6" || - network == "udp" || network == "udp4" || network == "udp6" || - network == "unix" || network == "unixpacket" || network == "unixgram" || - strings.HasPrefix(network, "ip:") || strings.HasPrefix(network, "ip4:") || strings.HasPrefix(network, "ip6:") || - network == "fd" || network == "fdgram" { - panic("network type " + network + " is reserved") - } - - if _, ok := networkTypes[strings.ToLower(network)]; ok { - panic("network type " + network + " is already registered") - } - - networkTypes[network] = getListener -} - var unixSocketsMu sync.Mutex -// getListenerFromPlugin returns a listener on the given network and address -// if a plugin has registered the network name. It may return (nil, nil) if -// no plugin can provide a listener. -func getListenerFromPlugin(ctx context.Context, network, host, port string, portOffset uint, config net.ListenConfig) (any, error) { - // get listener from plugin if network type is registered - if getListener, ok := networkTypes[network]; ok { - Log().Debug("getting listener from plugin", zap.String("network", network)) - return getListener(ctx, network, host, port, portOffset, config) - } - - return nil, nil -} - func listenerKey(network, addr string) string { return network + "/" + addr } -// ListenerFunc is a function that can return a listener given a network and address. -// The listeners must be capable of overlapping: with Caddy, new configs are loaded -// before old ones are unloaded, so listeners may overlap briefly if the configs -// both need the same listener. EXPERIMENTAL and subject to change. -type ListenerFunc func(ctx context.Context, network, host, portRange string, portOffset uint, cfg net.ListenConfig) (any, error) - -var networkTypes = map[string]ListenerFunc{} - // ListenerWrapper is a type that wraps a listener // so it can modify the input listener's methods. // Modules that implement this interface are found diff --git a/listeners_test.go b/listeners_test.go index c2cc255f21f..a4cadd3aab1 100644 --- a/listeners_test.go +++ b/listeners_test.go @@ -15,7 +15,6 @@ package caddy import ( - "os" "reflect" "testing" @@ -653,286 +652,3 @@ func TestSplitUnixSocketPermissionsBits(t *testing.T) { } } } - -// TestGetFdByName tests the getFdByName function for systemd socket activation. -func TestGetFdByName(t *testing.T) { - // Save original environment - originalFdNames := os.Getenv("LISTEN_FDNAMES") - - // Restore environment after test - defer func() { - if originalFdNames != "" { - os.Setenv("LISTEN_FDNAMES", originalFdNames) - } else { - os.Unsetenv("LISTEN_FDNAMES") - } - }() - - tests := []struct { - name string - fdNames string - socketName string - expectedFd int - expectError bool - }{ - { - name: "simple http socket", - fdNames: "http", - socketName: "http", - expectedFd: 3, - }, - { - name: "multiple different sockets - first", - fdNames: "http:https:dns", - socketName: "http", - expectedFd: 3, - }, - { - name: "multiple different sockets - second", - fdNames: "http:https:dns", - socketName: "https", - expectedFd: 4, - }, - { - name: "multiple different sockets - third", - fdNames: "http:https:dns", - socketName: "dns", - expectedFd: 5, - }, - { - name: "duplicate names - first occurrence (no index)", - fdNames: "web:web:api", - socketName: "web", - expectedFd: 3, - }, - { - name: "duplicate names - first occurrence (explicit index 0)", - fdNames: "web:web:api", - socketName: "web:0", - expectedFd: 3, - }, - { - name: "duplicate names - second occurrence (index 1)", - fdNames: "web:web:api", - socketName: "web:1", - expectedFd: 4, - }, - { - name: "complex duplicates - first api", - fdNames: "web:api:web:api:dns", - socketName: "api:0", - expectedFd: 4, - }, - { - name: "complex duplicates - second api", - fdNames: "web:api:web:api:dns", - socketName: "api:1", - expectedFd: 6, - }, - { - name: "complex duplicates - first web", - fdNames: "web:api:web:api:dns", - socketName: "web:0", - expectedFd: 3, - }, - { - name: "complex duplicates - second web", - fdNames: "web:api:web:api:dns", - socketName: "web:1", - expectedFd: 5, - }, - { - name: "socket not found", - fdNames: "http:https", - socketName: "missing", - expectError: true, - }, - { - name: "empty socket name", - fdNames: "http", - socketName: "", - expectError: true, - }, - { - name: "missing LISTEN_FDNAMES", - fdNames: "", - socketName: "http", - expectError: true, - }, - { - name: "index out of range", - fdNames: "web:web", - socketName: "web:2", - expectError: true, - }, - { - name: "negative index", - fdNames: "web", - socketName: "web:-1", - expectError: true, - }, - { - name: "invalid index format", - fdNames: "web", - socketName: "web:abc", - expectError: true, - }, - { - name: "too many colons", - fdNames: "web", - socketName: "web:0:extra", - expectError: true, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - // Set up environment - if tc.fdNames != "" { - os.Setenv("LISTEN_FDNAMES", tc.fdNames) - } else { - os.Unsetenv("LISTEN_FDNAMES") - } - - // Test the function - fd, err := getFdByName(tc.socketName) - - if tc.expectError { - if err == nil { - t.Errorf("Expected error but got none") - } - } else { - if err != nil { - t.Errorf("Expected no error but got: %v", err) - } - if fd != tc.expectedFd { - t.Errorf("Expected FD %d but got %d", tc.expectedFd, fd) - } - } - }) - } -} - -// TestParseNetworkAddressFdName tests parsing of fdname and fdgramname addresses. -func TestParseNetworkAddressFdName(t *testing.T) { - // Save and restore environment - originalFdNames := os.Getenv("LISTEN_FDNAMES") - defer func() { - if originalFdNames != "" { - os.Setenv("LISTEN_FDNAMES", originalFdNames) - } else { - os.Unsetenv("LISTEN_FDNAMES") - } - }() - - // Set up test environment - os.Setenv("LISTEN_FDNAMES", "http:https:dns") - - tests := []struct { - input string - expectAddr NetworkAddress - expectErr bool - }{ - { - input: "fdname/http", - expectAddr: NetworkAddress{ - Network: "fd", - Host: "3", - }, - }, - { - input: "fdname/https", - expectAddr: NetworkAddress{ - Network: "fd", - Host: "4", - }, - }, - { - input: "fdname/dns", - expectAddr: NetworkAddress{ - Network: "fd", - Host: "5", - }, - }, - { - input: "fdname/http:0", - expectAddr: NetworkAddress{ - Network: "fd", - Host: "3", - }, - }, - { - input: "fdname/https:0", - expectAddr: NetworkAddress{ - Network: "fd", - Host: "4", - }, - }, - { - input: "fdgramname/http", - expectAddr: NetworkAddress{ - Network: "fdgram", - Host: "3", - }, - }, - { - input: "fdgramname/https", - expectAddr: NetworkAddress{ - Network: "fdgram", - Host: "4", - }, - }, - { - input: "fdgramname/http:0", - expectAddr: NetworkAddress{ - Network: "fdgram", - Host: "3", - }, - }, - { - input: "fdname/nonexistent", - expectErr: true, - }, - { - input: "fdgramname/nonexistent", - expectErr: true, - }, - { - input: "fdname/http:99", - expectErr: true, - }, - { - input: "fdname/invalid:abc", - expectErr: true, - }, - // Test that old fd/N syntax still works - { - input: "fd/7", - expectAddr: NetworkAddress{ - Network: "fd", - Host: "7", - }, - }, - { - input: "fdgram/8", - expectAddr: NetworkAddress{ - Network: "fdgram", - Host: "8", - }, - }, - } - - for i, tc := range tests { - actualAddr, err := ParseNetworkAddress(tc.input) - - if tc.expectErr && err == nil { - t.Errorf("Test %d (%s): Expected error but got none", i, tc.input) - } - if !tc.expectErr && err != nil { - t.Errorf("Test %d (%s): Expected no error but got: %v", i, tc.input, err) - } - if !tc.expectErr && !reflect.DeepEqual(tc.expectAddr, actualAddr) { - t.Errorf("Test %d (%s): Expected %+v but got %+v", i, tc.input, tc.expectAddr, actualAddr) - } - } -} diff --git a/listeners_test_systemd.go b/listeners_test_systemd.go new file mode 100644 index 00000000000..b393a653f73 --- /dev/null +++ b/listeners_test_systemd.go @@ -0,0 +1,390 @@ +//go:build linux && !nosystemd + +package caddy + +import ( + "os" + "reflect" + "strconv" + "testing" +) + +// TestGetSdFd tests the getSdFd function for systemd socket activation. +func TestGetSdFd(t *testing.T) { + // Save original environment + originalFdNames := os.Getenv("LISTEN_FDNAMES") + originalFds := os.Getenv("LISTEN_FDS") + originalPid := os.Getenv("LISTEN_PID") + + // Restore environment after test + defer func() { + if originalFdNames != "" { + os.Setenv("LISTEN_FDNAMES", originalFdNames) + } else { + os.Unsetenv("LISTEN_FDNAMES") + } + if originalFds != "" { + os.Setenv("LISTEN_FDS", originalFds) + } else { + os.Unsetenv("LISTEN_FDS") + } + if originalPid != "" { + os.Setenv("LISTEN_PID", originalPid) + } else { + os.Unsetenv("LISTEN_PID") + } + }() + + tests := []struct { + name string + fdNames string + fds string + socketName string + expectedFd uint + expectError bool + }{ + { + name: "simple http socket", + fdNames: "http", + fds: "1", + socketName: "http", + expectedFd: 3, + }, + { + name: "multiple different sockets - first", + fdNames: "http:https:dns", + fds: "3", + socketName: "http", + expectedFd: 3, + }, + { + name: "multiple different sockets - second", + fdNames: "http:https:dns", + fds: "3", + socketName: "https", + expectedFd: 4, + }, + { + name: "multiple different sockets - third", + fdNames: "http:https:dns", + fds: "3", + socketName: "dns", + expectedFd: 5, + }, + { + name: "duplicate names - first occurrence (no index)", + fdNames: "web:web:api", + fds: "3", + socketName: "web", + expectedFd: 3, + }, + { + name: "duplicate names - first occurrence (explicit index 0)", + fdNames: "web:web:api", + fds: "3", + socketName: "web/0", + expectedFd: 3, + }, + { + name: "duplicate names - second occurrence (index 1)", + fdNames: "web:web:api", + fds: "3", + socketName: "web/1", + expectedFd: 4, + }, + { + name: "complex duplicates - first api", + fdNames: "web:api:web:api:dns", + fds: "5", + socketName: "api/0", + expectedFd: 4, + }, + { + name: "complex duplicates - second api", + fdNames: "web:api:web:api:dns", + fds: "5", + socketName: "api/1", + expectedFd: 6, + }, + { + name: "complex duplicates - first web", + fdNames: "web:api:web:api:dns", + fds: "5", + socketName: "web/0", + expectedFd: 3, + }, + { + name: "complex duplicates - second web", + fdNames: "web:api:web:api:dns", + fds: "5", + socketName: "web/1", + expectedFd: 5, + }, + { + name: "socket not found", + fdNames: "http:https", + fds: "2", + socketName: "missing", + expectError: true, + }, + { + name: "empty socket name", + fdNames: "http", + fds: "1", + socketName: "", + expectError: true, + }, + { + name: "missing LISTEN_FDNAMES", + fdNames: "", + fds: "", + socketName: "http", + expectError: true, + }, + { + name: "index out of range", + fdNames: "web:web", + fds: "2", + socketName: "web/2", + expectError: true, + }, + { + name: "negative index", + fdNames: "web", + fds: "1", + socketName: "web/-1", + expectError: true, + }, + { + name: "invalid index format", + fdNames: "web", + fds: "1", + socketName: "web/abc", + expectError: true, + }, + { + name: "too many colons", + fdNames: "web", + fds: "1", + socketName: "web/0/extra", + expectError: true, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + // Set up environment + if tc.fdNames != "" { + os.Setenv("LISTEN_FDNAMES", tc.fdNames) + } else { + os.Unsetenv("LISTEN_FDNAMES") + } + + if tc.fds != "" { + os.Setenv("LISTEN_FDS", tc.fds) + } else { + os.Unsetenv("LISTEN_FDS") + } + + os.Setenv("LISTEN_PID", strconv.Itoa(os.Getpid())) + + // Test the function + var ( + listenFdsWithNames map[string][]uint + err error + fd uint + ) + listenFdsWithNames, err = sdListenFdsWithNames() + if err == nil { + fd, err = getSdFd(listenFdsWithNames, tc.socketName, 0) + } + + if tc.expectError { + if err == nil { + t.Errorf("Expected error but got none") + } + } else { + if err != nil { + t.Errorf("Expected no error but got: %v", err) + } + if fd != tc.expectedFd { + t.Errorf("Expected FD %d but got %d", tc.expectedFd, fd) + } + } + }) + } +} + +// TestParseNetworkAddressSd tests parsing of sd and sdgram addresses. +func TestParseNetworkAddressSd(t *testing.T) { + // Save and restore environment + originalFdNames := os.Getenv("LISTEN_FDNAMES") + originalFds := os.Getenv("LISTEN_FDS") + originalPid := os.Getenv("LISTEN_PID") + + defer func() { + if originalFdNames != "" { + os.Setenv("LISTEN_FDNAMES", originalFdNames) + } else { + os.Unsetenv("LISTEN_FDNAMES") + } + if originalFds != "" { + os.Setenv("LISTEN_FDS", originalFds) + } else { + os.Unsetenv("LISTEN_FDS") + } + if originalPid != "" { + os.Setenv("LISTEN_PID", originalPid) + } else { + os.Unsetenv("LISTEN_PID") + } + }() + + // Set up test environment + os.Setenv("LISTEN_FDNAMES", "http:https:dns") + os.Setenv("LISTEN_FDS", "3") + os.Setenv("LISTEN_PID", strconv.Itoa(os.Getpid())) + + tests := []struct { + input string + expectedAddr NetworkAddress + expectedFd uint + expectErr bool + }{ + { + input: "sd/http", + expectedAddr: NetworkAddress{ + Network: "sd", + Host: "http", + }, + expectedFd: 3, + }, + { + input: "sd/https", + expectedAddr: NetworkAddress{ + Network: "sd", + Host: "https", + }, + expectedFd: 4, + }, + { + input: "sd/dns", + expectedAddr: NetworkAddress{ + Network: "sd", + Host: "dns", + }, + expectedFd: 5, + }, + { + input: "sd/http/0", + expectedAddr: NetworkAddress{ + Network: "sd", + Host: "http/0", + }, + expectedFd: 3, + }, + { + input: "sd/https/0", + expectedAddr: NetworkAddress{ + Network: "sd", + Host: "https/0", + }, + expectedFd: 4, + }, + { + input: "sdgram/http", + expectedAddr: NetworkAddress{ + Network: "sdgram", + Host: "http", + }, + expectedFd: 3, + }, + { + input: "sdgram/https", + expectedAddr: NetworkAddress{ + Network: "sdgram", + Host: "https", + }, + expectedFd: 4, + }, + { + input: "sdgram/http/0", + expectedAddr: NetworkAddress{ + Network: "sdgram", + Host: "http/0", + }, + expectedFd: 3, + }, + { + input: "sd/nonexistent", + expectErr: true, + }, + { + input: "sd/nonexistent", + expectErr: true, + }, + { + input: "sd/http/99", + expectErr: true, + }, + { + input: "sd/invalid/abc", + expectErr: true, + }, + // Test that old fd/N syntax still works + { + input: "fd/7", + expectedAddr: NetworkAddress{ + Network: "fd", + Host: "7", + }, + expectedFd: 7, + }, + { + input: "fdgram/8", + expectedAddr: NetworkAddress{ + Network: "fdgram", + Host: "8", + }, + expectedFd: 8, + }, + } + + for i, tc := range tests { + actualAddr, err := ParseNetworkAddress(tc.input) + var ( + listenFdsWithNames map[string][]uint + fd uint + ) + if err == nil { + switch actualAddr.Network { + case "fd": + fallthrough + case "fdgram": + var fd64 uint64 + fd64, err = strconv.ParseUint(actualAddr.Host, 0, strconv.IntSize) + if err == nil { + fd = uint(fd64) + } + case "sd": + fallthrough + case "sdgram": + listenFdsWithNames, err = sdListenFdsWithNames() + fd, err = getSdFd(listenFdsWithNames, actualAddr.Host, 0) + } + } + + if tc.expectErr && err == nil { + t.Errorf("Test %d (%s): Expected error but got none", i, tc.input) + } + if !tc.expectErr && err != nil { + t.Errorf("Test %d (%s): Expected no error but got: %v", i, tc.input, err) + } + if !tc.expectErr && !reflect.DeepEqual(tc.expectedAddr, actualAddr) { + t.Errorf("Test %d (%s): Expected %+v but got %+v", i, tc.input, tc.expectedAddr, actualAddr) + } + if !tc.expectErr && fd != tc.expectedFd { + t.Errorf("Expected FD %d but got %d", tc.expectedFd, fd) + } + } +} diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index ac30f40286e..7c095fa97a4 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -620,7 +620,7 @@ func (s *Server) findLastRouteWithHostMatcher() int { // not already done, and then uses that server to serve HTTP/3 over // the listener, with Server s as the handler. func (s *Server) serveHTTP3(addr caddy.NetworkAddress, tlsCfg *tls.Config) error { - h3net, err := getHTTP3Network(addr.Network) + h3net, err := caddy.GetHTTP3Network(addr.Network) if err != nil { return fmt.Errorf("starting HTTP/3 QUIC listener: %v", err) } @@ -1125,33 +1125,7 @@ const ( ClientIPVarKey string = "client_ip" ) -var networkTypesHTTP3 = map[string]string{ - "unixgram": "unixgram", - "udp": "udp", - "udp4": "udp4", - "udp6": "udp6", - "tcp": "udp", - "tcp4": "udp4", - "tcp6": "udp6", - "fdgram": "fdgram", -} - -// RegisterNetworkHTTP3 registers a mapping from non-HTTP/3 network to HTTP/3 -// network. This should be called during init() and will panic if the network -// type is standard, reserved, or already registered. -// -// EXPERIMENTAL: Subject to change. +// DEPRECATED: moved to caddy.RegisterNetworkHTTP3 func RegisterNetworkHTTP3(originalNetwork, h3Network string) { - if _, ok := networkTypesHTTP3[strings.ToLower(originalNetwork)]; ok { - panic("network type " + originalNetwork + " is already registered") - } - networkTypesHTTP3[originalNetwork] = h3Network -} - -func getHTTP3Network(originalNetwork string) (string, error) { - h3Network, ok := networkTypesHTTP3[strings.ToLower(originalNetwork)] - if !ok { - return "", fmt.Errorf("network '%s' cannot handle HTTP/3 connections", originalNetwork) - } - return h3Network, nil + caddy.RegisterNetworkHTTP3(originalNetwork, h3Network) } diff --git a/networks.go b/networks.go new file mode 100644 index 00000000000..52d5a4a38c4 --- /dev/null +++ b/networks.go @@ -0,0 +1,124 @@ +// Copyright 2015 Matthew Holt and The Caddy Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package caddy + +import ( + "context" + "fmt" + "net" + "strings" + "sync" + + "go.uber.org/zap" +) + +// IsUnixNetwork returns true if the netw is a unix network. +func IsUnixNetwork(netw string) bool { + return netw == "unix" || netw == "unixgram" || netw == "unixpacket" || netw == "unix+h2c" +} + +// IsIpNetwork returns true if the netw is an ip network. +func IsIpNetwork(netw string) bool { + return strings.HasPrefix(netw, "ip:") || strings.HasPrefix(netw, "ip4:") || strings.HasPrefix(netw, "ip6:") +} + +// IsFdNetwork returns true if the netw is a fd network. +func IsFdNetwork(netw string) bool { + return netw == "fd" || netw == "fdgram" +} + +// ListenerFunc is a function that can return a listener given a network and address. +// The listeners must be capable of overlapping: with Caddy, new configs are loaded +// before old ones are unloaded, so listeners may overlap briefly if the configs +// both need the same listener. EXPERIMENTAL and subject to change. +type ListenerFunc func(ctx context.Context, network, host, portRange string, portOffset uint, cfg net.ListenConfig) (any, error) + +var ( + networkPlugins = map[string]ListenerFunc{} + networkPluginsMu sync.RWMutex +) + +// RegisterNetwork registers a network plugin with Caddy so that if a listener is +// created for that network plugin, getListener will be invoked to get the listener. +// This should be called during init() and will panic if the network type is standard +// or reserved, or if it is already registered. EXPERIMENTAL and subject to change. +func RegisterNetwork(network string, getListener ListenerFunc) { + network = strings.TrimSpace(strings.ToLower(network)) + + if IsReservedNetwork(network) { + panic("network type " + network + " is reserved") + } + + if _, ok := networkPlugins[strings.ToLower(network)]; ok { + panic("network type " + network + " is already registered") + } + + networkPluginsMu.Lock() + defer networkPluginsMu.Unlock() + + networkPlugins[network] = getListener +} + +// getListenerFromPlugin returns a listener on the given network and address +// if a plugin has registered the network name. It may return (nil, nil) if +// no plugin can provide a listener. +func getListenerFromPlugin(ctx context.Context, network, host, port string, portOffset uint, config net.ListenConfig) (any, error) { + networkPluginsMu.RLock() + defer networkPluginsMu.RUnlock() + + // get listener from plugin if network is registered + if getListener, ok := networkPlugins[network]; ok { + Log().Debug("getting listener from plugin", zap.String("network", network)) + return getListener(ctx, network, host, port, portOffset, config) + } + + return nil, nil +} + +var ( + networkHTTP3Plugins = map[string]string{} + networkHTTP3PluginsMu sync.RWMutex +) + +// RegisterNetworkHTTP3 registers a mapping from non-HTTP/3 network to HTTP/3 +// network. This should be called during init() and will panic if the network +// type is standard, reserved, or already registered. +// +// EXPERIMENTAL: Subject to change. +func RegisterNetworkHTTP3(originalNetwork, h3Network string) { + if IsReservedNetwork(originalNetwork) { + panic("network type " + originalNetwork + " is reserved") + } + if _, ok := networkHTTP3Plugins[strings.ToLower(originalNetwork)]; ok { + panic("network type " + originalNetwork + " is already registered") + } + + networkHTTP3PluginsMu.Lock() + defer networkHTTP3PluginsMu.Unlock() + + networkHTTP3Plugins[originalNetwork] = h3Network +} + +func getHTTP3Plugin(originalNetwork string) (string, error) { + networkHTTP3PluginsMu.RLock() + defer networkHTTP3PluginsMu.RUnlock() + + h3Network, ok := networkHTTP3Plugins[strings.ToLower(originalNetwork)] + if !ok { + return "", fmt.Errorf("network '%s' cannot handle HTTP/3 connections", originalNetwork) + } + + return h3Network, nil +} diff --git a/networks_nosystemd.go b/networks_nosystemd.go new file mode 100644 index 00000000000..f56f79e0525 --- /dev/null +++ b/networks_nosystemd.go @@ -0,0 +1,42 @@ +//go:build !linux || nosystemd + +package caddy + +import ( + "context" + "net" +) + +func IsReservedNetwork(network string) bool { + return network == "tcp" || network == "tcp4" || network == "tcp6" || + network == "udp" || network == "udp4" || network == "udp6" || + IsUnixNetwork(network) || + IsIpNetwork(network) || + IsFdNetwork(network) +} + +func getListenerFromNetwork(ctx context.Context, network, host, port string, portOffset uint, config net.ListenConfig) (any, error) { + return getListenerFromPlugin(ctx, network, host, port, portOffset, config) +} + +func GetHTTP3Network(originalNetwork string) (string, error) { + switch originalNetwork { + case "unixgram": + return "unixgram", nil + case "udp": + return "udp", nil + case "udp4": + return "udp4", nil + case "udp6": + return "udp6", nil + case "tcp": + return "udp", nil + case "tcp4": + return "udp4", nil + case "tcp6": + return "udp6", nil + case "fdgram": + return "fdgram", nil + } + return getHTTP3Plugin(originalNetwork) +} diff --git a/networks_systemd.go b/networks_systemd.go new file mode 100644 index 00000000000..d99f46c87bc --- /dev/null +++ b/networks_systemd.go @@ -0,0 +1,178 @@ +//go:build linux && !nosystemd + +package caddy + +import ( + "context" + "errors" + "fmt" + "net" + "os" + "strconv" + "strings" + "sync" +) + +func IsSdNetwork(network string) bool { + return network == "sd" || network == "sdgram" +} + +func IsReservedNetwork(network string) bool { + return network == "tcp" || network == "tcp4" || network == "tcp6" || + network == "udp" || network == "udp4" || network == "udp6" || + IsUnixNetwork(network) || + IsIpNetwork(network) || + IsFdNetwork(network) || + IsSdNetwork(network) +} + +func sdListenFds() (int, error) { + lnPid, ok := os.LookupEnv("LISTEN_PID") + if !ok { + return 0, errors.New("LISTEN_PID is unset.") + } + + pid, err := strconv.Atoi(lnPid) + if err != nil { + return 0, err + } + + if pid != os.Getpid() { + return 0, fmt.Errorf("LISTEN_PID does not match pid: %d != %d", pid, os.Getpid()) + } + + lnFds, ok := os.LookupEnv("LISTEN_FDS") + if !ok { + return 0, errors.New("LISTEN_FDS is unset.") + } + + fds, err := strconv.Atoi(lnFds) + if err != nil { + return 0, err + } + + return fds, nil +} + +func sdListenFdsWithNames() (map[string][]uint, error) { + const lnFdsStart = 3 + + fds, err := sdListenFds() + if err != nil { + return nil, err + } + + lnFdnames, ok := os.LookupEnv("LISTEN_FDNAMES") + if !ok { + return nil, errors.New("LISTEN_FDNAMES is unset.") + } + + fdNames := strings.Split(lnFdnames, ":") + if fds != len(fdNames) { + return nil, fmt.Errorf("LISTEN_FDS does not match LISTEN_FDNAMES length: %d != %d", fds, len(fdNames)) + } + + nameToFiles := make(map[string][]uint, len(fdNames)) + for index, name := range fdNames { + nameToFiles[name] = append(nameToFiles[name], lnFdsStart+uint(index)) + } + + return nameToFiles, nil +} + +func getSdFd(nameToFiles map[string][]uint, host string, portOffset uint) (uint, error) { + name, index, li := host, portOffset, strings.LastIndex(host, "/") + if li >= 0 { + name = host[:li] + i, err := strconv.ParseUint(host[li+1:], 0, strconv.IntSize) + if err != nil { + return 0, err + } + index += uint(i) + } + + files, ok := nameToFiles[name] + if !ok { + return 0, fmt.Errorf("invalid listen fd name: %s", name) + } + + if uint(len(files)) <= index { + return 0, fmt.Errorf("invalid listen fd index: %d", index) + } + + return files[index], nil +} + +var ( + initNameToFiles map[string][]uint + initNameToFilesErr error + initNameToFilesMu sync.Mutex +) + +func getListenerFromSd(ctx context.Context, network, host, port string, portOffset uint, config net.ListenConfig) (any, error) { + func() { + initNameToFilesMu.Lock() + defer initNameToFilesMu.Unlock() + + if initNameToFiles == nil && initNameToFilesErr == nil { + initNameToFiles, initNameToFilesErr = sdListenFdsWithNames() + } + }() + + if initNameToFilesErr != nil { + return nil, initNameToFilesErr + } + + file, err := getSdFd(initNameToFiles, host, portOffset) + if err != nil { + return nil, err + } + + var fdNetwork string + switch network { + case "sd": + fdNetwork = "fd" + case "sdgram": + fdNetwork = "fdgram" + default: + return nil, fmt.Errorf("invalid network: %s", network) + } + + na, err := ParseNetworkAddress(JoinNetworkAddress(fdNetwork, strconv.FormatUint(uint64(file), 10), port)) + if err != nil { + return nil, err + } + + return na.Listen(ctx, portOffset, config) +} + +func getListenerFromNetwork(ctx context.Context, network, host, port string, portOffset uint, config net.ListenConfig) (any, error) { + if IsSdNetwork(network) { + return getListenerFromSd(ctx, network, host, port, portOffset, config) + } + return getListenerFromPlugin(ctx, network, host, port, portOffset, config) +} + +func GetHTTP3Network(originalNetwork string) (string, error) { + switch originalNetwork { + case "unixgram": + return "unixgram", nil + case "udp": + return "udp", nil + case "udp4": + return "udp4", nil + case "udp6": + return "udp6", nil + case "tcp": + return "udp", nil + case "tcp4": + return "udp4", nil + case "tcp6": + return "udp6", nil + case "fdgram": + return "fdgram", nil + case "sdgram": + return "sdgram", nil + } + return getHTTP3Plugin(originalNetwork) +}