当前位置: 首页>>代码示例>>Golang>>正文


Golang IP.IsGlobalUnicast方法代码示例

本文整理汇总了Golang中net.IP.IsGlobalUnicast方法的典型用法代码示例。如果您正苦于以下问题:Golang IP.IsGlobalUnicast方法的具体用法?Golang IP.IsGlobalUnicast怎么用?Golang IP.IsGlobalUnicast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net.IP的用法示例。


在下文中一共展示了IP.IsGlobalUnicast方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: routableIP

func routableIP(network string, ip net.IP) net.IP {
	if !ip.IsLoopback() && !ip.IsLinkLocalUnicast() && !ip.IsGlobalUnicast() {
		return nil
	}
	switch network {
	case "ip4":
		if ip := ip.To4(); ip != nil {
			return ip
		}
	case "ip6":
		if ip.IsLoopback() { // addressing scope of the loopback address depends on each implementation
			return nil
		}
		if ip := ip.To16(); ip != nil && ip.To4() == nil {
			return ip
		}
	default:
		if ip := ip.To4(); ip != nil {
			return ip
		}
		if ip := ip.To16(); ip != nil {
			return ip
		}
	}
	return nil
}
开发者ID:Celluliodio,项目名称:flannel,代码行数:26,代码来源:interface.go

示例2: IsGlobalIP

// IsGlobalIP determs passed ip address is global or not.
// if ip address is global , it returns address type(ip4 or ip6).
func IsGlobalIP(trial net.IP) string {
	type localIPrange struct {
		from net.IP
		to   net.IP
	}
	locals := []localIPrange{
		localIPrange{net.ParseIP("10.0.0.0"), net.ParseIP("10.255.255.255")},
		localIPrange{net.ParseIP("172.16.0.0"), net.ParseIP("172.31.255.255")},
		localIPrange{net.ParseIP("192.168.0.0"), net.ParseIP("192.168.255.255")}}

	if trial == nil || trial.IsLoopback() {
		return ""
	}
	//for udp6
	if trial.To4() == nil {
		if trial.IsGlobalUnicast() {
			return "ip6"
		}
		return ""
	}
	//for udp4
	for _, r := range locals {
		if bytes.Compare(trial, r.from) >= 0 && bytes.Compare(trial, r.to) <= 0 {
			return ""
		}
	}
	return "ip4"
}
开发者ID:shingetsu-gou,项目名称:go-nat,代码行数:30,代码来源:netstatus.go

示例3: ListenAddresses

func (d *Daemon) ListenAddresses() ([]string, error) {
	addresses := make([]string, 0)

	value, err := d.ConfigValueGet("core.https_address")
	if err != nil || value == "" {
		return addresses, err
	}

	localHost, localPort, err := net.SplitHostPort(value)
	if err != nil {
		localHost = value
		localPort = shared.DefaultPort
	}

	if localHost == "0.0.0.0" || localHost == "::" || localHost == "[::]" {
		ifaces, err := net.Interfaces()
		if err != nil {
			return addresses, err
		}

		for _, i := range ifaces {
			addrs, err := i.Addrs()
			if err != nil {
				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
				}

				if !ip.IsGlobalUnicast() {
					continue
				}

				if ip.To4() == nil {
					if localHost == "0.0.0.0" {
						continue
					}
					addresses = append(addresses, fmt.Sprintf("[%s]:%s", ip, localPort))
				} else {
					addresses = append(addresses, fmt.Sprintf("%s:%s", ip, localPort))
				}
			}
		}
	} else {
		if strings.Contains(localHost, ":") {
			addresses = append(addresses, fmt.Sprintf("[%s]:%s", localHost, localPort))
		} else {
			addresses = append(addresses, fmt.Sprintf("%s:%s", localHost, localPort))
		}
	}

	return addresses, nil
}
开发者ID:jsavikko,项目名称:lxd,代码行数:59,代码来源:daemon.go

示例4: isPublicIPv6

func isPublicIPv6(ip net.IP) bool {
	if ip.To4() != nil {
		// Not an IPv6 address (IPv4)
		// (To16() returns a v6 mapped v4 address so can't be used to check
		// that it's an actual v6 address)
		return false
	}

	return ip.IsGlobalUnicast()
}
开发者ID:wmwwmv,项目名称:syncthing,代码行数:10,代码来源:addresslister.go

