Documentation
¶
Overview ¶
Package conn dials and authenticates IMAP and SMTP connections for the protocol backends.
Index ¶
- Constants
- Variables
- func Acquire(ctx context.Context, account string) (release func(), err error)
- func BackOff(err error, try int) (time.Duration, bool)
- func DialIMAP(ctx context.Context, e Endpoint) (*imapclient.Client, error)
- func DialIMAPAbortable(ctx context.Context, e Endpoint, h *imapclient.UnilateralDataHandler) (*imapclient.Client, func(), error)
- func DialPOP3(ctx context.Context, e Endpoint) (c *pop3.Conn, close func(), err error)
- func DialSMTP(ctx context.Context, e Endpoint) (*smtp.Client, error)
- func HTTPStatusErr(resp *http.Response) error
- func HTTPTransport() *http.Transport
- func LogTrace(c *imapclient.Client, err error)
- func NewHTTPClient(t *http.Transport, rt http.RoundTripper) *http.Client
- func NewXOAuth2Client(username, token string) sasl.Client
- func RefusalReply(err error) string
- func RetryAfter(h http.Header) time.Duration
- func SameHost(req *http.Request, via []*http.Request) error
- func SameOrigin(a, b *url.URL) bool
- func Serial(ctx context.Context, account string, ...) error
- func Sleep(ctx context.Context, d time.Duration) error
- func StallBody(body io.ReadCloser, d time.Duration, cancel context.CancelFunc) io.ReadCloser
- func Streaming(c *http.Client) *http.Client
- type BusyError
- type Endpoint
- type FailureKind
- type Security
Constants ¶
const ( MaxRetries = 3 MaxBackOff = time.Minute )
Throttling: a busy answer is waited out and the request sent again, at most MaxRetries times, each wait at most MaxBackOff.
const TracePrefix = "imap> "
TracePrefix 标记已脱敏的 trace 行,报告移除前缀后保留其原始字节。
Variables ¶
var ( HeaderTimeout = time.Minute // until the response headers arrive RequestTimeout = 5 * time.Minute // a whole request, except on Streaming StallTimeout = time.Minute // a StallBody with no byte arriving )
Timeouts of one HTTP backend request, so a server that stops answering cannot hold an account's sync forever; vars so tests can shorten them.
var ClientVersion = "dev"
ClientVersion is sent in the IMAP ID command; release builds set it.
var ErrOtherHost = errors.New("refused a redirect to another host")
ErrOtherHost is a redirect, or a link, away from the account's host.
var ErrSMTPAuthDisabled = errors.New("SMTP AUTH is disabled for the mailbox")
ErrSMTPAuthDisabled is a backend.ErrAuthFailed whose server said SMTP AUTH is turned off for the mailbox (Microsoft's 535 5.7.139), not that the credentials are wrong.
var ErrStalled = errors.New("the server stopped sending")
ErrStalled is a response body that went too long without a byte arriving.
var SystemProxy = sysproxy.For
SystemProxy is the proxy for a request to u, nil for none: the user's; tests stand in for it.
var TLSRoots *x509.CertPool
TLSRoots verifies servers' certificates, nil for the system's; tests stand in for it.
Functions ¶
func Acquire ¶ added in v1.37.0
Acquire takes account's slot, waiting while another call holds it. account names the server and the user: the API's URL, a NUL, the user name. A wait that ctx ends says so and names the URL's host, so a request's deadline is never the bare context error (#755).
func BackOff ¶ added in v1.37.0
BackOff is how long to wait before sending again after try (0 for the first) failed with err: a *BusyError's BackOff, or 1s doubling with each try when it named none, at most MaxBackOff. False when err is not a *BusyError, or the retries are spent.
func DialIMAPAbortable ¶
func DialIMAPAbortable(ctx context.Context, e Endpoint, h *imapclient.UnilateralDataHandler) (*imapclient.Client, func(), error)
DialIMAPAbortable is DialIMAP that also returns abort, which closes the network connection at once. Unlike Client.Close it does not wait for a response literal being read, so it can cut off a download. h, if not nil, receives unsolicited server updates (IDLE).
func DialPOP3 ¶ added in v1.34.0
DialPOP3 connects to e and signs in with USER/PASS. close cuts the network connection at once, so a ctx that ends can unblock a read: go-pop3 takes no context. The library has no STLS, so it is done here before the connection is handed over.
func DialSMTP ¶
DialSMTP connects to e and authenticates with PLAIN, or XOAUTH2 when e has an AccessToken.
func HTTPStatusErr ¶ added in v1.37.0
HTTPStatusErr is the error for the statuses every HTTP backend reads alike: 401 is backend.ErrAuthFailed, 429 a *BusyError. Nil for any other, which the backend reads by its protocol.
func HTTPTransport ¶ added in v1.35.0
HTTPTransport is the HTTP backends' transport: it connects as IMAP and SMTP do, through the user's proxy, but to a server on this computer directly, and trusts TLSRoots.
func LogTrace ¶
func LogTrace(c *imapclient.Client, err error)
LogTrace logs the end of c's traffic, masked, when err is a protocol error: a server response go-imap could not parse. The log is what an error report carries, so a report of such an error shows what the server sent.
func NewHTTPClient ¶ added in v1.37.0
NewHTTPClient is an HTTP backend's client for one account: requests go through rt, nil for t itself, which sends them over t. It waits HeaderTimeout for an answer and RequestTimeout for a whole request, and follows a redirect only as SameHost allows.
func NewXOAuth2Client ¶
NewXOAuth2Client returns a SASL XOAUTH2 client (Google and Microsoft IMAP/SMTP).
func RefusalReply ¶ added in v1.34.0
RefusalReply is the server's reply to a refused login in err (a NO/BAD or an SMTP status, credentials masked), "" when err carries none.
func RetryAfter ¶ added in v1.37.0
RetryAfter is h's Retry-After, in seconds or as a date; 0 when it names none, or no time in the future.
func SameHost ¶ added in v1.37.0
SameHost is the HTTP backends' redirect policy: never from HTTPS to plain HTTP, and only to the host the request was for, so the credentials stay there (ErrOtherHost).
func SameOrigin ¶ added in v1.40.1
SameOrigin reports whether a and b are on the same host: the same host field, or the same host name in any case on the same port, a missing port read as the scheme's default, so https://h and https://H:443 match.
func Serial ¶ added in v1.37.0
func Serial(ctx context.Context, account string, wait func(context.Context, time.Duration) error, try func() error) error
Serial calls try for account, one call per account at a time, back-off waits included, so an account stays under the server's concurrency limit and stops while it is busy. It sends again after a *BusyError, waiting it out with wait (nil waits in real time), as BackOff says.
func StallBody ¶ added in v1.35.0
func StallBody(body io.ReadCloser, d time.Duration, cancel context.CancelFunc) io.ReadCloser
StallBody is body, failing with ErrStalled once d passes with no byte arriving; cancel, which should cancel the request, is called then and on Close. It bounds a download or event stream that an overall request timeout would cut off.
Types ¶
type BusyError ¶ added in v1.37.0
BusyError is a server's answer to slow down and try again after BackOff, 0 when it named no time. Err, when set, is the answer as the backend read it.
type Endpoint ¶
type Endpoint struct {
Host string
Port int
Security Security
Username, Password string
AccessToken string
}
Endpoint describes one server and the credentials to use on it. A non-empty AccessToken selects XOAUTH2 and Password is ignored.
type FailureKind ¶
type FailureKind string
FailureKind is the broad cause of a failed dial, for diagnostics.
const ( FailureDNS FailureKind = "dns" FailureConnect FailureKind = "connect" FailureTimeout FailureKind = "timeout" FailureTLS FailureKind = "tls" FailureAuth FailureKind = "auth" FailureProtocol FailureKind = "protocol" FailureProxyUnreachable FailureKind = "proxy_unreachable" FailureProxyRefused FailureKind = "proxy_refused" FailureProxyAuth FailureKind = "proxy_auth" // the proxy's credentials, never the account's )
func Classify ¶
func Classify(err error) FailureKind
Classify maps an error from DialIMAP or DialSMTP to a FailureKind; nil gives "".