From 8a0b284740c7f49d709e76796c439920d243fdde Mon Sep 17 00:00:00 2001 From: Milos Mileusnic Date: Sat, 27 Mar 2021 20:07:51 +0100 Subject: [PATCH] fix #1, fix #2; Add DNSServer settings, modifies dnsQuest --- README.md | 9 ++++++++- resolver.go | 14 ++++++++++---- spf_test.go | 11 ++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c6d0b19..1f43ffb 100644 --- a/README.md +++ b/README.md @@ -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") } ``` \ No newline at end of file diff --git a/resolver.go b/resolver.go index 565e3d0..1dc4648 100644 --- a/resolver.go +++ b/resolver.go @@ -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 : +// 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 { @@ -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() { diff --git a/spf_test.go b/spf_test.go index d1b68ba..5be218d 100644 --- a/spf_test.go +++ b/spf_test.go @@ -55,7 +55,7 @@ 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 { @@ -63,7 +63,16 @@ func TestCheckHost(t *testing.T) { 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) {