diff options
| author | Xe Iaso <me@xeiaso.net> | 2023-03-18 15:56:50 -0400 |
|---|---|---|
| committer | Xe Iaso <me@xeiaso.net> | 2023-03-18 15:57:01 -0400 |
| commit | e02edb4eb42ba2c1c397a802c60564436635035d (patch) | |
| tree | 6e2d1e578d21add918347c90bb688b4b23490e8e /cmd | |
| parent | d920c709b6cd878547fb9d165d53ad2a54d43a5f (diff) | |
| download | x-e02edb4eb42ba2c1c397a802c60564436635035d.tar.xz x-e02edb4eb42ba2c1c397a802c60564436635035d.zip | |
test
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/tshello/main.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/cmd/tshello/main.go b/cmd/tshello/main.go new file mode 100644 index 0000000..59c688f --- /dev/null +++ b/cmd/tshello/main.go @@ -0,0 +1,52 @@ +// The tshello server demonstrates how to use Tailscale as a library. +package main + +import ( + "flag" + "fmt" + "html" + "log" + "net/http" + "strings" + + "tailscale.com/tsnet" +) + +var ( + addr = flag.String("addr", ":80", "address to listen on") + hostname = flag.String("hostname", "tshello", "hostname to use on the tailnet") +) + +func main() { + flag.Parse() + s := new(tsnet.Server) + defer s.Close() + ln, err := s.Listen("tcp", *addr) + if err != nil { + log.Fatal(err) + } + defer ln.Close() + + lc, err := s.LocalClient() + if err != nil { + log.Fatal(err) + } + + log.Fatal(http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + who, err := lc.WhoIs(r.Context(), r.RemoteAddr) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + fmt.Fprintf(w, "<html><body><h1>Hello, world!</h1>\n") + fmt.Fprintf(w, "<p>You are <b>%s</b> from <b>%s</b> (%s)</p>", + html.EscapeString(who.UserProfile.LoginName), + html.EscapeString(firstLabel(who.Node.ComputedName)), + r.RemoteAddr) + }))) +} + +func firstLabel(s string) string { + s, _, _ = strings.Cut(s, ".") + return s +} |
