-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
364 lines (345 loc) · 13.7 KB
/
config.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// MIT License
//
// Copyright (c) 2023 TTBT Enterprises LLC
// Copyright (c) 2023 Robin Thellend <rthellend@rthellend.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package proxy
import (
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
"golang.org/x/exp/slices"
"golang.org/x/time/rate"
yaml "gopkg.in/yaml.v3"
)
const (
ModePlaintext = "PLAINTEXT"
ModeTCP = "TCP"
ModeTLS = "TLS"
ModeTLSPassthrough = "TLSPASSTHROUGH"
ModeHTTP = "HTTP"
ModeHTTPS = "HTTPS"
ModeConsole = "CONSOLE"
)
var (
validModes = []string{
ModeTCP,
ModeTLS,
ModeTLSPassthrough,
ModeHTTP,
ModeHTTPS,
ModeConsole,
}
)
// Config is the TLS proxy configuration.
type Config struct {
// Definitions is a section where yaml anchors can be defined. It is
// otherwise ignored by the proxy.
Definitions any `yaml:"definitions,omitempty"`
// HTTPAddr must be reachable from the internet via port 80 for the
// letsencrypt ACME http-01 challenge to work. If the httpAddr is empty,
// the proxy will only use tls-alpn-01 and tlsAddr must be reachable on
// port 443.
// See https://letsencrypt.org/docs/challenge-types/
HTTPAddr string `yaml:"httpAddr,omitempty"`
// TLSAddr is the address where the proxy will receive TLS connections
// and forward them to the backends.
TLSAddr string `yaml:"tlsAddr"`
// CacheDir is the directory where the proxy stores TLS certificates.
CacheDir string `yaml:"cacheDir,omitempty"`
// DefaultServerName is the server name to use when the TLS client
// doesn't use the Server Name Indication (SNI) extension.
DefaultServerName string `yaml:"defaultServerName,omitempty"`
// Backends is the list of service backends.
Backends []*Backend `yaml:"backends"`
// Email is optionally included in the requests to letsencrypt.
Email string `yaml:"email,omitempty"`
// MaxOpen is the maximum number of open incoming connections.
MaxOpen int `yaml:"maxOpen,omitempty"`
}
// Backend encapsulates the data of one backend.
type Backend struct {
// ServerNames is the list of all the server names for this service,
// e.g. example.com, www.example.com.
ServerNames []string `yaml:"serverNames"`
// ClientAuth indicates whether TLS client authentication is required
// for this service.
ClientAuth bool `yaml:"clientAuth,omitempty"`
// ClientACL optionally specifies which client identities are allowed
// to use this service. A nil value disabled the authorization check and
// allows any valid client certificate. Otherwise, the value is a slice
// of Subject strings from the client X509 certificate.
ClientACL *[]string `yaml:"clientACL,omitempty"`
// ClientCAs is either a file name or a set of PEM-encoded CA
// certificates that are used to authenticate clients.
ClientCAs string `yaml:"clientCAs,omitempty"`
// AllowIPs specifies a list of IP network addresses to allow, in CIDR
// format, e.g. 192.168.0.0/24.
//
// The rules are applied in this order:
// * If DenyIPs is specified, the remote addr must not match any of the
// IP addresses in the list.
// * If AllowIPs is specified, the remote addr must match at least one
// of the IP addresses on the list.
//
// If an IP address is blocked, the client receives a TLS "unrecognized
// name" alert, as if it connected to an unknown server name.
AllowIPs *[]string `yaml:"allowIPs,omitempty"`
// DenyIPs specifies a list of IP network addresses to deny, in CIDR
// format, e.g. 192.168.0.0/24. See AllowIPs.
DenyIPs *[]string `yaml:"denyIPs,omitempty"`
// ALPNProtos specifies the list of ALPN procotols supported by this
// backend. The ACME acme-tls/1 protocol doesn't need to be specified.
// The default values are: h2, http/1.1
// Set the value to an empty slice to disable ALPN.
// The negotiated protocol is forwarded to the backends that use TLS.
// https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml#alpn-protocol-ids
ALPNProtos *[]string `yaml:"alpnProtos,omitempty"`
// Mode controls how the proxy communicates with the backend.
// - PLAINTEXT: Use a plaintext, non-encrypted, TCP connection. This is
// the default mode.
// CLIENT --TLS--> PROXY ----> BACKEND SERVER
// - TLS: Open a new TLS connection. Set ForwardServerName, ForwardRootCAs,
// and/or InsecureSkipVerify to verify the identity of the server.
// CLIENT --TLS--> PROXY --TLS--> BACKEND SERVER
// - TLSPASSTHROUGH: Forward the whole TLS connection to the backend.
// In this mode, the proxy terminates the TCP connection, but not
// the TLS connection. The proxy uses the information from the TLS
// ClientHello message to route the TLS data to the right backend.
// It cannot see the plaintext data, and it cannot enforce client
// authentication & authorization.
// +-TCP-PROXY-TCP-+
// CLIENT -+------TLS------+-> BACKEND SERVER
// - HTTP: Parses the incoming connection as HTTPS and forwards the
// requests to the backends as HTTP requests. Only HTTP/1 is
// supported.
// CLIENT --HTTPS--> PROXY --HTTP--> BACKEND SERVER
// - HTTPS: Parses the incoming connection as HTTPS and forwards the
// requests to the backends as HTTPS requests. Only HTTP/1 is
// supported.
// CLIENT --HTTPS--> PROXY --HTTPS--> BACKEND SERVER
// - CONSOLE: Indicates that this backend is handled by the proxy itself
// to report its status and metrics. It is strongly recommended
// to use it with ClientAuth and ClientACL. Otherwise, information
// from the proxy's configuration can be leaked to anyone who knows
// the backend's server name.
// CLIENT --TLS--> PROXY CONSOLE
Mode string `yaml:"mode"`
// AddClientCertHeader indicates that the HTTP Client-Cert header should
// be added to the request when Mode is HTTP or HTTPS.
AddClientCertHeader bool `yaml:"addClientCertHeader,omitempty"`
// Addresses is a list of server addresses where requests are forwarded.
// When more than one address are specified, requests are distributed
// using a simple round robin.
Addresses []string `yaml:"addresses,omitempty"`
// InsecureSkipVerify disabled the verification of the backend server's
// TLS certificate. See https://pkg.go.dev/crypto/tls#Config
InsecureSkipVerify bool `yaml:"insecureSkipVerify,omitempty"`
// ForwardRateLimit specifies how fast requests can be forwarded to the
// backend servers. The default value is 5 requests per second.
ForwardRateLimit int `yaml:"forwardRateLimit"`
// ForwardServerName is the ServerName to send in the TLS handshake with
// the backend server. It is also used to verify the server's identify.
// This is particularly useful when the addresses use IP addresses
// instead of hostnames.
ForwardServerName string `yaml:"forwardServerName,omitempty"`
// ForwardRootCAs is either a file name or a set of PEM-encoded CA
// certificates that are used to authenticate backend servers.
ForwardRootCAs string `yaml:"forwardRootCAs,omitempty"`
// ForwardTimeout is the connection timeout to backend servers. If
// Addresses contains multiple addresses, this timeout indicates how
// long to wait before trying the next address in the list. The default
// value is 30 seconds.
ForwardTimeout time.Duration `yaml:"forwardTimeout"`
// TCP connections consist of two streams of data:
//
// CLIENT --> SERVER
// CLIENT <-- SERVER
//
// The CLIENT and the SERVER can send data to each other at the same.
// When one stream is closed, the other one can remain open and continue
// to transmit data indefinitely. The TCP connection is closed when both
// streams are closed. (Either end can close the whole connection at any
// time too)
//
// This is a normal feature of TCP connections, but very few
// applications / protocols use half-close connections.
//
// There are some broken clients and network devices doing Network
// Address Translation (NAT) that never close their end of the
// connection. This can result in TCP connections staying open doing
// nothing, but still using resources for a very long time.
//
// The parameters below can be used to control the behavior of the proxy
// when connections are half-closed. The default values should be
// appropriate for well-behaved servers and occasionally broken clients.
// ServerCloseEndsConnection indicates that the proxy will close the
// whole TCP connection when the server closes its end of it. The
// default value is true.
ServerCloseEndsConnection *bool `yaml:"serverCloseEndsConnection,omitempty"`
// ClientCloseEndsConnection indicates that the proxy will close the
// whole TCP connection when the client closes its end of it. The
// default value is false.
ClientCloseEndsConnection *bool `yaml:"clientCloseEndsConnection,omitempty"`
// HalfCloseTimeout is the amount of time to keep the TCP connection
// open when one stream is closed. The default value is 1 minute.
HalfCloseTimeout *time.Duration `yaml:"halfCloseTimeout,omitempty"`
tlsConfig *tls.Config
forwardRootCAs *x509.CertPool
limiter *rate.Limiter
allowIPs *[]*net.IPNet
denyIPs *[]*net.IPNet
httpServer *http.Server
httpConnChan chan net.Conn
mu sync.Mutex
next int
inFlight int
shutdown bool
}
func (cfg *Config) clone() *Config {
b, _ := yaml.Marshal(cfg)
var out Config
yaml.Unmarshal(b, &out)
return &out
}
// Check checks that the Config is valid, sets some default values, and
// initializes internal data structures.
func (cfg *Config) Check() error {
cfg.Definitions = nil
if cfg.CacheDir == "" {
d, err := os.UserCacheDir()
if err != nil {
return errors.New("CacheDir must be set in config")
}
cfg.CacheDir = filepath.Join(d, "tlsproxy", "letsencrypt")
}
if cfg.TLSAddr == "" {
cfg.TLSAddr = ":10443"
}
if cfg.MaxOpen == 0 {
n, err := openFileLimit()
if err != nil {
return errors.New("MaxOpen: value must be set")
}
cfg.MaxOpen = n/2 - 100
}
serverNames := make(map[string]bool)
for i, be := range cfg.Backends {
be.Mode = strings.ToUpper(be.Mode)
if be.Mode == "" || be.Mode == ModePlaintext {
be.Mode = ModeTCP
}
if !slices.Contains(validModes, be.Mode) {
return fmt.Errorf("backend[%d].Mode: value %q must be one of %v", i, be.Mode, validModes)
}
if be.Mode == ModeTLSPassthrough && be.ClientAuth {
return fmt.Errorf("backend[%d].ClientAuth: client auth is not compatible with TLS Passthrough", i)
}
if be.Mode == ModeHTTP || be.Mode == ModeHTTPS {
be.ALPNProtos = &[]string{"http/1.1", "http/1.0"}
}
if len(be.ServerNames) == 0 {
return fmt.Errorf("backend[%d].ServerNames: backend must have at least one server name", i)
}
if len(be.Addresses) == 0 && be.Mode != ModeConsole {
return fmt.Errorf("backend[%d].Addresses: backend must have at least one address", i)
}
if len(be.Addresses) > 0 && be.Mode == ModeConsole {
return fmt.Errorf("backend[%d].Addresses: Addresses should be empty when Mode is CONSOLE", i)
}
for j, sn := range be.ServerNames {
sn = strings.ToLower(sn)
be.ServerNames[j] = sn
if serverNames[sn] {
return fmt.Errorf("backend[%d].ServerNames: duplicate server name %q", i, sn)
}
serverNames[sn] = true
}
if be.ClientAuth && be.ClientCAs != "" {
_, err := loadCerts(be.ClientCAs)
if err != nil {
return fmt.Errorf("backend[%d].ClientCAs: %w", i, err)
}
}
if be.ForwardRootCAs != "" {
_, err := loadCerts(be.ForwardRootCAs)
if err != nil {
return fmt.Errorf("backend[%d].ForwardRootCAs: %w", i, err)
}
}
if be.ForwardTimeout == 0 {
be.ForwardTimeout = 30 * time.Second
}
if be.AllowIPs != nil {
ips := make([]*net.IPNet, 0, len(*be.AllowIPs))
for j, c := range *be.AllowIPs {
_, n, err := net.ParseCIDR(c)
if err != nil {
return fmt.Errorf("backend[%d].AllowIPs[%d]: %w", i, j, err)
}
ips = append(ips, n)
}
be.allowIPs = &ips
}
if be.DenyIPs != nil {
ips := make([]*net.IPNet, 0, len(*be.DenyIPs))
for j, c := range *be.DenyIPs {
_, n, err := net.ParseCIDR(c)
if err != nil {
return fmt.Errorf("backend[%d].DenyIPs[%d]: %w", i, j, err)
}
ips = append(ips, n)
}
be.denyIPs = &ips
}
be.ForwardServerName = strings.ToLower(be.ForwardServerName)
if be.ForwardRateLimit == 0 {
be.ForwardRateLimit = 5
}
be.limiter = rate.NewLimiter(rate.Limit(be.ForwardRateLimit), be.ForwardRateLimit)
}
return os.MkdirAll(cfg.CacheDir, 0o700)
}
// ReadConfig reads and validates a YAML config file.
func ReadConfig(filename string) (*Config, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
dec := yaml.NewDecoder(f)
dec.KnownFields(true)
var cfg Config
if err := dec.Decode(&cfg); err != nil {
return nil, err
}
if err := cfg.Check(); err != nil {
return nil, err
}
return &cfg, nil
}