8000 Use less restrictve Dialer interface in DialWithDialer by foxcpp · Pull Request #249 · emersion/go-imap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use less restrictve Dialer interface in DialWithDialer #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"log"
"net"
"os"
"syscall"
"sync"
"syscall"
"time"

"github.com/emersion/go-imap"
Expand Down Expand Up @@ -557,7 +557,7 @@ func (c *Client) SetDebug(w io.Writer) {
return conn, nil
})
if err != nil {
log.Println("SetDebug:",err)
log.Println("SetDebug:", err)
}

}
Expand Down Expand Up @@ -592,11 +592,16 @@ func Dial(addr string) (c *Client, err error) {
return
}

type Dialer interface {
// Dial connects to the given address via the proxy.
Dial(network, addr string) (net.Conn, error)
}

// DialWithDialer connects to an IMAP server using an unencrypted connection
// using dialer.Dial.
//
// Among other uses, this allows to apply a dial timeout.
func DialWithDialer(dialer *net.Dialer, address string) (c *Client, err error) {
func DialWithDialer(dialer Dialer, address string) (c *Client, err error) {
conn, err := dialer.Dial("tcp", address)
if err != nil {
return nil, err
Expand All @@ -606,8 +611,8 @@ func DialWithDialer(dialer *net.Dialer, address string) (c *Client, err error) {
// there is no way to set the client's Timeout for that action. As a
// workaround, if the dialer has a timeout set, use that for the connection's
// deadline.
if dialer.Timeout > 0 {
err = conn.SetDeadline(time.Now().Add(dialer.Timeout))
if netDialer, ok := dialer.(*net.Dialer); ok && netDialer.Timeout > 0 {
err = conn.SetDeadline(time.Now().Add(netDialer.Timeout))
if err != nil {
return
}
Expand All @@ -633,25 +638,27 @@ func DialTLS(addr string, tlsConfig *tls.Config) (c *Client, err error) {
// using dialer.Dial.
//
// Among other uses, this allows to apply a dial timeout.
func DialWithDialerTLS(dialer *net.Dialer, addr string,
func DialWithDialerTLS(dialer Dialer, addr string,
tlsConfig *tls.Config) (c *Client, err error) {
conn, err := tls.DialWithDialer(dialer, "tcp", addr, tlsConfig)

conn, err := dialer.Dial("tcp", addr)
if err != nil {
return
}
tlsConn := tls.Client(conn, tlsConfig)

// We don't return to the caller until we try to receive a greeting. As such,
// there is no way to set the client's Timeout for that action. As a
// workaround, if the dialer has a timeout set, use that for the connection's
// deadline.
if dialer.Timeout > 0 {
err = conn.SetDeadline(time.Now().Add(dialer.Timeout))
if netDialer, ok := dialer.(*net.Dialer); ok && netDialer.Timeout > 0 {
err = tlsConn.SetDeadline(time.Now().Add(netDialer.Timeout))
if err != nil {
return
}
}

c, err = New(conn)
c, err = New(tlsConn)
c.isTLS = true
return
}
0