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


Golang net.Contains函数代码示例

本文整理汇总了Golang中net.Contains函数的典型用法代码示例。如果您正苦于以下问题:Golang Contains函数的具体用法?Golang Contains怎么用?Golang Contains使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: isInAddressRange

// Helper function, checks if the given IP is within one of the reserved IP
// address ranges. See ipv4NetFiltered and ipv6NetFiltered variable
// initialization in init().
func isInAddressRange(ip net.IP, ipv4ranges, ipv6ranges []*net.IPNet) bool {
	if len(ip) == net.IPv4len {
		for _, net := range ipv4ranges {
			if net.Contains(ip) {
				return true
			}
		}
	} else if len(ip) == net.IPv6len {
		for _, net := range ipv6ranges {
			if net.Contains(ip) {
				return true
			}
		}
	}
	return false
}
开发者ID:HolmesProcessing,项目名称:Holmes-Totem-Service-Library,代码行数:19,代码来源:validation.go

示例2: lookup

func lookup(stmt *sql.Stmt, IP net.IP, nIP uint32) (*GeoIP, error) {
	var reserved bool
	for _, net := range reservedIPs {
		if net.Contains(IP) {
			reserved = true
			break
		}
	}
	geoip := GeoIP{Ip: IP.String()}
	if reserved {
		geoip.CountryCode = "RD"
		geoip.CountryName = "Reserved"
	} else {
		if err := stmt.QueryRow(nIP).Scan(
			&geoip.CountryCode,
			&geoip.CountryName,
			&geoip.RegionCode,
			&geoip.RegionName,
			&geoip.CityName,
			&geoip.ZipCode,
			&geoip.Latitude,
			&geoip.Longitude,
			&geoip.MetroCode,
			&geoip.AreaCode,
		); err != nil {
			return nil, err
		}
	}
	return &geoip, nil
}
开发者ID:jrumbut,项目名称:freegeoip,代码行数:30,代码来源:freegeoip.go

示例3: exportReverse

func exportReverse(db *database.DB, domain *database.Domain) (string, error) {
	_, net, err := net.ParseCIDR(domain.Name)
	if err != nil {
		panic("Export reverse on a non-CIDR")
	}
	if ones, _ := net.Mask.Size(); ones%8 != 0 {
		return "", fmt.Errorf("Reverse zone CIDR must be 8-bit aligned, cannot generate zone for %s", net)
	}

	ret := []string{
		fmt.Sprintf("$ORIGIN %s", arpaZone(net)),
		"$TTL 600",
		domain.SOA(),
		"",
	}

	for _, host := range db.Hosts {
		for _, addr := range sortedAddrs(host) {
			if !net.Contains(addr) {
				continue
			}

			if fqdn := host.Attrs["fqdn"]; fqdn != "" {
				ret = append(ret, fmt.Sprintf("%s IN PTR %s.", arpaHost(net, addr), fqdn))
			} else if hostname := host.Attrs["hostname"]; hostname != "" {
				if domain := ipDomain(host, addr); domain != "" {
					ret = append(ret, fmt.Sprintf("%s IN PTR %s.%s.", arpaHost(net, addr), hostname, domain))
				}
			}
		}
	}

	return strings.Join(ret, "\n"), nil
}
开发者ID:postfix,项目名称:gipam,代码行数:34,代码来源:reverse.go

示例4: dialerForRequest

func (p *PerHost) dialerForRequest(host string) Dialer {
	if ip := net.ParseIP(host); ip != nil {
		for _, net := range p.bypassNetworks {
			if net.Contains(ip) {
				return p.bypass
			}
		}
		for _, bypassIP := range p.bypassIPs {
			if bypassIP.Equal(ip) {
				return p.bypass
			}
		}
		return p.def
	}

	for _, zone := range p.bypassZones {
		if strings.HasSuffix(host, zone) {
			return p.bypass
		}
		if host == zone[1:] {
			// For a zone "example.com", we match "example.com"
			// too.
			return p.bypass
		}
	}
	for _, bypassHost := range p.bypassHosts {
		if bypassHost == host {
			return p.bypass
		}
	}
	return p.def
}
开发者ID:RajibTheKing,项目名称:gcc,代码行数:32,代码来源:per_host.go

示例5: ServeHTTP

