aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/conf/name.go
blob: 2b42c0e9ba23de90b9818387ac1558ef92b0c5bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019-2021 WireGuard LLC. All Rights Reserved.
 */

package conf

import (
	"errors"
	"regexp"
	"strconv"
	"strings"
)

var reservedNames = []string{
	"CON", "PRN", "AUX", "NUL",
	"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
	"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.MustCompile("^[a-zA-Z0-9_=+.-]{1,32}$")

func isReserved(name string) bool {
	if len(name) == 0 {
		return false
	}
	for _, reserved := range reservedNames {
		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
}

func hasSpecialChars(name string) bool {
	return strings.ContainsAny(name, specialChars) || strings.ContainsAny(name, netshellDllForbidden) || strings.ContainsAny(name, serviceNameForbidden)
}

func TunnelNameIsValid(name string) bool {
	// Aside from our own restrictions, let's impose the Windows restrictions first
	if isReserved(name) || hasSpecialChars(name) {
		return false
	}
	return allowedNameFormat.MatchString(name)
}

type naturalSortToken struct {
	maybeString string
	maybeNumber int
}
type naturalSortString struct {
	originalString string
	tokens         []naturalSortToken
}

var naturalSortDigitFinder = regexp.MustCompile(`\d+|\D+`)

func newNaturalSortString(s string) (t naturalSortString) {
	t.originalString = s
	s = strings.ToLower(strings.Join(strings.Fields(s), " "))
	x := naturalSortDigitFinder.FindAllString(s, -1)
	t.tokens = make([]naturalSortToken, len(x))
	for i, s := range x {
		if n, err := strconv.Atoi(s); err == nil {
			t.tokens[i].maybeNumber = n
		} else {
			t.tokens[i].maybeString = s
		}
	}
	return
}

func (f1 naturalSortToken) Cmp(f2 naturalSortToken) int {
	if len(f1.maybeString) == 0 {
		if len(f2.maybeString) > 0 || f1.maybeNumber < f2.maybeNumber {
			return -1
		} else if f1.maybeNumber > f2.maybeNumber {
			return 1
		}
	} else if len(f2.maybeString) == 0 || f1.maybeString > f2.maybeString {
		return 1
	} else if f1.maybeString < f2.maybeString {
		return -1
	}
	return 0
}

func TunnelNameIsLess(a, b string) bool {
	if a == b {
		return false
	}
	na, nb := newNaturalSortString(a), newNaturalSortString(b)
	for i, t := range nb.tokens {
		if i == len(na.tokens) {
			return true
		}
		switch na.tokens[i].Cmp(t) {
		case -1:
			return true
		case 1:
			return false
		}
	}
	return false
}

func ServiceNameOfTunnel(tunnelName string) (string, error) {
	if !TunnelNameIsValid(tunnelName) {
		return "", errors.New("Tunnel name is not valid")
	}
	return "WireGuardTunnel$" + tunnelName, nil
}