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 1 commit
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
64 changes: 54 additions & 10 deletions smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package glutton

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

// Client is a connection container
Expand All @@ -24,6 +28,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 +59,31 @@ 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 strings.Compare(client.r(g), ".\r\n") != 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be you want to add limit over reading data. Like break the loop when condition len(data) > dataVerbMaxLength comes true.
I think dataVerbMaxLength should be a constant.

Second Comment: You should log data you are reading

}
rwait()
client.w("250 OK")
} else if strings.Compare(query, "QUIT") == 0 {
client.w("Bye")
break
} else {
client.w("Recheck the command you entered.")
}
}
}