openvpn-mgt/utils.go

56 lines
1.1 KiB
Go

package main
import (
"encoding/base64"
"math/big"
"net"
"sort"
"github.com/pyke369/golang-support/uconfig"
)
func B64decode(s string) string {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return ""
}
return string(data)
}
// check if there is a member of "search" in "list". sort list to be more
// efficiant. adapt with sorting "search" too
func inArray(search, list []string) bool {
sort.Strings(list)
for _, g := range search {
i := sort.Search(len(list), func(i int) bool { return list[i] >= g })
if i < len(list) && list[i] == g {
return true
}
}
return false
}
// parse a uconf array
func parseConfigArray(config *uconfig.UConfig, configpath string) []string {
result := []string{}
for _, i := range config.GetPaths(configpath) {
if s := config.GetString(i, ""); s == "" {
continue
} else {
result = append(result, s)
}
}
return result
}
func nextIP(ip net.IP) net.IP {
// Convert to big.Int and increment
ipb := big.NewInt(0).SetBytes([]byte(ip))
ipb.Add(ipb, big.NewInt(1))
// Add leading zeros
b := ipb.Bytes()
b = append(make([]byte, len(ip)-len(b)), b...)
return net.IP(b)
}