| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2019-04-10 02:39:42 +08:00
										 |  |  |  * MinIO Cloud Storage, (C) 2017 MinIO, Inc. | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	"sort" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	"syscall" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 07:35:12 +08:00
										 |  |  | 	"github.com/minio/minio-go/v6/pkg/set" | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 	"github.com/minio/minio/cmd/config" | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 	"github.com/minio/minio/cmd/logger" | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // IPv4 addresses of local host.
 | 
					
						
							|  |  |  | var localIP4 = mustGetLocalIP4() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | // IPv6 address of local host.
 | 
					
						
							|  |  |  | var localIP6 = mustGetLocalIP6() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | // mustSplitHostPort is a wrapper to net.SplitHostPort() where error is assumed to be a fatal.
 | 
					
						
							|  |  |  | func mustSplitHostPort(hostPort string) (host, port string) { | 
					
						
							|  |  |  | 	host, port, err := net.SplitHostPort(hostPort) | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	// Strip off IPv6 zone information.
 | 
					
						
							|  |  |  | 	if i := strings.Index(host, "%"); i > -1 { | 
					
						
							|  |  |  | 		host = host[:i] | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-06 06:04:40 +08:00
										 |  |  | 	logger.FatalIf(err, "Unable to split host port %s", hostPort) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	return host, port | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | // mustGetLocalIP4 returns IPv4 addresses of localhost.  It panics on error.
 | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | func mustGetLocalIP4() (ipList set.StringSet) { | 
					
						
							|  |  |  | 	ipList = set.NewStringSet() | 
					
						
							|  |  |  | 	addrs, err := net.InterfaceAddrs() | 
					
						
							| 
									
										
										
										
											2018-05-10 06:11:24 +08:00
										 |  |  | 	logger.FatalIf(err, "Unable to get IP addresses of this host") | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for _, addr := range addrs { | 
					
						
							|  |  |  | 		var ip net.IP | 
					
						
							|  |  |  | 		switch v := addr.(type) { | 
					
						
							|  |  |  | 		case *net.IPNet: | 
					
						
							|  |  |  | 			ip = v.IP | 
					
						
							|  |  |  | 		case *net.IPAddr: | 
					
						
							|  |  |  | 			ip = v.IP | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ip.To4() != nil { | 
					
						
							|  |  |  | 			ipList.Add(ip.String()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ipList | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | // mustGetLocalIP6 returns IPv6 addresses of localhost.  It panics on error.
 | 
					
						
							|  |  |  | func mustGetLocalIP6() (ipList set.StringSet) { | 
					
						
							|  |  |  | 	ipList = set.NewStringSet() | 
					
						
							|  |  |  | 	addrs, err := net.InterfaceAddrs() | 
					
						
							|  |  |  | 	logger.FatalIf(err, "Unable to get IP addresses of this host") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, addr := range addrs { | 
					
						
							|  |  |  | 		var ip net.IP | 
					
						
							|  |  |  | 		switch v := addr.(type) { | 
					
						
							|  |  |  | 		case *net.IPNet: | 
					
						
							|  |  |  | 			ip = v.IP | 
					
						
							|  |  |  | 		case *net.IPAddr: | 
					
						
							|  |  |  | 			ip = v.IP | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 00:26:30 +08:00
										 |  |  | 		if ip.To4() == nil { | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 			ipList.Add(ip.String()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ipList | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // getHostIP returns IP address of given host.
 | 
					
						
							|  |  |  | func getHostIP(host string) (ipList set.StringSet, err error) { | 
					
						
							| 
									
										
										
										
											2017-08-14 04:03:06 +08:00
										 |  |  | 	var ips []net.IP | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ips, err = net.LookupIP(host); err != nil { | 
					
						
							| 
									
										
										
										
											2019-04-20 01:26:44 +08:00
										 |  |  | 		return ipList, err | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-14 04:03:06 +08:00
										 |  |  | 	ipList = set.NewStringSet() | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	for _, ip := range ips { | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 		ipList.Add(ip.String()) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ipList, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-13 00:22:35 +08:00
										 |  |  | // byLastOctetValue implements sort.Interface used in sorting a list
 | 
					
						
							|  |  |  | // of ip address by their last octet value in descending order.
 | 
					
						
							|  |  |  | type byLastOctetValue []net.IP | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (n byLastOctetValue) Len() int      { return len(n) } | 
					
						
							|  |  |  | func (n byLastOctetValue) Swap(i, j int) { n[i], n[j] = n[j], n[i] } | 
					
						
							|  |  |  | func (n byLastOctetValue) Less(i, j int) bool { | 
					
						
							|  |  |  | 	// This case is needed when all ips in the list
 | 
					
						
							|  |  |  | 	// have same last octets, Following just ensures that
 | 
					
						
							|  |  |  | 	// 127.0.0.1 is moved to the end of the list.
 | 
					
						
							| 
									
										
										
										
											2019-11-20 09:42:27 +08:00
										 |  |  | 	if n[i].IsLoopback() { | 
					
						
							| 
									
										
										
										
											2017-04-13 00:22:35 +08:00
										 |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-20 09:42:27 +08:00
										 |  |  | 	if n[j].IsLoopback() { | 
					
						
							| 
									
										
										
										
											2017-04-13 00:22:35 +08:00
										 |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return []byte(n[i].To4())[3] > []byte(n[j].To4())[3] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // sortIPs - sort ips based on higher octects.
 | 
					
						
							|  |  |  | // The logic to sort by last octet is implemented to
 | 
					
						
							|  |  |  | // prefer CIDRs with higher octects, this in-turn skips the
 | 
					
						
							|  |  |  | // localhost/loopback address to be not preferred as the
 | 
					
						
							|  |  |  | // first ip on the list. Subsequently this list helps us print
 | 
					
						
							|  |  |  | // a user friendly message with appropriate values.
 | 
					
						
							|  |  |  | func sortIPs(ipList []string) []string { | 
					
						
							|  |  |  | 	if len(ipList) == 1 { | 
					
						
							|  |  |  | 		return ipList | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ipV4s []net.IP | 
					
						
							|  |  |  | 	var nonIPs []string | 
					
						
							|  |  |  | 	for _, ip := range ipList { | 
					
						
							|  |  |  | 		nip := net.ParseIP(ip) | 
					
						
							|  |  |  | 		if nip != nil { | 
					
						
							|  |  |  | 			ipV4s = append(ipV4s, nip) | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			nonIPs = append(nonIPs, ip) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sort.Sort(byLastOctetValue(ipV4s)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var ips []string | 
					
						
							|  |  |  | 	for _, ip := range ipV4s { | 
					
						
							|  |  |  | 		ips = append(ips, ip.String()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return append(nonIPs, ips...) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | func getAPIEndpoints() (apiEndpoints []string) { | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	var ipList []string | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	if globalMinioHost == "" { | 
					
						
							| 
									
										
										
										
											2017-04-13 00:22:35 +08:00
										 |  |  | 		ipList = sortIPs(localIP4.ToSlice()) | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 		ipList = append(ipList, localIP6.ToSlice()...) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 		ipList = []string{globalMinioHost} | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, ip := range ipList { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:42:27 +08:00
										 |  |  | 		endpoint := fmt.Sprintf("%s://%s", getURLScheme(globalIsSSL), net.JoinHostPort(ip, globalMinioPort)) | 
					
						
							|  |  |  | 		apiEndpoints = append(apiEndpoints, endpoint) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return apiEndpoints | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | // isHostIP - helper for validating if the provided arg is an ip address.
 | 
					
						
							|  |  |  | func isHostIP(ipAddress string) bool { | 
					
						
							| 
									
										
										
										
											2017-08-11 07:54:19 +08:00
										 |  |  | 	host, _, err := net.SplitHostPort(ipAddress) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		host = ipAddress | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	// Strip off IPv6 zone information.
 | 
					
						
							|  |  |  | 	if i := strings.Index(host, "%"); i > -1 { | 
					
						
							|  |  |  | 		host = host[:i] | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-11 07:54:19 +08:00
										 |  |  | 	return net.ParseIP(host) != nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-25 06:02:40 +08:00
										 |  |  | // checkPortAvailability - check if given host and port is already in use.
 | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | // Note: The check method tries to listen on given port and closes it.
 | 
					
						
							|  |  |  | // It is possible to have a disconnected client in this tiny window of time.
 | 
					
						
							| 
									
										
										
										
											2019-06-25 06:02:40 +08:00
										 |  |  | func checkPortAvailability(host, port string) (err error) { | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	network := []string{"tcp", "tcp4", "tcp6"} | 
					
						
							|  |  |  | 	for _, n := range network { | 
					
						
							| 
									
										
										
										
											2019-06-25 06:02:40 +08:00
										 |  |  | 		l, err := net.Listen(n, net.JoinHostPort(host, port)) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			// As we are able to listen on this network, the port is not in use.
 | 
					
						
							|  |  |  | 			// Close the listener and continue check other networks.
 | 
					
						
							|  |  |  | 			if err = l.Close(); err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-02 01:42:18 +08:00
										 |  |  | 		} else if errors.Is(err, syscall.EADDRINUSE) { | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 			// As we got EADDRINUSE error, the port is in use by other process.
 | 
					
						
							|  |  |  | 			// Return the error.
 | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | // extractHostPort - extracts host/port from many address formats
 | 
					
						
							|  |  |  | // such as, ":9000", "localhost:9000", "http://localhost:9000/"
 | 
					
						
							|  |  |  | func extractHostPort(hostAddr string) (string, string, error) { | 
					
						
							|  |  |  | 	var addr, scheme string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if hostAddr == "" { | 
					
						
							|  |  |  | 		return "", "", errors.New("unable to process empty address") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 02:27:33 +08:00
										 |  |  | 	// Simplify the work of url.Parse() and always send a url with
 | 
					
						
							|  |  |  | 	if !strings.HasPrefix(hostAddr, "http://") && !strings.HasPrefix(hostAddr, "https://") { | 
					
						
							|  |  |  | 		hostAddr = "//" + hostAddr | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	// Parse address to extract host and scheme field
 | 
					
						
							|  |  |  | 	u, err := url.Parse(hostAddr) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-08-07 02:27:33 +08:00
										 |  |  | 		return "", "", err | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-07 02:27:33 +08:00
										 |  |  | 	addr = u.Host | 
					
						
							|  |  |  | 	scheme = u.Scheme | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	// Use the given parameter again if url.Parse()
 | 
					
						
							|  |  |  | 	// didn't return any useful result.
 | 
					
						
							|  |  |  | 	if addr == "" { | 
					
						
							|  |  |  | 		addr = hostAddr | 
					
						
							|  |  |  | 		scheme = "http" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// At this point, addr can be one of the following form:
 | 
					
						
							|  |  |  | 	//	":9000"
 | 
					
						
							|  |  |  | 	//	"localhost:9000"
 | 
					
						
							|  |  |  | 	//	"localhost" <- in this case, we check for scheme
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	host, port, err := net.SplitHostPort(addr) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if !strings.Contains(err.Error(), "missing port in address") { | 
					
						
							|  |  |  | 			return "", "", err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		host = addr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		switch scheme { | 
					
						
							|  |  |  | 		case "https": | 
					
						
							|  |  |  | 			port = "443" | 
					
						
							|  |  |  | 		case "http": | 
					
						
							|  |  |  | 			port = "80" | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return "", "", errors.New("unable to guess port from scheme") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return host, port, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // isLocalHost - checks if the given parameter
 | 
					
						
							|  |  |  | // correspond to one of the local IP of the
 | 
					
						
							|  |  |  | // current machine
 | 
					
						
							|  |  |  | func isLocalHost(host string) (bool, error) { | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	hostIPs, err := getHostIP(host) | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return false, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	// If intersection of two IP sets is not empty, then the host is localhost.
 | 
					
						
							|  |  |  | 	isLocalv4 := !localIP4.Intersection(hostIPs).IsEmpty() | 
					
						
							|  |  |  | 	isLocalv6 := !localIP6.Intersection(hostIPs).IsEmpty() | 
					
						
							|  |  |  | 	return isLocalv4 || isLocalv6, nil | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // sameLocalAddrs - returns true if two addresses, even with different
 | 
					
						
							|  |  |  | // formats, point to the same machine, e.g:
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | //  ':9000' and 'http://localhost:9000/' will return true
 | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | func sameLocalAddrs(addr1, addr2 string) (bool, error) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Extract host & port from given parameters
 | 
					
						
							|  |  |  | 	host1, port1, err := extractHostPort(addr1) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return false, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	host2, port2, err := extractHostPort(addr2) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return false, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var addr1Local, addr2Local bool | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if host1 == "" { | 
					
						
							|  |  |  | 		// If empty host means it is localhost
 | 
					
						
							|  |  |  | 		addr1Local = true | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// Host not empty, check if it is local
 | 
					
						
							|  |  |  | 		if addr1Local, err = isLocalHost(host1); err != nil { | 
					
						
							|  |  |  | 			return false, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if host2 == "" { | 
					
						
							|  |  |  | 		// If empty host means it is localhost
 | 
					
						
							|  |  |  | 		addr2Local = true | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// Host not empty, check if it is local
 | 
					
						
							|  |  |  | 		if addr2Local, err = isLocalHost(host2); err != nil { | 
					
						
							|  |  |  | 			return false, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// If both of addresses point to the same machine, check if
 | 
					
						
							|  |  |  | 	// have the same port
 | 
					
						
							|  |  |  | 	if addr1Local && addr2Local { | 
					
						
							|  |  |  | 		if port1 == port2 { | 
					
						
							|  |  |  | 			return true, nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | // CheckLocalServerAddr - checks if serverAddr is valid and local host.
 | 
					
						
							|  |  |  | func CheckLocalServerAddr(serverAddr string) error { | 
					
						
							|  |  |  | 	host, port, err := net.SplitHostPort(serverAddr) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		return config.ErrInvalidAddressFlag(err) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	// Strip off IPv6 zone information.
 | 
					
						
							|  |  |  | 	if i := strings.Index(host, "%"); i > -1 { | 
					
						
							|  |  |  | 		host = host[:i] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	// Check whether port is a valid port number.
 | 
					
						
							|  |  |  | 	p, err := strconv.Atoi(port) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		return config.ErrInvalidAddressFlag(err).Msg("invalid port number") | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} else if p < 1 || p > 65535 { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		return config.ErrInvalidAddressFlag(nil).Msg("port number must be between 1 to 65535") | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-24 03:07:39 +08:00
										 |  |  | 	// 0.0.0.0 is a wildcard address and refers to local network
 | 
					
						
							|  |  |  | 	// addresses. I.e, 0.0.0.0:9000 like ":9000" refers to port
 | 
					
						
							|  |  |  | 	// 9000 on localhost.
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	if host != "" && host != net.IPv4zero.String() && host != net.IPv6zero.String() { | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 		isLocalHost, err := isLocalHost(host) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 		if !isLocalHost { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 			return config.ErrInvalidAddressFlag(nil).Msg("host in server address should be this server") | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |