Google App Engine Golang – 如何获取用户的IP地址?

huangapple go评论72阅读模式
英文:

Google App Engine Golang - how to get user's IP address?

问题

我想将ReCAPTCHA集成到我的GAE Golang Web应用程序中。为了验证验证码,我需要获取用户的IP地址。如何从表单提交中获取用户的IP地址?

英文:

I want to integrate ReCAPTCHA to my GAE Golang web application. In order to verify a captcha, I need to get user's IP address. How can I fetch user's IP address from a form post?

答案1

得分: 49

使用net.SplitHostPort

ip, _, _ := net.SplitHostPort(r.RemoteAddr)
英文:

Use net.SplitHostPort:

ip, _, _ := net.SplitHostPort(r.RemoteAddr)

答案2

得分: 26

在你的处理函数中调用r.RemoteAddr来获取ip:port,像这样:

func renderIndexPage(w http.ResponseWriter, r *http.Request) {
  ip := strings.Split(r.RemoteAddr,":")[0] 
  
}

更新于02/15/2017
正如@Aigars Matulis所指出的,在当前版本中已经有一个函数可以做到这一点

ip, _, _ := net.SplitHostPort(r.RemoteAddr)
英文:

inside your handler function call r.RemoteAddr to receive ip:port

like this:

func renderIndexPage(w http.ResponseWriter, r *http.Request) {
  ip := strings.Split(r.RemoteAddr,":")[0] 
  
}

update 02/15/2017
as @Aigars Matulis pointed out, in current version there is already a function todo this

ip, _, _ := net.SplitHostPort(r.RemoteAddr)

答案3

得分: 26

上面的答案忽略了检查用户的IP是否由代理转发。在很多情况下,你在RemoteAddr中找到的IP是一个代理转发用户请求给你的代理的IP地址,而不是用户的IP地址!

一个更准确的解决方案应该像这样:

package main

import (
	"net"
	"net/http"
)

func GetIP(r *http.Request) string {
	if ipProxy := r.Header.Get("X-FORWARDED-FOR"); len(ipProxy) > 0 {
		return ipProxy
	}
	ip, _, _ := net.SplitHostPort(r.RemoteAddr)
	return ip
}
英文:

The answers above neglect to check if user's IP is forwarded by a proxy. In a lot of cases, the IP that you will find in the RemoteAddr is the IP of a proxy that is forwarding the user's request to you - not the user's IP address!

A more accurate solution would look like this:

package main

import (
	"net"
	"net/http"
)

func GetIP(r *http.Request) string {
	if ipProxy := r.Header.Get("X-FORWARDED-FOR"); len(ipProxy) > 0 {
		return ipProxy
	}
	ip, _, _ := net.SplitHostPort(r.RemoteAddr)
	return ip
}

答案4

得分: 0

r: &{Method:POST URL:/email Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.11 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.11] Accept-Language:[en-us] Accept-Encoding:[gzip, deflate] Connection:[keep-alive] Accept:[/] Referer:[http://127.0.0.1:8080/] Content-Length:[9] Content-Type:[application/x-www-form-urlencoded; charset=UTF-8] Origin:[http://127.0.0.1:8080]] Body:0xc420012800 ContentLength:9 TransferEncoding:[] Close:false Host:127.0.0.1:8081 Form:map[] PostForm:map[] MultipartForm: Trailer:map[] RemoteAddr:127.0.0.1:62232 RequestURI:/email TLS: Cancel: Response: ctx:0xc420017860}
ip := r.Referer()
fmt.Println(ip)

英文:

This worked for me. I run go in 8081 and made a request from port 8080.

fmt.Printf("r: %+v\n", r) // Print all fields that you get in request

Output:

> r: &{Method:POST URL:/email Proto:HTTP/1.1 ProtoMajor:1 ProtoMinor:1 Header:map[User-Agent:[Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.11 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.11] Accept-Language:[en-us] Accept-Encoding:[gzip, deflate] Connection:[keep-alive] Accept:[/] Referer:[http://127.0.0.1:8080/] Content-Length:[9] Content-Type:[application/x-www-form-urlencoded; charset=UTF-8] Origin:[http://127.0.0.1:8080]] Body:0xc420012800 ContentLength:9 TransferEncoding:[] Close:false Host:127.0.0.1:8081 Form:map[] PostForm:map[] MultipartForm:<nil> Trailer:map[] RemoteAddr:127.0.0.1:62232 RequestURI:/email TLS:<nil> Cancel:<nil> Response:<nil> ctx:0xc420017860}

The Referer and Origin have my client IP.

ip := r.Referer() // Get Referer value
fmt.Println(ip) // print ip

Output:

> http://127.0.0.1:8080/

huangapple
  • 本文由 发表于 2013年6月21日 03:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/17222021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定