Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smtp responses fixed and random wait added #63

Merged
merged 3 commits into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.")
}
}
}