mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-06-07 00:46:58 +03:00
Merge 512e1c8621
into aadf099f50
This commit is contained in:
commit
9d78514a2e
3 changed files with 58 additions and 10 deletions
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ go 1.14
|
|||
|
||||
require (
|
||||
github.com/GeertJohan/go.rice v1.0.2
|
||||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
|
||||
github.com/glendc/go-external-ip v0.0.0-20170425150139-139229dcdddd
|
||||
github.com/go-playground/universal-translator v0.17.0 // indirect
|
||||
github.com/gorilla/sessions v1.2.0
|
||||
|
|
2
go.sum
2
go.sum
|
@ -6,6 +6,8 @@ github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkK
|
|||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/appleboy/gofight/v2 v2.1.2/go.mod h1:frW+U1QZEdDgixycTj4CygQ48yLTUhplt43+Wczp3rw=
|
||||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
|
||||
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
|
||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
|
|
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