From 789cc6939ea212ea6098b8b3cd66f11b9d56edb1 Mon Sep 17 00:00:00 2001 From: Daniel Carrillo Date: Sun, 12 May 2024 19:24:10 +0200 Subject: [PATCH] Return 404 when the dns domain has any path different than / --- README.md | 4 ++-- router/dns.go | 2 +- router/dns_test.go | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8ca5a67..f014611 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) ``` diff --git a/router/dns.go b/router/dns.go index 00474b6..3b4b3db 100644 --- a/router/dns.go +++ b/router/dns.go @@ -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 diff --git a/router/dns_test.go b/router/dns_test.go index 2b3de20..a2e45ba 100644 --- a/router/dns_test.go +++ b/router/dns_test.go @@ -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