mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 01:27:01 +00:00
fix: context related lints (#576)
* fix: POP3 server now uses TLS HandshakeContext Signed-off-by: James Hillyerd <james@hillyerd.com> * fix: Web server now uses Listen with context Signed-off-by: James Hillyerd <james@hillyerd.com> * fix: replace interface{} with any Signed-off-by: James Hillyerd <james@hillyerd.com> --------- Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -37,7 +37,7 @@ func init() {
|
|||||||
startTime.Set(time.Now().UnixNano() / 1000000)
|
startTime.Set(time.Now().UnixNano() / 1000000)
|
||||||
|
|
||||||
// Goroutine count for status page.
|
// Goroutine count for status page.
|
||||||
expvar.Publish("goroutines", expvar.Func(func() interface{} {
|
expvar.Publish("goroutines", expvar.Func(func() any {
|
||||||
return runtime.NumGoroutine()
|
return runtime.NumGoroutine()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package pop3
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -101,7 +102,7 @@ func (s *Session) String() string {
|
|||||||
* 4. If bad cmd, respond error
|
* 4. If bad cmd, respond error
|
||||||
* 5. Goto 2
|
* 5. Goto 2
|
||||||
*/
|
*/
|
||||||
func (s *Server) startSession(id int, conn net.Conn) {
|
func (s *Server) startSession(ctx context.Context, id int, conn net.Conn) {
|
||||||
logger := log.With().Str("module", "pop3").Str("remote", conn.RemoteAddr().String()).
|
logger := log.With().Str("module", "pop3").Str("remote", conn.RemoteAddr().String()).
|
||||||
Int("session", id).Logger()
|
Int("session", id).Logger()
|
||||||
logger.Debug().Msgf("ForceTLS: %t", s.config.ForceTLS)
|
logger.Debug().Msgf("ForceTLS: %t", s.config.ForceTLS)
|
||||||
@@ -165,7 +166,7 @@ func (s *Server) startSession(id int, conn net.Conn) {
|
|||||||
// Send command to handler for current state
|
// Send command to handler for current state
|
||||||
switch ssn.state {
|
switch ssn.state {
|
||||||
case AUTHORIZATION:
|
case AUTHORIZATION:
|
||||||
ssn.authorizationHandler(cmd, arg)
|
ssn.authorizationHandler(ctx, cmd, arg)
|
||||||
continue
|
continue
|
||||||
case TRANSACTION:
|
case TRANSACTION:
|
||||||
ssn.transactionHandler(cmd, arg)
|
ssn.transactionHandler(cmd, arg)
|
||||||
@@ -206,7 +207,7 @@ func (s *Server) startSession(id int, conn net.Conn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AUTHORIZATION state
|
// AUTHORIZATION state
|
||||||
func (s *Session) authorizationHandler(cmd string, args []string) {
|
func (s *Session) authorizationHandler(ctx context.Context, cmd string, args []string) {
|
||||||
switch cmd {
|
switch cmd {
|
||||||
case "QUIT":
|
case "QUIT":
|
||||||
s.send("+OK Goodnight and good luck")
|
s.send("+OK Goodnight and good luck")
|
||||||
@@ -229,9 +230,11 @@ func (s *Session) authorizationHandler(cmd string, args []string) {
|
|||||||
s.logger.Debug().Msg("Initiating TLS context.")
|
s.logger.Debug().Msg("Initiating TLS context.")
|
||||||
|
|
||||||
// Start TLS connection handshake.
|
// Start TLS connection handshake.
|
||||||
|
tlsCtx, cancel := context.WithTimeout(ctx, s.config.Timeout)
|
||||||
|
defer cancel()
|
||||||
s.send("+OK Begin TLS Negotiation")
|
s.send("+OK Begin TLS Negotiation")
|
||||||
tlsConn := tls.Server(s.conn, s.tlsConfig)
|
tlsConn := tls.Server(s.conn, s.tlsConfig)
|
||||||
if err := tlsConn.Handshake(); err != nil {
|
if err := tlsConn.HandshakeContext(tlsCtx); err != nil {
|
||||||
s.logger.Error().Msgf("-ERR TLS handshake failed %v", err)
|
s.logger.Error().Msgf("-ERR TLS handshake failed %v", err)
|
||||||
s.ooSeq(cmd)
|
s.ooSeq(cmd)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -302,7 +302,7 @@ func setupPOPServer(t *testing.T, ds storage.Store, tls bool, forceTLS bool) *Se
|
|||||||
cfg := config.POP3{
|
cfg := config.POP3{
|
||||||
Addr: "127.0.0.1:2500",
|
Addr: "127.0.0.1:2500",
|
||||||
Domain: "inbucket.local",
|
Domain: "inbucket.local",
|
||||||
Timeout: 5,
|
Timeout: 5 * time.Second,
|
||||||
Debug: true,
|
Debug: true,
|
||||||
ForceTLS: forceTLS,
|
ForceTLS: forceTLS,
|
||||||
}
|
}
|
||||||
@@ -344,7 +344,7 @@ func setupPOPSession(t *testing.T, server *Server) net.Conn {
|
|||||||
// Start the session.
|
// Start the session.
|
||||||
server.wg.Add(1)
|
server.wg.Add(1)
|
||||||
sessionNum++
|
sessionNum++
|
||||||
go server.startSession(sessionNum, &mockConn{serverConn})
|
go server.startSession(context.Background(), sessionNum, &mockConn{serverConn})
|
||||||
|
|
||||||
return clientConn
|
return clientConn
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ func (s *Server) serve(ctx context.Context) {
|
|||||||
} else {
|
} else {
|
||||||
tempDelay = 0
|
tempDelay = 0
|
||||||
s.wg.Add(1)
|
s.wg.Add(1)
|
||||||
go s.startSession(sid, conn)
|
go s.startSession(ctx, sid, conn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,6 +130,11 @@ func NewServer(conf *config.Root, mm message.Manager, mh *msghub.Hub) *Server {
|
|||||||
|
|
||||||
// Start begins listening for HTTP requests
|
// Start begins listening for HTTP requests
|
||||||
func (s *Server) Start(ctx context.Context, readyFunc func()) {
|
func (s *Server) Start(ctx context.Context, readyFunc func()) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
listenCfg net.ListenConfig
|
||||||
|
)
|
||||||
|
|
||||||
server = &http.Server{
|
server = &http.Server{
|
||||||
Addr: rootConfig.Web.Addr,
|
Addr: rootConfig.Web.Addr,
|
||||||
Handler: requestLoggingWrapper(Router),
|
Handler: requestLoggingWrapper(Router),
|
||||||
@@ -140,8 +145,11 @@ func (s *Server) Start(ctx context.Context, readyFunc func()) {
|
|||||||
// We don't use ListenAndServe because it lacks a way to close the listener
|
// We don't use ListenAndServe because it lacks a way to close the listener
|
||||||
log.Info().Str("module", "web").Str("phase", "startup").Str("addr", server.Addr).
|
log.Info().Str("module", "web").Str("phase", "startup").Str("addr", server.Addr).
|
||||||
Msg("HTTP listening on tcp4")
|
Msg("HTTP listening on tcp4")
|
||||||
var err error
|
|
||||||
listener, err = net.Listen("tcp", server.Addr)
|
// This context is only used while the listener is resolving our address.
|
||||||
|
listenCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
listener, err = listenCfg.Listen(listenCtx, "tcp", server.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Str("module", "web").Str("phase", "startup").Err(err).
|
log.Error().Str("module", "web").Str("phase", "startup").Err(err).
|
||||||
Msg("HTTP failed to start TCP4 listener")
|
Msg("HTTP failed to start TCP4 listener")
|
||||||
|
|||||||
Reference in New Issue
Block a user