inet

package
v0.0.0-...-44b4573 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MPL-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OutgoingMailSubjectKeyword = "laitos" // Outgoing emails are encouraged to carry this string in their subject
	MailIOTimeoutSec           = 10       // MailIOTimeoutSec is the timeout for contacting MTA

	/*
		MaxOutstandingMailSize is the maximum size of mails buffered in memory, waiting to be delivered. Mails that arrive
		after this limit is reached will cause earlier mails to be dropped permanently.
	*/
	MaxOutstandingMailSize = 200 * 1048576
)
View Source
const HTTPPublicIPTimeoutSec = 10

HTTPPublicIPTimeoutSec is the timeout in seconds used when determining public IP and cloud detection.

View Source
const (
	/*
		MaxMailBodySize is the maximum size of a single mail message acceptable by popular Internet email services.
		The number defined here is slightly more generous than the norm.
	*/
	MaxMailBodySize = 32 * 1048576
)

Variables

View Source
var (
	// NeutralRecursiveResolvers is a list of public recursive DNS resolvers
	// that provide genuine answers without discrimination or filtering.
	// These resolvers must support both TCP and UDP.
	NeutralDNSResolverAddrs = []string{

		"1.1.1.1:53",
		"1.0.0.1:53",

		"8.8.8.8:53",
		"8.8.4.4:53",

		"74.82.42.42:53",

		"9.9.9.10:53",
		"149.112.112.10:53",
	}

	// NeutralRecursiveResolver is a DNS resolver that provides genuine answers
	// without discrimination or filtering. This is often useful for resolving
	// names downloaded from various blacklist projects.
	NeutralRecursiveResolver = &net.Resolver{
		PreferGo: true,
		Dial: func(ctx context.Context, network, address string) (conn net.Conn, e error) {

			resolverAddr := NeutralDNSResolverAddrs[rand.Intn(len(NeutralDNSResolverAddrs))]
			var d net.Dialer
			return d.DialContext(ctx, network, resolverAddr)
		},
	}
)
View Source
var CommonMailLogger = &lalog.Logger{
	ComponentName: "mailclient",
	ComponentID:   []lalog.LoggerIDField{{Key: "Common", Value: "Shared"}},
}

CommonMailLogger is shared by all mail clients to log mail delivery progress.

View Source
var RegexMailAddress = regexp.MustCompile(`[a-zA-Z0-9!#$%&'*+-/=?_{|}~.^]+@[a-zA-Z0-9!#$%&'*+-/=?_{|}~.^]+.[a-zA-Z0-9!#$%&'*+-/=?_{|}~.^]+`)

RegexMailAddress finds *@*.* that looks much like an Email address

Functions

func GetAWSRegion

func GetAWSRegion() string

GetAWSRegion returns the AWS region name specified in program environment "AWS_REGION"; if left unspecified, the function returns region name retrieved from EC2 metadata service. If the AWS region name cannot be determined, the function will return an empty string.

func GetPublicIP

func GetPublicIP() net.IP

GetPublicIP returns the latest public IP address of the computer. If the IP address cannot be determined, it will return an empty string. If the public IP has been determined recently (less than 3 minutes ago), the cached public IP will be returned.

func IsAWS

func IsAWS() bool

IsAWS returns true only if the program is running on Amazon Web Service.

func IsAlibaba

func IsAlibaba() bool

IsAlibaba returns true only if the program is running on Alibaba cloud.

func IsAzure

func IsAzure() bool

IsAzure returns true only if the program is running on Microsoft Azure virtual machine.

func IsGCE

func IsGCE() bool

IsGCE returns true only if the program is running on Google compute engine (or Google cloud platform, same thing).

func WalkMailMessage

func WalkMailMessage(mailMessage []byte, fun func(BasicMail, []byte) (bool, error)) error

WalkMailMessage dissects input mail message: If input message is a multipart message, run the function against each part individually. If input message is not a multipart mail message, run the function against the entire message.

As a special case, should the input mail message be encoded as "quoted-printable", the mail body will be rid of the quotes before passing into the function.

The function parameters are: MailProperties - properties of the entire mail message or part of multipart message. []byte - body of current mail part.

