Skip to content

Commit

Permalink
fix #1, fix #2; Add DNSServer settings, modifies dnsQuest
Browse files Browse the repository at this point in the history
  • Loading branch information
mileusna committed Mar 27, 2021
1 parent 4fa7f6f commit 8a0b284
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ import (
)

func main() {
// optional, set DNS server which will be used by resolver.
// Default is Google's 8.8.8.8:53
spf.DNSServer = "1.1.1.1:53"

ip := net.ParseIP("123.123.123.123")
r := spf.CheckHost(ip, "domain.com", "name@domain.com", "");
// returns spf check result
// "pass" / "fail" / "softfail" / "neutral" / "none" / "temperror" / "permerror"
// "PASS" / "FAIL" / "SOFTFAIL" / "NEUTRAL" / "NONE" / "TEMPERROR" / "PERMERROR"

// if you only need to retrive SPF record as string from DNS
spfRecord, _ := spf.LookupSPF("domain.com")
}
```
14 changes: 10 additions & 4 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import (
"github.com/miekg/dns"
)

// LookupSPF returns spf txt record
// if no records found or more than one record found, r value will be set accordingly to None or PermError
// If dns lookup faild, r will be set to TempError
// DNSServer global var to use for resolver in format <ip>:<port>
// By default it uses Google's 8.8.8.8:53
// Misconfigured DNSServer will cause SPF checks to return TEMPERROR.
var DNSServer = "8.8.8.8:53"

// LookupSPF returns spf txt record.
// if no records found or more than one record found, r value will be set accordingly to None or PermError.
// If dns lookup faild, r will be set to TempError.
func LookupSPF(domain string) (spf string, r Result) {
txts, err := lookupTXT(domain)
if err != nil {
Expand Down Expand Up @@ -128,9 +133,10 @@ func dnsQuest(d string, t uint16) (r *dns.Msg, rtt time.Duration, err error) {
m.Id = dns.Id()
m.SetQuestion(dns.Fqdn(d), t)
m.RecursionDesired = true
m.SetEdns0(4096, false)

c := new(dns.Client)
return c.Exchange(m, "8.8.8.8:53")
return c.Exchange(m, DNSServer)
}

func init() {
Expand Down
11 changes: 10 additions & 1 deletion spf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,24 @@ func TestCheckHost(t *testing.T) {
newTestData(ip, "gmail.com", "mileusna@gmail.com", "", spf.Softfail),
newTestData(ip, "hotmail.com", "mileusna@hotmail.com", "", spf.Softfail),
newTestData(ip2, "netmark.rs", "milos@netmark.rs", "", spf.Pass),
newTestData(ip2, "naslovi.net", "milos@naslovi.net", "", spf.Softfail),
newTestData(ip2, "naslovi.net", "milos@naslovi.net", "", spf.Pass),
}

for _, d := range data {
if r := spf.CheckHost(d.ip, d.domain, d.sender, d.helo); r != d.result {
t.Fatal("CheckHost", d.ip, d.domain, d.sender, "should", d.result, "returned:", r)
}
}
}

func TestDNSSettings(t *testing.T) {
spf.DNSServer = "127.2.2.1:53"

if spf.CheckHost(net.ParseIP("87.237.204.223"), "naslovi.net", "milos@naslovi.net", "") != "TEMPERROR" {
t.Error("Invalid DNS configuration should return TEMPERROR")
}

spf.DNSServer = "8.8.8.8:53"
}

func TestMacro(t *testing.T) {
Expand Down

0 comments on commit 8a0b284

Please sign in to comment.