forked from ghostunnel/ghostunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
500 lines (424 loc) · 16.9 KB
/
main.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*-
* Copyright 2015 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"crypto/tls"
"errors"
"fmt"
"io"
"log"
"log/syslog"
"net"
"net/http"
"net/http/pprof"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/cyberdelia/go-metrics-graphite"
"github.com/kavu/go_reuseport"
"github.com/mwitkow/go-http-dialer"
"github.com/rcrowley/go-metrics"
"github.com/square/go-sq-metrics"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
version = "v1.1.0"
defaultMetricsPrefix = "ghostunnel"
)
var (
app = kingpin.New("ghostunnel", "A simple SSL/TLS proxy with mutual authentication for securing non-TLS services.")
serverCommand = app.Command("server", "Server mode (TLS listener -> plain TCP/UNIX target).")
serverListenAddress = serverCommand.Flag("listen", "Address and port to listen on (HOST:PORT).").PlaceHolder("ADDR").Required().TCP()
serverForwardAddress = serverCommand.Flag("target", "Address to forward connections to (HOST:PORT, or unix:PATH).").PlaceHolder("ADDR").Required().String()
serverUnsafeTarget = serverCommand.Flag("unsafe-target", "If set, does not limit target to localhost, 127.0.0.1, [::1], or UNIX sockets.").Bool()
serverAllowAll = serverCommand.Flag("allow-all", "Allow all clients, do not check client cert subject.").Bool()
serverAllowedCNs = serverCommand.Flag("allow-cn", "Allow clients with given common name (can be repeated).").PlaceHolder("CN").Strings()
serverAllowedOUs = serverCommand.Flag("allow-ou", "Allow clients with given organizational unit name (can be repeated).").PlaceHolder("OU").Strings()
serverAllowedDNSs = serverCommand.Flag("allow-dns-san", "Allow clients with given DNS subject alternative name (can be repeated).").PlaceHolder("SAN").Strings()
serverAllowedIPs = serverCommand.Flag("allow-ip-san", "Allow clients with given IP subject alternative name (can be repeated).").PlaceHolder("SAN").IPList()
clientCommand = app.Command("client", "Client mode (plain TCP/UNIX listener -> TLS target).")
clientListenAddress = clientCommand.Flag("listen", "Address and port to listen on (HOST:PORT, or unix:PATH).").PlaceHolder("ADDR").Required().String()
// Note: can't use .TCP() for clientForwardAddress because we need to set the original string in tls.Config.ServerName.
clientForwardAddress = clientCommand.Flag("target", "Address to forward connections to (HOST:PORT).").PlaceHolder("ADDR").Required().String()
clientUnsafeListen = clientCommand.Flag("unsafe-listen", "If set, does not limit listen to localhost, 127.0.0.1, [::1], or UNIX sockets.").Bool()
clientServerName = clientCommand.Flag("override-server-name", "If set, overrides the server name used for hostname verification.").PlaceHolder("NAME").String()
clientConnectProxy = clientCommand.Flag("connect-proxy", "If set, connect to target over given HTTP CONNECT proxy. Must be HTTP/HTTPS URL.").PlaceHolder("URL").URL()
// TLS options
keystorePath = app.Flag("keystore", "Path to certificate and keystore (PEM with certificate/key, or PKCS12).").PlaceHolder("PATH").Required().String()
keystorePass = app.Flag("storepass", "Password for certificate and keystore (optional).").PlaceHolder("PASS").String()
caBundlePath = app.Flag("cacert", "Path to CA bundle file (PEM/X509). Uses system trust store by default.").String()
enabledCipherSuites = app.Flag("cipher-suites", "Set of cipher suites to enable, comma-separated, in order of preference (AES, CHACHA).").Default("AES,CHACHA").String()
pkcs11Module = app.Flag("pkcs11-module", "Path to PKCS11 module (SO) file (optional)").PlaceHolder("PATH").ExistingFile()
pkcs11TokenLabel = app.Flag("pkcs11-token-label", "Token label for slot/key in PKCS11 module (optional)").PlaceHolder("LABEL").String()
pkcs11PIN = app.Flag("pkcs11-pin", "PIN code for slot/key in PKCS11 module (optional)").PlaceHolder("PIN").String()
// Reloading and timeouts
timedReload = app.Flag("timed-reload", "Reload keystores every given interval (e.g. 300s), refresh listener/client on changes.").PlaceHolder("DURATION").Duration()
shutdownTimeout = app.Flag("shutdown-timeout", "Graceful shutdown timeout. Terminates after timeout even if connections still open.").Default("5m").Duration()
timeoutDuration = app.Flag("connect-timeout", "Timeout for establishing connections, handshakes.").Default("10s").Duration()
// Metrics options
metricsGraphite = app.Flag("metrics-graphite", "Collect metrics and report them to the given graphite instance (raw TCP).").PlaceHolder("ADDR").TCP()
metricsURL = app.Flag("metrics-url", "Collect metrics and POST them periodically to the given URL (via HTTP/JSON).").PlaceHolder("URL").String()
metricsPrefix = app.Flag("metrics-prefix", fmt.Sprintf("Set prefix string for all reported metrics (default: %s).", defaultMetricsPrefix)).PlaceHolder("PREFIX").Default(defaultMetricsPrefix).String()
metricsInterval = app.Flag("metrics-interval", "Collect (and post/send) metrics every specified interval.").Default("30s").Duration()
// Status & logging
statusAddress = app.Flag("status", "Enable serving /_status and /_metrics on given HOST:PORT (or unix:SOCKET).").PlaceHolder("ADDR").String()
enableProf = app.Flag("enable-pprof", "Enable serving /debug/pprof endpoints alongside /_status (for profiling).").Bool()
useSyslog = app.Flag("syslog", "Send logs to syslog instead of stderr.").Bool()
)
var exitFunc = os.Exit
// Context groups listening context data together
type Context struct {
watcher chan bool
status *statusHandler
statusHTTP *http.Server
dial func() (net.Conn, error)
metrics *sqmetrics.SquareMetrics
cert *certificate
}
// Dialer is an interface for dialers (either net.Dialer, or http_dialer.HttpTunnel)
type Dialer interface {
Dial(network, address string) (net.Conn, error)
}
// Global logger instance
var logger = log.New(os.Stderr, "", log.LstdFlags|log.Lmicroseconds)
func initLogger() {
if *useSyslog {
var err error
logger, err = syslog.NewLogger(syslog.LOG_NOTICE|syslog.LOG_DAEMON, log.LstdFlags|log.Lmicroseconds)
panicOnError(err)
}
// Set log prefix to PID
logger.SetPrefix(fmt.Sprintf("[%5d] ", os.Getpid()))
}
// panicOnError panics if err is not nil
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
// Validate flags for both, server and client mode
func validateFlags(app *kingpin.Application) error {
if *enableProf && *statusAddress == "" {
return fmt.Errorf("--enable-pprof requires --status to be set")
}
if *metricsURL != "" && !strings.HasPrefix(*metricsURL, "http://") && !strings.HasPrefix(*metricsURL, "https://") {
return fmt.Errorf("--metrics-url should start with http:// or https://")
}
return nil
}
// Validates that addr is either a unix socket or localhost
func validateUnixOrLocalhost(addr string) bool {
if strings.HasPrefix(addr, "unix:") {
return true
}
if strings.HasPrefix(addr, "127.0.0.1:") {
return true
}
if strings.HasPrefix(addr, "[::1]:") {
return true
}
if strings.HasPrefix(addr, "localhost:") {
return true
}
return false
}
// Validate flags for server mode
func serverValidateFlags() error {
if !(*serverAllowAll) && len(*serverAllowedCNs) == 0 && len(*serverAllowedOUs) == 0 && len(*serverAllowedDNSs) == 0 && len(*serverAllowedIPs) == 0 {
return fmt.Errorf("at least one of --allow-all, --allow-cn, --allow-ou, --allow-dns-san or --allow-ip-san is required")
}
if *serverAllowAll && (len(*serverAllowedCNs) > 0 || len(*serverAllowedOUs) > 0 || len(*serverAllowedDNSs) > 0 || len(*serverAllowedIPs) > 0) {
return fmt.Errorf("--allow-all and other access control flags are mutually exclusive")
}
if !*serverUnsafeTarget && !validateUnixOrLocalhost(*serverForwardAddress) {
return fmt.Errorf("--target must be unix:PATH, localhost:PORT, 127.0.0.1:PORT or [::1]:PORT (unless --unsafe-target is set)")
}
for _, suite := range strings.Split(*enabledCipherSuites, ",") {
_, ok := cipherSuites[strings.TrimSpace(suite)]
if !ok {
return fmt.Errorf("invalid cipher suite option: %s", suite)
}
}
return nil
}
// Validate flags for client mode
func clientValidateFlags() error {
if !*clientUnsafeListen && !validateUnixOrLocalhost(*clientListenAddress) {
return fmt.Errorf("--listen must be unix:PATH, localhost:PORT, 127.0.0.1:PORT or [::1]:PORT (unless --unsafe-listen is set)")
}
for _, suite := range strings.Split(*enabledCipherSuites, ",") {
_, ok := cipherSuites[strings.TrimSpace(suite)]
if !ok {
return fmt.Errorf("invalid cipher suite option: %s", suite)
}
}
return nil
}
func main() {
err := run(os.Args[1:])
if err != nil {
exitFunc(1)
}
exitFunc(0)
}
func run(args []string) error {
initLogger()
runtime.GOMAXPROCS(runtime.NumCPU())
app.Version(fmt.Sprintf("rev %s built with %s", version, runtime.Version()))
app.Validate(validateFlags)
command := kingpin.MustParse(app.Parse(args))
// metrics
if *metricsGraphite != nil {
logger.Printf("metrics enabled; reporting metrics via TCP to %s", *metricsGraphite)
go graphite.Graphite(metrics.DefaultRegistry, 1*time.Second, *metricsPrefix, *metricsGraphite)
}
if *metricsURL != "" {
logger.Printf("metrics enabled; reporting metrics via POST to %s", *metricsURL)
}
// read CA bundle for passing to metrics library
ca, err := caBundle(*caBundlePath)
if err != nil {
fmt.Fprintf(os.Stderr, "error: unable to build TLS config: %s\n", err)
return err
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: ca,
},
},
}
metrics := sqmetrics.NewMetrics(*metricsURL, *metricsPrefix, client, *metricsInterval, metrics.DefaultRegistry, logger)
// Set up file watchers (if requested)
watcher := make(chan bool, 1)
if *timedReload > 0 {
go watchFiles([]string{*keystorePath}, *timedReload, watcher)
}
cert, err := buildCertificate(*keystorePath, *keystorePass, *pkcs11Module, *pkcs11TokenLabel, *pkcs11PIN)
if err != nil {
fmt.Fprintf(os.Stderr, "error: unable to load certificates: %s\n", err)
return err
}
switch command {
case serverCommand.FullCommand():
if err := serverValidateFlags(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return err
}
logger.Printf("starting ghostunnel in server mode")
dial, err := serverBackendDialer()
if err != nil {
fmt.Fprintf(os.Stderr, "error: invalid target address: %s\n", err)
return err
}
status := newStatusHandler(dial)
context := &Context{watcher, status, nil, dial, metrics, cert}
// Start listening
err = serverListen(context)
if err != nil {
fmt.Fprintf(os.Stderr, "error from server listen: %s\n", err)
}
return err
case clientCommand.FullCommand():
if err := clientValidateFlags(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
return err
}
logger.Printf("starting ghostunnel in client mode")
network, address, host, err := parseUnixOrTCPAddress(*clientForwardAddress)
if err != nil {
fmt.Fprintf(os.Stderr, "error: invalid target address: %s\n", err)
return err
}
dial, err := clientBackendDialer(cert, network, address, host)
if err != nil {
fmt.Fprintf(os.Stderr, "error: unable to build dialer: %s\n", err)
return err
}
status := newStatusHandler(dial)
context := &Context{watcher, status, nil, dial, metrics, cert}
// Start listening
err = clientListen(context)
if err != nil {
fmt.Fprintf(os.Stderr, "error from client listen: %s\n", err)
}
return err
}
return errors.New("unknown command")
}
// Open listening socket in server mode. Take note that we create a
// "reusable port listener", meaning we pass SO_REUSEPORT to the kernel. This
// allows us to have multiple sockets listening on the same port and accept
// connections. This is useful for the purpose of replacing certificates
// in-place without having to take downtime, e.g. if a certificate is expiring.
func serverListen(context *Context) error {
config, err := buildConfig(*caBundlePath)
if err != nil {
logger.Printf("error trying to read CA bundle: %s", err)
return err
}
config.GetCertificate = context.cert.getCertificate
config.VerifyPeerCertificate = verifyPeerCertificate
listener, err := reuseport.NewReusablePortListener("tcp", (*serverListenAddress).String())
if err != nil {
logger.Printf("error trying to listen: %s", err)
return err
}
handlers := &sync.WaitGroup{}
handlers.Add(1)
proxy := &proxy{
quit: 0,
listener: tls.NewListener(listener, config),
handlers: &sync.WaitGroup{},
dial: context.dial,
}
if *statusAddress != "" {
err := context.serveStatus()
if err != nil {
logger.Printf("error serving /_status: %s", err)
return err
}
}
go proxy.accept()
context.status.Listening()
context.signalHandler(proxy, []io.Closer{listener})
proxy.handlers.Wait()
return nil
}
// Open listening socket in client mode.
func clientListen(context *Context) error {
// Setup listening socket
network, address, _, err := parseUnixOrTCPAddress(*clientListenAddress)
if err != nil {
logger.Printf("error parsing client listen address: %s", err)
return err
}
listener, err := net.Listen(network, address)
if err != nil {
logger.Printf("error opening socket: %s", err)
return err
}
// If this is a UNIX socket, make sure we cleanup files on close.
if ul, ok := listener.(*net.UnixListener); ok {
ul.SetUnlinkOnClose(true)
}
proxy := &proxy{
quit: 0,
listener: listener,
handlers: &sync.WaitGroup{},
dial: context.dial,
}
if *statusAddress != "" {
err := context.serveStatus()
if err != nil {
logger.Printf("error serving /_status: %s", err)
return err
}
}
go proxy.accept()
context.status.Listening()
context.signalHandler(proxy, []io.Closer{listener})
proxy.handlers.Wait()
return nil
}
// Serve /_status (if configured)
func (context *Context) serveStatus() error {
mux := http.NewServeMux()
mux.Handle("/_status", context.status)
mux.Handle("/_metrics", context.metrics)
if *enableProf {
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
}
config, err := buildConfig(*caBundlePath)
if err != nil {
return err
}
config.ClientAuth = tls.NoClientCert
config.GetCertificate = context.cert.getCertificate
network, address, _, err := parseUnixOrTCPAddress(*statusAddress)
if err != nil {
return err
}
var listener net.Listener
if network == "unix" {
listener, err = net.Listen(network, address)
listener.(*net.UnixListener).SetUnlinkOnClose(true)
} else {
listener, err = reuseport.NewReusablePortListener(network, address)
}
if err != nil {
fmt.Fprintf(os.Stderr, "error: unable to bind on status port: %s\n", err)
return err
}
if network != "unix" {
listener = tls.NewListener(listener, config)
}
context.statusHTTP = &http.Server{
Handler: mux,
ErrorLog: logger,
}
go context.statusHTTP.Serve(listener)
return nil
}
// Get backend dialer function in server mode (connecting to a unix socket or tcp port)
func serverBackendDialer() (func() (net.Conn, error), error) {
backendNet, backendAddr, _, err := parseUnixOrTCPAddress(*serverForwardAddress)
if err != nil {
return nil, err
}
return func() (net.Conn, error) {
return net.DialTimeout(backendNet, backendAddr, *timeoutDuration)
}, nil
}
// Get backend dialer function in client mode (connecting to a TLS port)
func clientBackendDialer(cert *certificate, network, address, host string) (func() (net.Conn, error), error) {
config, err := buildConfig(*caBundlePath)
if err != nil {
return nil, err
}
if *clientServerName == "" {
config.ServerName = host
} else {
config.ServerName = *clientServerName
}
var dialer Dialer
dialer = &net.Dialer{Timeout: *timeoutDuration}
if *clientConnectProxy != nil {
// Use HTTP CONNECT proxy to connect to target.
proxyConfig, err := buildConfig(*caBundlePath)
if err != nil {
return nil, err
}
config.ClientAuth = tls.NoClientCert
dialer = http_dialer.New(
*clientConnectProxy,
http_dialer.WithDialer(dialer.(*net.Dialer)),
http_dialer.WithTls(proxyConfig))
}
return func() (net.Conn, error) {
// Fetch latest cached certificate before initiating new connection
crt, _ := cert.getCertificate(nil)
config.Certificates = []tls.Certificate{*crt}
return dialWithDialer(dialer, *timeoutDuration, network, address, config)
}, nil
}