-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetup.go
88 lines (75 loc) · 1.56 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
package proxyprotocol
import (
"net"
"time"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
pp "github.com/mastercactapus/proxyprotocol"
)
type ppRules []pp.Rule
func init() {
caddy.RegisterPlugin("proxyprotocol", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
rules, err := parseConfig(c)
if err != nil {
return err
}
if len(rules) > 0 {
httpserver.GetConfig(c).AddListenerMiddleware(ppRules(rules).NewListener)
}
return nil
}
func parseConfig(c *caddy.Controller) (cfgs []pp.Rule, err error) {
for c.Next() {
if c.Val() != "proxyprotocol" {
continue
}
var subnets []*net.IPNet
t := 5 * time.Second
for _, arg := range c.RemainingArgs() {
_, n, err := net.ParseCIDR(arg)
if err != nil {
return nil, err
}
subnets = append(subnets, n)
}
if c.NextBlock() {
switch c.Val() {
case "timeout":
if !c.NextArg() {
return nil, c.ArgErr()
}
if c.Val() == "none" {
t = 0
break
}
t, err = time.ParseDuration(c.Val())
if err != nil {
return nil, err
}
if t < 0 {
return nil, c.ArgErr()
}
default:
return nil, c.ArgErr()
}
}
if len(subnets) == 0 {
subnets = append(subnets,
&net.IPNet{Mask: make([]byte, 4), IP: make([]byte, 4)},
&net.IPNet{Mask: make([]byte, 16), IP: make([]byte, 16)},
)
}
if c.NextBlock() {
return nil, c.ArgErr()
}
for _, n := range subnets {
cfgs = append(cfgs, pp.Rule{Subnet: n, Timeout: t})
}
}
return cfgs, nil
}