mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-07-11 17:53:58 +03:00
Merge 512e1c8621
into aadf099f50
This commit is contained in:
commit
9d78514a2e
3 changed files with 58 additions and 10 deletions
65
util/util.go
65
util/util.go
|
@ -7,12 +7,14 @@ import (
|
|||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
rice "github.com/GeertJohan/go.rice"
|
||||
"github.com/asaskevich/govalidator"
|
||||
externalip "github.com/glendc/go-external-ip"
|
||||
"github.com/labstack/gommon/log"
|
||||
"github.com/ngoduykhanh/wireguard-ui/model"
|
||||
|
@ -42,16 +44,10 @@ func BuildClientConfig(client model.Client, server model.Server, setting model.G
|
|||
|
||||
peerAllowedIPs := fmt.Sprintf("AllowedIPs = %s\n", strings.Join(client.AllowedIPs, ","))
|
||||
|
||||
desiredHost := setting.EndpointAddress
|
||||
desiredPort := server.Interface.ListenPort
|
||||
if strings.Contains(desiredHost, ":") {
|
||||
split := strings.Split(desiredHost, ":")
|
||||
desiredHost = split[0]
|
||||
if n, err := strconv.Atoi(split[1]); err == nil {
|
||||
desiredPort = n
|
||||
} else {
|
||||
log.Error("Endpoint appears to be incorrectly formatted: ", err)
|
||||
}
|
||||
desiredHost, desiredPort, err := ParseEndpoint(setting.EndpointAddress, server.Interface.ListenPort)
|
||||
|
||||
if err != nil {
|
||||
log.Error("Endpoint appears to be incorrectly formatted: ", err)
|
||||
}
|
||||
peerEndpoint := fmt.Sprintf("Endpoint = %s:%d\n", desiredHost, desiredPort)
|
||||
|
||||
|
@ -462,3 +458,52 @@ func LookupEnvOrStrings(key string, defaultVal []string) []string {
|
|||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func RemoveIPv6Brackets(host string) string {
|
||||
ipv6 := host
|
||||
if matchBrackets, _ := regexp.MatchString(`^\[.*\]$`, ipv6); matchBrackets == true {
|
||||
ipv6 = strings.Replace(ipv6, "[", "", -1)
|
||||
ipv6 = strings.Replace(ipv6, "]", "", -1)
|
||||
|
||||
//only remove brackets if valid ipv6 address
|
||||
if govalidator.IsIPv6(ipv6) {
|
||||
return ipv6
|
||||
}
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
func AddIPv6Brackets(host string) string {
|
||||
ipv6 := host
|
||||
|
||||
//only add brackets if valid ipv6 address
|
||||
if govalidator.IsIPv6(ipv6) {
|
||||
ipv6 = "[" + ipv6 + "]"
|
||||
return ipv6
|
||||
}
|
||||
return host
|
||||
}
|
||||
|
||||
func ParseEndpoint(host string, defaultPort int) (string, int, error) {
|
||||
port := defaultPort
|
||||
|
||||
//remove brackets from standalone IPv6 address
|
||||
host = RemoveIPv6Brackets(host)
|
||||
|
||||
if govalidator.IsIPv4(host) || govalidator.IsIPv6(host) || govalidator.IsDNSName(host) {
|
||||
return AddIPv6Brackets(host), port, nil
|
||||
}
|
||||
|
||||
//check if specific port contained
|
||||
host, strPort, err := net.SplitHostPort(host)
|
||||
if err != nil {
|
||||
return "", -1, errors.New("invalid Host")
|
||||
}
|
||||
|
||||
port, err = strconv.Atoi(strPort)
|
||||
if err != nil || port > 65535 || port < 0 {
|
||||
return "", -1, errors.New("invalid Port")
|
||||
}
|
||||
|
||||
return AddIPv6Brackets(host), port, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue