diff --git a/docs/netutil.md b/docs/netutil.md index 7ba2a60..ac1e83e 100644 --- a/docs/netutil.md +++ b/docs/netutil.md @@ -26,7 +26,9 @@ import ( - [GetIps](#GetIps) - [GetMacAddrs](#GetMacAddrs) - [GetPublicIpInfo](#GetPublicIpInfo) +- [GetRequestPublicIp](#GetRequestPublicIp) - [IsPublicIP](#IsPublicIP) +- [IsInternalIP](#IsInternalIP) - [HttpGet](#HttpGet) - [HttpDelete](#HttpDelete) - [HttpPost](#HttpPost) @@ -199,8 +201,51 @@ func main() { +### GetRequestPublicIp +

Get http request public ip.

+ +Signature: + +```go +func GetRequestPublicIp(req *http.Request) string +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + ip := "36.112.24.10" + + request1 := http.Request{ + Method: "GET", + Header: http.Header{ + "X-Forwarded-For": {ip}, + }, + } + publicIp1 := netutil.GetRequestPublicIp(&request1) + fmt.Println(publicIp1) //36.112.24.10 + + request2 := http.Request{ + Method: "GET", + Header: http.Header{ + "X-Real-Ip": {ip}, + }, + } + publicIp2 := netutil.GetRequestPublicIp(&request2) + fmt.Println(publicIp2) //36.112.24.10 +} +``` + + + ### IsPublicIP -

Checks if a ip is public or not.

+

Checks if an ip is public or not.

Signature: @@ -230,6 +275,36 @@ func main() { +### IsInternalIP +

Checks if an ip is intranet or not.

+ +Signature: + +```go +func IsInternalIP(IP net.IP) bool +``` +Example: + +```go +package main + +import ( + "fmt" + "net" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + ip1 := net.ParseIP("127.0.0.1") + ip2 := net.ParseIP("36.112.24.10") + + fmt.Println(netutil.IsInternalIP(ip1)) //true + fmt.Println(netutil.IsInternalIP(ip2)) //false +} +``` + + + ### HttpGet

Send http get request.

diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md index 72494ad..6a6b2d5 100644 --- a/docs/netutil_zh-CN.md +++ b/docs/netutil_zh-CN.md @@ -25,12 +25,14 @@ import ( - [GetIps](#GetIps) - [GetMacAddrs](#GetMacAddrs) - [GetPublicIpInfo](#GetPublicIpInfo) +- [GetRequestPublicIp](#GetRequestPublicIp) + - [IsPublicIP](#IsPublicIP) +- [IsInternalIP](#IsInternalIP) - [HttpGet](#HttpGet) - [HttpDelete](#HttpDelete) - [HttpPost](#HttpPost) - [HttpPut](#HttpPut) - - [HttpPatch](#HttpPatch) - [ParseHttpResponse](#ParseHttpResponse) @@ -197,6 +199,50 @@ func main() { +### GetRequestPublicIp +

获取http请求ip

+ +函数签名: + +```go +func GetRequestPublicIp(req *http.Request) string +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + ip := "36.112.24.10" + + request1 := http.Request{ + Method: "GET", + Header: http.Header{ + "X-Forwarded-For": {ip}, + }, + } + publicIp1 := netutil.GetRequestPublicIp(&request1) + fmt.Println(publicIp1) //36.112.24.10 + + request2 := http.Request{ + Method: "GET", + Header: http.Header{ + "X-Real-Ip": {ip}, + }, + } + publicIp2 := netutil.GetRequestPublicIp(&request2) + fmt.Println(publicIp2) //36.112.24.10 +} +``` + + + + ### IsPublicIP

判断ip是否是公共ip

@@ -227,6 +273,35 @@ func main() { +### IsInternalIP +

判断ip是否是局域网ip.

+ +函数签名: + +```go +func IsInternalIP(IP net.IP) bool +``` +例子: + +```go +package main + +import ( + "fmt" + "net" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + ip1 := net.ParseIP("127.0.0.1") + ip2 := net.ParseIP("36.112.24.10") + + fmt.Println(netutil.IsInternalIP(ip1)) //true + fmt.Println(netutil.IsInternalIP(ip2)) //false +} +``` + + ### HttpGet

发送http get请求