diff --git a/chatserver-telnet.go b/chatserver-telnet.go index ec69b2c..f0393b6 100644 --- a/chatserver-telnet.go +++ b/chatserver-telnet.go @@ -37,11 +37,14 @@ func handleTelnetConn(bufConn bufferedConn) { fmt.Fprintf(os.Stdout, "Ending socket\n") if nil != u { - delTcpChat <- *u + cleanTelnet <- *u } break } - buf := buffer[:count] + msg := string(buffer[:count]) + if "" == strings.TrimSpace(msg) { + continue + } // Rate Limit: Reasonable poor man's DoS prevention (Part 1) // A human does not send messages super fast and blocking the @@ -63,7 +66,7 @@ func handleTelnetConn(bufConn bufferedConn) { //fmt.Fprintf(os.Stdout, "buf{%s}\n", buf[:count]) // TODO use safer email testing - email = strings.TrimSpace(string(buf[:count])) + email = strings.TrimSpace(msg) emailParts := strings.Split(email, "@") if 2 != len(emailParts) { fmt.Fprintf(bufConn, "Email: ") @@ -120,7 +123,7 @@ func handleTelnetConn(bufConn bufferedConn) { continue } - if code != strings.TrimSpace(string(buf[:count])) { + if code != strings.TrimSpace(msg) { fmt.Fprintf(bufConn, "Incorrect Code\nAuth Code: ") } else { authn = true @@ -132,7 +135,7 @@ func handleTelnetConn(bufConn bufferedConn) { userCount: make(chan int, 1), newMsg: make(chan string, 10), // reasonably sized } - authTcpChat <- *u + authTelnet <- *u // prevent data race on len(myRawConns) // XXX (there can't be a race between these two lines, right?) count := <-u.userCount @@ -170,7 +173,7 @@ func handleTelnetConn(bufConn bufferedConn) { broadcastMsg <- myMsg{ ReceivedAt: time.Now(), sender: bufConn, - Message: string(buf[0:count]), + Message: strings.TrimRight(msg, "\r\n"), Channel: "general", User: email, } @@ -191,9 +194,9 @@ func handleTelnetBroadcast(u *tcpUser) { // https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/ timeoutDuration := 2 * time.Second u.bufConn.SetWriteDeadline(time.Now().Add(timeoutDuration)) - _, err := fmt.Fprintf(u.bufConn, msg) + _, err := fmt.Fprintf(u.bufConn, msg+"\r\n") if nil != err { - delTcpChat <- *u + cleanTelnet <- *u break } } diff --git a/chatserver.go b/chatserver.go index 6b98a05..d01e786 100644 --- a/chatserver.go +++ b/chatserver.go @@ -96,8 +96,8 @@ var broadcastMsg chan myMsg var virginConns chan net.Conn var wantsServerHello chan bufferedConn -var authTcpChat chan tcpUser -var delTcpChat chan tcpUser +var authTelnet chan tcpUser +var cleanTelnet chan tcpUser var gotClientHello chan bufferedConn // Http @@ -272,8 +272,6 @@ func sendAuthCode(cnf ConfMailer, to string) (string, error) { } if resp.StatusCode < 200 || resp.StatusCode >= 300 || "{" != string(body[0]) { fmt.Fprintf(os.Stdout, "[Mailgun] Uh-oh...\n[Maigun] Baby Brent says: %s\n", body) - } else { - fmt.Fprintf(os.Stdout, "[Mailgun] Status: %d", resp.StatusCode) } return code, nil @@ -322,7 +320,7 @@ func main() { // TCP & Authentication myRawConns := make(map[bufferedConn]tcpUser) wantsServerHello = make(chan bufferedConn, 128) - authTcpChat = make(chan tcpUser, 128) + authTelnet = make(chan tcpUser, 128) // HTTP & Authentication myAuthReqs := make(map[string]authReq) @@ -409,7 +407,7 @@ func main() { case conn := <-virginConns: // This is short lived go handleConnection(conn) - case u := <-authTcpChat: + case u := <-authTelnet: // allow to receive messages // (and be counted among the users) myRawConns[u.bufConn] = u @@ -442,7 +440,7 @@ func main() { delete(myAuthReqs, ar.Cid) case bufConn := <-wantsServerHello: go handleTelnetConn(bufConn) - case u := <-delTcpChat: + case u := <-cleanTelnet: // we can safely ignore this error, if any close(u.newMsg) u.bufConn.Close() @@ -476,7 +474,7 @@ func main() { // I wonder if we could use IP detection to get the client's tz // ... could probably make time for this in the authentication loop zone, _ := msg.ReceivedAt.Zone() - fmt.Fprintf(os.Stdout, tf+" [%s] (%s):\n\t%s", + fmt.Fprintf(os.Stdout, tf+" [%s] (%s): %s\r\n", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), zone, sender, @@ -500,7 +498,7 @@ func main() { // In the case that it's experiencing network issues, // well, these things happen when you're having network issues. // It can reconnect. - delTcpChat <- u + cleanTelnet <- u } /* @@ -515,7 +513,7 @@ func main() { conn.SetWriteDeadline(time.Now().Add(timeoutDuration)) _, err := fmt.Fprintf(conn, msg) if nil != err { - delTcpChat <- u + cleanTelnet <- u } }(conn) */