more cleanup

This commit is contained in:
AJ ONeal 2018-08-02 01:34:00 -06:00
parent 4342aa3901
commit 92340069b1
2 changed files with 19 additions and 18 deletions

View File

@ -37,11 +37,14 @@ func handleTelnetConn(bufConn bufferedConn) {
fmt.Fprintf(os.Stdout, "Ending socket\n") fmt.Fprintf(os.Stdout, "Ending socket\n")
if nil != u { if nil != u {
delTcpChat <- *u cleanTelnet <- *u
} }
break break
} }
buf := buffer[:count] msg := string(buffer[:count])
if "" == strings.TrimSpace(msg) {
continue
}
// Rate Limit: Reasonable poor man's DoS prevention (Part 1) // Rate Limit: Reasonable poor man's DoS prevention (Part 1)
// A human does not send messages super fast and blocking the // 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]) //fmt.Fprintf(os.Stdout, "buf{%s}\n", buf[:count])
// TODO use safer email testing // TODO use safer email testing
email = strings.TrimSpace(string(buf[:count])) email = strings.TrimSpace(msg)
emailParts := strings.Split(email, "@") emailParts := strings.Split(email, "@")
if 2 != len(emailParts) { if 2 != len(emailParts) {
fmt.Fprintf(bufConn, "Email: ") fmt.Fprintf(bufConn, "Email: ")
@ -120,7 +123,7 @@ func handleTelnetConn(bufConn bufferedConn) {
continue continue
} }
if code != strings.TrimSpace(string(buf[:count])) { if code != strings.TrimSpace(msg) {
fmt.Fprintf(bufConn, "Incorrect Code\nAuth Code: ") fmt.Fprintf(bufConn, "Incorrect Code\nAuth Code: ")
} else { } else {
authn = true authn = true
@ -132,7 +135,7 @@ func handleTelnetConn(bufConn bufferedConn) {
userCount: make(chan int, 1), userCount: make(chan int, 1),
newMsg: make(chan string, 10), // reasonably sized newMsg: make(chan string, 10), // reasonably sized
} }
authTcpChat <- *u authTelnet <- *u
// prevent data race on len(myRawConns) // prevent data race on len(myRawConns)
// XXX (there can't be a race between these two lines, right?) // XXX (there can't be a race between these two lines, right?)
count := <-u.userCount count := <-u.userCount
@ -170,7 +173,7 @@ func handleTelnetConn(bufConn bufferedConn) {
broadcastMsg <- myMsg{ broadcastMsg <- myMsg{
ReceivedAt: time.Now(), ReceivedAt: time.Now(),
sender: bufConn, sender: bufConn,
Message: string(buf[0:count]), Message: strings.TrimRight(msg, "\r\n"),
Channel: "general", Channel: "general",
User: email, User: email,
} }
@ -191,9 +194,9 @@ func handleTelnetBroadcast(u *tcpUser) {
// https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/ // https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
timeoutDuration := 2 * time.Second timeoutDuration := 2 * time.Second
u.bufConn.SetWriteDeadline(time.Now().Add(timeoutDuration)) u.bufConn.SetWriteDeadline(time.Now().Add(timeoutDuration))
_, err := fmt.Fprintf(u.bufConn, msg) _, err := fmt.Fprintf(u.bufConn, msg+"\r\n")
if nil != err { if nil != err {
delTcpChat <- *u cleanTelnet <- *u
break break
} }
} }

View File

@ -96,8 +96,8 @@ var broadcastMsg chan myMsg
var virginConns chan net.Conn var virginConns chan net.Conn
var wantsServerHello chan bufferedConn var wantsServerHello chan bufferedConn
var authTcpChat chan tcpUser var authTelnet chan tcpUser
var delTcpChat chan tcpUser var cleanTelnet chan tcpUser
var gotClientHello chan bufferedConn var gotClientHello chan bufferedConn
// Http // Http
@ -272,8 +272,6 @@ func sendAuthCode(cnf ConfMailer, to string) (string, error) {
} }
if resp.StatusCode < 200 || resp.StatusCode >= 300 || "{" != string(body[0]) { 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) 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 return code, nil
@ -322,7 +320,7 @@ func main() {
// TCP & Authentication // TCP & Authentication
myRawConns := make(map[bufferedConn]tcpUser) myRawConns := make(map[bufferedConn]tcpUser)
wantsServerHello = make(chan bufferedConn, 128) wantsServerHello = make(chan bufferedConn, 128)
authTcpChat = make(chan tcpUser, 128) authTelnet = make(chan tcpUser, 128)
// HTTP & Authentication // HTTP & Authentication
myAuthReqs := make(map[string]authReq) myAuthReqs := make(map[string]authReq)
@ -409,7 +407,7 @@ func main() {
case conn := <-virginConns: case conn := <-virginConns:
// This is short lived // This is short lived
go handleConnection(conn) go handleConnection(conn)
case u := <-authTcpChat: case u := <-authTelnet:
// allow to receive messages // allow to receive messages
// (and be counted among the users) // (and be counted among the users)
myRawConns[u.bufConn] = u myRawConns[u.bufConn] = u
@ -442,7 +440,7 @@ func main() {
delete(myAuthReqs, ar.Cid) delete(myAuthReqs, ar.Cid)
case bufConn := <-wantsServerHello: case bufConn := <-wantsServerHello:
go handleTelnetConn(bufConn) go handleTelnetConn(bufConn)
case u := <-delTcpChat: case u := <-cleanTelnet:
// we can safely ignore this error, if any // we can safely ignore this error, if any
close(u.newMsg) close(u.newMsg)
u.bufConn.Close() u.bufConn.Close()
@ -476,7 +474,7 @@ func main() {
// I wonder if we could use IP detection to get the client's tz // I wonder if we could use IP detection to get the client's tz
// ... could probably make time for this in the authentication loop // ... could probably make time for this in the authentication loop
zone, _ := msg.ReceivedAt.Zone() 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.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second(), zone, t.Hour(), t.Minute(), t.Second(), zone,
sender, sender,
@ -500,7 +498,7 @@ func main() {
// In the case that it's experiencing network issues, // In the case that it's experiencing network issues,
// well, these things happen when you're having network issues. // well, these things happen when you're having network issues.
// It can reconnect. // It can reconnect.
delTcpChat <- u cleanTelnet <- u
} }
/* /*
@ -515,7 +513,7 @@ func main() {
conn.SetWriteDeadline(time.Now().Add(timeoutDuration)) conn.SetWriteDeadline(time.Now().Add(timeoutDuration))
_, err := fmt.Fprintf(conn, msg) _, err := fmt.Fprintf(conn, msg)
if nil != err { if nil != err {
delTcpChat <- u cleanTelnet <- u
} }
}(conn) }(conn)
*/ */