diff options
Diffstat (limited to 'conf/name.go')
-rw-r--r-- | conf/name.go | 33 |
1 files changed, 24 insertions, 9 deletions
diff --git a/conf/name.go b/conf/name.go index 87c463af..0d084070 100644 --- a/conf/name.go +++ b/conf/name.go @@ -1,11 +1,12 @@ /* SPDX-License-Identifier: MIT * - * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved. */ package conf import ( + "errors" "regexp" "strconv" "strings" @@ -17,15 +18,13 @@ var reservedNames = []string{ "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9", } -const serviceNameForbidden = "$" -const netshellDllForbidden = "\\/:*?\"<>|\t" -const specialChars = "/\\<>:\"|?*\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x00" - -var allowedNameFormat *regexp.Regexp +const ( + serviceNameForbidden = "$" + netshellDllForbidden = "\\/:*?\"<>|\t" + specialChars = "/\\<>:\"|?*\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x00" +) -func init() { - allowedNameFormat = regexp.MustCompile("^[a-zA-Z0-9_=+.-]{1,32}$") -} +var allowedNameFormat = regexp.MustCompile("^[a-zA-Z0-9_=+.-]{1,32}$") func isReserved(name string) bool { if len(name) == 0 { @@ -35,6 +34,14 @@ func isReserved(name string) bool { if strings.EqualFold(name, reserved) { return true } + for i := len(name) - 1; i >= 0; i-- { + if name[i] == '.' { + if strings.EqualFold(name[:i], reserved) { + return true + } + break + } + } } return false } @@ -55,6 +62,7 @@ type naturalSortToken struct { maybeString string maybeNumber int } + type naturalSortString struct { originalString string tokens []naturalSortToken @@ -110,3 +118,10 @@ func TunnelNameIsLess(a, b string) bool { } return false } + +func ServiceNameOfTunnel(tunnelName string) (string, error) { + if !TunnelNameIsValid(tunnelName) { + return "", errors.New("Tunnel name is not valid") + } + return "WireGuardTunnel$" + tunnelName, nil +} |