-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsnetdemo.go
53 lines (49 loc) · 1.56 KB
/
tsnetdemo.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
// This code currently needs https://github.com/tailscale/tailscale/pull/3275
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"os"
"tailscale.com/client/tailscale"
"tailscale.com/tsnet"
)
func main() {
os.Setenv("TAILSCALE_USE_WIP_CODE", "true")
// TODO: comment out this line to avoid having to re-login each time you start this program
os.Setenv("TS_LOGIN", "1")
os.Setenv("HOME", "/perm/tsnetdemo")
hostname := flag.String("hostname", "tsnetdemo", "tailscale hostname")
allowedUser := flag.String("allowed_user", "", "the name of a tailscale user to allow")
flag.Parse()
s := &tsnet.Server{
Hostname: *hostname,
}
log.Printf("starting tailscale listener on hostname %s", *hostname)
ln, err := s.Listen("tcp", ":443")
if err != nil {
log.Fatal(err)
}
ln = tls.NewListener(ln, &tls.Config{
GetCertificate: tailscale.GetCertificate,
})
httpsrv := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
who, err := tailscale.WhoIs(r.Context(), r.RemoteAddr)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if who.UserProfile.LoginName != *allowedUser || *allowedUser == "" {
err := fmt.Sprintf("you are logged in as %q, but -allowed_user flag does not match!", who.UserProfile.LoginName)
log.Printf("forbidden: %v", err)
http.Error(w, err, http.StatusForbidden)
return
}
fmt.Fprintf(w, "hey there, %q! this message is served via the tsnet package from gokrazy!", who.UserProfile.LoginName)
}),
}
log.Fatal(httpsrv.Serve(ln))
}