Compare commits

..

No commits in common. "92340069b1b204a08390bef5d200c04f94eaf15b" and "845583e06bcf5e46fc3f24b7cb9fc527cbedbe70" have entirely different histories.

2 changed files with 18 additions and 22 deletions

View File

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

View File

@ -96,8 +96,8 @@ var broadcastMsg chan myMsg
var virginConns chan net.Conn
var wantsServerHello chan bufferedConn
var authTelnet chan tcpUser
var cleanTelnet chan tcpUser
var authTcpChat chan tcpUser
var delTcpChat chan tcpUser
var gotClientHello chan bufferedConn
// Http
@ -272,6 +272,8 @@ 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
@ -320,7 +322,7 @@ func main() {
// TCP & Authentication
myRawConns := make(map[bufferedConn]tcpUser)
wantsServerHello = make(chan bufferedConn, 128)
authTelnet = make(chan tcpUser, 128)
authTcpChat = make(chan tcpUser, 128)
// HTTP & Authentication
myAuthReqs := make(map[string]authReq)
@ -407,7 +409,7 @@ func main() {
case conn := <-virginConns:
// This is short lived
go handleConnection(conn)
case u := <-authTelnet:
case u := <-authTcpChat:
// allow to receive messages
// (and be counted among the users)
myRawConns[u.bufConn] = u
@ -431,16 +433,13 @@ 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 := <-cleanTelnet:
case u := <-delTcpChat:
// we can safely ignore this error, if any
close(u.newMsg)
u.bufConn.Close()
@ -474,7 +473,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): %s\r\n",
fmt.Fprintf(os.Stdout, tf+" [%s] (%s):\n\t%s",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second(), zone,
sender,
@ -498,7 +497,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.
cleanTelnet <- u
delTcpChat <- u
}
/*
@ -513,7 +512,7 @@ func main() {
conn.SetWriteDeadline(time.Now().Add(timeoutDuration))
_, err := fmt.Fprintf(conn, msg)
if nil != err {
cleanTelnet <- u
delTcpChat <- u
}
}(conn)
*/