add mutex to guard session's peer's metadata changing

This commit is contained in:
Anatolij Ostroumov 2023-06-14 10:37:39 +03:00
parent 92a3299f16
commit 68437c8c69
2 changed files with 8 additions and 4 deletions

View file

@ -61,7 +61,8 @@ func parseLine(line string) (cmd command) {
} }
func (session *session) handle(line string) { func (session *session) handle(line string) {
session.mutex.Lock()
defer session.mutex.Unlock()
cmd := parseLine(line) cmd := parseLine(line)
// Commands are dispatched to the appropriate handler functions. // Commands are dispatched to the appropriate handler functions.
@ -470,7 +471,7 @@ func (session *session) handleAUTH(cmd command) {
encodedUsername := "" encodedUsername := ""
if len(cmd.fields) < 3 { if len(cmd.fields) < 3 {
session.reply(334, "VXNlcm5hbWU6") session.reply(334, "VXNlcm5hbWU6") // `Username:`
if !session.scanner.Scan() { if !session.scanner.Scan() {
return return
} }
@ -486,7 +487,7 @@ func (session *session) handleAUTH(cmd command) {
return return
} }
session.reply(334, "UGFzc3dvcmQ6") session.reply(334, "UGFzc3dvcmQ6") // `Password:`
if !session.scanner.Scan() { if !session.scanner.Scan() {
return return

View file

@ -158,6 +158,8 @@ type session struct {
scanner *bufio.Scanner scanner *bufio.Scanner
tls bool tls bool
// mutex is used to guard editing peer's Metadata
mutex sync.Mutex
} }
func (srv *Server) newSession(c net.Conn) (s *session) { func (srv *Server) newSession(c net.Conn) (s *session) {
@ -170,11 +172,12 @@ func (srv *Server) newSession(c net.Conn) (s *session) {
peer: &Peer{ peer: &Peer{
Addr: c.RemoteAddr(), Addr: c.RemoteAddr(),
ServerName: srv.Hostname, ServerName: srv.Hostname,
Meta: make(map[string]interface{}, 0),
}, },
} }
// Check if the underlying connection is already TLS. // Check if the underlying connection is already TLS.
// This will happen if the Listerner provided Serve() // This will happen if the Listener provided Serve()
// is from tls.Listen() // is from tls.Listen()
var tlsConn *tls.Conn var tlsConn *tls.Conn