Compare commits

...

2 Commits

Author SHA1 Message Date
AJ ONeal 92340069b1 more cleanup 2018-08-02 01:34:00 -06:00
AJ ONeal 4342aa3901 comments 2018-08-02 01:13:26 -06:00
2 changed files with 22 additions and 18 deletions

View File

@ -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
}
}

View File

@ -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
@ -433,13 +431,16 @@ func main() {
if ok {
ar.Chan <- av
} else {
// sending empty object so that I can still send a copy
// rather than a pointer above. Maybe not the right way
// to do this, but it works for now.
ar.Chan <- authReq{}
}
case ar := <-delAuthReqs:
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()
@ -473,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,
@ -497,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
}
/*
@ -512,7 +513,7 @@ func main() {
conn.SetWriteDeadline(time.Now().Add(timeoutDuration))
_, err := fmt.Fprintf(conn, msg)
if nil != err {
delTcpChat <- u
cleanTelnet <- u
}
}(conn)
*/