-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
260 lines (222 loc) · 4.72 KB
/
service.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package goVirtualHost
import (
"context"
"errors"
"sync"
)
var alreadyOpened = errors.New("already opened")
func NewService() *Service {
service := &Service{
state: statePrepare,
listenables: listenables{},
serveables: serveables{},
vhosts: vhosts{},
}
return service
}
func (svc *Service) addVhostToServeable(vhost *vhost, params params) {
for _, param := range params {
var l *listenable
var s *serveable
l = svc.listenables.find(param.proto, param.ip, param.port)
if l != nil {
s = l.serveable
} else {
s = newServeable(param.useTLS)
l = newListenable(param.proto, param.ip, param.port)
l.serveable = s
svc.listenables = append(svc.listenables, l)
svc.serveables = append(svc.serveables, s)
}
// serveable -> vhost
s.vhosts = append(s.vhosts, vhost)
}
}
func (svc *Service) Add(info *HostInfo) (errs, warns []error) {
svc.mu.Lock()
defer svc.mu.Unlock()
if svc.state > statePrepare {
errs = append(errs, alreadyOpened)
return
}
vhostParams, hostNames, certKeyPaths, certs := info.parse()
errs, warns = svc.params.validate(vhostParams)
if len(errs) > 0 {
return
}
svc.params = append(svc.params, vhostParams...)
vhost := newVhost(hostNames, certKeyPaths, certs, info.Handler)
svc.vhosts = append(svc.vhosts, vhost)
svc.addVhostToServeable(vhost, vhostParams)
return
}
func (svc *Service) openListeners() (errs []error) {
for _, l := range svc.listenables {
err := l.open()
if err != nil {
errs = append(errs, err)
}
}
return
}
func (svc *Service) openServers() (errs []error) {
chServeErr := make(chan error)
go func() {
wg := &sync.WaitGroup{}
for _, lis := range svc.listenables {
wg.Add(1)
l := lis
go func() {
err := l.serveable.open(l)
if err != nil {
chServeErr <- err
}
wg.Done()
}()
}
wg.Wait()
close(chServeErr)
}()
for err := range chServeErr {
errs = append(errs, err)
}
return
}
func (svc *Service) Open() (errs []error) {
svc.mu.Lock()
if svc.state >= stateOpened {
svc.mu.Unlock()
errs = append(errs, alreadyOpened)
return
}
svc.state = stateOpened
svc.mu.Unlock()
svc.params = nil // release unused data
for _, s := range svc.serveables {
es := s.init()
errs = append(errs, es...)
}
if len(errs) > 0 {
return
}
defer svc.Close()
errs = svc.openListeners()
if len(errs) > 0 {
return
}
errs = svc.openServers()
return
}
func (svc *Service) ReloadCertificates() (errs []error) {
for _, s := range svc.serveables {
es := s.loadCertificates()
errs = append(errs, es...)
}
return
}
func (svc *Service) Shutdown(ctx context.Context) {
svc.mu.Lock()
if svc.state >= stateClosed {
svc.mu.Unlock()
return
}
svc.state = stateClosed
svc.mu.Unlock()
wg := &sync.WaitGroup{}
for _, lis := range svc.listenables {
l := lis
wg.Add(1)
go func() {
if l.serveable != nil {
l.serveable.shutdown(ctx)
}
l.close()
wg.Done()
}()
}
wg.Wait()
}
func (svc *Service) Close() {
svc.mu.Lock()
if svc.state >= stateClosed {
svc.mu.Unlock()
return
}
svc.state = stateClosed
svc.mu.Unlock()
for _, l := range svc.listenables {
if l.serveable != nil {
l.serveable.close()
}
l.close()
}
}
func (svc *Service) GetAccessibleURLs(includeLoopback bool) [][]string {
gotIPList := false
var ipv46s, ipv4s, ipv6s []string
var allHostNameUrls []string
vhUrls := make(map[*vhost][]string)
for _, l := range svc.listenables {
s := l.serveable
defaultVh := s.getDefaultVhost()
port := ""
if !isDefaultPort(l.port, s.useTLS) {
port = l.port
}
for _, vh := range s.vhosts {
if l.proto == unix {
url := "unix:" + l.port
vhUrls[vh] = append(vhUrls[vh], url)
continue
}
for _, hostname := range vh.hostNames {
var url string
if s.useTLS {
url = httpsUrl
} else {
url = httpUrl
}
url = url + hostname + port
if !contains(allHostNameUrls, url) {
allHostNameUrls = append(allHostNameUrls, url)
vhUrls[vh] = append(vhUrls[vh], url)
}
}
if vh == defaultVh {
var url string
if s.useTLS {
url = httpsUrl
} else {
url = httpUrl
}
if len(l.ip) > 0 {
url = url + l.ip + port
vhUrls[vh] = append(vhUrls[vh], url)
} else {
if !gotIPList {
gotIPList = true
ipv46s, ipv4s, ipv6s = getAllIfaceIPs(includeLoopback)
}
var ips []string
switch l.proto {
case tcp46:
ips = ipv46s
case tcp4:
ips = ipv4s
case tcp6:
ips = ipv6s
}
for _, ip := range ips {
ipUrl := url + ip + port
vhUrls[vh] = append(vhUrls[vh], ipUrl)
}
}
}
}
}
vhostsUrls := make([][]string, len(svc.vhosts))
for i, vhost := range svc.vhosts {
vhostsUrls[i] = vhUrls[vhost]
}
return vhostsUrls
}