mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-29 04:55:31 +00:00
71029cea2d
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// The tshello server demonstrates how to use Tailscale as a library.
|
|
package main
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"flag"
|
|
"fmt"
|
|
"html"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"tailscale.com/tsnet"
|
|
)
|
|
|
|
var (
|
|
addr = flag.String("addr", ":80", "address to listen on")
|
|
)
|
|
|
|
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)
|
|
}
|
|
|
|
if *addr == ":443" {
|
|
ln = tls.NewListener(ln, &tls.Config{
|
|
GetCertificate: lc.GetCertificate,
|
|
})
|
|
}
|
|
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
|
|
}
|