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-10 05:27:09 +00:00
|
|
|
"net/http/httputil"
|
2022-11-09 23:38:09 +00:00
|
|
|
"net/netip"
|
2022-11-10 05:27:09 +00:00
|
|
|
"net/url"
|
2022-11-10 22:16:37 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-11-09 14:55:17 +00:00
|
|
|
pathpkg "path"
|
2022-11-07 15:46:42 +00:00
|
|
|
"strconv"
|
2022-11-10 05:27:09 +00:00
|
|
|
"strings"
|
2022-11-10 22:16:37 +00:00
|
|
|
"sync"
|
2022-11-09 14:55:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"tailscale.com/ipn"
|
|
|
|
"tailscale.com/net/netutil"
|
2022-11-07 15:46:42 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-11-10 17:56:49 +00:00
|
|
|
"tailscale.com/util/strs"
|
2022-11-09 14:55:17 +00:00
|
|
|
)
|
|
|
|
|
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-07 15:46:42 +00:00
|
|
|
func (b *LocalBackend) HandleIngressTCPConn(ingressPeer *tailcfg.Node, target ipn.HostPort, srcAddr netip.AddrPort, getConn func() (net.Conn, bool), sendRST func()) {
|
|
|
|
b.mu.Lock()
|
|
|
|
sc := b.serveConfig
|
|
|
|
b.mu.Unlock()
|
|
|
|
|
|
|
|
if !sc.Valid() {
|
|
|
|
b.logf("localbackend: got ingress conn w/o serveConfig; rejecting")
|
|
|
|
sendRST()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !sc.AllowIngress().Get(target) {
|
|
|
|
b.logf("localbackend: got ingress conn for unconfigured %q; rejecting", target)
|
|
|
|
sendRST()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, port, err := net.SplitHostPort(string(target))
|
|
|
|
if err != nil {
|
|
|
|
b.logf("localbackend: got ingress conn for bad target %q; rejecting", target)
|
|
|
|
sendRST()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
port16, err := strconv.ParseUint(port, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
b.logf("localbackend: got ingress conn for bad target %q; rejecting", target)
|
|
|
|
sendRST()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO(bradfitz): pass ingressPeer etc in context to HandleInterceptedTCPConn,
|
|
|
|
// extend serveHTTPContext or similar.
|
|
|
|
b.HandleInterceptedTCPConn(uint16(port16), srcAddr, getConn, sendRST)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-11 03:58:40 +00:00
|
|
|
tcph, ok := sc.TCP().GetOk(dport)
|
2022-11-09 23:38:09 +00:00
|
|
|
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{
|
2022-11-10 05:16:20 +00:00
|
|
|
GetCertificate: b.getTLSServeCertForPort(dport),
|
2022-11-10 05:05:52 +00:00
|
|
|
},
|
|
|
|
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 != "" {
|
|
|
|
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()
|
|
|
|
|
2022-11-11 05:24:22 +00:00
|
|
|
if sni := tcph.TerminateTLS(); sni != "" {
|
|
|
|
conn = tls.Server(conn, &tls.Config{
|
|
|
|
GetCertificate: func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
|
|
defer cancel()
|
|
|
|
pair, err := b.GetCertPEM(ctx, sni)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cert, err := tls.X509KeyPair(pair.CertPEM, pair.KeyPEM)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cert, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-09 23:38:09 +00:00
|
|
|
// 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-10 22:16:37 +00:00
|
|
|
func (b *LocalBackend) getServeHandler(r *http.Request) (_ ipn.HTTPHandlerView, at string, ok bool) {
|
2022-11-09 21:15:59 +00:00
|
|
|
var z ipn.HTTPHandlerView // zero value
|
|
|
|
|
2022-11-09 14:55:17 +00:00
|
|
|
if r.TLS == nil {
|
2022-11-10 22:16:37 +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")
|
2022-11-10 22:16:37 +00:00
|
|
|
return z, "", false
|
2022-11-10 05:04:05 +00:00
|
|
|
}
|
2022-11-10 05:16:20 +00:00
|
|
|
wsc, ok := b.webServerConfig(r.TLS.ServerName, sctx.DestPort)
|
2022-11-09 14:55:17 +00:00
|
|
|
if !ok {
|
2022-11-10 22:16:37 +00:00
|
|
|
return z, "", false
|
2022-11-09 14:55:17 +00:00
|
|
|
}
|
2022-11-10 05:16:20 +00:00
|
|
|
|
2022-11-10 22:16:37 +00:00
|
|
|
if h, ok := wsc.Handlers().GetOk(r.URL.Path); ok {
|
|
|
|
return h, r.URL.Path, true
|
|
|
|
}
|
|
|
|
path := path.Clean(r.URL.Path)
|
2022-11-09 14:55:17 +00:00
|
|
|
for {
|
2022-11-10 22:16:37 +00:00
|
|
|
withSlash := path + "/"
|
|
|
|
if h, ok := wsc.Handlers().GetOk(withSlash); ok {
|
|
|
|
return h, withSlash, true
|
|
|
|
}
|
2022-11-09 21:15:59 +00:00
|
|
|
if h, ok := wsc.Handlers().GetOk(path); ok {
|
2022-11-10 22:16:37 +00:00
|
|
|
return h, path, true
|
2022-11-09 14:55:17 +00:00
|
|
|
}
|
|
|
|
if path == "/" {
|
2022-11-10 22:16:37 +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) {
|
2022-11-10 22:16:37 +00:00
|
|
|
h, mountPoint, ok := b.getServeHandler(r)
|
2022-11-09 14:55:17 +00:00
|
|
|
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-10 22:16:37 +00:00
|
|
|
b.serveFileOrDirectory(w, r, v, mountPoint)
|
2022-11-09 14:55:17 +00:00
|
|
|
return
|
|
|
|
}
|
2022-11-09 21:15:59 +00:00
|
|
|
if v := h.Proxy(); v != "" {
|
2022-11-10 05:27:09 +00:00
|
|
|
// TODO(bradfitz): this is a lot of setup per HTTP request. We should
|
|
|
|
// build the whole http.Handler with all the muxing and child handlers
|
|
|
|
// only on start/config change. But this works for now (2022-11-09).
|
2022-11-10 17:56:49 +00:00
|
|
|
targetURL, insecure := expandProxyArg(v)
|
|
|
|
u, err := url.Parse(targetURL)
|
2022-11-10 05:27:09 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "bad proxy config", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rp := httputil.NewSingleHostReverseProxy(u)
|
|
|
|
rp.Transport = &http.Transport{
|
|
|
|
DialContext: b.dialer.SystemDial,
|
2022-11-10 17:56:49 +00:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: insecure,
|
|
|
|
},
|
2022-11-10 05:27:09 +00:00
|
|
|
}
|
|
|
|
rp.ServeHTTP(w, r)
|
2022-11-09 14:55:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Error(w, "empty handler", 500)
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:16:37 +00:00
|
|
|
func (b *LocalBackend) serveFileOrDirectory(w http.ResponseWriter, r *http.Request, fileOrDir, mountPoint string) {
|
|
|
|
fi, err := os.Stat(fileOrDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if fi.Mode().IsRegular() {
|
|
|
|
if mountPoint != r.URL.Path {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f, err := os.Open(fileOrDir)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
http.ServeContent(w, r, path.Base(mountPoint), fi.ModTime(), f)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !fi.IsDir() {
|
|
|
|
http.Error(w, "not a file or directory", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(r.URL.Path) < len(mountPoint) && r.URL.Path+"/" == mountPoint {
|
|
|
|
http.Redirect(w, r, mountPoint, http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var fs http.Handler = http.FileServer(http.Dir(fileOrDir))
|
|
|
|
if mountPoint != "/" {
|
|
|
|
fs = http.StripPrefix(strings.TrimSuffix(mountPoint, "/"), fs)
|
|
|
|
}
|
|
|
|
fs.ServeHTTP(&fixLocationHeaderResponseWriter{
|
|
|
|
ResponseWriter: w,
|
|
|
|
mountPoint: mountPoint,
|
|
|
|
}, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixLocationHeaderResponseWriter is an http.ResponseWriter wrapper that, upon
|
|
|
|
// flushing HTTP headers, prefixes any Location header with the mount point.
|
|
|
|
type fixLocationHeaderResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
mountPoint string
|
|
|
|
fixOnce sync.Once // guards call to fix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fixLocationHeaderResponseWriter) fix() {
|
|
|
|
h := w.ResponseWriter.Header()
|
|
|
|
if v := h.Get("Location"); v != "" {
|
|
|
|
h.Set("Location", w.mountPoint+v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fixLocationHeaderResponseWriter) WriteHeader(code int) {
|
|
|
|
w.fixOnce.Do(w.fix)
|
|
|
|
w.ResponseWriter.WriteHeader(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fixLocationHeaderResponseWriter) Write(p []byte) (int, error) {
|
|
|
|
w.fixOnce.Do(w.fix)
|
|
|
|
return w.ResponseWriter.Write(p)
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:27:09 +00:00
|
|
|
// expandProxyArg returns a URL from s, where s can be of form:
|
|
|
|
//
|
|
|
|
// * port number ("8080")
|
|
|
|
// * host:port ("localhost:8080")
|
|
|
|
// * full URL ("http://localhost:8080", in which case it's returned unchanged)
|
2022-11-10 17:56:49 +00:00
|
|
|
// * insecure TLS ("https+insecure://127.0.0.1:4430")
|
|
|
|
func expandProxyArg(s string) (targetURL string, insecureSkipVerify bool) {
|
2022-11-10 05:27:09 +00:00
|
|
|
if s == "" {
|
2022-11-10 17:56:49 +00:00
|
|
|
return "", false
|
2022-11-10 05:27:09 +00:00
|
|
|
}
|
|
|
|
if strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://") {
|
2022-11-10 17:56:49 +00:00
|
|
|
return s, false
|
|
|
|
}
|
|
|
|
if rest, ok := strs.CutPrefix(s, "https+insecure://"); ok {
|
|
|
|
return "https://" + rest, true
|
2022-11-10 05:27:09 +00:00
|
|
|
}
|
|
|
|
if allNumeric(s) {
|
2022-11-10 17:56:49 +00:00
|
|
|
return "http://127.0.0.1:" + s, false
|
2022-11-10 05:27:09 +00:00
|
|
|
}
|
2022-11-10 17:56:49 +00:00
|
|
|
return "http://" + s, false
|
2022-11-10 05:27:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func allNumeric(s string) bool {
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if s[i] < '0' || s[i] > '9' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s != ""
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:16:20 +00:00
|
|
|
func (b *LocalBackend) webServerConfig(sniName string, port uint16) (c ipn.WebServerConfigView, ok bool) {
|
|
|
|
key := ipn.HostPort(fmt.Sprintf("%s:%v", sniName, port))
|
|
|
|
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
if !b.serveConfig.Valid() {
|
|
|
|
return c, false
|
2022-11-09 14:55:17 +00:00
|
|
|
}
|
2022-11-10 05:16:20 +00:00
|
|
|
return b.serveConfig.Web().GetOk(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *LocalBackend) getTLSServeCertForPort(port uint16) func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
return func(hi *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
if hi == nil || hi.ServerName == "" {
|
|
|
|
return nil, errors.New("no SNI ServerName")
|
|
|
|
}
|
|
|
|
_, ok := b.webServerConfig(hi.ServerName, port)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("no webserver configured for name/port")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-11-09 14:55:17 +00:00
|
|
|
}
|
|
|
|
}
|