2019-06-03 15:15:19 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2020-10-21 17:01:34 +02:00
|
|
|
"bufio"
|
2019-06-03 15:15:19 +02:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsNet checks whether the string host is a valid
|
|
|
|
// IPv6 or IPv4 address
|
|
|
|
func IsNet(host string) (bool, string) {
|
|
|
|
// DNS regex
|
|
|
|
validDNS := regexp.MustCompile(`^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z
|
|
|
|
]{2,3})$`)
|
|
|
|
// Check ipv6
|
|
|
|
if strings.HasPrefix(host, "[") {
|
|
|
|
// Parse an IP-Literal in RFC 3986 and RFC 6874.
|
|
|
|
// E.g., "[fe80::1]:80".
|
|
|
|
i := strings.LastIndex(host, "]")
|
|
|
|
if i < 0 {
|
2020-02-25 15:54:10 +01:00
|
|
|
log.Println("Unmatched [ in destination config")
|
2019-06-03 15:15:19 +02:00
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
if !validPort(host[i+1:]) {
|
2020-02-25 15:54:10 +01:00
|
|
|
log.Println("No valid port specified")
|
2019-06-03 15:15:19 +02:00
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
// trim brackets
|
|
|
|
if net.ParseIP(strings.Trim(host[:i+1], "[]")) != nil {
|
|
|
|
return true, host
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Ipv4 or DNS name
|
2020-02-25 15:54:10 +01:00
|
|
|
ss := strings.Split(host, ":")
|
2019-06-03 15:15:19 +02:00
|
|
|
if len(ss) > 1 {
|
|
|
|
if !validPort(":" + ss[1]) {
|
2020-02-25 15:54:10 +01:00
|
|
|
log.Println("No valid port specified")
|
2019-06-03 15:15:19 +02:00
|
|
|
return false, ""
|
|
|
|
}
|
2020-02-25 15:54:10 +01:00
|
|
|
// if not nil, its a valid IP adress
|
2019-06-03 15:15:19 +02:00
|
|
|
if net.ParseIP(ss[0]) != nil {
|
|
|
|
return true, host
|
|
|
|
}
|
2020-02-25 15:54:10 +01:00
|
|
|
// if "localhost", its valid
|
|
|
|
if strings.Compare("localhost", ss[0]) == 0 {
|
|
|
|
return true, host
|
|
|
|
}
|
|
|
|
// check against the regex
|
|
|
|
if validDNS.MatchString(ss[0]) {
|
2020-02-12 14:47:21 +01:00
|
|
|
return true, host
|
|
|
|
} else {
|
2020-02-25 15:54:10 +01:00
|
|
|
log.Println(fmt.Sprintf("DNS/IP: %s, Server Port: %s", ss[0], ss[1]))
|
|
|
|
return false, ""
|
|
|
|
}
|
2019-06-03 15:15:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, host
|
|
|
|
}
|
|
|
|
|
|
|
|
// validPort reports whether port is either an empty string
|
|
|
|
// or matches /^:\d*$/
|
|
|
|
func validPort(port string) bool {
|
|
|
|
if port == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if port[0] != ':' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, b := range port[1:] {
|
|
|
|
if b < '0' || b > '9' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadConfigFile takes two argument: folder and fileName.
|
2020-10-21 17:01:34 +02:00
|
|
|
// Create if not exist
|
2020-01-08 15:20:56 +01:00
|
|
|
// It reads its content, trims\n and \r, and return []byte
|
2019-06-03 15:15:19 +02:00
|
|
|
// All errors are Fatal.
|
|
|
|
func ReadConfigFile(folder string, fileName string) []byte {
|
2020-10-22 15:00:41 +02:00
|
|
|
f, err := os.OpenFile(folder+"/"+fileName, os.O_RDONLY|os.O_CREATE, 0666)
|
2019-06-03 15:15:19 +02:00
|
|
|
defer f.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
data := make([]byte, 100)
|
|
|
|
count, err := f.Read(data)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-01-08 15:20:56 +01:00
|
|
|
|
2020-02-25 15:54:10 +01:00
|
|
|
// trim \r and \n if present
|
|
|
|
r := bytes.TrimSuffix(data[:count], []byte("\n"))
|
|
|
|
return bytes.TrimSuffix(r, []byte("\r"))
|
2019-06-03 15:15:19 +02:00
|
|
|
}
|
2020-10-21 17:01:34 +02:00
|
|
|
|
|
|
|
// ReadConfigFileLines takes two argument: folder and fileName.
|
2020-10-22 15:00:41 +02:00
|
|
|
// Create if not exist
|
|
|
|
// It reads its content line by line,
|
2020-10-21 17:01:34 +02:00
|
|
|
// and return [][]byte
|
|
|
|
// All errors are Fatal.
|
|
|
|
func ReadConfigFileLines(folder string, fileName string) [][]byte {
|
|
|
|
res := [][]byte{}
|
2020-10-22 15:00:41 +02:00
|
|
|
f, err := os.OpenFile(folder+"/"+fileName, os.O_RDONLY|os.O_CREATE, 0666)
|
2020-10-21 17:01:34 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
|
|
|
for scanner.Scan() {
|
|
|
|
res = append(res, []byte(scanner.Text()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|