-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
114 lines (97 loc) · 3.46 KB
/
main.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// This code is forked from Tailscale codebase which is governed by
// a BSD-style licence. See https://github.com/tailscale/tailscale
//
// The link below is the code from which this code originates:
// https://github.com/tailscale/tailscale/blob/741ae9956e674177687062b5499a80db83505076/cmd/nginx-auth/nginx-auth.go
package main
import (
"flag"
"log"
"net"
"net/http"
"net/netip"
"net/url"
"strings"
"tailscale.com/client/tailscale"
)
var (
listenProto = flag.String("network", "tcp", "type of network to listen on, defaults to tcp")
listenAddr = flag.String("addr", "127.0.0.1:", "address to listen on, defaults to 127.0.0.1:")
headerRemoteIP = flag.String("remote-ip-header", "X-Forwarded-For", "HTTP header field containing the remote IP")
headerRemotePort = flag.String("remote-port-header", "X-Forwarded-Port", "HTTP header field containing the remote port")
debug = flag.Bool("debug", false, "enable debug logging")
)
func main() {
flag.Parse()
if *listenAddr == "" {
log.Fatal("listen address not set")
}
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Printf("received request with header %+v", r.Header)
}
remoteHost := r.Header.Get(*headerRemoteIP)
if remoteHost == "" {
w.WriteHeader(http.StatusBadRequest)
log.Printf("missing header %s", *headerRemoteIP)
return
}
remotePort := r.Header.Get(*headerRemotePort)
if remotePort == "" {
w.WriteHeader(http.StatusBadRequest)
log.Printf("missing header %s", *headerRemotePort)
return
}
remoteAddr, err := netip.ParseAddrPort(net.JoinHostPort(remoteHost, remotePort))
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
log.Printf("remote address and port are not valid: %v", err)
return
}
client := &tailscale.LocalClient{}
info, err := client.WhoIs(r.Context(), remoteAddr.String())
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
log.Printf("can't look up %s: %v", remoteAddr, err)
return
}
if len(info.Node.Tags) != 0 {
w.WriteHeader(http.StatusForbidden)
log.Printf("node %s is tagged", info.Node.Hostinfo.Hostname())
return
}
// tailnet of connected node. When accessing shared nodes, this
// will be empty because the tailnet of the sharee is not exposed.
var tailnet string
if !info.Node.Hostinfo.ShareeNode() {
var ok bool
_, tailnet, ok = strings.Cut(info.Node.Name, info.Node.ComputedName+".")
if !ok {
w.WriteHeader(http.StatusUnauthorized)
log.Printf("can't extract tailnet name from hostname %q", info.Node.Name)
return
}
tailnet = strings.TrimSuffix(tailnet, ".beta.tailscale.net")
}
if expectedTailnet := r.Header.Get("Expected-Tailnet"); expectedTailnet != "" && expectedTailnet != tailnet {
w.WriteHeader(http.StatusForbidden)
log.Printf("user is part of tailnet %s, wanted: %s", tailnet, url.QueryEscape(expectedTailnet))
return
}
h := w.Header()
h.Set("Tailscale-Login", strings.Split(info.UserProfile.LoginName, "@")[0])
h.Set("Tailscale-User", info.UserProfile.LoginName)
h.Set("Tailscale-Name", info.UserProfile.DisplayName)
h.Set("Tailscale-Profile-Picture", info.UserProfile.ProfilePicURL)
h.Set("Tailscale-Tailnet", tailnet)
w.WriteHeader(http.StatusNoContent)
})
ln, err := net.Listen(*listenProto, *listenAddr)
if err != nil {
log.Fatalf("can't listen on %s: %v", *listenAddr, err)
}
defer ln.Close()
log.Printf("listening on %s", ln.Addr())
log.Fatal(http.Serve(ln, mux))
}