Don't treat localhost specially when assigning bind address

If we listen on 127.0.0.1:80 for `localhost` but :80 for everything else,
then a hostname in the hosts file that resolves to 127.0.0.1 will be
served on :80 (unless the bind directive is used) but the OS will use
the socket listening at 127.0.0.1:80, thus giving a "No such site" error
even though the site is there, but it's on the other listener at :80.

Two ways to fix this: 1) Leave as-is and require the user to set "bind
127.0.0.1" in their Caddyfile for all sites that are resolved in the
hosts file, or 2) Take out this special case and let localhost sites
listen on :80 (unless the user changes that with the bind directive, of
course). Having localhost bind to any interface is a little annoying
(unsettling?) but probably best in the long run.

https://forum.caddyserver.com/t/wildcard-virtual-domains-with-wildcard-roots/221/9?u=matt
This commit is contained in:
Matthew Holt 2016-06-27 13:14:17 -06:00
parent 6f05faa670
commit 58085edc16
2 changed files with 28 additions and 7 deletions

View file

@ -206,11 +206,11 @@ func groupSiteConfigsByListenAddr(configs []*SiteConfig) (map[string][]*SiteConf
groups := make(map[string][]*SiteConfig)
for _, conf := range configs {
if caddy.IsLoopback(conf.Addr.Host) && conf.ListenHost == "" {
// special case: one would not expect a site served
// at loopback to be connected to from the outside.
conf.ListenHost = conf.Addr.Host
}
// We would add a special case here so that localhost addresses
// bind to 127.0.0.1 if conf.ListenHost is not already set, which
// would prevent outsiders from even connecting; but that was problematic:
// https://forum.caddyserver.com/t/wildcard-virtual-domains-with-wildcard-roots/221/5?u=matt
if conf.Addr.Port == "" {
conf.Addr.Port = Port
}

View file

@ -11,8 +11,8 @@ import (
// by longest matching path.
type vhostTrie struct {
edges map[string]*vhostTrie
site *SiteConfig // also known as a virtual host
path string // the path portion of the key for this node
site *SiteConfig // site to match on this node; also known as a virtual host
path string // the path portion of the key for the associated site
}
// newVHostTrie returns a new vhostTrie.
@ -137,3 +137,24 @@ func (t *vhostTrie) splitHostPath(key string) (host, path string) {
}
return
}
// String returns a list of all the entries in t; assumes that
// t is a root node.
func (t *vhostTrie) String() string {
var s string
for host, edge := range t.edges {
s += edge.str(host)
}
return s
}
func (t *vhostTrie) str(prefix string) string {
var s string
for key, edge := range t.edges {
if edge.site != nil {
s += prefix + key + "\n"
}
s += edge.str(prefix + key)
}
return s
}