diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1201253 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +test: check + +check: lint + go test -v + +lint: + gofmt -w=true -s=true -l=true ./ + golint ./... + go vet ./... + + + diff --git a/go.mod b/go.mod index a883ba1..972bce1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/chrj/smtpd -go 1.14 +go 1.19 diff --git a/smtpd.go b/smtpd.go index 852ff30..2ee1ca2 100644 --- a/smtpd.go +++ b/smtpd.go @@ -55,10 +55,10 @@ type Server struct { // mu guards doneChan and makes closing it and listener atomic from // perspective of Serve() - mu sync.Mutex - doneChan chan struct{} - listener *net.Listener - waitgrp sync.WaitGroup + mu sync.Mutex + doneChan chan struct{} + listener *net.Listener + waitgrp sync.WaitGroup inShutdown atomicBool // true when server is in shutdown } @@ -66,10 +66,11 @@ type Server struct { type Protocol string const ( - // SMTP + // SMTP means Simple Mail Transfer Protocol SMTP Protocol = "SMTP" - // Extended SMTP + // ESMTP means Extended Simple Mail Transfer Protocol, because it has some extra features + // Simple Mail Transfer Protocol doesn't have ESMTP = "ESMTP" ) @@ -228,7 +229,7 @@ func (srv *Server) Shutdown(wait bool) error { // First close the listener srv.mu.Lock() if srv.listener != nil { - lnerr = (*srv.listener).Close(); + lnerr = (*srv.listener).Close() } srv.closeDoneChanLocked() srv.mu.Unlock() @@ -254,7 +255,7 @@ func (srv *Server) Wait() error { // Address returns the listening address of the server func (srv *Server) Address() net.Addr { - return (*srv.listener).Addr(); + return (*srv.listener).Addr() } func (srv *Server) configureDefaults() { @@ -433,28 +434,27 @@ func (session *session) close() { session.conn.Close() } - // From net/http/server.go -func (s *Server) shuttingDown() bool { - return s.inShutdown.isSet() +func (srv *Server) shuttingDown() bool { + return srv.inShutdown.isSet() } -func (s *Server) getDoneChan() <-chan struct{} { - s.mu.Lock() - defer s.mu.Unlock() - return s.getDoneChanLocked() +func (srv *Server) getDoneChan() <-chan struct{} { + srv.mu.Lock() + defer srv.mu.Unlock() + return srv.getDoneChanLocked() } -func (s *Server) getDoneChanLocked() chan struct{} { - if s.doneChan == nil { - s.doneChan = make(chan struct{}) +func (srv *Server) getDoneChanLocked() chan struct{} { + if srv.doneChan == nil { + srv.doneChan = make(chan struct{}) } - return s.doneChan + return srv.doneChan } -func (s *Server) closeDoneChanLocked() { - ch := s.getDoneChanLocked() +func (srv *Server) closeDoneChanLocked() { + ch := srv.getDoneChanLocked() select { case <-ch: // Already closed. Don't close again. diff --git a/smtpd_test.go b/smtpd_test.go index 3f8488a..0913a24 100644 --- a/smtpd_test.go +++ b/smtpd_test.go @@ -394,11 +394,11 @@ func TestAuthNotSupported(t *testing.T) { func TestAuthBypass(t *testing.T) { addr, closer := runsslserver(t, &smtpd.Server{ - Authenticator: func(peer smtpd.Peer, username, password string) error { + Authenticator: func(peer smtpd.Peer, username, password string) error { return smtpd.Error{Code: 550, Message: "Denied"} }, - ForceTLS: true, - ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile), + ForceTLS: true, + ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile), }) defer closer()