Skip to content

Commit

Permalink
Pool accepting specific tls.Config (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
wscherphof authored and jordan-wright committed Jan 15, 2018
1 parent 4e83f79 commit 94ae17d
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Pool struct {
mut *sync.Mutex
lastBuildErr *timestampedErr
closing chan struct{}
tlsConfig *tls.Config
}

type client struct {
Expand All @@ -42,8 +43,8 @@ var (
ErrTimeout = errors.New("timed out")
)

func NewPool(address string, count int, auth smtp.Auth) *Pool {
return &Pool{
func NewPool(address string, count int, auth smtp.Auth, opt_tlsConfig ...*tls.Config) (pool *Pool, err error) {
pool = &Pool{
addr: address,
auth: auth,
max: count,
Expand All @@ -52,6 +53,14 @@ func NewPool(address string, count int, auth smtp.Auth) *Pool {
closing: make(chan struct{}),
mut: &sync.Mutex{},
}
if len(opt_tlsConfig) == 1 {
pool.tlsConfig = opt_tlsConfig[0]
} else if host, _, e := net.SplitHostPort(address); e != nil {
return nil, e
} else {
pool.tlsConfig = &tls.Config{ServerName: host}
}
return
}

// go1.1 didn't have this method
Expand Down Expand Up @@ -164,17 +173,12 @@ func (p *Pool) makeOne() {
}()
}

func startTLS(c *client, addr string) (bool, error) {
func startTLS(c *client, t *tls.Config) (bool, error) {
if ok, _ := c.Extension("STARTTLS"); !ok {
return false, nil
}

host, _, err := net.SplitHostPort(addr)
if err != nil {
return false, err
}

if err := c.StartTLS(&tls.Config{ServerName: host}); err != nil {
if err := c.StartTLS(t); err != nil {
return false, err
}

Expand All @@ -200,7 +204,7 @@ func (p *Pool) build() (*client, error) {
}
c := &client{cl, 0}

if _, err := startTLS(c, p.addr); err != nil {
if _, err := startTLS(c, p.tlsConfig); err != nil {
c.Close()
return nil, err
}
Expand Down

0 comments on commit 94ae17d

Please sign in to comment.