Skip to content

Commit

Permalink
suggest long URL for new go links if peer exists
Browse files Browse the repository at this point in the history
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 <will@tailscale.com>
  • Loading branch information
willnorris committed Mar 15, 2024
1 parent 0b61ec1 commit 6c79b50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 19 additions & 3 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ type visitData struct {
// homeData is the data used by the homeTmpl template.
type homeData struct {
Short string
Long string
Clicks []visitData
}

Expand Down Expand Up @@ -379,7 +380,7 @@ func serveHandler() http.Handler {
})
}

func serveHome(w http.ResponseWriter, short string) {
func serveHome(w http.ResponseWriter, r *http.Request, short string) {
var clicks []visitData

stats.mu.Lock()
Expand All @@ -401,8 +402,23 @@ func serveHome(w http.ResponseWriter, short string) {
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
}
}
}
}

homeTmpl.Execute(w, homeData{
Short: short,
Long: long,
Clicks: clicks,
})
}
Expand Down Expand Up @@ -442,7 +458,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
switch r.Method {
case "GET":
serveHome(w, "")
serveHome(w, r, "")
case "POST":
serveSave(w, r)
}
Expand All @@ -460,7 +476,7 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
link, err := db.Load(short)
if errors.Is(err, fs.ErrNotExist) {
w.WriteHeader(http.StatusNotFound)
serveHome(w, short)
serveHome(w, r, short)
return
}
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion tmpl/home.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{{ define "main" }}
<h2 class="text-xl font-bold pb-2">Create a new link</h2>

{{ with .Long }}
<p class="">Did you mean <a class="text-blue-600 hover:underline" href="{{.}}">{{.}}</a> ? Create a go link for it now:</p>
{{ end }}
<form method="POST" action="/" class="flex flex-wrap">
<div class="flex">
<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>
<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."
class="p-2 my-2 rounded-r-md border-gray-300 placeholder:text-gray-400">
<span class="flex m-2 items-center">&rarr;</span>
</div>
<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">
<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">
<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>
</form>
<p class="text-sm text-gray-500"><a class="text-blue-600 hover:underline" href="/.help">Help and advanced options</a></p>
Expand Down

0 comments on commit 6c79b50

Please sign in to comment.