func (h *handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
	tmp := strings.Split(r.RemoteAddr, ":")
	ip := net.ParseIP(tmp[0])

	if ip.String() == "127.0.0.1" {
		// allow the proxy header to be used instead..
		forwarded_for := r.Header.Get("X-Forwarded-For")
		if forwarded_for != "" {
			log.Infof("TODO: Forwarded for %s", forwarded_for)
		}

	}

	allowed := false
	for _, net := range h.nets {
		if net.Contains(ip) {
			allowed = true
			break
		}
	}
	// fmt.Printf("%s access=%v\n", tmp[0], allowed)
	if allowed == false {
		rw.WriteHeader(http.StatusForbidden)
		fmt.Fprintf(rw, "Forbidden")
		return
	}
	h.Handler.ServeHTTP(rw, r)
}
开发者ID:sigmonsays,项目名称:voyager,代码行数:28,代码来源:acl.go

示例6: GeoipLookup

func GeoipLookup(stmt *sql.Stmt, ip string) (*GeoIP, error) {
	IP := net.ParseIP(ip)
	reserved := false
	for _, net := range reservedIPs {
		if net.Contains(IP) {
			reserved = true
			break
		}
	}
	geoip := GeoIP{Ip: ip}
	if reserved {
		geoip.CountryCode = "RD"
		geoip.CountryName = "Reserved"
	} else {
		var uintIP uint32
		b := bytes.NewBuffer(IP.To4())
		binary.Read(b, binary.BigEndian, &uintIP)
		if err := stmt.QueryRow(uintIP).Scan(
			&geoip.CountryCode,
			&geoip.CountryName,
			&geoip.RegionCode,
			&geoip.RegionName,
			&geoip.CityName,
			&geoip.ZipCode,
			&geoip.Latitude,
			&geoip.Longitude,
			&geoip.MetroCode,
			&geoip.AreaCode,
		); err != nil {
			return nil, err
		}
	}
	return &geoip, nil
}
开发者ID:hatahet,项目名称:freegeoip,代码行数:34,代码来源:freegeoip.go

示例7: handleProxy

// handleProxy /p/fileid=asdf;token=asdf;gid=123;page=321;passkey=asdf/filename
func (s *DefaultServer) handleProxy(c *gin.Context) {
	mode := s.cfg.Settings.ProxyMode
	if mode == ProxyDisabled {
		c.String(http.StatusForbidden, "proxy disabled")
		return
	}
	args := ParseArgs(c.Param("kwds"))
	fileID := args.Get("fileid")
	f, err := FileFromID(fileID)
	if err != nil {
		log.Println("proxy:", "bad file id", fileID, err)
		c.String(http.StatusBadRequest, "400: bad file id")
		return
	}
	galleryID := args.GetInt("gid")
	page := args.GetInt("page")
	passkey := args.Get("passkey")
	token := args.Get("token")
	filename := c.Param("filename")

	if s.db.Exists(f) {
		log.Println("proxy:", "file already exists; serving from cache", f)
		s.frontend.Handle(f, c.Writer)
		s.events <- Event{EventSent, f}
		return
	}

	if mode == ProxyLocalNetworksOpen || mode == ProxyLocalNetworksProtected {
		ip, err := FromRequest(c.Request)
		if err != nil {
			log.Println("proxy:", "unable to extract ip from", c.Request.RemoteAddr)
			c.String(http.StatusInternalServerError, "unable to parse ip")
			return
		}
		var isLocal bool
		for _, net := range LocalNetworks {
			if net.Contains(ip) {
				isLocal = true
				break
			}
		}
		if !isLocal {
			log.Println("proxy:", "access from non-local network by", ip, "<-", c.Request.RemoteAddr)
			c.String(http.StatusForbidden, "bad ip")
			return
		}
	}

	if mode == ProxyLocalNetworksProtected || mode == ProxyAllNetworksProtected {
		// checking passkey
		if passkey != s.proxyPasskey(f) {
			log.Println("proxy:", "bad passkey provided")
			c.String(http.StatusForbidden, "bad passkey")
			return
		}
	}

	s.proxy(c, f, token, galleryID, page, filename)
}
开发者ID:RyuaNerin,项目名称:hath,代码行数:60,代码来源:server.go

示例8: Contains

// Contains returns true if IP is in Networks.
func (n Networks) Contains(ip net.IP) bool {
	for _, net := range n {
		if net.Contains(ip) {
			return true
		}
	}
	return false
}
开发者ID:philipz,项目名称:scope,代码行数:9,代码来源:networks.go

示例9: isPrivateV4

