| 
									
										
										
										
											2021-04-19 03:41:13 +08:00
										 |  |  | // Copyright (c) 2015-2021 MinIO, Inc.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is part of MinIO Object Storage stack
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or
 | 
					
						
							|  |  |  | // (at your option) any later version.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU Affero General Public License for more details.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License
 | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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" | 
					
						
							| 
									
										
										
										
											2021-08-09 21:57:54 +08:00
										 |  |  | 	"runtime" | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	"sort" | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-15 00:38:05 +08:00
										 |  |  | 	"github.com/minio/minio-go/v7/pkg/set" | 
					
						
							| 
									
										
										
										
											2021-06-02 05:59:40 +08:00
										 |  |  | 	"github.com/minio/minio/internal/config" | 
					
						
							|  |  |  | 	"github.com/minio/minio/internal/logger" | 
					
						
							| 
									
										
										
										
											2021-06-15 05:54:37 +08:00
										 |  |  | 	xnet "github.com/minio/pkg/net" | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // IPv4 addresses of local host.
 | 
					
						
							|  |  |  | var localIP4 = mustGetLocalIP4() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // mustSplitHostPort is a wrapper to net.SplitHostPort() where error is assumed to be a fatal.
 | 
					
						
							|  |  |  | func mustSplitHostPort(hostPort string) (host, port string) { | 
					
						
							| 
									
										
										
										
											2019-11-27 03:42:10 +08:00
										 |  |  | 	xh, err := xnet.ParseHost(hostPort) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		logger.FatalIf(err, "Unable to split host port %s", hostPort) | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-11-27 03:42:10 +08:00
										 |  |  | 	return xh.Name, xh.Port.String() | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 05:42:28 +08:00
										 |  |  | // mustGetLocalIPs returns IPs of local interface
 | 
					
						
							|  |  |  | func mustGetLocalIPs() (ipList []net.IP) { | 
					
						
							| 
									
										
										
										
											2021-08-09 21:57:54 +08:00
										 |  |  | 	ifs, err := net.Interfaces() | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-09 21:57:54 +08:00
										 |  |  | 	for _, interf := range ifs { | 
					
						
							|  |  |  | 		addrs, err := interf.Addrs() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2021-08-09 21:57:54 +08:00
										 |  |  | 		if runtime.GOOS == "windows" && interf.Flags&net.FlagUp == 0 { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for _, addr := range addrs { | 
					
						
							|  |  |  | 			var ip net.IP | 
					
						
							|  |  |  | 			switch v := addr.(type) { | 
					
						
							|  |  |  | 			case *net.IPNet: | 
					
						
							|  |  |  | 				ip = v.IP | 
					
						
							|  |  |  | 			case *net.IPAddr: | 
					
						
							|  |  |  | 				ip = v.IP | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 05:42:28 +08:00
										 |  |  | 			ipList = append(ipList, ip) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ipList | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 05:42:28 +08:00
										 |  |  | // mustGetLocalIP4 returns IPv4 addresses of localhost.  It panics on error.
 | 
					
						
							|  |  |  | func mustGetLocalIP4() (ipList set.StringSet) { | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	ipList = set.NewStringSet() | 
					
						
							| 
									
										
										
										
											2022-10-22 05:42:28 +08:00
										 |  |  | 	for _, ip := range mustGetLocalIPs() { | 
					
						
							|  |  |  | 		if ip.To4() != nil { | 
					
						
							|  |  |  | 			ipList.Add(ip.String()) | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2022-10-22 05:42:28 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 05:42:28 +08:00
										 |  |  | // mustGetLocalIP6 returns IPv6 addresses of localhost.  It panics on error.
 | 
					
						
							|  |  |  | func mustGetLocalIP6() (ipList set.StringSet) { | 
					
						
							|  |  |  | 	ipList = set.NewStringSet() | 
					
						
							|  |  |  | 	for _, ip := range mustGetLocalIPs() { | 
					
						
							| 
									
										
										
										
											2018-12-19 00:26:30 +08:00
										 |  |  | 		if ip.To4() == nil { | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 			ipList.Add(ip.String()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2022-10-22 05:42:28 +08:00
										 |  |  | 	return | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // getHostIP returns IP address of given host.
 | 
					
						
							|  |  |  | func getHostIP(host string) (ipList set.StringSet, err error) { | 
					
						
							| 
									
										
										
										
											2022-01-21 05:03:15 +08:00
										 |  |  | 	addrs, err := globalDNSCache.LookupHost(GlobalContext, host) | 
					
						
							|  |  |  | 	if 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() | 
					
						
							| 
									
										
										
										
											2022-01-21 05:03:15 +08:00
										 |  |  | 	for _, addr := range addrs { | 
					
						
							|  |  |  | 		ipList.Add(addr) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ipList, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-13 00:22:35 +08:00
										 |  |  | // 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) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-25 04:28:18 +08:00
										 |  |  | 	sort.Slice(ipV4s, func(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.
 | 
					
						
							|  |  |  | 		if ipV4s[i].IsLoopback() { | 
					
						
							|  |  |  | 			return false | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if ipV4s[j].IsLoopback() { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return []byte(ipV4s[i].To4())[3] > []byte(ipV4s[j].To4())[3] | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2017-04-13 00:22:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var ips []string | 
					
						
							|  |  |  | 	for _, ip := range ipV4s { | 
					
						
							|  |  |  | 		ips = append(ips, ip.String()) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return append(nonIPs, ips...) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-18 11:27:04 +08:00
										 |  |  | func getConsoleEndpoints() (consoleEndpoints []string) { | 
					
						
							| 
									
										
										
										
											2021-07-13 06:21:07 +08:00
										 |  |  | 	if globalBrowserRedirectURL != nil { | 
					
						
							|  |  |  | 		return []string{globalBrowserRedirectURL.String()} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-06-18 11:27:04 +08:00
										 |  |  | 	var ipList []string | 
					
						
							|  |  |  | 	if globalMinioConsoleHost == "" { | 
					
						
							|  |  |  | 		ipList = sortIPs(mustGetLocalIP4().ToSlice()) | 
					
						
							|  |  |  | 		ipList = append(ipList, mustGetLocalIP6().ToSlice()...) | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ipList = []string{globalMinioConsoleHost} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, ip := range ipList { | 
					
						
							|  |  |  | 		endpoint := fmt.Sprintf("%s://%s", getURLScheme(globalIsTLS), net.JoinHostPort(ip, globalMinioConsolePort)) | 
					
						
							|  |  |  | 		consoleEndpoints = append(consoleEndpoints, endpoint) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return consoleEndpoints | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | func getAPIEndpoints() (apiEndpoints []string) { | 
					
						
							| 
									
										
										
										
											2021-08-03 12:50:20 +08:00
										 |  |  | 	if globalMinioEndpoint != "" { | 
					
						
							|  |  |  | 		return []string{globalMinioEndpoint} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	var ipList []string | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	if globalMinioHost == "" { | 
					
						
							| 
									
										
										
										
											2019-12-20 05:45:56 +08:00
										 |  |  | 		ipList = sortIPs(mustGetLocalIP4().ToSlice()) | 
					
						
							|  |  |  | 		ipList = append(ipList, mustGetLocalIP6().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 { | 
					
						
							| 
									
										
										
										
											2020-12-22 13:42:38 +08:00
										 |  |  | 		endpoint := fmt.Sprintf("%s://%s", getURLScheme(globalIsTLS), net.JoinHostPort(ip, globalMinioPort)) | 
					
						
							| 
									
										
										
										
											2019-11-20 09:42:27 +08:00
										 |  |  | 		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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2019-11-27 03:42:10 +08:00
										 |  |  | func isLocalHost(host string, port string, localPort 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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 00:40:20 +08:00
										 |  |  | 	nonInterIPV4s := mustGetLocalIP4().Intersection(hostIPs) | 
					
						
							|  |  |  | 	if nonInterIPV4s.IsEmpty() { | 
					
						
							|  |  |  | 		hostIPs = hostIPs.ApplyFunc(func(ip string) string { | 
					
						
							|  |  |  | 			if net.ParseIP(ip).IsLoopback() { | 
					
						
							|  |  |  | 				// Any loopback IP which is not 127.0.0.1
 | 
					
						
							|  |  |  | 				// convert it to check for intersections.
 | 
					
						
							|  |  |  | 				return "127.0.0.1" | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return ip | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 		nonInterIPV4s = mustGetLocalIP4().Intersection(hostIPs) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	nonInterIPV6s := mustGetLocalIP6().Intersection(hostIPs) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	// If intersection of two IP sets is not empty, then the host is localhost.
 | 
					
						
							| 
									
										
										
										
											2020-04-08 00:40:20 +08:00
										 |  |  | 	isLocalv4 := !nonInterIPV4s.IsEmpty() | 
					
						
							|  |  |  | 	isLocalv6 := !nonInterIPV6s.IsEmpty() | 
					
						
							| 
									
										
										
										
											2019-11-27 03:42:10 +08:00
										 |  |  | 	if port != "" { | 
					
						
							|  |  |  | 		return (isLocalv4 || isLocalv6) && (port == localPort), nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-12-14 15:37:46 +08:00
										 |  |  | 	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:
 | 
					
						
							| 
									
										
										
										
											2022-08-27 03:52:29 +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 | 
					
						
							| 
									
										
										
										
											2021-11-17 01:28:29 +08:00
										 |  |  | 	} else if addr1Local, err = isLocalHost(host1, port1, port1); err != nil { | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 		// Host not empty, check if it is local
 | 
					
						
							| 
									
										
										
										
											2021-11-17 01:28:29 +08:00
										 |  |  | 		return false, err | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if host2 == "" { | 
					
						
							|  |  |  | 		// If empty host means it is localhost
 | 
					
						
							|  |  |  | 		addr2Local = true | 
					
						
							| 
									
										
										
										
											2021-11-17 01:28:29 +08:00
										 |  |  | 	} else if addr2Local, err = isLocalHost(host2, port2, port2); err != nil { | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 		// Host not empty, check if it is local
 | 
					
						
							| 
									
										
										
										
											2021-11-17 01:28:29 +08:00
										 |  |  | 		return false, err | 
					
						
							| 
									
										
										
										
											2017-05-17 12:13:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 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 { | 
					
						
							| 
									
										
										
										
											2019-11-27 03:42:10 +08:00
										 |  |  | 	host, err := xnet.ParseHost(serverAddr) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2019-10-05 01:35:33 +08:00
										 |  |  | 		return config.ErrInvalidAddressFlag(err) | 
					
						
							| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2019-11-27 03:42:10 +08:00
										 |  |  | 	if host.Name != "" && host.Name != net.IPv4zero.String() && host.Name != net.IPv6zero.String() { | 
					
						
							|  |  |  | 		localHost, err := isLocalHost(host.Name, host.Port.String(), host.Port.String()) | 
					
						
							| 
									
										
										
										
											2017-04-12 06:44:27 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-27 03:42:10 +08:00
										 |  |  | 		if !localHost { | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							|  |  |  | } |