From 42282e2001609e7ba150274b6293480ab8856b29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 22 Nov 2021 17:35:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96=E8=BF=9C?= =?UTF-8?q?=E7=AB=AFIP=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/ip.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/util/ip.go b/util/ip.go index ef534be..9ae7966 100644 --- a/util/ip.go +++ b/util/ip.go @@ -7,7 +7,10 @@ // Date : 2021-03-09 5:56 下午 package util -import "net" +import ( + "net" + "net/http" +) // GetHostIP 获取本机IP地址 // @@ -27,3 +30,24 @@ func GetHostIP() string { } return hostIP } + +// GetRemoteIp 获取远端IP +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 5:35 下午 2021/11/22 +func GetRemoteIp(req *http.Request) string { + + // Try via request + + ip, _, err := net.SplitHostPort(req.RemoteAddr) + + if err != nil { + return "::1" + } + userIP := net.ParseIP(ip) + if userIP == nil { + return "::1" + } + return userIP.String() +}