-
Notifications
You must be signed in to change notification settings - Fork 64
/
armor.go
318 lines (281 loc) · 6.79 KB
/
armor.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package armor
import (
"crypto/tls"
"net"
"sync"
"time"
"github.com/hashicorp/serf/serf"
"github.com/labstack/armor/plugin"
"github.com/labstack/armor/store"
"github.com/labstack/armor/util"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/color"
"github.com/labstack/gommon/log"
)
type (
Armor struct {
mutex sync.RWMutex
Name string `json:"name"`
Address string `json:"address"`
Port string `json:"-"`
TLS *TLS `json:"tls"`
Admin *Admin `json:"admin"`
Storm *Storm `json:"storm"`
Postgres *Postgres `json:"postgres"`
Cluster *Cluster `json:"cluster"`
ReadTimeout time.Duration `json:"read_timeout"`
WriteTimeout time.Duration `json:"write_timeout"`
RawPlugins []plugin.RawPlugin `json:"plugins"`
Hosts Hosts `json:"hosts"`
RootDir string `json:"-"`
Store store.Store `json:"-"`
Plugins []plugin.Plugin `json:"-"`
Echo *echo.Echo `json:"-"`
Logger *log.Logger `json:"-"`
Colorer *color.Color `json:"-"`
DefaultConfig bool `json:"-"`
}
TLS struct {
Address string `json:"address"`
Port string `json:"-"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
Auto bool `json:"auto"`
CacheDir string `json:"cache_dir"`
Email string `json:"email"`
DirectoryURL string `json:"directory_url"`
Secured bool `json:"secured"`
}
Admin struct {
Address string `json:"address"`
}
Storm struct {
URI string `json:"uri"`
}
Postgres struct {
URI string `json:"uri"`
}
Cluster struct {
*serf.Serf
Address string `json:"address"`
Peers []string `json:"peers"`
}
Host struct {
mutex sync.RWMutex
initialized bool
Name string `json:"-"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
RawPlugins []plugin.RawPlugin `json:"plugins"`
Paths Paths `json:"paths"`
Plugins []plugin.Plugin `json:"-"`
Group *echo.Group `json:"-"`
ClientCAs []string `json:"client_ca"`
TLSConfig *tls.Config `json:"-"`
}
Path struct {
mutex sync.RWMutex
initialized bool
Name string `json:"-"`
RawPlugins []plugin.RawPlugin `json:"plugins"`
Plugins []plugin.Plugin `json:"-"`
Group *echo.Group `json:"-"`
}
Hosts map[string]*Host
Paths map[string]*Path
)
const (
Version = "0.4.14"
Website = "https://armor.labstack.com"
)
var (
prePlugins = map[string]bool{
plugin.PluginLogger: true,
plugin.PluginRedirect: true,
plugin.PluginHTTPSRedirect: true,
plugin.PluginHTTPSWWWRedirect: true,
plugin.PluginHTTPSNonWWWRedirect: true,
plugin.PluginWWWRedirect: true,
plugin.PluginAddTrailingSlash: true,
plugin.PluginRemoveTrailingSlash: true,
plugin.PluginNonWWWRedirect: true,
plugin.PluginRewrite: true,
}
)
func (a *Armor) FindHost(name string, add bool) (h *Host) {
a.mutex.Lock()
defer a.mutex.Unlock()
h = a.Hosts[name]
// Host lookup
if h == nil && !add {
return
}
// Add host
if h == nil {
h = new(Host)
a.Hosts[name] = h
}
// Initialize host
if !h.initialized {
h.Name = name
h.Paths = make(Paths)
h.Group = a.Echo.Host(net.JoinHostPort(name, a.Port))
routers := a.Echo.Routers()
routers[net.JoinHostPort(name, a.TLS.Port)] = routers[name]
h.initialized = true
}
return
}
func (a *Armor) AddPlugin(p plugin.Plugin) {
a.mutex.Lock()
defer a.mutex.Unlock()
if p.Order() < 0 {
a.Echo.Pre(p.Process)
} else {
a.Echo.Use(p.Process)
}
a.Plugins = append(a.Plugins, p)
}
func (a *Armor) UpdatePlugin(plugin plugin.Plugin) {
a.mutex.RLock()
defer a.mutex.RUnlock()
for _, p := range a.Plugins {
if p.Name() == plugin.Name() {
p.Update(plugin)
}
}
}
func (a *Armor) LoadPlugin(p *store.Plugin, update bool) {
if p.Host == "" && p.Path == "" {
// Global level
p := plugin.Decode(p.Raw, a.Echo, a.Logger)
p.Initialize()
if update {
a.UpdatePlugin(p)
} else {
a.AddPlugin(p)
}
} else if p.Host != "" && p.Path == "" {
// Host level
host := a.FindHost(p.Host, true)
p := plugin.Decode(p.Raw, a.Echo, a.Logger)
p.Initialize()
if update {
host.UpdatePlugin(p)
} else {
host.AddPlugin(p)
}
} else if p.Host != "" && p.Path != "" {
// Path level
host := a.FindHost(p.Host, true)
path := host.FindPath(p.Path)
p := plugin.Decode(p.Raw, a.Echo, a.Logger)
p.Initialize()
if update {
path.UpdatePlugin(p)
} else {
path.AddPlugin(p)
}
}
}
func (a *Armor) SavePlugins() {
plugins := []*store.Plugin{}
// Global plugins
for _, rp := range a.RawPlugins {
plugins = append(plugins, &store.Plugin{
Name: rp.Name(),
Config: rp.JSON(),
})
}
for hn, host := range a.Hosts {
// Host plugins
for _, rp := range host.RawPlugins {
plugins = append(plugins, &store.Plugin{
Name: rp.Name(),
Host: hn,
Config: rp.JSON(),
})
}
for pn, path := range host.Paths {
// Path plugins
for _, rp := range path.RawPlugins {
plugins = append(plugins, &store.Plugin{
Name: rp.Name(),
Host: hn,
Path: pn,
Config: rp.JSON(),
})
}
}
}
// Delete
if err := a.Store.DeleteBySource("file"); err != nil {
panic(err)
}
// Save
i, j := -50, 0
for _, p := range plugins {
p.Source = store.File
p.ID = util.ID()
now := time.Now()
p.CreatedAt = now
p.UpdatedAt = now
if _, ok := prePlugins[p.Name]; ok {
i++
p.Order = i
} else {
j++
p.Order = j
}
if err := a.Store.AddPlugin(p); err != nil {
panic(err)
}
}
}
func (h *Host) FindPath(name string) (p *Path) {
h.mutex.Lock()
defer h.mutex.Unlock()
p = h.Paths[name]
// Add path
if p == nil {
p = new(Path)
h.Paths[name] = p
}
// Initialize path
if !p.initialized {
p.Name = name
p.Group = h.Group.Group(name)
p.initialized = true
}
return
}
func (h *Host) AddPlugin(p plugin.Plugin) {
h.mutex.Lock()
defer h.mutex.Unlock()
h.Group.Use(p.Process)
h.Plugins = append(h.Plugins, p)
}
func (h *Host) UpdatePlugin(plugin plugin.Plugin) {
h.mutex.RLock()
defer h.mutex.RUnlock()
for _, p := range h.Plugins {
if p.Name() == plugin.Name() {
p.Update(plugin)
}
}
}
func (p *Path) AddPlugin(plugin plugin.Plugin) {
p.mutex.Lock()
defer p.mutex.Unlock()
p.Group.Use(plugin.Process)
p.Plugins = append(p.Plugins, plugin)
}
func (p *Path) UpdatePlugin(plugin plugin.Plugin) {
p.mutex.RLock()
defer p.mutex.RUnlock()
for _, p := range p.Plugins {
if p.Name() == plugin.Name() {
p.Update(plugin)
}
}
}