Skip to content
Permalink
Browse files

(fallback): Add fallback tests for Git type

  • Loading branch information
erbesharat committed Sep 12, 2019
1 parent 982f278 commit 97a881d2486ed2676768b9bb6c7845e1ec138cc5
Showing with 87 additions and 42 deletions.
  1. +61 −37 fallback.go
  2. +14 −0 fallback_test.go
  3. +4 −3 git.go
  4. +1 −1 record.go
  5. +2 −1 txtdirect.go
  6. +5 −0 txtdirect_test.go
@@ -19,6 +19,10 @@ type Fallback struct {

fallbackType string
code int

// Which record to use for fallback. (last record or path record)
last bool
aggregation bool
}

// fallback redirects the request to the given fallback address
@@ -42,44 +46,12 @@ func fallback(w http.ResponseWriter, r *http.Request, fallbackType string, code
// Fetch records from request's context and set the []record type on them
f.fetchRecords()

// Redirect to first record's `to=` field
if fallbackType == "to" && f.lastRecord.To != "" {
http.Redirect(w, r, f.lastRecord.To, code)
f.countFallback(f.lastRecord.Type)
}

// Redirect to first record's `website=` field
if fallbackType == "website" && f.lastRecord.Website != "" {
http.Redirect(w, r, f.lastRecord.Website, code)
f.countFallback(f.lastRecord.Type)
}

// Redirect to first record's `root=` field
if fallbackType == "root" && f.lastRecord.Root != "" {
http.Redirect(w, r, f.lastRecord.Root, code)
f.countFallback(f.lastRecord.Type)
}

// Redirect to path record's `website=` field
if fallbackType == "website" && f.pathRecord.Website != "" {
http.Redirect(w, r, f.pathRecord.Website, code)
f.countFallback(f.pathRecord.Type)
if !f.lastRecordFallback() {
if !f.pathFallback() {
// If non of the above cases applied on the record, jump into global redirects
f.globalFallbacks(f.lastRecord.Type)
}
}

// Redirect to path record's `root=` field
if fallbackType == "root" && f.pathRecord.Root != "" {
http.Redirect(w, r, f.pathRecord.Root, code)
f.countFallback(f.pathRecord.Type)
}

// Redirect to path record's `to=` field
if f.pathRecord.To != "" {
http.Redirect(w, r, f.pathRecord.To, code)
f.countFallback(f.pathRecord.Type)
}

// If non of the above cases applied on the record, jump into global redirects
f.globalFallbacks(f.lastRecord.Type)
log.Printf("[txtdirect]: %s > %s", r.Host+r.URL.Path, w.Header().Get("Location"))
return
}
@@ -124,3 +96,55 @@ func (f *Fallback) fetchRecords() {
}
f.lastRecord = f.records[len(f.records)-1]
}

// Checks the last record's `to=`, `website=`, and `root=` field to fallback
// Returns false if it can't find an endpoint to fallback to
func (f *Fallback) lastRecordFallback() bool {
// Redirect to first record's `to=` field
if f.fallbackType == "to" && f.lastRecord.To != "" {
http.Redirect(f.rw, f.request, f.lastRecord.To, f.code)
f.countFallback(f.lastRecord.Type)
return true
}

// Redirect to first record's `website=` field
if f.fallbackType == "website" && f.lastRecord.Website != "" {
http.Redirect(f.rw, f.request, f.lastRecord.Website, f.code)
f.countFallback(f.lastRecord.Type)
return true
}

// Redirect to first record's `root=` field
if f.fallbackType == "root" && f.lastRecord.Root != "" {
http.Redirect(f.rw, f.request, f.lastRecord.Root, f.code)
f.countFallback(f.lastRecord.Type)
return true
}
return false
}

// Checks the path record's `to=`, `website=`, and `root=` field to fallback
// Returns false if it can't find an endpoint to fallback to
func (f *Fallback) pathFallback() bool {
// Redirect to path record's `website=` field
if f.fallbackType == "website" && f.pathRecord.Website != "" {
http.Redirect(f.rw, f.request, f.pathRecord.Website, f.code)
f.countFallback(f.pathRecord.Type)
return true
}

// Redirect to path record's `root=` field
if f.fallbackType == "root" && f.pathRecord.Root != "" {
http.Redirect(f.rw, f.request, f.pathRecord.Root, f.code)
f.countFallback(f.pathRecord.Type)
return true
}

// Redirect to path record's `to=` field
if f.pathRecord.To != "" {
http.Redirect(f.rw, f.request, f.pathRecord.To, f.code)
f.countFallback(f.pathRecord.Type)
return true
}
return false
}
@@ -186,6 +186,20 @@ func Test_fallbackE2E(t *testing.T) {
to: "https://gometa.path.to.test",
headers: http.Header{},
},
{
url: "https://fallbackgit.test",
enable: []string{"git"},
code: 302,
website: "https://torporxy.okkur.org",
headers: http.Header{},
},
{
url: "https://path.fallbackgit.test/torproxy",
enable: []string{"path", "git"},
code: 302,
website: "https://torporxy.okkur.org",
headers: http.Header{},
},
}
for _, test := range tests {
req := httptest.NewRequest("GET", test.url, nil)
7 git.go
@@ -68,10 +68,11 @@ func (g *Git) Proxy() error {
return nil
}

func (g *Git) VerifyGitQuery() error {
func (g *Git) ValidGitQuery() bool {
if !strings.HasPrefix(g.req.Header.Get("User-Agent"), "git") {
log.Errorf("[txtdirect]: The incoming request is not from a Git client.")
fallback(g.rw, g.req, "website", g.rec.Code, g.c)
log.Errorf("[txtdirect]: The incoming request is not from a Git client.")
return false
}
return nil
return true
}
@@ -193,7 +193,7 @@ func ParseURI(uri string, w http.ResponseWriter, r *http.Request, c Config) stri
url, err := url.Parse(uri)
if err != nil {
fallback(w, r, "global", http.StatusMovedPermanently, c)
return uri
return ""
}
return url.String()
}
@@ -274,7 +274,8 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
if rec.Type == "git" {
git := NewGit(w, r, c, rec)

if err := git.VerifyGitQuery(); err != nil {
// Triggers fallback when request isn't from a Git client
if !git.ValidGitQuery() {
return nil
}

@@ -75,6 +75,11 @@ var txts = map[string]string{
"_redirect.fallbackgometa.test.": "v=txtv0;type=path;to=https://gometa.path.to.test",
"_redirect.pathto.fallbackgometa.test.": "v=txtv0;to=wrong:/url.test;type=gometa",
"_redirect.redirect.fallbackgometa.test.": "v=txtv0;to=https://github.com/okkur/reposeed-server/;type=gometa",

// type=git
"_redirect.fallbackgit.test.": "v=txtv0;to=https://github.com/okkur/torproxy.git;website=https://torporxy.okkur.org;type=git",
"_redirect.path.fallbackgit.test.": "v=txtv0;to=https://about.okkur.org/;type=path",
"_redirect.torproxy.path.fallbackgit.test.": "v=txtv0;to=https://github.com/okkur/torproxy.git;website=https://torporxy.okkur.org;type=git",
}

// Testing DNS server port

0 comments on commit 97a881d

Please sign in to comment.
You can’t perform that action at this time.