| // Copyright 2016 The Upspin Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package serverutil |
| |
| import ( |
| "net" |
| |
| "upspin.io/config" |
| ) |
| |
| // IsLoopback returns true if the name only resolves to loopback addresses. |
| func IsLoopback(addr string) bool { |
| host, _, err := net.SplitHostPort(addr) |
| if err != nil { |
| host = addr |
| } |
| if host == "localhost" || host == "127.0.0.1" || host == "::1" { |
| return true |
| } |
| // Check for local IPC. |
| if config.IsLocal(host) { |
| return true |
| } |
| // Check for loopback network. |
| ips, err := net.LookupIP(host) |
| if err != nil { |
| return false |
| } |
| for _, ip := range ips { |
| if !ip.IsLoopback() { |
| return false |
| } |
| } |
| return true |
| } |