Skip to content

Commit

Permalink
Return 404 when the dns domain has any path different than /
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarrillo committed May 12, 2024
1 parent b57bede commit 789cc69
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Take a look at [ifconfig.es](https://ifconfig.es) a live site using `whatismyip`

Get your public IP easily from the command line:

```text
```bash
curl ifconfig.es
127.0.0.1

Expand All @@ -42,7 +42,7 @@ curl -6 ifconfig.es

Get the IP of your DNS provider:

```text
```bash
curl -L dns.ifconfig.es
2a04:e4c0:47::67 (Spain / OPENDNS)
```
Expand Down
2 changes: 1 addition & 1 deletion router/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func GetDNSDiscoveryHandler(store *cache.Cache, domain string, redirectPort stri
return
}

if ctx.Request.Host == domain {
if ctx.Request.Host == domain && ctx.Request.URL.Path == "/" {
ctx.Redirect(http.StatusFound, fmt.Sprintf("http://%s.%s%s", uuid.New().String(), domain, redirectPort))
ctx.Abort()
return
Expand Down
13 changes: 13 additions & 0 deletions router/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ func TestGetDNSDiscoveryHandler(t *testing.T) {
assert.Equal(t, testIP.ipv4+"\n", w.Body.String())
})

t.Run("return 404 if there is a path", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/path", nil)
req.Host = domain

w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
c.Request = req
handler(c)
app.ServeHTTP(w, req)

assert.Equal(t, http.StatusNotFound, w.Code)
})

t.Run("redirects if host is domain", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
req.Host = domain
Expand Down

0 comments on commit 789cc69

Please sign in to comment.