-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
326 lines (303 loc) · 8.67 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
// Copyright 2024 Sergey Grankin
// Copyright 2022 Tailscale Inc & Contributors
// SPDX-License-Identifier: BSD-3-Clause
// tsnet-proxy exposes an HTTP server the tailnet.
package main
import (
"context"
"crypto/tls"
"errors"
"flag"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/httputil"
"net/netip"
"net/url"
"regexp"
"strings"
"github.com/quic-go/quic-go/http3"
"golang.org/x/sync/errgroup"
"tailscale.com/tailcfg"
"tailscale.com/client/tailscale"
"tailscale.com/tsnet"
)
func main() {
var (
configDir = flag.String("config-dir", "", "Directory to use for tailnet state")
hostname = flag.String("hostname", "", "Hostname to use on the tailnet")
httpAddr = flag.String("http", "127.0.0.1:80", "Address to forward HTTP requests to")
useHTTPS = flag.Bool("https", true, "Serve over HTTPS if enabled on the tailnet")
verbose = flag.Bool("verbose", false, "Be verbose")
proxyConf = proxyConfFlag("forward", "Forward extra ports. FROM_PORT[:TO_ADDR]:TO_PORT[/NETWORK]")
)
flag.Parse()
if *hostname == "" {
log.Fatal("-hostname is required")
}
srv := &tsnet.Server{
Hostname: *hostname,
Logf: func(format string, args ...any) {},
Dir: *configDir,
RunWebClient: true,
}
if *verbose {
srv.Logf = log.Printf
}
if err := run(srv, *httpAddr, *useHTTPS, *proxyConf); err != nil {
log.Fatal(err)
}
}
func run(srv *tsnet.Server, httpAddr string, useHTTPS bool, proxyConf []proxyConf) error {
ctx := context.Background()
if err := srv.Start(); err != nil {
return err
}
lc, err := srv.LocalClient()
if err != nil {
return err
}
// Wait for tailscale to come up...
if _, err := srv.Up(ctx); err != nil {
return fmt.Errorf("tailcale up: %v", err)
}
status, err := lc.Status(ctx)
if err != nil {
return fmt.Errorf("tailscale status: %v", err)
}
enableTLS := useHTTPS && status.Self.HasCap(tailcfg.CapabilityHTTPS) && len(srv.CertDomains()) > 0
fqdn := strings.TrimSuffix(status.Self.DNSName, ".")
if useHTTPS && !enableTLS {
return fmt.Errorf("HTTPS requested but unavailable; check your tailscale config")
}
var handler http.Handler = &httputil.ReverseProxy{
Rewrite: func(pr *httputil.ProxyRequest) {
pr.Out.URL.Scheme = "http"
pr.Out.URL.Host = srv.Hostname
pr.Out.Host = pr.In.Host // Preserve the host header.
pr.SetXForwarded()
},
Transport: &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
return net.Dial("tcp", httpAddr)
},
},
}
handler = setAuthHeaders(lc, handler)
g, ctx := errgroup.WithContext(ctx)
if enableTLS {
log.Printf("Listening on :443, Serving https://%s/ ...", fqdn)
httpsHandler := handler
g.Go(func() error {
return listenHTTPS(ctx, srv, lc, httpsHandler, status.TailscaleIPs, 443)
})
handler = redirectHandler(fqdn)
}
g.Go(func() error {
log.Print("Listening on :80")
return listenHTTP(srv, handler, 80)
})
for _, pc := range proxyConf {
g.Go(func() error {
log.Printf("Proxying %+v", pc)
return proxy(srv, pc.network, pc.listenAddr, pc.dialAddr)
})
}
g.Go(func() error {
// If any listener errors out, shut down the server (which in turn closes down all other listeners).
<-ctx.Done()
return srv.Close()
})
return g.Wait()
}
type proxyConf struct {
network string
listenAddr string
dialAddr string
}
func proxyConfFlag(name, usage string) *[]proxyConf {
r := regexp.MustCompile(`^(?P<port>\d+)(:(?P<addr>.+))?:(?P<toPort>\d+)(/(?P<network>\w+))?$`)
value := &[]proxyConf{}
flag.Func(name, usage, func(s string) error {
matched := match(r, s)
if matched == nil {
return fmt.Errorf("value %q must match regexp %v", s, r)
}
if matched["network"] == "" {
matched["network"] = "tcp"
}
if matched["addr"] == "" {
matched["addr"] = "127.0.0.1"
}
conf := proxyConf{
network: matched["network"],
listenAddr: ":" + matched["port"],
dialAddr: matched["addr"] + ":" + matched["toPort"],
}
*value = append(*value, conf)
return nil
})
return value
}
func match(r *regexp.Regexp, s string) map[string]string {
match := r.FindStringSubmatch(s)
if match == nil {
return nil
}
result := map[string]string{}
for i, name := range r.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
return result
}
// proxy forwards connections from listenAddr to dialAddr.
// All standard network types are supported.
func proxy(srv *tsnet.Server, network, listenAdr, dialAddr string) error {
lis, err := srv.Listen(network, listenAdr)
if err != nil {
return err
}
defer lis.Close()
for {
conn1, err := lis.Accept()
if errors.Is(err, net.ErrClosed) {
return nil
} else if err != nil {
return err
}
log.Printf("Accepted connection on %s", listenAdr)
conn2, err := net.Dial(network, dialAddr)
if err != nil {
conn1.Close()
return err
}
go func() {
if err := pipe(conn1, conn2); err != nil {
log.Printf("Error while piping %s->%s: %v", listenAdr, dialAddr, err)
}
}()
}
}
// pipe connects two connections and bidirectionally copies data between them.
func pipe(conn1, conn2 net.Conn) error {
defer conn1.Close()
defer conn2.Close()
g := errgroup.Group{}
closingCopy := func(conn1, conn2 net.Conn) error {
_, err := io.Copy(conn1, conn2)
// If the connection can be partially closed, signal that there is no more data coming.
if wc, ok := conn1.(interface {
CloseWrite() error
}); ok {
wc.CloseWrite()
}
return err
}
g.Go(func() error { return closingCopy(conn1, conn2) })
g.Go(func() error { return closingCopy(conn2, conn1) })
return g.Wait()
}
func listenH3(srv *tsnet.Server, lc *tailscale.LocalClient, handler http.Handler, ips []netip.Addr, port uint16) error {
h3 := http3.Server{
TLSConfig: &tls.Config{GetCertificate: lc.GetCertificate},
Handler: handler,
}
g := errgroup.Group{}
for _, ip := range ips {
pc, err := srv.ListenPacket("udp", netip.AddrPortFrom(ip, port).String())
if err != nil {
return err
}
g.Go(func() error {
defer pc.Close()
return h3.Serve(pc)
})
}
defer h3.Close()
return g.Wait()
}
func listenHTTPS(ctx context.Context, srv *tsnet.Server, lc *tailscale.LocalClient, handler http.Handler, ips []netip.Addr, port uint16) error {
h3 := http3.Server{
TLSConfig: &tls.Config{GetCertificate: lc.GetCertificate},
Handler: handler,
}
g, ctx := errgroup.WithContext(ctx)
for _, ip := range ips {
g.Go(func() error {
pc, err := srv.ListenPacket("udp", netip.AddrPortFrom(ip, port).String())
if err != nil {
return err
}
defer pc.Close()
return h3.Serve(pc)
})
}
h2 := http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
h3.SetQUICHeaders(w.Header())
handler.ServeHTTP(w, r)
}),
}
g.Go(func() error {
lis, err := srv.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return err
}
lis = tls.NewListener(lis, &tls.Config{
GetCertificate: lc.GetCertificate,
NextProtos: []string{"h2", "http/1.1"}, // Enable HTTP/2.
})
defer lis.Close()
return h2.Serve(lis)
})
g.Go(func() error {
// If anything errored, or the original context was cancelled, shut down the servers (immediately).
<-ctx.Done()
return errors.Join(h2.Close(), h3.Close())
})
return g.Wait()
}
func listenHTTP(srv *tsnet.Server, handler http.Handler, port uint16) error {
lis, err := srv.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return err
}
defer lis.Close()
return http.Serve(lis, handler)
}
// redirectHandler returns the http.Handler for serving all plaintext HTTP
// requests. It redirects all requests to the HTTPs version of the same URL.
func redirectHandler(hostname string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r,
(&url.URL{
Scheme: "https",
Host: hostname,
Path: r.URL.Path,
RawQuery: r.URL.RawQuery,
}).String(),
http.StatusPermanentRedirect)
})
}
// setAuthHeaders adds Tailscale-* headers populated with the authenticated user's info.
func setAuthHeaders(lc *tailscale.LocalClient, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
who, err := lc.WhoIs(r.Context(), r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
r.Header.Set("Tailscale-Name", who.UserProfile.DisplayName)
r.Header.Set("Tailscale-User", who.UserProfile.LoginName)
r.Header.Set("Tailscale-Login", strings.Split(who.UserProfile.LoginName, "@")[0])
r.Header.Set("Tailscale-Profile-Picture", who.UserProfile.ProfilePicURL)
tailnet, _ := strings.CutPrefix(who.Node.Name, who.Node.ComputedName+".")
r.Header.Set("Tailscale-Tailnet", tailnet)
next.ServeHTTP(w, r)
})
}