forked from tailscale/golink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
golink.go
1013 lines (880 loc) · 25.6 KB
/
golink.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022 Tailscale Inc & Contributors
// SPDX-License-Identifier: BSD-3-Clause
// The golink server runs http://go/, a private shortlink service for tailnets.
package golink
import (
"bufio"
"bytes"
"context"
"crypto/rand"
"embed"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"html/template"
"io/fs"
"log"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"sync"
texttemplate "text/template"
"time"
"golang.org/x/net/xsrftoken"
"tailscale.com/client/tailscale"
"tailscale.com/hostinfo"
"tailscale.com/ipn"
"tailscale.com/tailcfg"
"tailscale.com/tsnet"
"tailscale.com/util/dnsname"
)
const (
defaultHostname = "go"
// Used as a placeholder short name for generating the XSRF defense token,
// when creating new links.
newShortName = ".new"
// If the caller sends this header set to a non-empty value, we will allow
// them to make the call even without an XSRF token. JavaScript in browser
// cannot set this header, per the [Fetch Spec].
//
// [Fetch Spec]: https://fetch.spec.whatwg.org
secHeaderName = "Sec-Golink"
)
var (
verbose = flag.Bool("verbose", false, "be verbose")
controlURL = flag.String("control-url", ipn.DefaultControlURL, "the URL base of the control plane (i.e. coordination server)")
sqlitefile = flag.String("sqlitedb", "", "path of SQLite database to store links")
dev = flag.String("dev-listen", "", "if non-empty, listen on this addr and run in dev mode; auto-set sqlitedb if empty and don't use tsnet")
useHTTPS = flag.Bool("https", true, "serve golink over HTTPS if enabled on tailnet")
snapshot = flag.String("snapshot", "", "file path of snapshot file")
hostname = flag.String("hostname", defaultHostname, "service name")
resolveFromBackup = flag.String("resolve-from-backup", "", "resolve a link from snapshot file and exit")
allowUnknownUsers = flag.Bool("allow-unknown-users", false, "allow unknown users to save links")
)
var stats struct {
mu sync.Mutex
clicks ClickStats // short link -> number of times visited
// dirty identifies short link clicks that have not yet been stored.
dirty ClickStats
}
// LastSnapshot is the data snapshot (as returned by the /.export handler)
// that will be loaded on startup.
var LastSnapshot []byte
//go:embed static tmpl/*.html tmpl/*.xml
var embeddedFS embed.FS
// db stores short links.
var db *SQLiteDB
var localClient *tailscale.LocalClient
func Run() error {
flag.Parse()
hostinfo.SetApp("golink")
// if resolving from backup, set sqlitefile and snapshot flags to
// restore links into an in-memory sqlite database.
if *resolveFromBackup != "" {
*sqlitefile = ":memory:"
snapshot = resolveFromBackup
if flag.NArg() != 1 {
log.Fatal("--resolve-from-backup also requires a link to be resolved")
}
}
if *sqlitefile == "" {
if devMode() {
tmpdir, err := os.MkdirTemp("", "golink_dev_*")
if err != nil {
return err
}
*sqlitefile = filepath.Join(tmpdir, "golink.db")
log.Printf("Dev mode temp db: %s", *sqlitefile)
} else {
return errors.New("--sqlitedb is required")
}
}
var err error
if db, err = NewSQLiteDB(*sqlitefile); err != nil {
return fmt.Errorf("NewSQLiteDB(%q): %w", *sqlitefile, err)
}
if *snapshot != "" {
if LastSnapshot != nil {
log.Printf("LastSnapshot already set; ignoring --snapshot")
} else {
var err error
LastSnapshot, err = os.ReadFile(*snapshot)
if err != nil {
log.Fatalf("error reading snapshot file %q: %v", *snapshot, err)
}
}
}
if err := restoreLastSnapshot(); err != nil {
log.Printf("restoring snapshot: %v", err)
}
if err := initStats(); err != nil {
log.Printf("initializing stats: %v", err)
}
// if link specified on command line, resolve and exit
if flag.NArg() > 0 {
u, err := url.Parse(flag.Arg(0))
if err != nil {
log.Fatal(err)
}
dst, err := resolveLink(u)
if err != nil {
log.Fatal(err)
}
fmt.Println(dst.String())
os.Exit(0)
}
// flush stats periodically
go flushStatsLoop()
if *dev != "" {
// override default hostname for dev mode
if *hostname == defaultHostname {
if h, p, err := net.SplitHostPort(*dev); err == nil {
if h == "" {
h = "localhost"
}
*hostname = fmt.Sprintf("%s:%s", h, p)
}
}
log.Printf("Running in dev mode on %s ...", *dev)
log.Fatal(http.ListenAndServe(*dev, serveHandler()))
}
if *hostname == "" {
return errors.New("--hostname, if specified, cannot be empty")
}
// create tsNet server and wait for it to be ready & connected.
srv := &tsnet.Server{
ControlURL: *controlURL,
Hostname: *hostname,
Logf: func(format string, args ...any) {},
RunWebClient: true,
}
if *verbose {
srv.Logf = log.Printf
}
if err := srv.Start(); err != nil {
return err
}
localClient, _ = srv.LocalClient()
out:
for {
upCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
status, err := srv.Up(upCtx)
if err == nil && status != nil {
break out
}
}
statusCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
status, err := localClient.Status(statusCtx)
if err != nil {
return err
}
enableTLS := *useHTTPS && status.Self.HasCap(tailcfg.CapabilityHTTPS) && len(srv.CertDomains()) > 0
fqdn := strings.TrimSuffix(status.Self.DNSName, ".")
httpHandler := serveHandler()
if enableTLS {
httpsHandler := HSTS(httpHandler)
httpHandler = redirectHandler(fqdn)
httpsListener, err := srv.ListenTLS("tcp", ":443")
if err != nil {
return err
}
log.Println("Listening on :443")
go func() {
log.Printf("Serving https://%s/ ...", fqdn)
if err := http.Serve(httpsListener, httpsHandler); err != nil {
log.Fatal(err)
}
}()
}
httpListener, err := srv.Listen("tcp", ":80")
log.Println("Listening on :80")
if err != nil {
return err
}
log.Printf("Serving http://%s/ ...", *hostname)
if err := http.Serve(httpListener, httpHandler); err != nil {
return err
}
return nil
}
var (
// homeTmpl is the template used by the http://go/ index page where you can
// create or edit links.
homeTmpl *template.Template
// detailTmpl is the template used by the link detail page to view or edit links.
detailTmpl *template.Template
// successTmpl is the template used when a link is successfully created or updated.
successTmpl *template.Template
// helpTmpl is the template used by the http://go/.help page
helpTmpl *template.Template
// allTmpl is the template used by the http://go/.all page
allTmpl *template.Template
// deleteTmpl is the template used after a link has been deleted.
deleteTmpl *template.Template
// opensearchTmpl is the template used by the http://go/.opensearch page
opensearchTmpl *template.Template
)
type visitData struct {
Short string
NumClicks int
}
// homeData is the data used by homeTmpl.
type homeData struct {
Short string
Long string
Clicks []visitData
XSRF string
}
// deleteData is the data used by deleteTmpl.
type deleteData struct {
Short string
Long string
XSRF string
}
var xsrfKey string
func init() {
homeTmpl = newTemplate("base.html", "home.html")
detailTmpl = newTemplate("base.html", "detail.html")
successTmpl = newTemplate("base.html", "success.html")
helpTmpl = newTemplate("base.html", "help.html")
allTmpl = newTemplate("base.html", "all.html")
deleteTmpl = newTemplate("base.html", "delete.html")
opensearchTmpl = newTemplate("opensearch.xml")
b := make([]byte, 24)
rand.Read(b)
xsrfKey = base64.StdEncoding.EncodeToString(b)
}
var tmplFuncs = template.FuncMap{
"go": func() string {
return *hostname
},
}
// newTemplate creates a new template with the specified files in the tmpl directory.
// The first file name is used as the template name,
// and tmplFuncs are registered as available funcs.
// This func panics if unable to parse files.
func newTemplate(files ...string) *template.Template {
if len(files) == 0 {
return nil
}
tf := make([]string, 0, len(files))
for _, f := range files {
tf = append(tf, "tmpl/"+f)
}
t := template.New(files[0]).Funcs(tmplFuncs)
return template.Must(t.ParseFS(embeddedFS, tf...))
}
// initStats initializes the in-memory stats counter with counts from db.
func initStats() error {
stats.mu.Lock()
defer stats.mu.Unlock()
clicks, err := db.LoadStats()
if err != nil {
return err
}
stats.clicks = clicks
stats.dirty = make(ClickStats)
return nil
}
// flushStats writes any pending link stats to db.
func flushStats() error {
stats.mu.Lock()
defer stats.mu.Unlock()
if len(stats.dirty) == 0 {
return nil
}
if err := db.SaveStats(stats.dirty); err != nil {
return err
}
stats.dirty = make(ClickStats)
return nil
}
// flushStatsLoop will flush stats every minute. This function never returns.
func flushStatsLoop() {
for {
if err := flushStats(); err != nil {
log.Printf("flushing stats: %v", err)
}
time.Sleep(time.Minute)
}
}
// deleteLinkStats removes the link stats from memory.
func deleteLinkStats(link *Link) {
stats.mu.Lock()
delete(stats.clicks, link.Short)
delete(stats.dirty, link.Short)
stats.mu.Unlock()
db.DeleteStats(link.Short)
}
// redirectHandler returns the http.Handler for serving all plaintext HTTP
// requests. It redirects all requests to the HTTPs version of the same URL.
func redirectHandler(hostname string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, (&url.URL{Scheme: "https", Host: hostname, Path: r.URL.Path}).String(), http.StatusFound)
})
}
// HSTS wraps the provided handler and sets Strict-Transport-Security header on
// responses. It inspects the Host header to ensure we do not specify HSTS
// response on non fully qualified domain name origins.
func HSTS(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
host, found := r.Header["Host"]
if found {
host := host[0]
fqdn, err := dnsname.ToFQDN(host)
if err == nil {
segCount := fqdn.NumLabels()
if segCount > 1 {
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
}
}
}
h.ServeHTTP(w, r)
})
}
// serverHandler returns the main http.Handler for serving all requests.
func serveHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/.detail/", serveDetail)
mux.HandleFunc("/.export", serveExport)
mux.HandleFunc("/.help", serveHelp)
mux.HandleFunc("/.opensearch", serveOpenSearch)
mux.HandleFunc("/.all", serveAll)
mux.HandleFunc("/.delete/", serveDelete)
mux.Handle("/.static/", http.StripPrefix("/.", http.FileServer(http.FS(embeddedFS))))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// all internal URLs begin with a leading "."; any other URL is treated as a go link.
// Serve go links directly without passing through the ServeMux,
// which sometimes modifies the request URL path, which we don't want.
if !strings.HasPrefix(r.URL.Path, "/.") {
serveGo(w, r)
return
}
mux.ServeHTTP(w, r)
})
}
func serveHome(w http.ResponseWriter, r *http.Request, short string) {
var clicks []visitData
stats.mu.Lock()
for short, numClicks := range stats.clicks {
clicks = append(clicks, visitData{
Short: short,
NumClicks: numClicks,
})
}
stats.mu.Unlock()
sort.Slice(clicks, func(i, j int) bool {
if clicks[i].NumClicks != clicks[j].NumClicks {
return clicks[i].NumClicks > clicks[j].NumClicks
}
return clicks[i].Short < clicks[j].Short
})
if len(clicks) > 200 {
clicks = clicks[:200]
}
var long string
if short != "" && localClient != nil {
// if a peer exists with the short name, suggest it as the long URL
st, err := localClient.Status(r.Context())
if err == nil {
for _, p := range st.Peer {
if host, _, ok := strings.Cut(p.DNSName, "."); ok && host == short {
long = "http://" + host + "/"
break
}
}
}
}
cu, err := currentUser(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
homeTmpl.Execute(w, homeData{
Short: short,
Long: long,
Clicks: clicks,
XSRF: xsrftoken.Generate(xsrfKey, cu.login, newShortName),
})
}
func serveAll(w http.ResponseWriter, _ *http.Request) {
if err := flushStats(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
links, err := db.LoadAll()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sort.Slice(links, func(i, j int) bool {
return links[i].Short < links[j].Short
})
allTmpl.Execute(w, links)
}
func serveHelp(w http.ResponseWriter, _ *http.Request) {
helpTmpl.Execute(w, nil)
}
func serveOpenSearch(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/opensearchdescription+xml")
opensearchTmpl.Execute(w, nil)
}
func serveGo(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
switch r.Method {
case "GET":
serveHome(w, r, "")
case "POST":
serveSave(w, r)
}
return
}
short, remainder, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, "/"), "/")
// redirect {name}+ links to /.detail/{name}
if strings.HasSuffix(short, "+") {
http.Redirect(w, r, "/.detail/"+strings.TrimSuffix(short, "+"), http.StatusFound)
return
}
link, err := db.Load(short)
if errors.Is(err, fs.ErrNotExist) {
w.WriteHeader(http.StatusNotFound)
serveHome(w, r, short)
return
}
if err != nil {
log.Printf("serving %q: %v", short, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
stats.mu.Lock()
if stats.clicks == nil {
stats.clicks = make(ClickStats)
}
stats.clicks[link.Short]++
if stats.dirty == nil {
stats.dirty = make(ClickStats)
}
stats.dirty[link.Short]++
stats.mu.Unlock()
cu, _ := currentUser(r)
env := expandEnv{Now: time.Now().UTC(), Path: remainder, user: cu.login, query: r.URL.Query()}
target, err := expandLink(link.Long, env)
if err != nil {
log.Printf("expanding %q: %v", link.Long, err)
if errors.Is(err, errNoUser) {
http.Error(w, "link requires a valid user", http.StatusUnauthorized)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// http.Redirect always cleans the redirect URL, which we don't always want.
// Instead, manually set status and Location header.
w.Header().Set("Location", target.String())
w.WriteHeader(http.StatusFound)
}
// acceptHTML returns whether the request can accept a text/html response.
func acceptHTML(r *http.Request) bool {
return strings.Contains(strings.ToLower(r.Header.Get("Accept")), "text/html")
}
// detailData is the data used by the detailTmpl template.
type detailData struct {
// Editable indicates whether the current user can edit the link.
Editable bool
Link *Link
XSRF string
}
func serveDetail(w http.ResponseWriter, r *http.Request) {
short := strings.TrimPrefix(r.URL.Path, "/.detail/")
link, err := db.Load(short)
if errors.Is(err, fs.ErrNotExist) {
http.NotFound(w, r)
return
}
if short != link.Short {
// redirect to canonical short name
http.Redirect(w, r, "/.detail/"+link.Short, http.StatusFound)
return
}
if err != nil {
log.Printf("serving detail %q: %v", short, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !acceptHTML(r) {
w.Header().Set("Content-Type", "application/json")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
enc.Encode(link)
return
}
cu, err := currentUser(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
canEdit := canEditLink(r.Context(), link, cu)
ownerExists, err := userExists(r.Context(), link.Owner)
if err != nil {
log.Printf("looking up tailnet user %q: %v", link.Owner, err)
}
data := detailData{
Link: link,
Editable: canEdit,
XSRF: xsrftoken.Generate(xsrfKey, cu.login, link.Short),
}
if canEdit && !ownerExists {
data.Link.Owner = cu.login
}
detailTmpl.Execute(w, data)
}
type expandEnv struct {
Now time.Time
// Path is the remaining path after short name. For example, in
// "http://go/who/amelie", Path is "amelie".
Path string
// user is the current user, if any.
// For example, "[email protected]" or "foo@github".
user string
// query is the query parameters from the original request.
query url.Values
}
var errNoUser = errors.New("no user")
// User returns the current user, or errNoUser if there is no user.
func (e expandEnv) User() (string, error) {
if e.user == "" {
return "", errNoUser
}
return e.user, nil
}
var expandFuncMap = texttemplate.FuncMap{
"PathEscape": url.PathEscape,
"QueryEscape": url.QueryEscape,
"TrimSuffix": strings.TrimSuffix,
"Match": regexMatch,
}
func regexMatch(pattern string, s string) bool {
b, _ := regexp.MatchString(pattern, s)
return b
}
// expandLink returns the expanded long URL to redirect to, executing any
// embedded templates with env data.
//
// If long does not include templates, the default behavior is to append
// env.Path to long.
func expandLink(long string, env expandEnv) (*url.URL, error) {
if !strings.Contains(long, "{{") {
// default behavior is to append remaining path to long URL
if strings.HasSuffix(long, "/") {
long += "{{.Path}}"
} else {
long += "{{with .Path}}/{{.}}{{end}}"
}
}
tmpl, err := texttemplate.New("").Funcs(expandFuncMap).Parse(long)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, env); err != nil {
return nil, err
}
u, err := url.Parse(buf.String())
if err != nil {
return nil, err
}
// add query parameters from original request
if len(env.query) > 0 {
query := u.Query()
for key, values := range env.query {
for _, v := range values {
query.Add(key, v)
}
}
u.RawQuery = query.Encode()
}
return u, nil
}
func devMode() bool { return *dev != "" }
const peerCapName = "tailscale.com/cap/golink"
type capabilities struct {
Admin bool `json:"admin"`
}
type user struct {
login string
isAdmin bool
}
// currentUser returns the Tailscale user associated with the request.
// In most cases, this will be the user that owns the device that made the request.
// For tagged devices, the value "tagged-devices" is returned.
// If the user can't be determined (such as requests coming through a subnet router),
// an error is returned unless the -allow-unknown-users flag is set.
var currentUser = func(r *http.Request) (user, error) {
if devMode() {
return user{login: "[email protected]"}, nil
}
whois, err := localClient.WhoIs(r.Context(), r.RemoteAddr)
if err != nil {
if *allowUnknownUsers {
// Don't report the error if we are allowing unknown users.
return user{}, nil
}
return user{}, err
}
login := whois.UserProfile.LoginName
caps, _ := tailcfg.UnmarshalCapJSON[capabilities](whois.CapMap, peerCapName)
for _, cap := range caps {
if cap.Admin {
return user{login: login, isAdmin: true}, nil
}
}
return user{login: login}, nil
}
// userExists returns whether a user exists with the specified login in the current tailnet.
func userExists(ctx context.Context, login string) (bool, error) {
const userTaggedDevices = "tagged-devices" // owner of tagged devices
if login == userTaggedDevices {
return false, nil
}
if devMode() {
// in dev mode, just assume the user exists
return true, nil
}
st, err := localClient.Status(ctx)
if err != nil {
return false, err
}
for _, user := range st.User {
if user.LoginName == userTaggedDevices {
continue
}
if user.LoginName == login {
return true, nil
}
}
return false, nil
}
var reShortName = regexp.MustCompile(`^\w[\w\-\.]*$`)
func serveDelete(w http.ResponseWriter, r *http.Request) {
short := strings.TrimPrefix(r.URL.Path, "/.delete/")
if short == "" {
http.Error(w, "short required", http.StatusBadRequest)
return
}
cu, err := currentUser(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
link, err := db.Load(short)
if errors.Is(err, fs.ErrNotExist) {
http.NotFound(w, r)
return
}
if !canEditLink(r.Context(), link, cu) {
http.Error(w, fmt.Sprintf("cannot delete link owned by %q", link.Owner), http.StatusForbidden)
return
}
// Deletion by CLI has never worked because it has always required the XSRF
// token. (Refer to commit c7ac33d04c33743606f6224009a5c73aa0b8dec0.) If we
// want to enable deletion via CLI and to honor allowUnknownUsers for
// deletion, we could change the below to a call to isRequestAuthorized. For
// now, always require the XSRF token, thus maintaining the status quo.
if !xsrftoken.Valid(r.PostFormValue("xsrf"), xsrfKey, cu.login, link.Short) {
http.Error(w, "invalid XSRF token", http.StatusBadRequest)
return
}
if err := db.Delete(short); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
deleteLinkStats(link)
deleteTmpl.Execute(w, deleteData{
Short: link.Short,
Long: link.Long,
XSRF: xsrftoken.Generate(xsrfKey, cu.login, newShortName),
})
}
// serveSave handles requests to save or update a Link. Both short name and
// long URL are validated for proper format. Existing links may only be updated
// by their owner.
func serveSave(w http.ResponseWriter, r *http.Request) {
short, long := r.FormValue("short"), r.FormValue("long")
if short == "" || long == "" {
http.Error(w, "short and long required", http.StatusBadRequest)
return
}
if !reShortName.MatchString(short) {
http.Error(w, "short may only contain letters, numbers, dash, and period", http.StatusBadRequest)
return
}
if _, err := texttemplate.New("").Funcs(expandFuncMap).Parse(long); err != nil {
http.Error(w, fmt.Sprintf("long contains an invalid template: %v", err), http.StatusBadRequest)
return
}
cu, err := currentUser(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
link, err := db.Load(short)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !canEditLink(r.Context(), link, cu) {
http.Error(w, fmt.Sprintf("cannot update link owned by %q", link.Owner), http.StatusForbidden)
return
}
// short name to use for XSRF token.
// For new link creation, the special newShortName value is used.
tokenShortName := newShortName
if link != nil {
tokenShortName = link.Short
}
if !isRequestAuthorized(r, cu, tokenShortName) {
http.Error(w, "invalid XSRF token", http.StatusBadRequest)
return
}
// allow transferring ownership to valid users. If empty, set owner to current user.
owner := r.FormValue("owner")
if owner != "" {
exists, err := userExists(r.Context(), owner)
if err != nil {
log.Printf("looking up tailnet user %q: %v", owner, err)
}
if !exists {
http.Error(w, "new owner not a valid user: "+owner, http.StatusBadRequest)
return
}
} else {
owner = cu.login
}
now := time.Now().UTC()
if link == nil {
link = &Link{
Short: short,
Created: now,
}
}
link.Short = short
link.Long = long
link.LastEdit = now
link.Owner = owner
if err := db.Save(link); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if acceptHTML(r) {
successTmpl.Execute(w, homeData{Short: short})
} else {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(link)
}
}
// canEditLink returns whether the specified user has permission to edit link.
// Admin users can edit all links.
// Non-admin users can only edit their own links or links without an active owner.
func canEditLink(ctx context.Context, link *Link, u user) bool {
if link == nil || link.Owner == "" {
// new or unowned link
return true
}
if u.isAdmin || link.Owner == u.login {
return true
}
owned, err := userExists(ctx, link.Owner)
if err != nil {
log.Printf("looking up tailnet user %q: %v", link.Owner, err)
}
// Allow editing if the link is currently unowned
return err == nil && !owned
}
// serveExport prints a snapshot of the link database. Links are JSON encoded
// and printed one per line. This format is used to restore link snapshots on
// startup.
func serveExport(w http.ResponseWriter, _ *http.Request) {
if err := flushStats(); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
links, err := db.LoadAll()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sort.Slice(links, func(i, j int) bool {
return links[i].Short < links[j].Short
})
encoder := json.NewEncoder(w)
for _, link := range links {
if err := encoder.Encode(link); err != nil {
panic(http.ErrAbortHandler)
}
}
}
func restoreLastSnapshot() error {
bs := bufio.NewScanner(bytes.NewReader(LastSnapshot))
var restored int
for bs.Scan() {
link := new(Link)
if err := json.Unmarshal(bs.Bytes(), link); err != nil {
return err
}
if link.Short == "" {
continue
}
_, err := db.Load(link.Short)
if err == nil {
continue // exists
} else if !errors.Is(err, fs.ErrNotExist) {
return err
}
if err := db.Save(link); err != nil {
return err
}
restored++
}
if restored > 0 && *verbose {
log.Printf("Restored %v links.", restored)
}
return bs.Err()
}
func resolveLink(link *url.URL) (*url.URL, error) {
path := link.Path
// if link was specified as "go/name", it will parse with no scheme or host.
// Trim "go" prefix from beginning of path.
if link.Host == "" {
path = strings.TrimPrefix(path, *hostname)
}
short, remainder, _ := strings.Cut(strings.TrimPrefix(path, "/"), "/")
l, err := db.Load(short)
if err != nil {
return nil, err
}
dst, err := expandLink(l.Long, expandEnv{Now: time.Now().UTC(), Path: remainder})
if err == nil {
if dst.Host == "" || dst.Host == *hostname {
dst, err = resolveLink(dst)
}
}