0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2025-09-11 12:24:27 +00:00

specify ip address / proper error validation in backend / refactor

This commit is contained in:
vx3r
2020-02-06 13:30:36 +09:00
parent 4edd5cb44e
commit b7c88b747b
8 changed files with 268 additions and 35 deletions

View File

@ -5,7 +5,11 @@ import (
"io/ioutil"
"net"
"os"
"strings"
"regexp"
)
var (
RegexpEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
)
// ReadFile file content
@ -86,7 +90,31 @@ func GetAllAddressesFromCidr(cidr string) ([]string, error) {
// IsIPv6 check if given ip is IPv6
func IsIPv6(address string) bool {
return strings.Count(address, ":") >= 2
ip := net.ParseIP(address)
if ip == nil {
return false
}
return ip.To4() == nil
}
// IsValidIp check if ip is valid
func IsValidIp(ip string) bool {
return net.ParseIP(ip) != nil
}
// IsValidCidr check if CIDR is valid
func IsValidCidr(cidr string) bool {
_, _, err := net.ParseCIDR(cidr)
return err == nil
}
// GetIpFromCidr get ip from cidr
func GetIpFromCidr(cidr string) (string, error) {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return "", err
}
return ip.String(), nil
}
// http://play.golang.org/p/m8TNTtygK0