mirror of
https://github.com/tailscale/tailscale.git
synced 2026-05-05 12:16:44 +02:00
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
258 lines
6.9 KiB
Go
258 lines
6.9 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package tsnet_test
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"tailscale.com/tsnet"
|
|
)
|
|
|
|
// ExampleServer shows you how to construct a ready-to-use tsnet instance.
|
|
func ExampleServer() {
|
|
srv := new(tsnet.Server)
|
|
if err := srv.Start(); err != nil {
|
|
log.Fatalf("can't start tsnet server: %v", err)
|
|
}
|
|
defer srv.Close()
|
|
}
|
|
|
|
// ExampleServer_hostname shows you how to set a tsnet server's hostname.
|
|
//
|
|
// This setting lets you control the host name of your program on your
|
|
// tailnet. By default this will be the name of your program (such as foo
|
|
// for a program stored at /usr/local/bin/foo). You can also override this
|
|
// by setting the Hostname field.
|
|
func ExampleServer_hostname() {
|
|
srv := &tsnet.Server{
|
|
Hostname: "kirito",
|
|
}
|
|
|
|
// do something with srv
|
|
_ = srv
|
|
}
|
|
|
|
// ExampleServer_dir shows you how to configure the persistent directory for
|
|
// a tsnet application. This is where the Tailscale node information is stored
|
|
// so that your application can reconnect to your tailnet when the application
|
|
// is restarted.
|
|
//
|
|
// By default, tsnet will store data in your user configuration directory based
|
|
// on the name of the binary. Note that this folder must already exist or tsnet
|
|
// calls will fail.
|
|
func ExampleServer_dir() {
|
|
dir := filepath.Join("/data", "tsnet")
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
srv := &tsnet.Server{
|
|
Dir: dir,
|
|
}
|
|
|
|
// do something with srv
|
|
_ = srv
|
|
}
|
|
|
|
// ExampleServer_multipleInstances shows you how to configure multiple instances
|
|
// of tsnet per program. This allows you to have multiple Tailscale nodes in the
|
|
// same process/container.
|
|
func ExampleServer_multipleInstances() {
|
|
baseDir := "/data"
|
|
var servers []*tsnet.Server
|
|
for _, hostname := range []string{"ichika", "nino", "miku", "yotsuba", "itsuki"} {
|
|
os.MkdirAll(filepath.Join(baseDir, hostname), 0700)
|
|
srv := &tsnet.Server{
|
|
Hostname: hostname,
|
|
AuthKey: os.Getenv("TS_AUTHKEY"),
|
|
Ephemeral: true,
|
|
Dir: filepath.Join(baseDir, hostname),
|
|
}
|
|
if err := srv.Start(); err != nil {
|
|
log.Fatalf("can't start tsnet server: %v", err)
|
|
}
|
|
servers = append(servers, srv)
|
|
}
|
|
|
|
// When you're done, close the instances
|
|
defer func() {
|
|
for _, srv := range servers {
|
|
srv.Close()
|
|
}
|
|
}()
|
|
}
|
|
|
|
// ExampleServer_ignoreLogsSometimes shows you how to ignore all of the log messages
|
|
// written by a tsnet instance, but allows you to opt-into them if a command-line
|
|
// flag is set.
|
|
func ExampleServer_ignoreLogsSometimes() {
|
|
tsnetVerbose := flag.Bool("tsnet-verbose", false, "if set, verbosely log tsnet information")
|
|
hostname := flag.String("tsnet-hostname", "hikari", "hostname to use on the tailnet")
|
|
|
|
srv := &tsnet.Server{
|
|
Hostname: *hostname,
|
|
}
|
|
|
|
if *tsnetVerbose {
|
|
srv.Logf = log.New(os.Stderr, fmt.Sprintf("[tsnet:%s] ", *hostname), log.LstdFlags).Printf
|
|
}
|
|
}
|
|
|
|
// ExampleServer_HTTPClient shows you how to make HTTP requests over your tailnet.
|
|
//
|
|
// If you want to make outgoing HTTP connections to resources on your tailnet, use
|
|
// the HTTP client that the tsnet.Server exposes.
|
|
func ExampleServer_HTTPClient() {
|
|
srv := &tsnet.Server{}
|
|
cli := srv.HTTPClient()
|
|
|
|
resp, err := cli.Get("https://hello.ts.net")
|
|
if resp == nil {
|
|
log.Fatal(err)
|
|
}
|
|
// do something with resp
|
|
_ = resp
|
|
}
|
|
|
|
// ExampleServer_Start demonstrates the Start method, which should be called if
|
|
// you need to explicitly start it. Note that the Start method is implicitly
|
|
// called if needed.
|
|
func ExampleServer_Start() {
|
|
srv := new(tsnet.Server)
|
|
|
|
if err := srv.Start(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Be sure to close the server instance at some point. It will stay open until
|
|
// either the OS process ends or the server is explicitly closed.
|
|
defer srv.Close()
|
|
}
|
|
|
|
// ExampleServer_Listen shows you how to create a TCP listener on your tailnet and
|
|
// then makes an HTTP server on top of that.
|
|
func ExampleServer_Listen() {
|
|
srv := &tsnet.Server{
|
|
Hostname: "tadaima",
|
|
}
|
|
|
|
ln, err := srv.Listen("tcp", ":80")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Fatal(http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, "Hi there! Welcome to the tailnet!")
|
|
})))
|
|
}
|
|
|
|
// ExampleServer_ListenTLS shows you how to create a TCP listener on your tailnet and
|
|
// then makes an HTTPS server on top of that.
|
|
func ExampleServer_ListenTLS() {
|
|
srv := &tsnet.Server{
|
|
Hostname: "aegis",
|
|
}
|
|
|
|
ln, err := srv.ListenTLS("tcp", ":443")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Fatal(http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, "Hi there! Welcome to the tailnet!")
|
|
})))
|
|
}
|
|
|
|
// ExampleServer_ListenFunnel shows you how to create an HTTPS service on both your tailnet
|
|
// and the public internet via Funnel.
|
|
func ExampleServer_ListenFunnel() {
|
|
srv := &tsnet.Server{
|
|
Hostname: "ophion",
|
|
}
|
|
|
|
ln, err := srv.ListenFunnel("tcp", ":443")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Fatal(http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, "Hi there! Welcome to the tailnet!")
|
|
})))
|
|
}
|
|
|
|
// ExampleServer_ListenFunnel_funnelOnly shows you how to create a funnel-only HTTPS service.
|
|
func ExampleServer_ListenFunnel_funnelOnly() {
|
|
srv := new(tsnet.Server)
|
|
srv.Hostname = "ophion"
|
|
ln, err := srv.ListenFunnel("tcp", ":443", tsnet.FunnelOnly())
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Fatal(http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, "Hi there! Welcome to the tailnet!")
|
|
})))
|
|
}
|
|
|
|
// ExampleServer_ListenService demonstrates how to advertise an HTTPS Service.
|
|
func ExampleServer_ListenService() {
|
|
s := &tsnet.Server{
|
|
Hostname: "tsnet-services-demo",
|
|
}
|
|
defer s.Close()
|
|
|
|
ln, err := s.ListenService("svc:my-service", tsnet.ServiceModeHTTP{
|
|
HTTPS: true,
|
|
Port: 443,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer ln.Close()
|
|
|
|
log.Printf("Listening on https://%v\n", ln.FQDN)
|
|
log.Fatal(http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, "<html><body><h1>Hello, tailnet!</h1>")
|
|
})))
|
|
}
|
|
|
|
// ExampleServer_ListenService_reverseProxy demonstrates how to advertise a
|
|
// Service targeting a reverse proxy. This is useful when the backing server is
|
|
// external to the tsnet application.
|
|
func ExampleServer_ListenService_reverseProxy() {
|
|
// targetAddress represents the address of the backing server.
|
|
const targetAddress = "1.2.3.4:80"
|
|
|
|
// We will use a reverse proxy to direct traffic to the backing server.
|
|
reverseProxy := httputil.NewSingleHostReverseProxy(&url.URL{
|
|
Scheme: "http",
|
|
Host: targetAddress,
|
|
})
|
|
|
|
s := &tsnet.Server{
|
|
Hostname: "tsnet-services-demo",
|
|
}
|
|
defer s.Close()
|
|
|
|
ln, err := s.ListenService("svc:my-service", tsnet.ServiceModeHTTP{
|
|
HTTPS: true,
|
|
Port: 443,
|
|
})
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer ln.Close()
|
|
|
|
log.Printf("Listening on https://%v\n", ln.FQDN)
|
|
log.Fatal(http.Serve(ln, reverseProxy))
|
|
}
|