MCPcopy Index your code
hub / github.com/coder/coder / client

Method client

coderd/notifications/dispatch/smtp.go:275–352  ·  view source on GitHub ↗

client creates an SMTP client capable of communicating over a plain or TLS-encrypted connection.

(ctx context.Context, host string, port string)

Source from the content-addressed store, hash-verified

273
274// client creates an SMTP client capable of communicating over a plain or TLS-encrypted connection.
275func (s *SMTPHandler) client(ctx context.Context, host string, port string) (*smtp.Client, error) {
276 var (
277 c *smtp.Client
278 conn net.Conn
279 d net.Dialer
280 err error
281 )
282
283 // Outer context has a deadline (see CODER_NOTIFICATIONS_DISPATCH_TIMEOUT).
284 deadline, ok := ctx.Deadline()
285 if !ok {
286 return nil, xerrors.Errorf("context has no deadline")
287 }
288 // Align with context deadline.
289 d.Deadline = deadline
290
291 tlsCfg, err := s.tlsConfig()
292 if err != nil {
293 return nil, xerrors.Errorf("build TLS config: %w", err)
294 }
295
296 smarthost := fmt.Sprintf("%s:%s", host, port)
297 useTLS := false
298
299 // Use TLS if known TLS port(s) are used or TLS is forced.
300 if port == "465" || s.cfg.ForceTLS {
301 useTLS = true
302
303 // STARTTLS is only used on plain connections to upgrade.
304 if s.cfg.TLS.StartTLS {
305 s.log.Warn(ctx, "STARTTLS is not allowed on TLS connections; disabling STARTTLS")
306 s.cfg.TLS.StartTLS = false
307 }
308 }
309
310 // Dial a TLS or plain connection to the smarthost.
311 if useTLS {
312 conn, err = tls.DialWithDialer(&d, "tcp", smarthost, tlsCfg)
313 if err != nil {
314 return nil, xerrors.Errorf("establish TLS connection to server: %w", err)
315 }
316 } else {
317 conn, err = d.DialContext(ctx, "tcp", smarthost)
318 if err != nil {
319 return nil, xerrors.Errorf("establish plain connection to server: %w", err)
320 }
321 }
322
323 // If the connection is plain, and STARTTLS is configured, try to upgrade the connection.
324 if s.cfg.TLS.StartTLS {
325 c, err = smtp.NewClientStartTLS(conn, tlsCfg)
326 if err != nil {
327 return nil, xerrors.Errorf("upgrade connection with STARTTLS: %w", err)
328 }
329 } else {
330 c = smtp.NewClient(conn)
331
332 // HELO is performed here and not always because smtp.NewClientStartTLS greets the server already to establish

Callers 1

dispatchMethod · 0.95

Calls 4

tlsConfigMethod · 0.95
helloMethod · 0.95
DialContextMethod · 0.65
ErrorfMethod · 0.45

Tested by

no test coverage detected