英文:
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
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:
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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论