-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.go
137 lines (120 loc) · 2.92 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
package altonions
import (
"net"
"net/http"
"regexp"
"strconv"
"strings"
"time"
proxyprotocol "github.com/mastercactapus/caddy-proxyprotocol"
"github.com/caddyserver/caddy"
"github.com/caddyserver/caddy/caddyhttp/header"
"github.com/caddyserver/caddy/caddyhttp/httpserver"
)
type Config struct {
Onions []string
MaxAge uint32
Persist bool
}
func init() {
caddy.RegisterPlugin("altonions", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
var config Config
var proxyConfigs []proxyprotocol.Config
var err error
hsv3, _ := regexp.Compile("[a-z2-8]{56}\\.onion:[0-9]{1,5}")
for c.Next() {
var proxyConfig proxyprotocol.Config
for c.NextArg() {
onion := c.Val()
if !hsv3.MatchString(onion) {
return c.ArgErr()
}
config.Onions = append(config.Onions, onion)
}
if c.NextBlock() {
switch c.Val() {
case "ma":
if !c.NextArg() {
return c.ArgErr()
}
seconds, err := strconv.ParseUint(c.Val(), 10, 32)
if err != nil {
return err
}
config.MaxAge = uint32(seconds)
case "persist":
if !c.NextArg() {
return c.ArgErr()
}
config.Persist, err = strconv.ParseBool(c.Val())
if err != nil {
return err
}
case "subnets":
if !c.NextArg() {
return c.ArgErr()
}
_, n, err := net.ParseCIDR(c.Val())
if err != nil {
return err
}
proxyConfig.Subnets = append(proxyConfig.Subnets, n)
case "timeout":
if !c.NextArg() {
return c.ArgErr()
}
proxyConfig.Timeout, err = time.ParseDuration(c.Val())
if err != nil {
return err
}
default:
return c.ArgErr()
}
}
if proxyConfig.Subnets == nil {
_, cidr, _ := net.ParseCIDR("::1/128")
proxyConfig.Subnets = append(proxyConfig.Subnets, cidr)
_, cidr, _ = net.ParseCIDR("127.0.0.1/32")
proxyConfig.Subnets = append(proxyConfig.Subnets, cidr)
}
proxyConfigs = append(proxyConfigs, proxyConfig)
if c.NextBlock() {
return c.ArgErr()
}
if c.Next() {
return c.ArgErr()
}
}
if proxyConfigs == nil {
return c.ArgErr()
}
// Generate the Alt-Svc header
var altOnions []string
for _, onion := range config.Onions {
value := "h2=\"" + onion + "\""
if config.MaxAge != 0 {
value = value + "; ma=" + strconv.FormatUint(uint64(config.MaxAge), 10)
}
if config.Persist {
value = value + "; persist=1"
}
altOnions = append(altOnions, value)
}
// Setup the Alt-Svc header middleware
var head header.Rule
head.Headers = http.Header{}
head.Path = "/"
head.Headers.Add("Alt-Svc", strings.Join(altOnions[:], ","))
httpserver.GetConfig(c).AddMiddleware(func(next httpserver.Handler) httpserver.Handler {
return header.Headers{Next: next, Rules: []header.Rule{head}}
})
// Setup the proxy protocol listener middleware
proxylistener := proxyprotocol.Configs(proxyConfigs).NewListener
httpserver.GetConfig(c).AddListenerMiddleware(proxylistener)
return nil
}