1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

feat: add func GetRequestPublicIp and IsInternalIP (#40)

* add: add func GetRequestPublicIp and IsInternalIP

* fix: fix test fileutil fail

Co-authored-by: zhanghu <305835360@qq.com>
This commit is contained in:
tangqiu0205
2022-07-13 09:52:35 +08:00
committed by GitHub
parent 56b6844a2d
commit 957878dd98
3 changed files with 82 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import (
"io/ioutil"
"net"
"net/http"
"strings"
)
// GetInternalIp return internal ipv4
@@ -24,6 +25,26 @@ func GetInternalIp() string {
return ""
}
// GetRequestPublicIp return the requested public ip
func GetRequestPublicIp(req *http.Request) string {
var ip string
for _, ip = range strings.Split(req.Header.Get("X-Forwarded-For"), ",") {
if ip = strings.TrimSpace(ip); ip != "" && !IsInternalIP(net.ParseIP(ip)) {
return ip
}
}
if ip = strings.TrimSpace(req.Header.Get("X-Real-Ip")); ip != "" && !IsInternalIP(net.ParseIP(ip)) {
return ip
}
if ip, _, _ = net.SplitHostPort(req.RemoteAddr); !IsInternalIP(net.ParseIP(ip)) {
return ip
}
return ip
}
// GetPublicIpInfo return public ip information
// return the PublicIpInfo struct
func GetPublicIpInfo() (*PublicIpInfo, error) {
@@ -122,3 +143,17 @@ func IsPublicIP(IP net.IP) bool {
}
return false
}
// IsInternalIP verify an ip is intranet or not
func IsInternalIP(IP net.IP) bool {
if IP.IsLoopback() {
return true
}
if ip4 := IP.To4(); ip4 != nil {
return ip4[0] == 10 ||
(ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31) ||
(ip4[0] == 169 && ip4[1] == 254) ||
(ip4[0] == 192 && ip4[1] == 168)
}
return false
}

View File

@@ -2,6 +2,7 @@ package netutil
import (
"net"
"net/http"
"testing"
"github.com/duke-git/lancet/v2/internal"
@@ -15,6 +16,30 @@ func TestGetInternalIp(t *testing.T) {
assert.IsNotNil(ip)
}
func TestGetRequestPublicIp(t *testing.T) {
assert := internal.NewAssert(t, "TestGetPublicIpInfo")
ip := "36.112.24.10"
request := http.Request{
Method: "GET",
Header: http.Header{
"X-Forwarded-For": {ip},
},
}
publicIp := GetRequestPublicIp(&request)
assert.Equal(publicIp, ip)
request = http.Request{
Method: "GET",
Header: http.Header{
"X-Real-Ip": {ip},
},
}
publicIp = GetRequestPublicIp(&request)
assert.Equal(publicIp, ip)
}
func TestGetPublicIpInfo(t *testing.T) {
assert := internal.NewAssert(t, "TestGetPublicIpInfo")
@@ -43,6 +68,25 @@ func TestIsPublicIP(t *testing.T) {
}
}
func TestIsInternalIP(t *testing.T) {
assert := internal.NewAssert(t, "TestIsInternalIP")
ips := []net.IP{
net.ParseIP("127.0.0.1"),
net.ParseIP("192.168.0.1"),
net.ParseIP("10.91.210.131"),
net.ParseIP("172.20.16.1"),
net.ParseIP("36.112.24.10"),
}
expected := []bool{true, true, true, true, false}
for i := 0; i < len(ips); i++ {
actual := IsInternalIP(ips[i])
assert.Equal(expected[i], actual)
}
}
func TestGetIps(t *testing.T) {
ips := GetIps()
t.Log(ips)