2021-01-29 00:06:23 +00:00
|
|
|
// Copyright (c) 2021 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.
|
|
|
|
|
|
|
|
// The hello binary runs hello.ipn.dev.
|
|
|
|
package main // import "tailscale.com/cmd/hello"
|
|
|
|
|
|
|
|
import (
|
2021-01-29 21:18:23 +00:00
|
|
|
"context"
|
2021-03-02 02:47:27 +00:00
|
|
|
_ "embed"
|
2021-01-29 00:06:23 +00:00
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-02-15 19:16:10 +00:00
|
|
|
"os"
|
2021-01-29 00:06:23 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-03-02 03:00:37 +00:00
|
|
|
"tailscale.com/client/tailscale"
|
2021-04-13 15:13:46 +00:00
|
|
|
"tailscale.com/client/tailscale/apitype"
|
2021-01-29 00:06:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
httpAddr = flag.String("http", ":80", "address to run an HTTP server on, or empty for none")
|
|
|
|
httpsAddr = flag.String("https", ":443", "address to run an HTTPS server on, or empty for none")
|
2021-02-15 19:16:10 +00:00
|
|
|
testIP = flag.String("test-ip", "", "if non-empty, look up IP and exit before running a server")
|
2021-01-29 00:06:23 +00:00
|
|
|
)
|
|
|
|
|
2021-03-02 02:47:27 +00:00
|
|
|
//go:embed hello.tmpl.html
|
|
|
|
var embeddedTemplate string
|
|
|
|
|
2021-01-29 00:06:23 +00:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
2021-02-15 19:16:10 +00:00
|
|
|
if *testIP != "" {
|
2021-03-02 03:00:37 +00:00
|
|
|
res, err := tailscale.WhoIs(context.Background(), *testIP)
|
2021-02-15 19:16:10 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
e := json.NewEncoder(os.Stdout)
|
|
|
|
e.SetIndent("", "\t")
|
|
|
|
e.Encode(res)
|
|
|
|
return
|
|
|
|
}
|
2021-03-02 03:00:37 +00:00
|
|
|
if devMode() {
|
|
|
|
// Parse it optimistically
|
|
|
|
var err error
|
|
|
|
tmpl, err = template.New("home").Parse(embeddedTemplate)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ignoring template error in dev mode: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-02 02:47:27 +00:00
|
|
|
if embeddedTemplate == "" {
|
|
|
|
log.Fatalf("embeddedTemplate is empty; must be build with Go 1.16+")
|
|
|
|
}
|
|
|
|
tmpl = template.Must(template.New("home").Parse(embeddedTemplate))
|
2021-02-11 18:53:33 +00:00
|
|
|
}
|
2021-01-29 00:06:23 +00:00
|
|
|
|
|
|
|
http.HandleFunc("/", root)
|
|
|
|
log.Printf("Starting hello server.")
|
|
|
|
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
if *httpAddr != "" {
|
|
|
|
log.Printf("running HTTP server on %s", *httpAddr)
|
|
|
|
go func() {
|
|
|
|
errc <- http.ListenAndServe(*httpAddr, nil)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if *httpsAddr != "" {
|
|
|
|
log.Printf("running HTTPS server on %s", *httpsAddr)
|
|
|
|
go func() {
|
|
|
|
errc <- http.ListenAndServeTLS(*httpsAddr,
|
|
|
|
"/etc/hello/hello.ipn.dev.crt",
|
|
|
|
"/etc/hello/hello.ipn.dev.key",
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
log.Fatal(<-errc)
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:53:33 +00:00
|
|
|
func devMode() bool { return *httpsAddr == "" && *httpAddr != "" }
|
|
|
|
|
|
|
|
func getTmpl() (*template.Template, error) {
|
|
|
|
if devMode() {
|
2021-03-02 03:00:37 +00:00
|
|
|
tmplData, err := ioutil.ReadFile("hello.tmpl.html")
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Printf("using baked-in template in dev mode; can't find hello.tmpl.html in current directory")
|
|
|
|
return tmpl, nil
|
|
|
|
}
|
|
|
|
return template.New("home").Parse(string(tmplData))
|
2021-02-11 18:53:33 +00:00
|
|
|
}
|
|
|
|
return tmpl, nil
|
|
|
|
}
|
|
|
|
|
2021-03-02 03:00:37 +00:00
|
|
|
// tmpl is the template used in prod mode.
|
|
|
|
// In dev mode it's only used if the template file doesn't exist on disk.
|
|
|
|
// It's initialized by main after flag parsing.
|
|
|
|
var tmpl *template.Template
|
2021-01-29 00:06:23 +00:00
|
|
|
|
|
|
|
type tmplData struct {
|
2021-02-11 22:42:07 +00:00
|
|
|
DisplayName string // "Foo Barberson"
|
|
|
|
LoginName string // "foo@bar.com"
|
|
|
|
ProfilePicURL string // "https://..."
|
|
|
|
MachineName string // "imac5k"
|
|
|
|
MachineOS string // "Linux"
|
|
|
|
IP string // "100.2.3.4"
|
2021-01-29 00:06:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 15:13:46 +00:00
|
|
|
func tailscaleIP(who *apitype.WhoIsResponse) string {
|
2021-03-15 21:59:35 +00:00
|
|
|
if who == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, nodeIP := range who.Node.Addresses {
|
|
|
|
if nodeIP.IP.Is4() && nodeIP.IsSingleIP() {
|
|
|
|
return nodeIP.IP.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, nodeIP := range who.Node.Addresses {
|
|
|
|
if nodeIP.IsSingleIP() {
|
|
|
|
return nodeIP.IP.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2021-01-29 00:06:23 +00:00
|
|
|
func root(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 != "/" {
|
|
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
return
|
|
|
|
}
|
2021-02-11 18:53:33 +00:00
|
|
|
tmpl, err := getTmpl()
|
|
|
|
if err != nil {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
http.Error(w, "template error: "+err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-02 03:00:37 +00:00
|
|
|
who, err := tailscale.WhoIs(r.Context(), r.RemoteAddr)
|
2021-02-11 18:57:08 +00:00
|
|
|
var data tmplData
|
2021-01-29 00:06:23 +00:00
|
|
|
if err != nil {
|
2021-02-11 18:57:08 +00:00
|
|
|
if devMode() {
|
2021-02-15 19:16:10 +00:00
|
|
|
log.Printf("warning: using fake data in dev mode due to whois lookup error: %v", err)
|
2021-02-11 18:57:08 +00:00
|
|
|
data = tmplData{
|
2021-02-11 22:42:07 +00:00
|
|
|
DisplayName: "Taily Scalerson",
|
|
|
|
LoginName: "taily@scaler.son",
|
|
|
|
ProfilePicURL: "https://placekitten.com/200/200",
|
|
|
|
MachineName: "scaled",
|
|
|
|
MachineOS: "Linux",
|
|
|
|
IP: "100.1.2.3",
|
2021-02-11 18:57:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-02 03:00:37 +00:00
|
|
|
log.Printf("whois(%q) error: %v", r.RemoteAddr, err)
|
2021-02-11 18:57:08 +00:00
|
|
|
http.Error(w, "Your Tailscale works, but we failed to look you up.", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data = tmplData{
|
2021-02-11 22:42:07 +00:00
|
|
|
DisplayName: who.UserProfile.DisplayName,
|
|
|
|
LoginName: who.UserProfile.LoginName,
|
|
|
|
ProfilePicURL: who.UserProfile.ProfilePicURL,
|
2021-02-12 00:38:26 +00:00
|
|
|
MachineName: firstLabel(who.Node.ComputedName),
|
2021-02-11 22:42:07 +00:00
|
|
|
MachineOS: who.Node.Hostinfo.OS,
|
2021-03-15 21:59:35 +00:00
|
|
|
IP: tailscaleIP(who),
|
2021-02-11 18:57:08 +00:00
|
|
|
}
|
2021-01-29 00:06:23 +00:00
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
2021-02-11 18:57:08 +00:00
|
|
|
tmpl.Execute(w, data)
|
2021-01-29 00:06:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 00:38:26 +00:00
|
|
|
// firstLabel s up until the first period, if any.
|
|
|
|
func firstLabel(s string) string {
|
|
|
|
if i := strings.Index(s, "."); i != -1 {
|
|
|
|
return s[:i]
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|