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

Updates #13038

Change-Id: I1cacb1b0f8c3d0e4c36b7890155f7b1ad0d23575
Signed-off-by: Maisem Ali <maisem@tailscale.com>
dependabot/go_modules/github.com/docker/docker-26.1.5incompatible
Maisem Ali 4 months ago committed by Maisem Ali
parent 072d1a4b77
commit d0e8375b53

@ -11,6 +11,7 @@
package main package main
import ( import (
"context"
"errors" "errors"
"flag" "flag"
"io" "io"
@ -59,10 +60,10 @@ func serveCmd(w http.ResponseWriter, cmd string, args ...string) {
} }
type localClientRoundTripper struct { 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 = req.Clone(req.Context())
req.RequestURI = "" req.RequestURI = ""
return rt.lc.DoLocalRequest(req) return rt.lc.DoLocalRequest(req)
@ -96,9 +97,31 @@ func main() {
log.Printf("Tailscale Test Agent running.") 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 var hs http.Server
hs.Handler = &mux hs.Handler = &serveMux
var ( var (
stMu sync.Mutex stMu sync.Mutex
newSet = set.Set[net.Conn]{} // conns in StateNew newSet = set.Set[net.Conn]{} // conns in StateNew
@ -121,17 +144,16 @@ func main() {
} }
} }
conns := make(chan net.Conn, 1) 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") io.WriteString(w, "TTA\n")
return 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") serveCmd(w, "tailscale", "up", "--login-server=http://control.tailscale")
}) })
go hs.Serve(chanListener(conns)) go hs.Serve(chanListener(conns))
@ -151,8 +173,6 @@ func main() {
continue continue
} }
conns <- c conns <- c
time.Sleep(time.Second)
} }
} }

@ -10,11 +10,15 @@ import (
"flag" "flag"
"log" "log"
"net" "net"
"net/http"
"net/http/httputil"
"net/url"
"os" "os"
"time" "time"
"tailscale.com/tstest/natlab/vnet" "tailscale.com/tstest/natlab/vnet"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/util/must"
) )
var ( var (
@ -70,6 +74,16 @@ func main() {
s.WriteStartingBanner(os.Stdout) s.WriteStartingBanner(os.Stdout)
nc := s.NodeAgentClient(node1) 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() { go func() {
getStatus := func() { getStatus := func() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)

Loading…
Cancel
Save