diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 97d9a10..f55b37e 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -15,6 +15,7 @@ import ( var ( listenPort string = "1302" + password string = "" isLogging bool = false logLocation string listenerList []chan string @@ -92,9 +93,31 @@ func populateChat(conn net.Conn) { } +func getPasswd(conn net.Conn) error { + conn.Write([]byte("Password: ")) + userPassNewline, err := bufio.NewReader(conn).ReadString('\n') + userPass := strings.TrimRight(userPassNewline, "\n") + if err != nil { + e := fmt.Errorf("Node %v didn't respond to password prompt!\n", getIP(conn)) + return e + } + if userPass != password { + e := fmt.Errorf("Node %v attempted connecting with an incorrect password!\n", getIP(conn)) + return e + } + return nil +} + func handleConn(conn net.Conn, chatChan chan string) { defer conn.Close() + if password != "" { + if err := getPasswd(conn); err != nil { + log.Print(err) + return + } + } + go receiveMessageServer(conn, chatChan) //////////////////////////////////