cmd/{tta,vnet}: proxy to gokrazy UI

Signed-off-by: Maisem Ali <maisem@tailscale.com>
This commit is contained in:
Maisem Ali 2024-08-08 15:37:47 -07:00
parent b3b207da2d
commit 9db832f435
2 changed files with 46 additions and 12 deletions

View File

@ -11,6 +11,7 @@
package main
import (
"context"
"errors"
"flag"
"io"
@ -59,10 +60,10 @@ func serveCmd(w http.ResponseWriter, cmd string, args ...string) {
}
type localClientRoundTripper struct {
lc *tailscale.LocalClient
lc tailscale.LocalClient
}
func (rt localClientRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
func (rt *localClientRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req = req.Clone(req.Context())
req.RequestURI = ""
return rt.lc.DoLocalRequest(req)
@ -96,9 +97,31 @@ func main() {
log.Printf("Tailscale Test Agent running.")
var mux http.ServeMux
gokRP := httputil.NewSingleHostReverseProxy(must.Get(url.Parse("http://gokrazy")))
gokRP.Transport = &http.Transport{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
if network != "tcp" {
return nil, errors.New("unexpected network")
}
if addr != "gokrazy:80" {
return nil, errors.New("unexpected addr")
}
var d net.Dialer
return d.DialContext(ctx, "unix", "/run/gokrazy-http.sock")
},
}
var ttaMux http.ServeMux // agent mux
var serveMux http.ServeMux
serveMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-TTA-GoKrazy") == "1" {
gokRP.ServeHTTP(w, r)
return
}
ttaMux.ServeHTTP(w, r)
})
var hs http.Server
hs.Handler = &mux
hs.Handler = &serveMux
var (
stMu sync.Mutex
newSet = set.Set[net.Conn]{} // conns in StateNew
@ -121,17 +144,16 @@ func main() {
}
}
conns := make(chan net.Conn, 1)
var lc tailscale.LocalClient
rp := httputil.NewSingleHostReverseProxy(must.Get(url.Parse("http://local-tailscaled.sock")))
rp.Transport = localClientRoundTripper{&lc}
mux.Handle("/localapi/", rp)
lcRP := httputil.NewSingleHostReverseProxy(must.Get(url.Parse("http://local-tailscaled.sock")))
lcRP.Transport = new(localClientRoundTripper)
ttaMux.Handle("/localapi/", lcRP)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ttaMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "TTA\n")
return
})
mux.HandleFunc("/up", func(w http.ResponseWriter, r *http.Request) {
ttaMux.HandleFunc("/up", func(w http.ResponseWriter, r *http.Request) {
serveCmd(w, "tailscale", "up", "--login-server=http://control.tailscale")
})
go hs.Serve(chanListener(conns))
@ -151,8 +173,6 @@ func main() {
continue
}
conns <- c
time.Sleep(time.Second)
}
}

View File

@ -10,11 +10,15 @@
"flag"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"time"
"tailscale.com/tstest/natlab/vnet"
"tailscale.com/types/logger"
"tailscale.com/util/must"
)
var (
@ -70,6 +74,16 @@ func main() {
s.WriteStartingBanner(os.Stdout)
nc := s.NodeAgentClient(node1)
go func() {
rp := httputil.NewSingleHostReverseProxy(must.Get(url.Parse("http://gokrazy")))
d := rp.Director
rp.Director = func(r *http.Request) {
d(r)
r.Header.Set("X-TTA-GoKrazy", "1")
}
rp.Transport = nc.HTTPClient.Transport
http.ListenAndServe(":8080", rp)
}()
go func() {
getStatus := func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)