mirror of
https://github.com/caddyserver/caddy.git
synced 2026-06-30 05:42:50 +00:00
Adding support for ServerIP context
This commit is contained in:
parent
c8514ad7b7
commit
73d52490d0
2 changed files with 62 additions and 0 deletions
|
|
@ -84,6 +84,29 @@ func (c Context) IP() string {
|
|||
return ip
|
||||
}
|
||||
|
||||
// To mock the net.InterfaceAddrs from the test.
|
||||
var networkInterfacesFn = net.InterfaceAddrs
|
||||
|
||||
// ServerIP gets the (local) IP address of the server.
|
||||
// TODO: The bind directive should be honored in this method (see PR #1474).
|
||||
func (c Context) ServerIP() string {
|
||||
addrs, err := networkInterfacesFn()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, address := range addrs {
|
||||
// Validate the address and check if it's not a loopback
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil || ipnet.IP.To16() != nil {
|
||||
return ipnet.IP.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// URI returns the raw, unprocessed request URI (including query
|
||||
// string and hash) obtained directly from the Request-Line of
|
||||
// the HTTP request.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue