Skip to content

Commit 6c79b50

Browse files
committed
suggest long URL for new go links if peer exists
If you visit a non-existent go link, we render the home page and pre- populate the "short" input with the name of the link, and autofocus the "long" input so that you can simply paste a long URL and submit. It is common (at least at Tailscale) to create go links that correspond to the name of a device on the tailnet. For example, go/who points to http://who/. With this change, when you visit a non-existent go link, we check to see if a peer exists on the tailnet with that name, and if so we suggest that as the long URL. Signed-off-by: Will Norris <[email protected]>
1 parent 0b61ec1 commit 6c79b50

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

golink.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ type visitData struct {
257257
// homeData is the data used by the homeTmpl template.
258258
type homeData struct {
259259
Short string
260+
Long string
260261
Clicks []visitData
261262
}
262263

@@ -379,7 +380,7 @@ func serveHandler() http.Handler {
379380
})
380381
}
381382

382-
func serveHome(w http.ResponseWriter, short string) {
383+
func serveHome(w http.ResponseWriter, r *http.Request, short string) {
383384
var clicks []visitData
384385

385386
stats.mu.Lock()
@@ -401,8 +402,23 @@ func serveHome(w http.ResponseWriter, short string) {
401402
clicks = clicks[:200]
402403
}
403404

405+
var long string
406+
if short != "" && localClient != nil {
407+
// if a peer exists with the short name, suggest it as the long URL
408+
st, err := localClient.Status(r.Context())
409+
if err == nil {
410+
for _, p := range st.Peer {
411+
if host, _, ok := strings.Cut(p.DNSName, "."); ok && host == short {
412+
long = "http://" + host + "/"
413+
break
414+
}
415+
}
416+
}
417+
}
418+
404419
homeTmpl.Execute(w, homeData{
405420
Short: short,
421+
Long: long,
406422
Clicks: clicks,
407423
})
408424
}
@@ -442,7 +458,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
442458
if r.URL.Path == "/" {
443459
switch r.Method {
444460
case "GET":
445-
serveHome(w, "")
461+
serveHome(w, r, "")
446462
case "POST":
447463
serveSave(w, r)
448464
}
@@ -460,7 +476,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
460476
link, err := db.Load(short)
461477
if errors.Is(err, fs.ErrNotExist) {
462478
w.WriteHeader(http.StatusNotFound)
463-
serveHome(w, short)
479+
serveHome(w, r, short)
464480
return
465481
}
466482
if err != nil {

tmpl/home.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
{{ define "main" }}
22
<h2 class="text-xl font-bold pb-2">Create a new link</h2>
33

4+
{{ with .Long }}
5+
<p class="">Did you mean <a class="text-blue-600 hover:underline" href="{{.}}">{{.}}</a> ? Create a go link for it now:</p>
6+
{{ end }}
47
<form method="POST" action="/" class="flex flex-wrap">
58
<div class="flex">
69
<label for=short class="flex my-2 px-2 items-center bg-gray-100 border border-r-0 border-gray-300 rounded-l-md text-gray-700">http://go/</label>
710
<input id=short name=short required type=text size=15 placeholder="shortname" value="{{.Short}}" pattern="\w[\w\-\.]*" title="Must start with letter or number; may contain letters, numbers, dashes, and periods."
811
class="p-2 my-2 rounded-r-md border-gray-300 placeholder:text-gray-400">
912
<span class="flex m-2 items-center">&rarr;</span>
1013
</div>
11-
<input name=long required type=text size=40 placeholder="https://destination-url"{{if .Short}} autofocus{{end}} class="p-2 my-2 mr-2 max-w-full rounded-md border-gray-300 placeholder:text-gray-400">
14+
<input name=long required type=text size=40 placeholder="https://destination-url"{{if .Short}} value="{{.Long}}" autofocus{{end}} class="p-2 my-2 mr-2 max-w-full rounded-md border-gray-300 placeholder:text-gray-400">
1215
<button type=submit class="py-2 px-4 my-2 rounded-md bg-blue-500 border-blue-500 text-white hover:bg-blue-600 hover:border-blue-600">Create</button>
1316
</form>
1417
<p class="text-sm text-gray-500"><a class="text-blue-600 hover:underline" href="/.help">Help and advanced options</a></p>

0 commit comments

Comments
 (0)