-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
70 lines (53 loc) · 1.64 KB
/
middleware.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
package teler
import (
"net/http"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/teler-sh/teler-waf"
)
// Middleware integrates the robust security features of teler WAF into the
// Caddy web server, ensuring your web servers remain secure and resilient
// against web-based attacks.
type Middleware struct {
// Options holds the settings for teler WAF.
teler.Options `json:"-"`
// Format is the type of configuration file, either "json" or "yaml".
Format string `json:"format"`
// LoadFrom is the path to the configuration file.
LoadFrom string `json:"load_from"`
// Inline is the configuration options written directly as a string.
Inline string `json:"inline"`
// t is an instance of teler WAF.
t *teler.Teler
}
// CaddyModule returns the Caddy module information.
func (Middleware) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: moduleID,
New: func() caddy.Module { return new(Middleware) },
}
}
// Provision implements caddy.Provisioner.
func (m *Middleware) Provision(ctx caddy.Context) error {
var err error
m.Options, err = getTelerOptions(m)
if err != nil {
return err
}
// NOTE(dwisiswant0): force no standard error output
m.Options.NoStderr = true
m.t = teler.New(m.Options)
return nil
}
// Validate implements caddy.Validator.
func (m *Middleware) Validate() error {
if m.t == nil {
return ErrNoTelerInstance
}
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
handler := m.t.CaddyHandler(next)
return handler.ServeHTTP(w, r)
}