示例5: IsPublicIP

// IsPublicIP returns true if the given IP can be routed on the Internet
func IsPublicIP(ip net.IP) bool {
	if !ip.IsGlobalUnicast() {
		return false
	}
	for _, mask := range privateMasks {
		if mask.Contains(ip) {
			return false
		}
	}
	return true
}
开发者ID:mapix,项目名称:xff,代码行数:12,代码来源:xff.go

示例6: isBadIPv4

func isBadIPv4(ip net.IP) bool {
	if ip.To4() == nil {
		panic("cannot be called for IPv6")
	}

	if ip.Equal(net.IPv4bcast) || !ip.IsGlobalUnicast() ||
		netPrivateClassA.Contains(ip) || netPrivateClassB.Contains(ip) || netPrivateClassC.Contains(ip) ||
		netTestNet.Contains(ip) || net6To4Relay.Contains(ip) {
		return true
	}

	return false
}
开发者ID:hakobe,项目名称:paranoidhttp,代码行数:13,代码来源:client.go

示例7: getClientConnectURLs

// getClientConnectURLs returns suitable URLs for clients to connect to the listen
// port based on the server options' Host and Port. If the Host corresponds to
// "any" interfaces, this call returns the list of resolved IP addresses.
func (s *Server) getClientConnectURLs() []string {
	s.mu.Lock()
	defer s.mu.Unlock()

	sPort := strconv.Itoa(s.opts.Port)
	urls := make([]string, 0, 1)

	ipAddr, err := net.ResolveIPAddr("ip", s.opts.Host)
	// If the host is "any" (0.0.0.0 or ::), get specific IPs from available
	// interfaces.
	if err == nil && ipAddr.IP.IsUnspecified() {
		var ip net.IP
		ifaces, _ := net.Interfaces()
		for _, i := range ifaces {
			addrs, _ := i.Addrs()
			for _, addr := range addrs {
				switch v := addr.(type) {
				case *net.IPNet:
					ip = v.IP
				case *net.IPAddr:
					ip = v.IP
				}
				// Skip non global unicast addresses
				if !ip.IsGlobalUnicast() || ip.IsUnspecified() {
					ip = nil
					continue
				}
				urls = append(urls, net.JoinHostPort(ip.String(), sPort))
			}
		}
	}
	if err != nil || len(urls) == 0 {
		// We are here if s.opts.Host is not "0.0.0.0" nor "::", or if for some
		// reason we could not add any URL in the loop above.
		// We had a case where a Windows VM was hosed and would have err == nil
		// and not add any address in the array in the loop above, and we
		// ended-up returning 0.0.0.0, which is problematic for Windows clients.
		// Check for 0.0.0.0 or :: specifically, and ignore if that's the case.
		if s.opts.Host == "0.0.0.0" || s.opts.Host == "::" {
			Errorf("Address %q can not be resolved properly", s.opts.Host)
		} else {
			urls = append(urls, net.JoinHostPort(s.opts.Host, sPort))
		}
	}
	return urls
}
开发者ID:apcera,项目名称:sample-apps,代码行数:49,代码来源:server.go

示例8: isBlacklistedIP

func (p *Parser) isBlacklistedIP(addr net.IP) bool {
	// if whitelisted then return false
	for _, w := range p.WhitelistedIPNetworks {
		if w.Contains(addr) {
			return false
		}
	}

	// if blacklisted then return true
	for _, b := range p.BlacklistedIPNetworks {
		if b.Contains(addr) {
			return true
		}
	}

	// by default we disable local addresses and bradcast ones
	return !addr.IsGlobalUnicast()
}
开发者ID:dyatlov,项目名称:go-url2oembed,代码行数:18,代码来源:parser.go

示例9: getLocalAddresses

func getLocalAddresses() ([]net.IP, error) {
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return nil, err
	}

	privates := make([]net.IPNet, 0, len(privateRanges))
	for _, s := range privateRanges {
		_, ipnet, err := net.ParseCIDR(s)
		if err != nil {
			panic(err)
		}
		privates = append(privates, *ipnet)
	}

	result := make([]net.IP, 0, 5)

