Skip to content

Commit 8dcbd98

Browse files
committed
cmd/derper: show more information on home page
- Basic description of DERP If configured to do so, also show - Mailto link to [email protected] - Link to Tailscale Security Policies - Link to Tailscale Acceptable Use Policy Updates tailscale/corp#24092 Signed-off-by: Percy Wegmann <[email protected]>
1 parent 065825e commit 8dcbd98

File tree

3 files changed

+92
-19
lines changed

3 files changed

+92
-19
lines changed

cmd/derper/depaware.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
264264
hash/fnv from google.golang.org/protobuf/internal/detrand
265265
hash/maphash from go4.org/mem
266266
html from net/http/pprof+
267+
html/template from tailscale.com/cmd/derper
267268
io from bufio+
268269
io/fs from crypto/x509+
269270
io/ioutil from github.com/mitchellh/go-ps+
@@ -308,6 +309,8 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
308309
sync/atomic from context+
309310
syscall from crypto/rand+
310311
text/tabwriter from runtime/pprof
312+
text/template from html/template
313+
text/template/parse from html/template+
311314
time from compress/gzip+
312315
unicode from bytes+
313316
unicode/utf16 from crypto/x509+

cmd/derper/derper.go

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"expvar"
2020
"flag"
2121
"fmt"
22+
"html/template"
2223
"io"
2324
"log"
2425
"math"
@@ -212,25 +213,16 @@ func main() {
212213
tsweb.AddBrowserHeaders(w)
213214
w.Header().Set("Content-Type", "text/html; charset=utf-8")
214215
w.WriteHeader(200)
215-
io.WriteString(w, `<html><body>
216-
<h1>DERP</h1>
217-
<p>
218-
This is a <a href="https://tailscale.com/">Tailscale</a> DERP server.
219-
</p>
220-
<p>
221-
Documentation:
222-
</p>
223-
<ul>
224-
<li><a href="https://tailscale.com/kb/1232/derp-servers">About DERP</a></li>
225-
<li><a href="https://pkg.go.dev/tailscale.com/derp">Protocol & Go docs</a></li>
226-
<li><a href="https://github.com/tailscale/tailscale/tree/main/cmd/derper#derp">How to run a DERP server</a></li>
227-
</ul>
228-
`)
229-
if !*runDERP {
230-
io.WriteString(w, `<p>Status: <b>disabled</b></p>`)
231-
}
232-
if tsweb.AllowDebugAccess(r) {
233-
io.WriteString(w, "<p>Debug info at <a href='/debug/'>/debug/</a>.</p>\n")
216+
err := homePageTemplate.Execute(w, templateData{
217+
ShowAbuseInfo: validProdHostname.MatchString(*hostname),
218+
Disabled: !*runDERP,
219+
AllowDebug: tsweb.AllowDebugAccess(r),
220+
})
221+
if err != nil {
222+
if r.Context().Err() == nil {
223+
log.Printf("homePageTemplate.Execute: %v", err)
224+
}
225+
return
234226
}
235227
}))
236228
mux.Handle("/robots.txt", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -468,3 +460,52 @@ func init() {
468460
return 0
469461
}))
470462
}
463+
464+
type templateData struct {
465+
ShowAbuseInfo bool
466+
Disabled bool
467+
AllowDebug bool
468+
}
469+
470+
// homePageTemplate renders the home page using [templateData].
471+
var homePageTemplate = template.Must(template.New("home").Parse(`<html><body>
472+
<h1>DERP</h1>
473+
<p>
474+
This is a <a href="https://tailscale.com/">Tailscale</a> DERP server.
475+
</p>
476+
477+
<p>
478+
It provides STUN, interactive connectivity establishment, and relaying of end-to-end encrypted traffic
479+
for Tailscale clients.
480+
</p>
481+
482+
{{if .ShowAbuseInfo }}
483+
<p>
484+
If you suspect abuse, please contact <a href="mailto:[email protected]">[email protected]</a>.
485+
</p>
486+
{{end}}
487+
488+
<p>
489+
Documentation:
490+
</p>
491+
492+
<ul>
493+
{{if .ShowAbuseInfo }}
494+
<li><a href="https://tailscale.com/security-policies">Tailscale Security Policies</a></li>
495+
<li><a href="https://tailscale.com/tailscale-aup">Tailscale Acceptable Use Policies</a></li>
496+
{{end}}
497+
<li><a href="https://tailscale.com/kb/1232/derp-servers">About DERP</a></li>
498+
<li><a href="https://pkg.go.dev/tailscale.com/derp">Protocol & Go docs</a></li>
499+
<li><a href="https://github.com/tailscale/tailscale/tree/main/cmd/derper#derp">How to run a DERP server</a></li>
500+
</ul>
501+
502+
{{if .Disabled}}
503+
<p>Status: <b>disabled</b></p>
504+
{{end}}
505+
506+
{{if .AllowDebug}}
507+
<p>Debug info at <a href='/debug/'>/debug/</a>.</p>
508+
{{end}}
509+
</body>
510+
</html>
511+
`))

cmd/derper/derper_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
package main
55

66
import (
7+
"bytes"
78
"context"
9+
"fmt"
810
"net/http"
911
"net/http/httptest"
1012
"strings"
@@ -110,3 +112,30 @@ func TestDeps(t *testing.T) {
110112
},
111113
}.Check(t)
112114
}
115+
116+
func TestTemplate(t *testing.T) {
117+
buf := &bytes.Buffer{}
118+
err := homePageTemplate.Execute(buf, templateData{
119+
ShowAbuseInfo: true,
120+
Disabled: true,
121+
AllowDebug: true,
122+
})
123+
if err != nil {
124+
t.Fatal(err)
125+
}
126+
127+
str := buf.String()
128+
if !strings.Contains(str, "If you suspect abuse") {
129+
t.Error("Output is missing abuse mailto")
130+
}
131+
if !strings.Contains(str, "Tailscale Security Policies") {
132+
t.Error("Output is missing Tailscale Security Policies link")
133+
}
134+
if !strings.Contains(str, "Status:") {
135+
t.Error("Output is missing disabled status")
136+
}
137+
if !strings.Contains(str, "Debug info") {
138+
t.Error("Output is missing debug info")
139+
}
140+
fmt.Println(buf.String())
141+
}

0 commit comments

Comments
 (0)