The function returns two parameters: bool - if true, continue processing the next part, otherwise cease processing. error - stop processing and return this error immediately.

Types

type BasicMail

type BasicMail struct {
	Subject      string // Mail subject
	FromAddress  string // From address of mail, minus person's name.
	ReplyAddress string // Address to which a reply to this mail shall be delivered
	ContentType  string // Mail content type
}

Mail properties such as subject and content type. If the mail is a multi-part mail, the ContentType string will be able to tell the correct content type to a multipart reader.

func ReadMailMessage

func ReadMailMessage(mailMessage []byte) (prop BasicMail, parsedMail *mail.Message, err error)

Parse headers of the mail message and return some basic properties about the mail.

type HTTPRequest

type HTTPRequest struct {
	// TimeoutSec is the timeout of the execution of the entire HTTP request, defaults to 30 seconds.
	TimeoutSec int
	// Method is the HTTP method name, defaults to "GET".
	Method string
	// Header is the collection of additional request headers, defaults to nil.
	Header http.Header
	// ContentType is the request content type, defaults to "application/x-www-form-urlencoded".
	ContentType string
	// Body is the HTTP request body, defaults to nil.
	Body io.Reader
	// RequestFunc is invoked shortly before executing the HTTP request, allowing caller to further customise the request, defaults to nil.
	RequestFunc func(*http.Request) error
	// MaxBytes is the maximum size of response body to read, defaults to 4MB.
	MaxBytes int
	// MaxRetry is the maximum number of retries to make in case of an IO error, 4xx, or 5xx response, defaults to 3.
	MaxRetry int
	// UseNeutralDNSResolver instructs the HTTP client to use the neutral & recursive public DNS resolver instead of the default resolver of the system.
	UseNeutralDNSResolver bool
}

HTTPRequest defines all of the parameters necessary for making an outgoing HTTP request using the DoHTTP function.

func (*HTTPRequest) FillBlanks

func (req *HTTPRequest) FillBlanks()

FillBlanks gives sets the parameters of the HTTP request using sensible default values.

type HTTPResponse

type HTTPResponse struct {
	StatusCode int
	Header     http.Header
	Body       []byte
}

HTTPResponse encapsulates the response code, header, and response body in its entirety.

func DoHTTP

func DoHTTP(ctx context.Context, reqParam HTTPRequest, urlTemplate string, urlValues ...interface{}) (resp HTTPResponse, err error)

DoHTTP makes an HTTP request and returns its HTTP response. Placeholders in the URL template must always use %s.

func (*HTTPResponse) GetBodyUpTo

func (resp *HTTPResponse) GetBodyUpTo(nBytes int) []byte

GetBodyUpTo returns response body but only up to the specified number of bytes.

func (*HTTPResponse) Non2xxToError

func (resp *HTTPResponse) Non2xxToError() error

Non2xxToError returns an error only if the HTTP response status is not 2xx.

type MailClient

type MailClient struct {
	MailFrom     string `json:"MailFrom"`     // FROM address of the outgoing mails
	MTAHost      string `json:"MTAHost"`      // Server name or IP address of mail transportation agent
	MTAPort      int    `json:"MTAPort"`      // Port number of SMTP service on mail transportation agent
	AuthUsername string `json:"AuthUsername"` // (Optional) Username for plain authentication, if the SMTP server requires it.
	AuthPassword string `json:"AuthPassword"` // (Optional) Password for plain authentication, if the SMTP server requires it.
}

Send emails via SMTP.

func (*MailClient) IsConfigured

func (client *MailClient) IsConfigured() bool

Return true only if all mail parameters are present.

func (*MailClient) SelfTest

func (client *MailClient) SelfTest() error

Try to contact MTA and see if connection is possible.

func (*MailClient) Send

func (client *MailClient) Send(subject string, textBody string, recipients ...string) error

Deliver mail to all recipients. Block until mail is sent or an error has occurred.

func (*MailClient) SendRaw

func (client *MailClient) SendRaw(fromAddr string, rawMailBody []byte, recipients ...string) error

Deliver unmodified mail body to all recipients. Block until mail is sent or an error has occurred.

Jump to

Keyboard shortcuts

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