-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
159 lines (140 loc) · 3.74 KB
/
setup.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
package sgw
import (
"fmt"
"io"
"net/http"
"os"
"github.com/airdb/sgw/waf/checker"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"go.uber.org/zap"
"github.com/airdb/sgw/secmod/ipmod"
)
const ModuleName = "sgw"
var log *zap.Logger
var args Middleware
func init() {
caddy.RegisterModule(Middleware{})
httpcaddyfile.RegisterHandlerDirective("waf", parseCaddyfile)
// init log.
log = caddy.Log().Named(ModuleName)
}
// Middleware implements an HTTP handler that writes the
// visitor's IP address to a file or stream.
type Middleware struct {
// The file or stream to write to. Can be "stdout"
// or "stderr".
// IPVendor string `json:"ipvendor,omitempty"`
Output string `json:"output,omitempty"`
IPData []string `json:"ipdata,omitempty"`
Orders []string `json:"orders"`
StrategyOrders []string `json:"strategy_orders"`
w io.Writer
logger *zap.Logger
}
// CaddyModule returns the Caddy module information.
func (Middleware) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.waf",
New: func() caddy.Module { return new(Middleware) },
}
}
// Provision implements caddy.Provisioner.
// Provision > Validate > ServeHTTP
func (m *Middleware) Provision(ctx caddy.Context) error {
switch m.Output {
case "stdout":
m.w = os.Stdout
case "stderr":
m.w = os.Stderr
default:
m.logger = ctx.Logger()
// return fmt.Errorf("an output stream is required")
}
return nil
}
// Validate implements caddy.Validator.
func (m *Middleware) Validate() error {
// init ip info.
ipVendor := args.IPData[0]
switch ipVendor {
case "ipip":
ipmod.NewIPIP(args.IPData[1])
case "ip2location":
ipmod.NewIP2Location(args.IPData[1])
}
/*
if ipVendor == "ipip" {
checker.NewIPIP(args.IPVendor)
}
*/
if m.w == nil {
return fmt.Errorf("no writer")
}
return nil
}
// ServeHTTP implements caddyhttp.MiddlewareHandler.
func (m Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
m.logger.Info("ServeHTTP--", zap.String("key", "value"))
err := checker.RunSecCheck(w, r)
if err != nil {
fmt.Println("run security check middleware failed", err)
return nil
}
return next.ServeHTTP(w, r)
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler, Syntax:
//
// waf <name> [<option>]
func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
for nesting := d.Nesting(); d.NextBlock(nesting); {
// m.log.Info("UnmarshalCaddyfile", zap.String("key", d.Val()), zap.String("value", d.RemainingArgs()[0]))
// fmt.Println("------UnmarshalCaddyfile", d.Val())
switch d.Val() {
case "output":
// Only 1 arg is allowed.
if d.NextArg() {
m.Output = d.Val()
}
case "ipvendor":
// TODO: check value
/*
if d.NextArg() {
m.IPVendor = d.Val()
}
*/
// multiple args is allowed.
m.IPData = d.RemainingArgs()
case "orders":
m.Orders = d.RemainingArgs()
case "strategyOrders":
m.StrategyOrders = d.RemainingArgs()
}
}
/*
if !d.Args(&m.Output) {
return d.ArgErr()
}
*/
}
return nil
}
// parseCaddyfile unmarshals tokens from h into a new Middleware.
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
// var m Middleware
err := args.UnmarshalCaddyfile(h.Dispenser)
if err != nil {
return nil, err
}
return args, nil
}
// Interface guards
var (
_ caddy.Provisioner = (*Middleware)(nil)
_ caddy.Validator = (*Middleware)(nil)
_ caddyhttp.MiddlewareHandler = (*Middleware)(nil)
_ caddyfile.Unmarshaler = (*Middleware)(nil)
)