Skip to content

IP Handler

poteto edited this page Nov 27, 2024 · 3 revisions

IP Handler

RealIP: Mostly Recommended

  1. Get from X-Forwarded-For
  2. Get from X-Real-Ip
  3. Get from RemoteAddr

How to use

  1. RegisterIPRanges
iph := &ipHandler{}
_, ipnet, _ := net.ParseCIDR("10.0.0.0/24")
iph.RegisterTrustIPRange(ipnet)
  1. GetIP
ipString, _ := iph.RealIP(ctx)

Version

It requires >=0.22.0

Get the IP of the requestor from the X-Forwarded-For header.

Each time a proxy is connected, it is given behind it. For example, if x -> a -> b -> app, x, a, b are listed.

If you want to get the first request, it seems like you should get the first x, but not because the initial value can be set freely.

Therefore, the following is obtained in order from right to left, and the unregistered ip is assumed to be the request source ip.

  1. RegisterIPRanges
iph := &ipHandler{}
_, ipnet, _ := net.ParseCIDR("10.0.0.0/24")
iph.RegisterTrustIPRange(ipnet)
  1. GetIP
ipString, _ := iph.GetIPFromXFFHeader(ctx)

version

it requires >=0.16.0

Remote IP

From RemoteAddr

func hoge(ctx poteto.Context) {
  ip, err := ctx.GetRemoteIP()
}

Clone this wiki locally