diff options
| author | Nick Khyl <nickk@tailscale.com> | 2024-12-05 13:16:48 -0600 |
|---|---|---|
| committer | Nick Khyl <nickk@tailscale.com> | 2024-12-05 13:16:48 -0600 |
| commit | 0267fe83b200f1702a2fa0a395442c02a053fadb (patch) | |
| tree | 63654c55225eeb834de59a5a0bc8d19033c6145b /tsnet | |
| parent | 87546a5edf6b6503a87eeb2d666baba57398a066 (diff) | |
| download | tailscale-1.78.0.tar.xz tailscale-1.78.0.zip | |
VERSION.txt: this is v1.78.0v1.78.0
Signed-off-by: Nick Khyl <nickk@tailscale.com>
Diffstat (limited to 'tsnet')
| -rw-r--r-- | tsnet/example/tshello/tshello.go | 120 | ||||
| -rw-r--r-- | tsnet/example/tsnet-http-client/tsnet-http-client.go | 88 | ||||
| -rw-r--r-- | tsnet/example/web-client/web-client.go | 92 | ||||
| -rw-r--r-- | tsnet/example_tshello_test.go | 144 |
4 files changed, 222 insertions, 222 deletions
diff --git a/tsnet/example/tshello/tshello.go b/tsnet/example/tshello/tshello.go index 0cadcdd83..2110c4d96 100644 --- a/tsnet/example/tshello/tshello.go +++ b/tsnet/example/tshello/tshello.go @@ -1,60 +1,60 @@ -// 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 -} +// 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
+}
diff --git a/tsnet/example/tsnet-http-client/tsnet-http-client.go b/tsnet/example/tsnet-http-client/tsnet-http-client.go index 9666fe999..cda52eef7 100644 --- a/tsnet/example/tsnet-http-client/tsnet-http-client.go +++ b/tsnet/example/tsnet-http-client/tsnet-http-client.go @@ -1,44 +1,44 @@ -// 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 ( - "flag" - "fmt" - "log" - "os" - "path/filepath" - - "tailscale.com/tsnet" -) - -func main() { - flag.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage: %s <url in tailnet>\n", filepath.Base(os.Args[0])) - os.Exit(2) - } - flag.Parse() - - if flag.NArg() != 1 { - flag.Usage() - } - tailnetURL := flag.Arg(0) - - s := new(tsnet.Server) - defer s.Close() - - if err := s.Start(); err != nil { - log.Fatal(err) - } - - cli := s.HTTPClient() - - resp, err := cli.Get(tailnetURL) - if err != nil { - log.Fatal(err) - } - - resp.Write(os.Stdout) -} +// 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 (
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "path/filepath"
+
+ "tailscale.com/tsnet"
+)
+
+func main() {
+ flag.Usage = func() {
+ fmt.Fprintf(os.Stderr, "Usage: %s <url in tailnet>\n", filepath.Base(os.Args[0]))
+ os.Exit(2)
+ }
+ flag.Parse()
+
+ if flag.NArg() != 1 {
+ flag.Usage()
+ }
+ tailnetURL := flag.Arg(0)
+
+ s := new(tsnet.Server)
+ defer s.Close()
+
+ if err := s.Start(); err != nil {
+ log.Fatal(err)
+ }
+
+ cli := s.HTTPClient()
+
+ resp, err := cli.Get(tailnetURL)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ resp.Write(os.Stdout)
+}
diff --git a/tsnet/example/web-client/web-client.go b/tsnet/example/web-client/web-client.go index 541efbaed..dee7fedfa 100644 --- a/tsnet/example/web-client/web-client.go +++ b/tsnet/example/web-client/web-client.go @@ -1,46 +1,46 @@ -// Copyright (c) Tailscale Inc & AUTHORS -// SPDX-License-Identifier: BSD-3-Clause - -// The web-client command demonstrates serving the Tailscale web client over tsnet. -package main - -import ( - "flag" - "log" - "net/http" - - "tailscale.com/client/web" - "tailscale.com/tsnet" -) - -var ( - addr = flag.String("addr", "localhost:8060", "address of Tailscale web client") -) - -func main() { - flag.Parse() - - s := &tsnet.Server{RunWebClient: true} - defer s.Close() - - lc, err := s.LocalClient() - if err != nil { - log.Fatal(err) - } - - // Serve the Tailscale web client. - ws, err := web.NewServer(web.ServerOpts{ - Mode: web.LoginServerMode, - LocalClient: lc, - }) - if err != nil { - log.Fatal(err) - } - defer ws.Shutdown() - log.Printf("Serving Tailscale web client on http://%s", *addr) - if err := http.ListenAndServe(*addr, ws); err != nil { - if err != http.ErrServerClosed { - log.Fatal(err) - } - } -} +// Copyright (c) Tailscale Inc & AUTHORS
+// SPDX-License-Identifier: BSD-3-Clause
+
+// The web-client command demonstrates serving the Tailscale web client over tsnet.
+package main
+
+import (
+ "flag"
+ "log"
+ "net/http"
+
+ "tailscale.com/client/web"
+ "tailscale.com/tsnet"
+)
+
+var (
+ addr = flag.String("addr", "localhost:8060", "address of Tailscale web client")
+)
+
+func main() {
+ flag.Parse()
+
+ s := &tsnet.Server{RunWebClient: true}
+ defer s.Close()
+
+ lc, err := s.LocalClient()
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // Serve the Tailscale web client.
+ ws, err := web.NewServer(web.ServerOpts{
+ Mode: web.LoginServerMode,
+ LocalClient: lc,
+ })
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer ws.Shutdown()
+ log.Printf("Serving Tailscale web client on http://%s", *addr)
+ if err := http.ListenAndServe(*addr, ws); err != nil {
+ if err != http.ErrServerClosed {
+ log.Fatal(err)
+ }
+ }
+}
diff --git a/tsnet/example_tshello_test.go b/tsnet/example_tshello_test.go index d534bcfd1..4dec48233 100644 --- a/tsnet/example_tshello_test.go +++ b/tsnet/example_tshello_test.go @@ -1,72 +1,72 @@ -// Copyright (c) Tailscale Inc & AUTHORS -// SPDX-License-Identifier: BSD-3-Clause - -package tsnet_test - -import ( - "flag" - "fmt" - "html" - "log" - "net/http" - "strings" - - "tailscale.com/tsnet" -) - -func firstLabel(s string) string { - s, _, _ = strings.Cut(s, ".") - return s -} - -// 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.159) 56(84) bytes of data. -// 64 bytes from tshello.your-tailnet.ts.net (100.105.183.159): icmp_seq=1 ttl=64 time=25.0 ms -// 64 bytes from tshello.your-tailnet.ts.net (100.105.183.159): icmp_seq=2 ttl=64 time=1.12 ms -// -// Then connect to it using curl: -// -// $ curl http://tshello -// <html><body><h1>Hello, world!</h1> -// <p>You are <b>Xe</b> from <b>pneuma</b> (100.78.40.86:49214)</p> -// -// From here you can do anything you want with the Go standard library HTTP stack, or anything -// that is compatible with it (Gin/Gonic, Gorilla/mux, etc.). -func Example_tshello() { - var ( - addr = flag.String("addr", ":80", "address to listen on") - hostname = flag.String("hostname", "tshello", "hostname to use on the tailnet") - ) - - flag.Parse() - s := new(tsnet.Server) - s.Hostname = *hostname - 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, tailnet!</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) - }))) -} +// Copyright (c) Tailscale Inc & AUTHORS
+// SPDX-License-Identifier: BSD-3-Clause
+
+package tsnet_test
+
+import (
+ "flag"
+ "fmt"
+ "html"
+ "log"
+ "net/http"
+ "strings"
+
+ "tailscale.com/tsnet"
+)
+
+func firstLabel(s string) string {
+ s, _, _ = strings.Cut(s, ".")
+ return s
+}
+
+// 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.159) 56(84) bytes of data.
+// 64 bytes from tshello.your-tailnet.ts.net (100.105.183.159): icmp_seq=1 ttl=64 time=25.0 ms
+// 64 bytes from tshello.your-tailnet.ts.net (100.105.183.159): icmp_seq=2 ttl=64 time=1.12 ms
+//
+// Then connect to it using curl:
+//
+// $ curl http://tshello
+// <html><body><h1>Hello, world!</h1>
+// <p>You are <b>Xe</b> from <b>pneuma</b> (100.78.40.86:49214)</p>
+//
+// From here you can do anything you want with the Go standard library HTTP stack, or anything
+// that is compatible with it (Gin/Gonic, Gorilla/mux, etc.).
+func Example_tshello() {
+ var (
+ addr = flag.String("addr", ":80", "address to listen on")
+ hostname = flag.String("hostname", "tshello", "hostname to use on the tailnet")
+ )
+
+ flag.Parse()
+ s := new(tsnet.Server)
+ s.Hostname = *hostname
+ 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, tailnet!</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)
+ })))
+}
|
