Skip to content
Permalink
Browse files

(record): Add ref= to record struct

  • Loading branch information
erbesharat committed Aug 17, 2019
1 parent 20a2621 commit d4e86701c68dc0fc90d6b8b219c3bebcb2a46154
Showing with 40 additions and 11 deletions.
  1. +9 −0 record.go
  2. +20 −0 record_test.go
  3. +10 −10 txtdirect.go
  4. +1 −1 txtdirect_test.go
@@ -33,6 +33,7 @@ type record struct {
From string
Root string
Re string
Ref bool
}

// getRecord uses the given host to find a TXT record
@@ -98,6 +99,14 @@ func (r *record) Parse(str string, w http.ResponseWriter, req *http.Request, c C
l = strings.TrimPrefix(l, "re=")
r.Re = l

case strings.HasPrefix(l, "ref="):
l, err := strconv.ParseBool(strings.TrimPrefix(l, "ref="))
if err != nil {
fallback(w, req, "global", http.StatusMovedPermanently, c)
return err
}
r.Ref = l

case strings.HasPrefix(l, "root="):
l = strings.TrimPrefix(l, "root=")
l = ParseURI(l, w, req, c)
@@ -135,6 +135,26 @@ func TestParse(t *testing.T) {
},
nil,
},
{
"v=txtv0;ref=true;code=302",
record{
Version: "txtv0",
Type: "host",
Code: 302,
Ref: true,
},
nil,
},
{
"v=txtv0;ref=false;code=302",
record{
Version: "txtv0",
Type: "host",
Code: 302,
Ref: false,
},
nil,
},
}

for i, test := range tests {
@@ -128,16 +128,6 @@ func isIP(host string) bool {
func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
w.Header().Set("Server", "TXTDirect")

// Add referer header
if r.Header.Get("Referer") == "" {
host := r.Host
if strings.Contains(host, ":") {
hostSlice := strings.Split(host, ":")
host = hostSlice[0]
}
w.Header().Set("Referer", host)
}

host := r.Host
path := r.URL.Path

@@ -170,6 +160,16 @@ func Redirect(w http.ResponseWriter, r *http.Request, c Config) error {
return nil
}

// Add referer header
if rec.Ref && r.Header.Get("Referer") == "" {
host := r.Host
if strings.Contains(host, ":") {
hostSlice := strings.Split(host, ":")
host = hostSlice[0]
}
w.Header().Set("Referer", host)
}

if !contains(c.Enable, rec.Type) {
return fmt.Errorf("option disabled")
}
@@ -34,7 +34,7 @@ import (
// Testing TXT records
var txts = map[string]string{
// type=host
"_redirect.host.e2e.test.": "v=txtv0;to=https://plain.host.test;type=host;code=302",
"_redirect.host.e2e.test.": "v=txtv0;to=https://plain.host.test;type=host;ref=true;code=302",
"_redirect.nocode.host.e2e.test.": "v=txtv0;to=https://nocode.host.test;type=host",
"_redirect.noversion.host.e2e.test.": "to=https://noversion.host.test;type=host",
"_redirect.noto.host.e2e.test.": "v=txtv0;type=host",

0 comments on commit d4e8670

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