addrLoop:
	for _, a := range addrs {
		var ip net.IP

		if ipaddr, ok := a.(*net.IPAddr); ok {
			ip = ipaddr.IP
		}

		if ipnet, ok := a.(*net.IPNet); ok {
			ip = ipnet.IP
		}

		if !ip.IsGlobalUnicast() {
			continue
		}

		for _, n := range privates {
			if n.Contains(ip) {
				continue addrLoop
			}
		}

		result = append(result, ip)
	}

	return result, nil
}
开发者ID:justinian,项目名称:dodyndns,代码行数:44,代码来源:address.go

示例10: isPublicIPv4

func isPublicIPv4(ip net.IP) bool {
	ip = ip.To4()
	if ip == nil {
		// Not an IPv4 address (IPv6)
		return false
	}

	// IsGlobalUnicast below only checks that it's not link local or
	// multicast, and we want to exclude private (NAT:ed) addresses as well.
	rfc1918 := []net.IPNet{
		{IP: net.IP{10, 0, 0, 0}, Mask: net.IPMask{255, 0, 0, 0}},
		{IP: net.IP{172, 16, 0, 0}, Mask: net.IPMask{255, 240, 0, 0}},
		{IP: net.IP{192, 168, 0, 0}, Mask: net.IPMask{255, 255, 0, 0}},
	}
	for _, n := range rfc1918 {
		if n.Contains(ip) {
			return false
		}
	}

	return ip.IsGlobalUnicast()
}
开发者ID:wmwwmv,项目名称:syncthing,代码行数:22,代码来源:addresslister.go

示例11: isGlobalIP

func isGlobalIP(ip net.IP) bool {
	if ip == nil {
		return false
	}
	if !ip.IsGlobalUnicast() {
		return false
	}
	if ip4 := ip.To4(); ip4 != nil {
		if ip4[0] == 10 {
			return false
		}
		if ip4[0] == 192 && ip4[1] == 168 {
			return false
		}
		if ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31 {
			return false
		}
	}
	// TODO(tav): Exclude unique local addresses within the block fc00::/7 for
	// IPv6 addresses.
	return true
}
开发者ID:tav,项目名称:oldproto,代码行数:22,代码来源:http.go

示例12: ConnectionAllowed

func (self *defaultRuler) ConnectionAllowed(requestee, requested net.IP) RulerResult {
	if !requested.IsGlobalUnicast() {
		return DenyConnection
	}
	addrs, err := net.InterfaceAddrs()
	if err != nil {
		return DenyConnection
	}
	for _, addr := range addrs {
		switch ipa := addr.(type) {
		case *net.IPAddr:
			if ipa.IP.Equal(requested) {
				return DenyConnection
			}
		case *net.IPNet:
			if ipa.Contains(requested) {
				return DenyConnection
			}
		}
	}
	return AllowConnection
}
开发者ID:nmaier,项目名称:gosocksv5d,代码行数:22,代码来源:ruler.go

示例13: routableIP

func routableIP(network string, ip net.IP) net.IP {
	if !ip.IsLoopback() && !ip.IsLinkLocalUnicast() && !ip.IsGlobalUnicast() {
		return nil
	}
	switch network {
	case "ip4":
		if ip := ip.To4(); ip != nil {
			return ip
		}
	case "ip6":
		if ip := ip.To16(); ip != nil && ip.To4() == nil {
			return ip
		}
	default:
		if ip := ip.To4(); ip != nil {
			return ip
		}
		if ip := ip.To16(); ip != nil {
			return ip
		}
	}
	return nil
}
开发者ID:XavierBerger,项目名称:go.net,代码行数:23,代码来源:interface.go

示例14: usableAddress

func usableAddress(ip net.IP) bool {
	return ip.To4() != nil && ip.IsGlobalUnicast()
}
开发者ID:ericcapricorn,项目名称:deis,代码行数:3,代码来源:coreos.go

示例15: isUnicast

func isUnicast(ip net.IP) bool {
	return ip.To4() != nil && (ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast())
}
开发者ID:NicholeGit,项目名称:go,代码行数:3,代码来源:mocktransponder_test.go


注:本文中的net.IP.IsGlobalUnicast方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。