mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-14 17:52:28 +08:00
feat: net.go, add IsInternalIP, GetRequestPublicIp, EncodeUrl
This commit is contained in:
@@ -6,8 +6,8 @@
|
|||||||
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
|
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
|
||||||
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
|
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
|
||||||
// params[0] is header which type should be http.Header or map[string]string,
|
// params[0] is header which type should be http.Header or map[string]string,
|
||||||
// params[1] is query param which type should be url.Values or map[string]interface{}, when content-type header is
|
// params[1] is query string param which type should be url.Values or map[string]string, when content-type header is
|
||||||
// multipart/form-data or application/x-www-form-urlencoded must pass url.Values params
|
// multipart/form-data or application/x-www-form-urlencoded
|
||||||
// params[2] is post body which type should be []byte.
|
// params[2] is post body which type should be []byte.
|
||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
package netutil
|
package netutil
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetInternalIp return internal ipv4
|
// GetInternalIp return internal ipv4
|
||||||
@@ -47,6 +49,26 @@ func GetPublicIpInfo() (*PublicIpInfo, error) {
|
|||||||
return &ip, nil
|
return &ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// GetIps return all ipv4 of system
|
// GetIps return all ipv4 of system
|
||||||
func GetIps() []string {
|
func GetIps() []string {
|
||||||
var ips []string
|
var ips []string
|
||||||
@@ -122,3 +144,29 @@ func IsPublicIP(IP net.IP) bool {
|
|||||||
}
|
}
|
||||||
return false
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// EncodeUrl encode url
|
||||||
|
func EncodeUrl(urlStr string) (string, error) {
|
||||||
|
URL, err := url.Parse(urlStr)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
URL.RawQuery = URL.Query().Encode()
|
||||||
|
|
||||||
|
return URL.String(), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -82,9 +82,18 @@ func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryPar
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if req.Header.Get("Content-Type") == "multipart/form-data" || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
if strings.Contains(req.Header.Get("Content-Type"), "multipart/form-data") || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
||||||
formData := queryParam.(url.Values)
|
if formData, ok := queryParam.(url.Values); ok {
|
||||||
err = setBodyByte(req, []byte(formData.Encode()))
|
err = setBodyByte(req, []byte(formData.Encode()))
|
||||||
|
}
|
||||||
|
if formData, ok := queryParam.(map[string]string); ok {
|
||||||
|
postData := url.Values{}
|
||||||
|
for k, v := range formData {
|
||||||
|
postData.Set(k, v)
|
||||||
|
}
|
||||||
|
err = setBodyByte(req, []byte(postData.Encode()))
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
err = setBodyByte(req, body)
|
err = setBodyByte(req, body)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user