Home

tailroute @main - refs - log -
-
https://git.jolheiser.com/tailroute.git
Router for tailnet and funnel across tailscale
tailroute / cmd / tailproxy / main.go
- raw -
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
	"flag"
	"fmt"
	"net/http"
	"net/http/httputil"
	"net/url"
	"os"

	"go.jolheiser.com/tailroute"
)

// Args are commandline arguments
type Args struct {
	Hostname string
	AuthKey  string
	Funnel   bool
	DataDir  string
	Port     int
}

func main() {
	args := &Args{
		DataDir: ".tailproxy",
	}
	fs := flag.NewFlagSet("tailproxy", flag.ExitOnError)
	fs.StringVar(&args.Hostname, "hostname", args.Hostname, "Tailscale hostname")
	fs.StringVar(&args.AuthKey, "auth-key", args.AuthKey, "Tailscale auth key")
	fs.BoolVar(&args.Funnel, "funnel", args.Funnel, "Expose on Tailscale funnel")
	fs.StringVar(&args.DataDir, "data-dir", args.DataDir, "tsnet data directory")
	fs.IntVar(&args.Port, "port", args.Port, "Port to proxy")
	if err := fs.Parse(os.Args[1:]); err != nil {
		panic(err)
	}
	os.Setenv("TS_AUTHKEY", args.AuthKey)

	proxyURL, err := url.Parse(fmt.Sprintf("http://localhost:%d", args.Port))
	if err != nil {
		panic(err)
	}

	proxy := httputil.NewSingleHostReverseProxy(proxyURL)
	handler := http.HandlerFunc(proxy.ServeHTTP)

	r := tailroute.Router{
		Tailnet: handler,
	}
	if args.Funnel {
		r.Funnel = handler
	}

	if err := r.Serve(args.Hostname, args.DataDir); err != nil {
		panic(err)
	}
}