Skip to content

Commit d732222

Browse files
committed
listeners: Add network interface binding support with IP version modes
1 parent b54e870 commit d732222

File tree

4 files changed

+1170
-1
lines changed

4 files changed

+1170
-1
lines changed

caddyconfig/httpcaddyfile/addresses.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,38 @@ func (st *ServerType) listenersForServerBlockAddress(sblock serverBlock, addr Ad
338338
if err != nil {
339339
return nil, fmt.Errorf("parsing network address: %v", err)
340340
}
341-
if _, ok := listeners[addr.String()]; !ok {
341+
342+
// Check if this is an interface with multi-address modes - expand to multiple IPs
343+
if networkAddr.IsInterfaceNetwork() && strings.Contains(networkAddr.Host, caddy.InterfaceDelimiter) {
344+
parts := strings.SplitN(networkAddr.Host, caddy.InterfaceDelimiter, 2)
345+
if len(parts) == 2 {
346+
mode := caddy.InterfaceBindingMode(parts[1])
347+
// Check if this mode returns multiple addresses
348+
if mode == caddy.InterfaceBindingAll || mode == caddy.InterfaceBindingIPv4 || mode == caddy.InterfaceBindingIPv6 {
349+
// Resolve IPs for the interface with the specified mode
350+
ipAddresses, err := caddy.ResolveInterfaceNameWithMode(parts[0], mode)
351+
if err != nil {
352+
return nil, fmt.Errorf("resolving interface %s with mode '%s': %v", parts[0], mode, err)
353+
}
354+
355+
// Create a listener for each IP
356+
for _, ip := range ipAddresses {
357+
ipNA := networkAddr
358+
ipNA.Host = ip
359+
if _, ok := listeners[ipNA.String()]; !ok {
360+
listeners[ipNA.String()] = map[string]struct{}{}
361+
}
362+
for _, protocol := range lnCfgVal.protocols {
363+
listeners[ipNA.String()][protocol] = struct{}{}
364+
}
365+
}
366+
continue
367+
}
368+
}
369+
}
370+
371+
// Normal case - single address
372+
if _, ok := listeners[networkAddr.String()]; !ok {
342373
listeners[networkAddr.String()] = map[string]struct{}{}
343374
}
344375
for _, protocol := range lnCfgVal.protocols {

caddyconfig/httpcaddyfile/builtins.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func init() {
6060
// bind <addresses...> [{
6161
// protocols [h1|h2|h2c|h3] [...]
6262
// }]
63+
//
64+
// Addresses can be network addresses (host:port) or network interface names (e.g., eth0:80).
65+
// Interface names will be resolved to their IP addresses at bind time.
6366
func parseBind(h Helper) ([]ConfigValue, error) {
6467
h.Next() // consume directive name
6568
var addresses, protocols []string

0 commit comments

Comments
 (0)