0
0
mirror of https://github.com/vx3r/wg-gen-web.git synced 2024-11-04 17:31:17 +00:00
wg-gen-web/util/util.go

129 lines
2.5 KiB
Go

package util
import (
"errors"
"io/ioutil"
"net"
"os"
"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
func ReadFile(path string) (bytes []byte, err error) {
bytes, err = ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return bytes, nil
}
// WriteFile content to file
func WriteFile(path string, bytes []byte) (err error) {
err = ioutil.WriteFile(path, bytes, 0644)
if err != nil {
return err
}
return nil
}
// FileExists check if file exists
func FileExists(name string) bool {
info, err := os.Stat(name)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// DirectoryExists check if directory exists
func DirectoryExists(name string) bool {
info, err := os.Stat(name)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
// GetAvailableIp search for an available in cidr against a list of reserved ips
func GetAvailableIp(cidr string, reserved []string) (string, error) {
addresses, err := GetAllAddressesFromCidr(cidr)
if err != nil {
return "", err
}
for _, addresse := range addresses {
ok := true
for _, r := range reserved {
if addresse == r {
ok = false
break
}
}
if ok {
return addresse, nil
}
}
return "", errors.New("no more available address from cidr")
}
// GetAllAddressesFromCidr get all ip addresses from cidr
func GetAllAddressesFromCidr(cidr string) ([]string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
var ips []string
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
}
// remove network address and broadcast address (and server ip .1)
return ips[2 : len(ips)-1], nil
}
// IsIPv6 check if given ip is IPv6
func IsIPv6(address string) bool {
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
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}