2022-11-09 14:55:17 +00:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ipnlocal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2022-11-10 05:04:05 +00:00
|
|
|
"fmt"
|
2022-11-09 14:55:17 +00:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2022-11-09 23:38:09 +00:00
|
|
|
"net/netip"
|
2022-11-09 14:55:17 +00:00
|
|
|
pathpkg "path"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/net/netutil"
|
|
|
|
)
|
|
|
|
|
2022-11-10 05:04:05 +00:00
|
|
|
// serveHTTPContextKey is the context.Value key for a *serveHTTPContext.
|
|
|
|
type serveHTTPContextKey struct{}
|
|
|
|
|
|
|
|
type serveHTTPContext struct {
|
|
|
|
SrcAddr netip.AddrPort
|
|
|
|
DestPort uint16
|
|
|
|
}
|
|
|
|
|
2022-11-09 23:38:09 +00:00
|
|
|
func (b *LocalBackend) HandleInterceptedTCPConn(dport uint16, srcAddr netip.AddrPort, getConn func() (net.Conn, bool), sendRST func()) {
|
|
|
|
b.mu.Lock()
|
|
|
|
sc := b.serveConfig
|
|
|
|
b.mu.Unlock()
|
2022-11-09 14:55:17 +00:00
|
|
|
|
2022-11-09 23:38:09 +00:00
|
|
|
if !sc.Valid() {
|
|
|
|
b.logf("[unexpected] localbackend: got TCP conn w/o serveConfig; from %v to port %v", srcAddr, dport)
|
|
|
|
sendRST()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tcph, ok := sc.TCP().GetOk(int(dport))
|
|
|
|
if !ok {
|
|
|
|
b.logf("[unexpected] localbackend: got TCP conn without TCP config for port %v; from %v", dport, srcAddr)
|
|
|
|
sendRST()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:05:52 +00:00
|
|
|
if tcph.HTTPS() {
|
|
|
|
conn, ok := getConn()
|
|
|
|
if !ok {
|
|
|
|
b.logf("localbackend: getConn didn't complete from %v to port %v", srcAddr, dport)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hs := &http.Server{
|
|
|
|
TLSConfig: &tls.Config{
|
|
|
|
GetCertificate: b.getTLSServeCert,
|
|
|
|
},
|
|
|
|
Handler: http.HandlerFunc(b.serveWebHandler),
|
2022-11-10 05:04:05 +00:00
|
|
|
BaseContext: func(_ net.Listener) context.Context {
|
|
|
|
return context.WithValue(context.Background(), serveHTTPContextKey{}, &serveHTTPContext{
|
|
|
|
SrcAddr: srcAddr,
|
|
|
|
DestPort: dport,
|
|
|
|
})
|
|
|
|
},
|
2022-11-10 05:05:52 +00:00
|
|
|
}
|
|
|
|
hs.ServeTLS(netutil.NewOneConnListener(conn, nil), "", "")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-09 23:38:09 +00:00
|
|
|
if backDst := tcph.TCPForward(); backDst != "" {
|
|
|
|
if tcph.TerminateTLS() {
|
|
|
|
b.logf("TODO(bradfitz): finish")
|
|
|
|
sendRST()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
backConn, err := b.dialer.SystemDial(ctx, "tcp", backDst)
|
|
|
|
cancel()
|
|
|
|
if err != nil {
|
|
|
|
b.logf("localbackend: failed to TCP proxy port %v (from %v) to %s: %v", dport, srcAddr, backDst, err)
|
|
|
|
sendRST()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn, ok := getConn()
|
|
|
|
if !ok {
|
|
|
|
b.logf("localbackend: getConn didn't complete from %v to port %v", srcAddr, dport)
|
|
|
|
backConn.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
defer backConn.Close()
|
|
|
|
|
|
|
|
// TODO(bradfitz): do the RegisterIPPortIdentity and
|
|
|
|
// UnregisterIPPortIdentity stuff that netstack does
|
|
|
|
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(backConn, conn)
|
|
|
|
errc <- err
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
_, err := io.Copy(conn, backConn)
|
|
|
|
errc <- err
|
|
|
|
}()
|
|
|
|
<-errc
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:05:52 +00:00
|
|
|
b.logf("closing TCP conn to port %v (from %v) with actionless TCPPortHandler", dport, srcAddr)
|
|
|
|
sendRST()
|
2022-11-09 14:55:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 21:15:59 +00:00
|
|
|
func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView, ok bool) {
|
|
|
|
var z ipn.HTTPHandlerView // zero value
|
|
|
|
|
2022-11-09 14:55:17 +00:00
|
|
|
if r.TLS == nil {
|
2022-11-09 21:15:59 +00:00
|
|
|
return z, false
|
2022-11-09 14:55:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 05:04:05 +00:00
|
|
|
sctx, ok := r.Context().Value(serveHTTPContextKey{}).(*serveHTTPContext)
|
|
|
|
if !ok {
|
|
|
|
b.logf("[unexpected] localbackend: no serveHTTPContext in request")
|
|
|
|
return z, false
|
|
|
|
}
|
2022-11-09 14:55:17 +00:00
|
|
|
sni := r.TLS.ServerName
|
2022-11-10 05:04:05 +00:00
|
|
|
key := ipn.HostPort(fmt.Sprintf("%s:%v", sni, sctx.DestPort))
|
2022-11-09 14:55:17 +00:00
|
|
|
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
2022-11-09 21:15:59 +00:00
|
|
|
if !b.serveConfig.Valid() {
|
|
|
|
return z, false
|
|
|
|
}
|
|
|
|
|
|
|
|
wsc, ok := b.serveConfig.Web().GetOk(key)
|
2022-11-09 14:55:17 +00:00
|
|
|
if !ok {
|
2022-11-09 21:15:59 +00:00
|
|
|
return z, false
|
2022-11-09 14:55:17 +00:00
|
|
|
}
|
|
|
|
path := r.URL.Path
|
|
|
|
for {
|
2022-11-09 21:15:59 +00:00
|
|
|
if h, ok := wsc.Handlers().GetOk(path); ok {
|
2022-11-09 14:55:17 +00:00
|
|
|
return h, true
|
|
|
|
}
|
|
|
|
if path == "/" {
|
2022-11-09 21:15:59 +00:00
|
|
|
return z, false
|
2022-11-09 14:55:17 +00:00
|
|
|
}
|
|
|
|
path = pathpkg.Dir(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LocalBackend) serveWebHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
h, ok := b.getServeHandler(r)
|
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2022-11-09 21:15:59 +00:00
|
|
|
if s := h.Text(); s != "" {
|
2022-11-09 14:55:17 +00:00
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
io.WriteString(w, s)
|
|
|
|
return
|
|
|
|
}
|
2022-11-09 21:15:59 +00:00
|
|
|
if v := h.Path(); v != "" {
|
2022-11-09 14:55:17 +00:00
|
|
|
io.WriteString(w, "TODO(bradfitz): serve file")
|
|
|
|
return
|
|
|
|
}
|
2022-11-09 21:15:59 +00:00
|
|
|
if v := h.Proxy(); v != "" {
|
2022-11-09 14:55:17 +00:00
|
|
|
io.WriteString(w, "TODO(bradfitz): proxy")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Error(w, "empty handler", 500)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LocalBackend) getTLSServeCert(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
if hi == nil || hi.ServerName == "" {
|
|
|
|
return nil, errors.New("no SNI ServerName")
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
pair, err := b.GetCertPEM(ctx, hi.ServerName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cert, err := tls.X509KeyPair(pair.CertPEM, pair.KeyPEM)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cert, nil
|
|
|
|
}
|