Home / Function/ ClientIP() — gin Function Reference

ClientIP() — gin Function Reference

Architecture documentation for the ClientIP() function in context.go from the gin codebase.

Entity Profile

Dependency Diagram

graph TD
  016d869c_1ec8_be53_c826_1bf4397776e5["ClientIP()"]
  8e2593fb_827b_15b4_e0ed_5b628a19e625["requestHeader()"]
  016d869c_1ec8_be53_c826_1bf4397776e5 -->|calls| 8e2593fb_827b_15b4_e0ed_5b628a19e625
  a4eb6295_848c_5468_4e03_d8442b59ed6c["Value()"]
  016d869c_1ec8_be53_c826_1bf4397776e5 -->|calls| a4eb6295_848c_5468_4e03_d8442b59ed6c
  01891b46_e452_3c97_22a6_476eca4e34a6["RemoteIP()"]
  016d869c_1ec8_be53_c826_1bf4397776e5 -->|calls| 01891b46_e452_3c97_22a6_476eca4e34a6
  778fced8_99f9_7da2_cb60_f438189040cf["String()"]
  016d869c_1ec8_be53_c826_1bf4397776e5 -->|calls| 778fced8_99f9_7da2_cb60_f438189040cf
  style 016d869c_1ec8_be53_c826_1bf4397776e5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

context.go lines 975–1024

func (c *Context) ClientIP() string {
	// Check if we're running on a trusted platform, continue running backwards if error
	if c.engine.TrustedPlatform != "" {
		// Developers can define their own header of Trusted Platform or use predefined constants
		if addr := c.requestHeader(c.engine.TrustedPlatform); addr != "" {
			return addr
		}
	}

	// Legacy "AppEngine" flag
	if c.engine.AppEngine {
		log.Println(`The AppEngine flag is going to be deprecated. Please check issues #2723 and #2739 and use 'TrustedPlatform: gin.PlatformGoogleAppEngine' instead.`)
		if addr := c.requestHeader("X-Appengine-Remote-Addr"); addr != "" {
			return addr
		}
	}

	var (
		trusted  bool
		remoteIP net.IP
	)
	// If gin is listening a unix socket, always trust it.
	localAddr, ok := c.Request.Context().Value(http.LocalAddrContextKey).(net.Addr)
	if ok && strings.HasPrefix(localAddr.Network(), "unix") {
		trusted = true
	}

	// Fallback
	if !trusted {
		// It also checks if the remoteIP is a trusted proxy or not.
		// In order to perform this validation, it will see if the IP is contained within at least one of the CIDR blocks
		// defined by Engine.SetTrustedProxies()
		remoteIP = net.ParseIP(c.RemoteIP())
		if remoteIP == nil {
			return ""
		}
		trusted = c.engine.isTrustedProxy(remoteIP)
	}

	if trusted && c.engine.ForwardedByClientIP && c.engine.RemoteIPHeaders != nil {
		for _, headerName := range c.engine.RemoteIPHeaders {
			headerValue := strings.Join(c.Request.Header.Values(headerName), ",")
			ip, valid := c.engine.validateHeader(headerValue)
			if valid {
				return ip
			}
		}
	}
	return remoteIP.String()
}

Domain

Subdomains

Frequently Asked Questions

What does ClientIP() do?
ClientIP() is a function in the gin codebase.
What does ClientIP() call?
ClientIP() calls 4 function(s): RemoteIP, String, Value, requestHeader.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free