Skip to content

Commit

Permalink
Smtp responses fixed and random wait added (#63)
Browse files Browse the repository at this point in the history
smtp responses fixed and random wait added
  • Loading branch information
HashCode55 authored and glaslos committed May 16, 2017
1 parent deca925 commit 8ec2df8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
1 change: 0 additions & 1 deletion app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func main() {

// Setting up the logger
logger := log.New()

// Write log to file and stdout
f, err := os.OpenFile(*logPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
onErrorExit(err)
Expand Down
73 changes: 63 additions & 10 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ package glutton

import (
"bufio"
"math/rand"
"net"
"regexp"
"strings"
"time"
)

// maximum lines that can be read after the "DATA" command
const maxDataRead = 500

// Client is a connection container
type Client struct {
conn net.Conn
Expand All @@ -24,6 +31,29 @@ func (c *Client) r(g *Glutton) string {
return reply
}

func rwait() {
// makes the process sleep for random time
rand.Seed(time.Now().Unix())
// between 0.5 - 1.5 seconds
rtime := rand.Intn(1500) + 500
duration := time.Duration(rtime) * time.Millisecond
time.Sleep(duration)
}
func validateMail(query string) bool {
email := regexp.MustCompile("^MAIL FROM:<.+@.+>$") // naive regex
if email.MatchString(query) {
return true
}
return false
}
func validateRCPT(query string) bool {
rcpt := regexp.MustCompile("^RCPT TO:<.+@.+>$")
if rcpt.MatchString(query) {
return true
}
return false
}

// HandleSMTP takes a net.Conn and does basic SMTP communication
func (g *Glutton) HandleSMTP(conn net.Conn) {
defer conn.Close()
Expand All @@ -32,14 +62,37 @@ func (g *Glutton) HandleSMTP(conn net.Conn) {
bufin: bufio.NewReader(conn),
bufout: bufio.NewWriter(conn),
}
client.w("220 Welcome")
g.logger.Infof("[smpt ] Payload 1: %q", client.r(g))
client.w("250 Is it me?")
g.logger.Infof("[smpt ] Payload 2: %q", client.r(g))
client.w("250 Sender")
g.logger.Infof("[smpt ] Payload 3: %q", client.r(g))
client.w("250 Recipient")
g.logger.Infof("[smpt ] Payload 4: %q", client.r(g))
client.w("354 Ok Send data ending with <CRLF>.<CRLF>")
g.logger.Infof("[smpt ] Payload 5: %q", client.r(g))
rwait()
client.w("220 Welcome!")
for {
query := strings.Trim(client.r(g), "\r\n")
g.logger.Infof("[smtp ] Payload : %q", query)
if strings.HasPrefix(query, "HELO ") {
rwait()
client.w("250 Hello! Pleased to meet you.")
} else if validateMail(query) {
rwait()
client.w("250 OK")
} else if validateRCPT(query) {
rwait()
client.w("250 OK")
} else if strings.Compare(query, "DATA") == 0 {
client.w("354 End data with <CRLF>.<CRLF>")
for readctr := maxDataRead; readctr >= 0; readctr -= 1 {
data := client.r(g)
g.logger.Infof("[smtp ] Data : %q", data)
// exit condition
if strings.Compare(data, ".\r\n") == 0 {
break
}
}
rwait()
client.w("250 OK")
} else if strings.Compare(query, "QUIT") == 0 {
client.w("Bye")
break
} else {
client.w("Recheck the command you entered.")
}
}
}

0 comments on commit 8ec2df8

Please sign in to comment.