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.
()
| 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.). |
| 39 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…