mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 01:19:20 +08:00 
			
		
		
		
	caddyhttp: Auto-redirects from all bind addresses (fix #3443)
This commit is contained in:
		@ -81,8 +81,10 @@ func (app *App) automaticHTTPSPhase1(ctx caddy.Context, repl *caddy.Replacer) er
 | 
				
			|||||||
	uniqueDomainsForCerts := make(map[string]struct{})
 | 
						uniqueDomainsForCerts := make(map[string]struct{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// this maps domain names for automatic HTTP->HTTPS
 | 
						// this maps domain names for automatic HTTP->HTTPS
 | 
				
			||||||
	// redirects to their destination server address
 | 
						// redirects to their destination server addresses
 | 
				
			||||||
	redirDomains := make(map[string]caddy.NetworkAddress)
 | 
						// (there might be more than 1 if bind is used; see
 | 
				
			||||||
 | 
						// https://github.com/caddyserver/caddy/issues/3443)
 | 
				
			||||||
 | 
						redirDomains := make(map[string][]caddy.NetworkAddress)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for srvName, srv := range app.Servers {
 | 
						for srvName, srv := range app.Servers {
 | 
				
			||||||
		// as a prerequisite, provision route matchers; this is
 | 
							// as a prerequisite, provision route matchers; this is
 | 
				
			||||||
@ -220,7 +222,7 @@ func (app *App) automaticHTTPSPhase1(ctx caddy.Context, repl *caddy.Replacer) er
 | 
				
			|||||||
			// an empty string to indicate a catch-all, which we have to
 | 
								// an empty string to indicate a catch-all, which we have to
 | 
				
			||||||
			// treat special later
 | 
								// treat special later
 | 
				
			||||||
			if len(serverDomainSet) == 0 {
 | 
								if len(serverDomainSet) == 0 {
 | 
				
			||||||
				redirDomains[""] = addr
 | 
									redirDomains[""] = append(redirDomains[""], addr)
 | 
				
			||||||
				continue
 | 
									continue
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -230,7 +232,7 @@ func (app *App) automaticHTTPSPhase1(ctx caddy.Context, repl *caddy.Replacer) er
 | 
				
			|||||||
				// port, we'll have to choose one, so prefer the HTTPS port
 | 
									// port, we'll have to choose one, so prefer the HTTPS port
 | 
				
			||||||
				if _, ok := redirDomains[d]; !ok ||
 | 
									if _, ok := redirDomains[d]; !ok ||
 | 
				
			||||||
					addr.StartPort == uint(app.httpsPort()) {
 | 
										addr.StartPort == uint(app.httpsPort()) {
 | 
				
			||||||
					redirDomains[d] = addr
 | 
										redirDomains[d] = append(redirDomains[d], addr)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -278,9 +280,11 @@ uniqueDomainsLoop:
 | 
				
			|||||||
	// we need to reduce the mapping, i.e. group domains by address
 | 
						// we need to reduce the mapping, i.e. group domains by address
 | 
				
			||||||
	// since new routes are appended to servers by their address
 | 
						// since new routes are appended to servers by their address
 | 
				
			||||||
	domainsByAddr := make(map[string][]string)
 | 
						domainsByAddr := make(map[string][]string)
 | 
				
			||||||
	for domain, addr := range redirDomains {
 | 
						for domain, addrs := range redirDomains {
 | 
				
			||||||
		addrStr := addr.String()
 | 
							for _, addr := range addrs {
 | 
				
			||||||
		domainsByAddr[addrStr] = append(domainsByAddr[addrStr], domain)
 | 
								addrStr := addr.String()
 | 
				
			||||||
 | 
								domainsByAddr[addrStr] = append(domainsByAddr[addrStr], domain)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// these keep track of the redirect server address(es)
 | 
						// these keep track of the redirect server address(es)
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user