mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	httpcaddyfile: Fix bind when IPv6 is specified with network (#4950)
* fix listening on IPv6 addresses: use net.JoinHostPort Commit 1e18afb5c862d62be130d563785de5c58f08ae8e broke my caddy setup. This commit fixes it. * Refactor solution; simplify, add descriptive comment * Move network to host, not copy Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									62b0685375
								
							
						
					
					
						commit
						a1ad20e472
					
				@ -219,7 +219,7 @@ func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key str
 | 
				
			|||||||
		return nil, fmt.Errorf("[%s] scheme and port violate convention", key)
 | 
							return nil, fmt.Errorf("[%s] scheme and port violate convention", key)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// the bind directive specifies hosts, but is optional
 | 
						// the bind directive specifies hosts (and potentially network), but is optional
 | 
				
			||||||
	lnHosts := make([]string, 0, len(sblock.pile["bind"]))
 | 
						lnHosts := make([]string, 0, len(sblock.pile["bind"]))
 | 
				
			||||||
	for _, cfgVal := range sblock.pile["bind"] {
 | 
						for _, cfgVal := range sblock.pile["bind"] {
 | 
				
			||||||
		lnHosts = append(lnHosts, cfgVal.Value.([]string)...)
 | 
							lnHosts = append(lnHosts, cfgVal.Value.([]string)...)
 | 
				
			||||||
@ -234,11 +234,23 @@ func (st *ServerType) listenerAddrsForServerBlockKey(sblock serverBlock, key str
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// use a map to prevent duplication
 | 
						// use a map to prevent duplication
 | 
				
			||||||
	listeners := make(map[string]struct{})
 | 
						listeners := make(map[string]struct{})
 | 
				
			||||||
	for _, host := range lnHosts {
 | 
						for _, lnHost := range lnHosts {
 | 
				
			||||||
		// host can have network + host (e.g. "tcp6/localhost") but
 | 
							// normally we would simply append the port,
 | 
				
			||||||
		// will/should not have port information because this usually
 | 
							// but if lnHost is IPv6, we need to ensure it
 | 
				
			||||||
		// comes from the bind directive, so we append the port
 | 
							// is enclosed in [ ]; net.JoinHostPort does
 | 
				
			||||||
		addr, err := caddy.ParseNetworkAddress(host + ":" + lnPort)
 | 
							// this for us, but lnHost might also have a
 | 
				
			||||||
 | 
							// network type in front (e.g. "tcp/") leading
 | 
				
			||||||
 | 
							// to "[tcp/::1]" which causes parsing failures
 | 
				
			||||||
 | 
							// later; what we need is "tcp/[::1]", so we have
 | 
				
			||||||
 | 
							// to split the network and host, then re-combine
 | 
				
			||||||
 | 
							network, host, ok := strings.Cut(lnHost, "/")
 | 
				
			||||||
 | 
							if !ok {
 | 
				
			||||||
 | 
								host = network
 | 
				
			||||||
 | 
								network = ""
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							host = strings.Trim(host, "[]") // IPv6
 | 
				
			||||||
 | 
							networkAddr := caddy.JoinNetworkAddress(network, host, lnPort)
 | 
				
			||||||
 | 
							addr, err := caddy.ParseNetworkAddress(networkAddr)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return nil, fmt.Errorf("parsing network address: %v", err)
 | 
								return nil, fmt.Errorf("parsing network address: %v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user