func isPrivateV4(ip net.IP) bool {
	for _, net := range privateNetworks {
		if net.Contains(ip) {
			return true
		}
	}
	return false
}
开发者ID:joeblackwaslike,项目名称:boulder,代码行数:8,代码来源:dns.go

示例10: isPrivateIp

func isPrivateIp(ip net.IP) bool {
	for _, net := range privateIpNets {
		if net.Contains(ip) {
			return true
		}
	}
	return false
}
开发者ID:DrWrong,项目名称:monica,代码行数:8,代码来源:context.go

示例11: cidrMatches

func cidrMatches(ip net.IP, cidr string) bool {
	_, net, err := net.ParseCIDR(cidr)
	if err != nil {
		http_sheep.Baa(1, "Invalid CIDR for allowed_gre_addr in the configuration file: %s", cidr)
		return false
	}
	return net.Contains(ip)
}
开发者ID:krjoshi,项目名称:tegu,代码行数:8,代码来源:http_mirror_api.go

示例12: GeoipLookup

func GeoipLookup(db *sql.DB, ip string) (*GeoIP, error) {
	IP := net.ParseIP(ip)
	reserved := false
	for _, net := range reservedIPs {
		if net.Contains(IP) {
			reserved = true
			break
		}
	}
	geoip := GeoIP{Ip: ip}
	if reserved {
		geoip.CountryCode = "RD"
		geoip.CountryName = "Reserved"
	} else {
		q := `SELECT
		  city_location.country_code, country_blocks.country_name,
		  city_location.region_code, region_names.region_name,
		  city_location.city_name, city_location.postal_code,
		  city_location.latitude, city_location.longitude,
		  city_location.metro_code, city_location.area_code
		FROM city_blocks
		  NATURAL JOIN city_location
		  INNER JOIN country_blocks ON
		    city_location.country_code = country_blocks.country_code
		  LEFT OUTER JOIN region_names ON
		    city_location.country_code = region_names.country_code
		    AND
		    city_location.region_code = region_names.region_code
		WHERE city_blocks.ip_start <= ?
		ORDER BY city_blocks.ip_start DESC LIMIT 1`
		stmt, err := db.Prepare(q)
		if err != nil {
			if debug {
				log.Println("[debug] SQLite", err.Error())
			}
			return nil, err
		}
		defer stmt.Close()
		var uintIP uint32
		b := bytes.NewBuffer(IP.To4())
		binary.Read(b, binary.BigEndian, &uintIP)
		err = stmt.QueryRow(uintIP).Scan(
			&geoip.CountryCode,
			&geoip.CountryName,
			&geoip.RegionCode,
			&geoip.RegionName,
			&geoip.CityName,
			&geoip.ZipCode,
			&geoip.Latitude,
			&geoip.Longitude,
			&geoip.MetroCode,
			&geoip.AreaCode)
		if err != nil {
			return nil, err
		}
	}
	return &geoip, nil
}
开发者ID:Racemetric,项目名称:freegeoip,代码行数:58,代码来源:freegeoip.go

示例13: isFiltered

// Helper function, checks if the given IP is within one of the reserved IP
// address ranges. See ipv4NetFiltered and ipv6NetFiltered variable
// initialization in init().
func isFiltered(ip net.IP) bool {
	if len(ip) == net.IPv4len {
		for _, net := range ipv4NetFiltered {
			if net.Contains(ip) {
				return true
			}
		}

	} else if len(ip) == net.IPv6len {
		for _, net := range ipv6NetFiltered {
			if net.Contains(ip) {
				return true
			}
		}
	}

	return false
}
开发者ID:HolmesProcessing,项目名称:Holmes-Totem,代码行数:21,代码来源:query-object.go

示例14: ipFilter

func ipFilter(addr string, nets *[]*net.IPNet) bool {
	if ip := net.ParseIP(addr); ip != nil {
		for _, net := range *nets {
			if net.Contains(ip) {
				return true
			}
		}
	}
	return false
}
开发者ID:DeanChina,项目名称:gotty,代码行数:10,代码来源:utils.go

示例15: IsTrustedIP

// IsTrustedIP returns true if the provided IP address came from
// a trusted Nexmo server.
func IsTrustedIP(ipStr string) bool {
	ip := net.ParseIP(ipStr)

	for _, net := range subnets {
		if net.Contains(ip) {
			return true
		}
	}
	return false
}
开发者ID:kmchen,项目名称:gonexmo,代码行数:12,代码来源:ip.go


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