cmd/hello: add VR support at /vr

Signed-off-by: Christine Dodrill <xe@tailscale.com>
This commit is contained in:
Christine Dodrill 2021-02-21 22:58:39 -05:00
parent 39f7a61e9c
commit 9465bf5b84
2 changed files with 89 additions and 8 deletions

View File

@ -43,10 +43,12 @@ func main() {
return
}
if !devMode() {
tmpl = template.Must(template.New("home").Parse(slurpHTML()))
tmpl = template.Must(template.New("home").Parse(slurpHTML("hello.tmpl.html")))
vrTmpl = template.Must(template.New("vr").Parse(slurpHTML("hellovr.tmpl.html")))
}
http.HandleFunc("/", root)
http.HandleFunc("/vr", vr)
log.Printf("Starting hello server.")
errc := make(chan error, 1)
@ -69,8 +71,8 @@ func main() {
log.Fatal(<-errc)
}
func slurpHTML() string {
slurp, err := ioutil.ReadFile("hello.tmpl.html")
func slurpHTML(name string) string {
slurp, err := ioutil.ReadFile(name)
if err != nil {
log.Fatal(err)
}
@ -79,14 +81,20 @@ func slurpHTML() string {
func devMode() bool { return *httpsAddr == "" && *httpAddr != "" }
func getTmpl() (*template.Template, error) {
func getTmpl(name string) (*template.Template, error) {
if devMode() {
return template.New("home").Parse(slurpHTML())
return template.New(name).Parse(slurpHTML(name))
}
switch name {
case "hellovr.tmpl.html":
return vrTmpl, nil
default:
return tmpl, nil
}
return tmpl, nil
}
var tmpl *template.Template // not used in dev mode, initialized by main after flag parse
var vrTmpl *template.Template
type tmplData struct {
DisplayName string // "Foo Barberson"
@ -106,7 +114,7 @@ func root(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+host, http.StatusFound)
return
}
if r.RequestURI != "/" {
if r.RequestURI != "/" && r.RequestURI != "/vr" {
http.Redirect(w, r, "/", http.StatusFound)
return
}
@ -115,7 +123,64 @@ func root(w http.ResponseWriter, r *http.Request) {
http.Error(w, "no remote addr", 500)
return
}
tmpl, err := getTmpl()
tmpl, err := getTmpl("hello.tmpl.html")
if err != nil {
w.Header().Set("Content-Type", "text/plain")
http.Error(w, "template error: "+err.Error(), 500)
return
}
who, err := whoIs(ip)
var data tmplData
if err != nil {
if devMode() {
log.Printf("warning: using fake data in dev mode due to whois lookup error: %v", err)
data = tmplData{
DisplayName: "Taily Scalerson",
LoginName: "taily@scaler.son",
ProfilePicURL: "https://placekitten.com/200/200",
MachineName: "scaled",
MachineOS: "Linux",
IP: "100.1.2.3",
}
} else {
log.Printf("whois(%q) error: %v", ip, err)
http.Error(w, "Your Tailscale works, but we failed to look you up.", 500)
return
}
} else {
data = tmplData{
DisplayName: who.UserProfile.DisplayName,
LoginName: who.UserProfile.LoginName,
ProfilePicURL: who.UserProfile.ProfilePicURL,
MachineName: firstLabel(who.Node.ComputedName),
MachineOS: who.Node.Hostinfo.OS,
IP: ip,
}
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
tmpl.Execute(w, data)
}
func vr(w http.ResponseWriter, r *http.Request) {
if r.TLS == nil && *httpsAddr != "" {
host := r.Host
if strings.Contains(r.Host, "100.101.102.103") {
host = "hello.ipn.dev"
}
http.Redirect(w, r, "https://"+host, http.StatusFound)
return
}
if r.RequestURI != "/" && r.RequestURI != "/vr" {
http.Redirect(w, r, "/", http.StatusFound)
return
}
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
http.Error(w, "no remote addr", 500)
return
}
tmpl, err := getTmpl("hellovr.tmpl.html")
if err != nil {
w.Header().Set("Content-Type", "text/plain")
http.Error(w, "template error: "+err.Error(), 500)

View File

@ -0,0 +1,16 @@
<html>
<head>
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
</head>
<body>
<a-scene>
<a-text position="-1.65 2 -5" color="#00FF00" value="You're connected over Tailscale!"></a-text>
<a-text position="0.5 1 -5" color="#00FF00" value="This device is signed in as:\n{{.DisplayName}}\n{{.LoginName}}\n{{.MachineName}}\n{{.IP}}"></a-text>
<a-sky color="#000000"></a-sky>
<a-image position="-0.75 0.25 -5" src="{{.ProfilePicURL}}" height=2 width=2></a-image>
<a-entity>
<a-entity camera look-controls wasd-controls></a-entity>
</a-entity>
</a-scene>
</body>
</html>