// IP returns the client's network address based on `X-Forwarded-For`
// or `X-Real-IP` request header.
350825
func (ctx *Context) IP(trustedProxy ...bool) net.IP {
	trusted := ctx.Setting(SetTrustedProxy).(bool)
	if len(trustedProxy) > 0 {
		trusted = trustedProxy[0]
	}

	if trusted {
		ip := ctx.Req.Header.Get(HeaderXForwardedFor)
		if ip == "" {
			ip = ctx.Req.Header.Get(HeaderXRealIP)
		} else if i := strings.IndexByte(ip, ','); i >= 0 {
			ip = ip[0:i]
		}

		if realIP := net.ParseIP(ip); realIP != nil {
			return realIP
		}
	}

	ra := ctx.Req.RemoteAddr
	ra, _, _ = net.SplitHostPort(ra)
	return net.ParseIP(ra)
}
1253482
func supportedIP(ip net.IP) net.IP {
	if supportsIPv4 {
		if v4 := ip.To4(); v4 != nil {
			return v4
		}
	}
	if supportsIPv6 && len(ip) == net.IPv6len {
		return ip
	}
	return nil
}
1225697
func GetSourceIP(r *http.Request) string {
	var addr string

	if fwd := r.Header.Get(xForwardedFor); fwd != "" {
		// Only grab the first (client) address. Note that '192.168.0.1,
		// 10.1.1.1' is a valid key for X-Forwarded-For where addresses after
		// the first may represent forwarding proxies earlier in the chain.
		s := strings.Index(fwd, ", ")
		if s == -1 {
			s = len(fwd)
		}
		addr = fwd[:s]
	} else if fwd := r.Header.Get(xRealIP); fwd != "" {
		// X-Real-IP should only contain one IP address (the client making the
		// request).
		addr = fwd
	} else if fwd := r.Header.Get(forwarded); fwd != "" {
		// match should contain at least two elements if the protocol was
		// specified in the Forwarded header. The first element will always be
		// the 'for=' capture, which we ignore. In the case of multiple IP
		// addresses (for=8.8.8.8, 8.8.4.4, 172.16.1.20 is valid) we only
		// extract the first, which should be the client IP.
		if match := forRegex.FindStringSubmatch(fwd); len(match) > 1 {
			// IPv6 addresses in Forwarded headers are quoted-strings. We strip
			// these quotes.
			addr = strings.Trim(match[1], `"`)
		}
	}

	if addr != "" {
		return addr
	}

	// Default to remote address if headers not set.
	addr, _, _ = net.SplitHostPort(r.RemoteAddr)
	return addr
}
521798
func Dec(IP net.IP) net.IP {
	IP = checkIPv4(IP)
	decIP := make([]byte, len(IP))
	copy(decIP, IP)
	decIP = checkIPv4(decIP)
	for j := len(decIP) - 1; j >= 0; j-- {
		decIP[j]--
		if decIP[j] < 255 {
			break
		}
	}
	return decIP
}
477993
func (t *Task) IP(srcs ...string) string {
	if ips := t.IPs(srcs...); len(ips) > 0 {
		return ips[0].String()
	}
	return ""
}
1313203
func (f *FlagSet) IP(name string, value net.IP, usage string) *net.IP {
	p := new(net.IP)
	f.IPVarP(p, name, "", value, usage)
	return p
}
273757
func (a AddrSpec) Address() string {
	if 0 != len(a.IP) {
		return net.JoinHostPort(a.IP.String(), strconv.Itoa(a.Port))
	}
	return net.JoinHostPort(a.FQDN, strconv.Itoa(a.Port))
}
682620
func (n *Node) IP() net.IP {
	var ip net.IP
	n.Load((*enr.IP)(&ip))
	return ip
}
1040441
func (d *Driver) GetIP() (string, error) {
	ip, err := net.ChooseBindAddress(nil)
	if err != nil {
		return "", err
	}
	return ip.String(), nil
}
1164896
func copyIP(src net.IP) net.IP {
	return net.IP(copyBytes(src))
}
529609
func IP() (string, error) {
	ips := make(chan string, 1)
	errs := make(chan error, 1)
	for _, server := range ipServers {
		server := server
		go func() {
			resp, err := http.Get(server)
			if err != nil {
				errs <- err
				return
			}
			body, _ := ioutil.ReadAll(resp.Body)
			ipAddr := strings.TrimSpace(string(body))
			ip := net.ParseIP(ipAddr)
			if ip == nil {
				errs <- fmt.Errorf("invalid ip address: %s", ipAddr)
				return
			}
			ips <- ip.String()
		}()
	}
	var errors []error
	var resps []string
	respMap := make(map[string]int)
	for (len(resps) < 2 || len(respMap) > 1) && len(resps)+len(errors) < len(ipServers) {
		select {
		case resp := <-ips:
			resps = append(resps, resp)
			respMap[resp]++
		case err := <-errs:
			errors = append(errors, err)
		}
	}
	if len(resps) == 0 {
		return "", errors[0]
	}
	// pick best ip address
	var best string
	mostVotes := 0
	for addr, votes := range respMap {
		if votes > mostVotes {
			mostVotes = votes
			best = addr
		}
	}
	return best, nil
}
778692
func RemoteIP(r *http.Request) string {
	addr := RemoteAddr(r)

	// Try parsing it as "IP:port"
	if ip, _, err := net.SplitHostPort(addr); err == nil {
		return ip
	}

	return addr
}
921463
func (s *DedicatedIp) SetIp(v string) *DedicatedIp {
	s.Ip = &v
	return s
}
221343
func (p *PerHost) AddIP(ip net.IP) {
	p.bypassIPs = append(p.bypassIPs, ip)
}
821074
func (ctx *Context) ClientIP() string {
	clientIP := strings.TrimSpace(ctx.requestHeader("X-Real-Ip"))
	if len(clientIP) > 0 {
		return clientIP
	}
	clientIP = ctx.requestHeader("X-Forwarded-For")
	if index := strings.IndexByte(clientIP, ','); index >= 0 {
		clientIP = clientIP[0:index]
	}
	clientIP = strings.TrimSpace(clientIP)
	if len(clientIP) > 0 {
		return clientIP
	}
	if ip, _, err := net.SplitHostPort(strings.TrimSpace(ctx.Request.RemoteAddr)); err == nil {
		return ip
	}
	return ""
}
810082
func (s *PutDedicatedIpInPoolInput) SetIp(v string) *PutDedicatedIpInPoolInput {
	s.Ip = &v
	return s
}
762198
func (p *parserMixin) IP() (target *net.IP) {
	target = new(net.IP)
	p.IPVar(target)
	return
}