forked from ghostunnel/ghostunnel
/
main.go
265 lines (227 loc) · 8.21 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
/*-
* 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"
"fmt"
"log"
"log/syslog"
"net"
"net/http"
"net/http/pprof"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/kavu/go_reuseport"
"gopkg.in/alecthomas/kingpin.v2"
)
// These are initialized via -ldflags
var buildRevision = "unknown"
var buildCompiler = "unknown"
var app = kingpin.New("ghostunnel", "A simple SSL/TLS proxy with mutual authentication for securing non-TLS services.")
var (
listenAddress = app.Flag("listen", "Address and port to listen on (HOST:PORT).").PlaceHolder("ADDR").Required().TCP()
forwardAddress = app.Flag("target", "Address to foward connections to (HOST:PORT, or unix:PATH).").PlaceHolder("ADDR").Required().String()
unsafeTarget = app.Flag("unsafe-target", "If set, does not limit target to localhost, 127.0.0.1 or [::1].").Bool()
keystorePath = app.Flag("keystore", "Path to certificate and keystore (PKCS12).").PlaceHolder("PATH").Required().String()
keystorePass = app.Flag("storepass", "Password for certificate and keystore (optional).").PlaceHolder("PASS").String()
caBundlePath = app.Flag("cacert", "Path to certificate authority bundle file (PEM/X509).").Required().String()
timedReload = app.Flag("timed-reload", "Reload keystores every N seconds, refresh listener on changes.").PlaceHolder("N").Int()
allowAll = app.Flag("allow-all", "Allow all clients, do not check client cert subject.").Bool()
allowedCNs = app.Flag("allow-cn", "Allow clients with given common name (can be repeated).").PlaceHolder("CN").Strings()
allowedOUs = app.Flag("allow-ou", "Allow clients with organizational unit name (can be repeated).").PlaceHolder("OU").Strings()
statusPort = app.Flag("status-port", "Enable serving /_status on given localhost:PORT (shows tunnel/backend health status).").PlaceHolder("PORT").Int()
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()
)
// Context groups listening context data together
type Context struct {
watcher chan bool
listeners *sync.WaitGroup
status *statusHandler
dial func() (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 process ID to distinguish parent/child
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
func validateFlags(app *kingpin.Application) error {
if !(*allowAll) && len(*allowedCNs) == 0 && len(*allowedOUs) == 0 {
return fmt.Errorf("at least one of --allow-all, --allow-cn or --allow-ou is required")
}
if *allowAll && len(*allowedCNs) != 0 {
return fmt.Errorf("--allow-all and --allow-cn are mutually exclusive")
}
if *allowAll && len(*allowedOUs) != 0 {
return fmt.Errorf("--allow-all and --allow-ou are mutually exclusive")
}
if *enableProf && *statusPort == 0 {
return fmt.Errorf("--enable-pprof requires --status-port to be set")
}
if *statusPort < 0 || *statusPort > 65535 {
return fmt.Errorf("--status-port invalid, must be 1 <= PORT <= 65535")
}
if !validateTarget(*forwardAddress) {
return fmt.Errorf("--target must be localhost:port, 127.0.0.1:port or [::1]:port")
}
return nil
}
func main() {
initLogger()
runtime.GOMAXPROCS(runtime.NumCPU())
app.Version(fmt.Sprintf("rev %s built with %s", buildRevision, buildCompiler))
app.Validate(validateFlags)
if len(os.Args) == 1 {
fmt.Fprintf(os.Stderr, "error: no flags provided, try --help\n")
os.Exit(1)
}
_, err := app.Parse(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s, try --help\n", err)
os.Exit(1)
}
err, dial := backendDialer()
if err != nil {
fmt.Fprintf(os.Stderr, "error: invalid backend address: %s", err)
os.Exit(1)
}
status := newStatusHandler(dial)
if *statusPort != 0 {
mux := http.NewServeMux()
mux.Handle("/_status", status)
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))
}
listener, err := reuseport.NewReusablePortListener("tcp4", fmt.Sprintf("localhost:%d", *statusPort))
if err != nil {
fmt.Fprintf(os.Stderr, "error: unable to bind on status port: %s", err)
os.Exit(1)
}
logger.Printf("status port enabled; serving status on http://localhost:%d/_status", *statusPort)
go func() {
server := &http.Server{
Handler: mux,
ErrorLog: logger,
}
logger.Fatal(server.Serve(listener))
}()
}
listeners := &sync.WaitGroup{}
listeners.Add(1)
// Set up file watchers (if requested)
watcher := make(chan bool, 1)
if *timedReload > 0 {
go watchFiles([]string{*keystorePath, *caBundlePath}, time.Duration(*timedReload)*time.Second, watcher)
}
// Start listening
started := make(chan bool, 1)
go listen(started, &Context{watcher, listeners, status, dial})
up := <-started
if !up {
logger.Print("failed to start initial listener")
os.Exit(1)
}
logger.Print("initial startup completed, waiting for connections")
listeners.Wait()
logger.Print("all listeners closed, shutting down")
}
func validateTarget(addr string) bool {
if *unsafeTarget {
return true
}
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
}
// Open listening socket. 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 purposes of replacing certificates
// in-place without having to take downtime, e.g. if a certificate is
// expiring.
func listen(started chan bool, context *Context) {
// Open raw listening socket
network, address := decodeAddress(*listenAddress)
rawListener, err := reuseport.NewReusablePortListener(network, address)
if err != nil {
logger.Printf("error opening socket: %s", err)
started <- false
return
}
// Wrap listening socket with TLS listener.
tlsConfig, err := buildConfig(*keystorePath, *keystorePass, *caBundlePath)
if err != nil {
logger.Printf("error setting up TLS: %s", err)
started <- false
return
}
leaf := tlsConfig.Certificates[0].Leaf
listener := tls.NewListener(rawListener, tlsConfig)
logger.Printf("listening on %s", *listenAddress)
defer listener.Close()
handlers := &sync.WaitGroup{}
handlers.Add(1)
// A channel to allow signal handlers to notify our accept loop that it
// should shut down.
stopper := make(chan bool, 1)
go accept(listener, handlers, stopper, leaf, context.dial)
go signalHandler(listener, stopper, context)
started <- true
context.status.Listening()
logger.Printf("listening with cert serial no. %d (expiring %s)", leaf.SerialNumber, leaf.NotAfter.String())
handlers.Wait()
context.listeners.Done()
}
// Get backend dialer function
func backendDialer() (error, func() (net.Conn, error)) {
backendNet, backendAddr, err := parseTarget(*forwardAddress)
if err != nil {
return err, nil
}
return nil, func() (net.Conn, error) {
return net.Dial(backendNet, backendAddr)
}
}