spf

package module
v0.0.0-...-3e270dd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2020 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Package spf

Documentation

Package spf parses an SPF record and determines if a given IP address is allowed to send email based on that record. SPF handles all of the mechanisms defined at http://www.open-spf.org/SPF_Record_Syntax/.

Example

package main

import "github.com/asggo/spf"

func main() {

        SMTPClientIP := "1.1.1.1"
        envelopeFrom := "info@example.com"

        result, err := spf.SPFTest(SMTPClientIP, envelopeFrom)
        if err != nil {
                panic(err)
        }

        switch result {
        case spf.Pass:
                // allow action
        case spf.Fail:
                // deny action
        }
	//...
}

Documentation

Overview

Package spf can parse an SPF record and determine if a given IP address is allowed to send email based on that record. SPF can handle all of the mechanisms defined at http://www.openspf.org/SPF_Record_Syntax. The redirect mechanism is ignored.

Index

Constants

View Source
const (
	MaxCount = 10
)

Variables

View Source
var (
	ErrNoRecord         = errors.New("No SPF Record found.")
	ErrFailedLookup     = errors.New("DNS Lookup failed.")
	ErrInvalidSPF       = errors.New("Invalid SPF string.")
	ErrIncludeLoop      = errors.New("Include loop detected.")
	ErrInvalidMechanism = errors.New("Invalid mechanism in SPF string.")
	ErrMaxCount         = errors.New("Exceeded maximum lookups.")
)
View Source
var (
	ErrNoMatch = errors.New("Client was not covered by the mechanism.")
)

Functions

This section is empty.

Types

type Mechanism

type Mechanism struct {
	Name   string
	Domain string
	Prefix string
	Result Result
	Count  int
}

Mechanism represents a single mechanism in an SPF record.

func NewMechanism

func NewMechanism(str, domain string) (Mechanism, error)

NewMechanism creates a new Mechanism struct using the given string and domain name. When the mechanism does not define the domain, the provided domain is used as the default.

func (*Mechanism) Evaluate

func (m *Mechanism) Evaluate(ip string, count int) (Result, error)

Evaluate determines if the given IP address is covered by the mechanism. If the IP is covered, the mechanism result is returned and error is nil. If the IP is not covered an error is returned. The caller must check for the error to determine if the result is valid.

func (*Mechanism) ResultTag

func (m *Mechanism) ResultTag() string

ResultTag maps the Result code to a suitable char

func (*Mechanism) SPFString

func (m *Mechanism) SPFString() string

SPFString return a string representation of a mechanism, suitable for using in a TXT record.

func (*Mechanism) String

func (m *Mechanism) String() string

Return a Mechanism as a string

func (*Mechanism) Valid

func (m *Mechanism) Valid() bool

Ensure the mechanism is valid

type Result

type Result string
const (
	Pass      Result = "Pass"
	Neutral   Result = "Neutral"
	Fail      Result = "Fail"
	SoftFail  Result = "SoftFail"
	None      Result = "None"
	TempError Result = "TempError"
	PermError Result = "PermError"
)

func SPFTest

func SPFTest(ip, email string) (Result, error)

SPFTest determines the clients sending status for the given email addres.

SPFTest will return one of the following results: Pass, Fail, SoftFail, Neutral, None, TempError, or PermError

type SPF

type SPF struct {
	Raw        string
	Domain     string
	Version    string
	Mechanisms []Mechanism
	Count      int
}

SPF represents an SPF record for a particular Domain. The SPF record holds all of the Allow, Deny, and Neutral mechanisms.

func NewSPF

func NewSPF(domain, record string, count int) (SPF, error)

Create a new SPF record for the given domain using the provided string. If the provided string is not valid an error is returned.

func (*SPF) SPFString

func (s *SPF) SPFString() string

SPFString returns a formatted SPF object as a string suitable for use in a TXT record.

func (*SPF) String

func (s *SPF) String() string

Return an SPF record as a string.

func (*SPF) Test

func (s *SPF) Test(ip string) Result

Test evaluates each mechanism to determine the result for the client. Mechanisms are evaluated in order until one of them provides a valid result. If no valid results are provided, the default result of "Neutral" is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL