Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ipv6 support for client configuration #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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=
Expand Down
65 changes: 55 additions & 10 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -38,16 +40,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)

Expand Down Expand Up @@ -457,3 +453,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
}