tsnet

package module
v0.9.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2023 License: BSD-3-Clause Imports: 12 Imported by: 4

README

go-http-server-tsnet

Experimental Tailscale tsnet implementation of the aaronland/go-http-server interfaces.

This package creates a Tailscale virtual private service for HTTP resources. It wraps all the http.Handler instances in a middleware wrapper that derives and stores the Tailscale user and machine that is accessing the server.

Documentation

Go Reference

Known-knowns

  • Automagic TLS (port 443) certificates don't work with errors whose words I understand but not their meaning.
  • Under the hood there is extra undocumented hoop-jumping to make passing TS auth keys as query parameters (to the server URI constructor) work.

Example

Error handling removed for the sake of brevity.

$> go run cmd/example/main.go \
	-server-uri 'tsnet://testing:80?auth-key={TS_AUTH_KEY}'

Or:

package main

import (
	"context"
	"flag"
	"net/http"

	"github.com/aaronland/go-http-server"
	_ "github.com/aaronland/go-http-server-tsnet"
	"github.com/aaronland/go-http-server-tsnet/http/www"
)

func main() {

	server_uri := flag.String("server-uri", "tsnet://testing:80", "A valid aaronland/go-http-server URI.")
	flag.Parse()

	ctx := context.Background()
	s, _ := server.NewServer(ctx, *server_uri)

	handler := www.ExampleHandler()

	mux := http.NewServeMux()
	mux.Handle("/", handler)

	log.Printf("Listening on %s", s.Address())
	s.ListenAndServe(ctx, mux)
}

And:

package www

import (
	"fmt"
	"net/http"

	"github.com/aaronland/go-http-server-tsnet"
)

func ExampleHandler() http.Handler {

	fn := func(rsp http.ResponseWriter, req *http.Request) {

		// tsnet.SetWhoIs is called/assigned by the middleware handler
		// implemented in tsnet.go
		
		who, _ := tsnet.GetWhoIs(req)

		login_name := who.UserProfile.LoginName
		computed_name := who.Node.ComputedName

		msg := fmt.Sprintf("Hello, %s (%s)", login_name, computed_name)
		rsp.Write([]byte(msg))
	}

	h := http.HandlerFunc(fn)
	return h
}

See also

Documentation

Overview

Package tsnet provides metehods for creating Tailscale virtual private service for HTTP resources.

Index

Constants

View Source
const WHOIS_CONTEXT_KEY string = "x-urn:aaronland:tsnet#whois"

WHOIS_CONTEXT_KEY is the key used to store `apitype.WhoIsResponse` instances in a `context.Context` instance.

Variables

This section is empty.

Functions

func GetWhoIs

func GetWhoIs(req *http.Request) (*apitype.WhoIsResponse, error)

GetWhoIs will return the Tailscale `apitype.WhoIsResponse` instance stored in the 'req.Context'.

func NewTSNetServer

func NewTSNetServer(ctx context.Context, uri string) (server.Server, error)

NewTSNetServer returns a new `TSNetServer` instance configured by 'uri' which is expected to be defined in the form of:

tsnet://{HOSTNAME}:{PORT}?{PARAMETERS}

Valid parameters are: * `auth-key` is a valid Tailscale auth key. If absent it is assumed that a valid `TS_AUTH_KEY` environment variable has already been set.

func SetWhoIs

func SetWhoIs(req *http.Request, who *apitype.WhoIsResponse) *http.Request

SetWhoIs will store 'who' in 'req.Context'.

Types

type TSNetServer

type TSNetServer struct {
	server.Server
	// contains filtered or unexported fields
}

TSNServer implements the `Server` interface for a `net/http` server running as a Tailscale virtual private service.

func (*TSNetServer) Address

func (s *TSNetServer) Address() string

Address returns the fully-qualified URI where the server instance can be contacted.

func (*TSNetServer) ListenAndServe

func (s *TSNetServer) ListenAndServe(ctx context.Context, mux http.Handler) error

ListenAndServe starts the server and listens for requests using 'mux' for routing. Additionally each handler in mux will be wrapped by a middleware handler that will ensure a Tailscale `api.WhoIsResponse` instance can be derived from the current request and then store that value in the request's context. This value can be retrieved using the `GetWhoIs` method.

Directories

Path Synopsis
cmd
example
example is a command-line application demonstrating how a tsnet-enabled HTTP server works.
example is a command-line application demonstrating how a tsnet-enabled HTTP server works.
Package http provides methods for creating tsnet-specific `http.Handler` instances
Package http provides methods for creating tsnet-specific `http.Handler` instances
www
Package www provides user-facing HTTP handlers
Package www provides user-facing HTTP handlers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL