2020-01-30 06:45:49 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
2020-02-06 04:30:36 +00:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-04-19 05:13:44 +00:00
|
|
|
// RegexpEmail check valid email
|
2020-02-06 04:30:36 +00:00
|
|
|
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])?)*$")
|
2020-01-30 06:45:49 +00:00
|
|
|
)
|
|
|
|
|
2020-01-30 07:17:29 +00:00
|
|
|
// ReadFile file content
|
2020-01-30 06:45:49 +00:00
|
|
|
func ReadFile(path string) (bytes []byte, err error) {
|
|
|
|
bytes, err = ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes, nil
|
|
|
|
}
|
|
|
|
|
2020-01-30 07:17:29 +00:00
|
|
|
// WriteFile content to file
|
2020-01-30 06:45:49 +00:00
|
|
|
func WriteFile(path string, bytes []byte) (err error) {
|
|
|
|
err = ioutil.WriteFile(path, bytes, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-30 07:17:29 +00:00
|
|
|
// FileExists check if file exists
|
2020-01-30 06:45:49 +00:00
|
|
|
func FileExists(name string) bool {
|
|
|
|
info, err := os.Stat(name)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !info.IsDir()
|
|
|
|
}
|
|
|
|
|
2020-01-30 07:17:29 +00:00
|
|
|
// DirectoryExists check if directory exists
|
2020-01-30 06:45:49 +00:00
|
|
|
func DirectoryExists(name string) bool {
|
|
|
|
info, err := os.Stat(name)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return info.IsDir()
|
|
|
|
}
|
|
|
|
|
2020-01-30 07:17:29 +00:00
|
|
|
// GetAvailableIp search for an available in cidr against a list of reserved ips
|
2020-01-30 06:45:49 +00:00
|
|
|
func GetAvailableIp(cidr string, reserved []string) (string, error) {
|
2020-02-17 04:12:22 +00:00
|
|
|
ip, ipnet, err := net.ParseCIDR(cidr)
|
2020-01-30 06:45:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-02-17 04:12:22 +00:00
|
|
|
// this two addresses are not usable
|
|
|
|
broadcastAddr := BroadcastAddr(ipnet).String()
|
|
|
|
networkAddr := ipnet.IP.String()
|
|
|
|
|
|
|
|
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
|
2020-01-30 06:45:49 +00:00
|
|
|
ok := true
|
2020-02-17 04:12:22 +00:00
|
|
|
address := ip.String()
|
2020-01-30 06:45:49 +00:00
|
|
|
for _, r := range reserved {
|
2020-02-17 04:12:22 +00:00
|
|
|
if address == r {
|
2020-01-30 06:45:49 +00:00
|
|
|
ok = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 04:12:22 +00:00
|
|
|
if ok && address != networkAddr && address != broadcastAddr {
|
|
|
|
return address, nil
|
2020-01-30 06:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("no more available address from cidr")
|
|
|
|
}
|
|
|
|
|
2020-02-05 01:53:53 +00:00
|
|
|
// IsIPv6 check if given ip is IPv6
|
|
|
|
func IsIPv6(address string) bool {
|
2020-02-06 04:30:36 +00:00
|
|
|
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
|
2020-02-05 01:53:53 +00:00
|
|
|
}
|
|
|
|
|
2020-01-30 06:45:49 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-17 04:12:22 +00:00
|
|
|
|
|
|
|
// BroadcastAddr returns the last address in the given network, or the broadcast address.
|
|
|
|
func BroadcastAddr(n *net.IPNet) net.IP {
|
|
|
|
// The golang net package doesn't make it easy to calculate the broadcast address. :(
|
|
|
|
var broadcast net.IP
|
|
|
|
if len(n.IP) == 4 {
|
|
|
|
broadcast = net.ParseIP("0.0.0.0").To4()
|
|
|
|
} else {
|
|
|
|
broadcast = net.ParseIP("::")
|
|
|
|
}
|
|
|
|
for i := 0; i < len(n.IP); i++ {
|
|
|
|
broadcast[i] = n.IP[i] | ^n.Mask[i]
|
|
|
|
}
|
|
|
|
return broadcast
|
|
|
|
}
|