-
Notifications
You must be signed in to change notification settings - Fork 56
/
util.go
217 lines (184 loc) · 4.41 KB
/
util.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package goVirtualHost
import (
"mjpclab.dev/ghfs/src/shimgo"
"net"
"sort"
"strings"
)
var whitespaceRemover = strings.NewReplacer(
" ", "",
"\t", "",
"\v", "",
)
func extractHostName(host string) string {
hostLen := len(host)
if hostLen == 0 {
return host
}
if hostLen >= 5 && host[0] == '[' { // [IPV6]:port, "[" "ip" "]" ":" "port", 5 parts
maxIndex := hostLen - 1
closeIndex := strings.IndexByte(host, ']')
if closeIndex == maxIndex {
return host[1:closeIndex]
}
if closeIndex > 1 && closeIndex < maxIndex && host[closeIndex+1] == ':' {
return host[1:closeIndex]
}
}
colonIndex := shimgo.Strings_LastIndexByte(host, ':')
if colonIndex >= 0 {
return host[:colonIndex]
}
return host
}
func normalizeHostNames(inputs []string) []string {
output := make([]string, 0, len(inputs))
for _, str := range inputs {
if len(str) > 0 {
name := strings.ToLower(str)
output = append(output, name)
}
}
return output
}
func getDefaultPort(useTLS bool) string {
if useTLS {
return ":443"
} else {
return ":80"
}
}
func isDefaultPort(port string, useTLS bool) bool {
if useTLS {
return port == ":443"
} else {
return port == ":80"
}
}
func isDigits(input string) bool {
for i, length := 0, len(input); i < length; i++ {
b := input[i]
if b < '0' || b > '9' {
return false
}
}
return true
}
func splitListen(listen string, useTLS bool) (l43proto, ip, port string) {
listen = whitespaceRemover.Replace(listen)
// empty, use default tcp port
if len(listen) == 0 {
return tcp46, "", getDefaultPort(useTLS)
}
// :port
if listen[0] == ':' {
return tcp46, "", listen
}
// port
if isDigits(listen) {
return tcp46, "", ":" + listen
}
// unix socket path
if strings.IndexByte(listen, '/') >= 0 {
return unix, "", listen
}
colonIndex := strings.IndexByte(listen, ':')
lastColonIndex := shimgo.Strings_LastIndexByte(listen, ':')
// ipv6
squareEnd := strings.IndexByte(listen, ']')
isIPv6 := listen[0] == '[' && squareEnd > 0 && colonIndex > 0 && colonIndex < squareEnd
if isIPv6 {
var ip, port string
if lastColonIndex == squareEnd+1 { // has port number
ip = listen[:lastColonIndex]
port = listen[lastColonIndex:]
} else {
ip = listen
port = getDefaultPort(useTLS)
}
if isWildcardIPv6(ip) {
ip = ""
}
return tcp6, ip, port
}
// ipv4
dotIndex1 := strings.IndexByte(listen, '.')
dotIndex2 := dotIndex1 + 1 + strings.IndexByte(listen[dotIndex1+1:], '.')
dotIndex3 := dotIndex2 + 1 + strings.IndexByte(listen[dotIndex2+1:], '.')
dotIndex4 := dotIndex3 + 1 + strings.IndexByte(listen[dotIndex3+1:], '.')
lastDotIndex := shimgo.Strings_LastIndexByte(listen, '.')
isIPv4 := dotIndex1 > 0 && dotIndex2 > dotIndex1 && dotIndex3 > dotIndex2 && dotIndex4 == dotIndex3 &&
colonIndex == lastColonIndex &&
(lastColonIndex == -1 || lastColonIndex > lastDotIndex+1)
if isIPv4 {
var ip, port string
if colonIndex >= 0 { // has port number
ip = listen[:colonIndex]
port = listen[colonIndex:]
} else {
ip = listen
port = getDefaultPort(useTLS)
}
if isWildcardIPv4(ip) {
ip = ""
}
return tcp4, ip, port
}
// suppose to be a domain with port
if colonIndex >= 0 {
return tcp46, listen[:colonIndex], listen[colonIndex:]
}
// suppose to be a domain
return tcp46, listen, getDefaultPort(useTLS)
}
func isWildcardIPv4(ip string) bool {
return ip == "0.0.0.0"
}
func isWildcardIPv6(ip string) bool {
// min len==4, [::]
// max len==41, [0000:0000:0000:0000:0000:0000:0000:0000]
if len(ip) < 4 || len(ip) > 41 {
return false
}
// remove brackets
ip = ip[1 : len(ip)-1]
for _, c := range ip {
switch c {
case '0', ':':
continue
}
return false
}
return true
}
func getAllIfaceIPs(includeLoopback bool) (all, allv4, allv6 []string) {
var allAddrs, allAddrsV4, allAddrsV6 ipAddrs
netAddrs, _ := net.InterfaceAddrs()
for _, netAddr := range netAddrs {
var netIP net.IP
switch v := netAddr.(type) {
case *net.IPNet:
netIP = v.IP
case *net.IPAddr:
netIP = v.IP
default:
continue
}
addr, _ := newIPAddr(netIP)
if addr.isNonLoopback || includeLoopback {
allAddrs = append(allAddrs, addr)
if addr.version == ip4ver {
allAddrsV4 = append(allAddrsV4, addr)
} else if addr.version == ip6ver {
allAddrsV6 = append(allAddrsV6, addr)
}
}
}
sort.Sort(allAddrs)
all = allAddrs.String()
sort.Sort(allAddrsV4)
allv4 = allAddrsV4.String()
sort.Sort(allAddrsV6)
allv6 = allAddrsV6.String()
return
}