MCPcopy
hub / github.com/tailscale/tailscale / Example_tshello

Function Example_tshello

tsnet/example_tshello_test.go:39–72  ·  view source on GitHub ↗

Example_tshello is a full example on using tsnet. When you run this program it will print an authentication link. Open it in your favorite web browser and add it to your tailnet like any other machine. Open another terminal window and try to ping it: $ ping tshello -c 2 PING tshello (100.105.183.

()

Source from the content-addressed store, hash-verified

37// From here you can do anything you want with the Go standard library HTTP stack, or anything
38// that is compatible with it (Gin/Gonic, Gorilla/mux, etc.).
39func Example_tshello() {
40 var (
41 addr = flag.String("addr", ":80", "address to listen on")
42 hostname = flag.String("hostname", "tshello", "hostname to use on the tailnet")
43 )
44
45 flag.Parse()
46 s := new(tsnet.Server)
47 s.Hostname = *hostname
48 defer s.Close()
49 ln, err := s.Listen("tcp", *addr)
50 if err != nil {
51 log.Fatal(err)
52 }
53 defer ln.Close()
54
55 lc, err := s.LocalClient()
56 if err != nil {
57 log.Fatal(err)
58 }
59
60 log.Fatal(http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
61 who, err := lc.WhoIs(r.Context(), r.RemoteAddr)
62 if err != nil {
63 http.Error(w, err.Error(), 500)
64 return
65 }
66 fmt.Fprintf(w, "<html><body><h1>Hello, tailnet!</h1>\n")
67 fmt.Fprintf(w, "<p>You are <b>%s</b> from <b>%s</b> (%s)</p>",
68 html.EscapeString(who.UserProfile.LoginName),
69 html.EscapeString(firstLabel(who.Node.ComputedName)),
70 r.RemoteAddr)
71 })))
72}

Callers

nothing calls this directly

Calls 11

firstLabelFunction · 0.70
StringMethod · 0.65
CloseMethod · 0.65
ListenMethod · 0.65
FatalMethod · 0.65
WhoIsMethod · 0.65
ContextMethod · 0.65
ErrorMethod · 0.65
ParseMethod · 0.45
LocalClientMethod · 0.45
ServeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…