From 68ee275d92f3597f87c5d22db70fe5e180b4fc5f Mon Sep 17 00:00:00 2001 From: steden <1470804@qq.com> Date: Wed, 1 Jan 2025 18:43:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96GetRealIp?= =?UTF-8?q?=20=E8=8E=B7=E5=8F=96=E7=9C=9F=E5=AE=9E=E7=9A=84ip=E5=9C=B0?= =?UTF-8?q?=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- serveMux.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/serveMux.go b/serveMux.go index a455efd..6cd0dc0 100644 --- a/serveMux.go +++ b/serveMux.go @@ -1,11 +1,6 @@ package webapi import ( - "github.com/farseer-go/collections" - "github.com/farseer-go/fs/flog" - "github.com/farseer-go/webapi/context" - "github.com/farseer-go/webapi/middleware" - "github.com/farseer-go/webapi/websocket" "net" "net/http" "net/url" @@ -13,6 +8,12 @@ import ( "sort" "strings" "sync" + + "github.com/farseer-go/collections" + "github.com/farseer-go/fs/flog" + "github.com/farseer-go/webapi/context" + "github.com/farseer-go/webapi/middleware" + "github.com/farseer-go/webapi/websocket" ) type serveMux struct { @@ -303,3 +304,12 @@ func appendSorted(es []*context.HttpRoute, e *context.HttpRoute) []*context.Http func GetHttpContext() *context.HttpContext { return context.RoutineHttpContext.Get() } + +// GetRealIp 获取真实的ip地址 +func GetRealIp() string { + httpCtx := GetHttpContext() + if httpCtx == nil { + return "127.0.0.1" + } + return httpCtx.URI.GetRealIp() +}