2024-01-01 11:06:08 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2024-01-01 14:34:25 +03:00
|
|
|
"strings"
|
2024-01-01 11:06:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func parseIP(ip string) (net.IP, error) {
|
|
|
|
parsedIP := net.ParseIP(ip)
|
|
|
|
|
|
|
|
if ipv4 := parsedIP.To4(); ipv4 != nil {
|
|
|
|
return ipv4, nil
|
|
|
|
}
|
|
|
|
if parsedIP != nil{
|
|
|
|
return parsedIP, nil
|
|
|
|
}
|
|
|
|
return nil, &net.ParseError{Type: "IP address", Text: ip}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeTrustIP(trustedIP string) (string, error) {
|
2024-01-01 14:34:25 +03:00
|
|
|
if strings.Contains(trustedIP, "/") {
|
|
|
|
return trustedIP, nil
|
|
|
|
}
|
2024-01-01 11:06:08 +03:00
|
|
|
ip, err := parseIP(trustedIP)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var mapRenderIP = map [int]func(trustIP string) string{
|
|
|
|
net.IPv4len: func(trustIP string) string{
|
|
|
|
return trustIP + "/32"
|
|
|
|
},
|
|
|
|
net.IPv6len: func(trustIP string) string{
|
|
|
|
return trustIP + "/32"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fn, isExistKey := mapRenderIP[len(ip)]
|
|
|
|
|
|
|
|
if isExistKey != true{
|
|
|
|
return "", &net.ParseError{Type: "IP address", Text: trustedIP}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fn(trustedIP), nil
|
|
|
|
}
|