/
nomail.go
74 lines (65 loc) · 1.3 KB
/
nomail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// nomail.go
//
// A simple SMTP server that refuses to do anything
//
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"unicode"
)
const (
SmtpNoServiceHere = 554
SmtpBadSequenceOfCommands = 503
)
func sendError(conn net.Conn, code int, message string) {
fmtmsg := fmt.Sprintf("%03d %s\r\n", code, message)
conn.Write([]byte(fmtmsg))
}
func handleConnection(conn net.Conn) {
defer conn.Close()
tcpconn, ok := conn.(*net.TCPConn)
if ok {
tcpconn.SetLinger(-1)
tcpconn.SetKeepAlive(true)
}
readbuf := bufio.NewReader(conn)
sendError(conn, SmtpNoServiceHere, "This is not the SMTP Service you are looking for, move along.")
for {
cmd, err := readbuf.ReadString('\n')
if err != nil {
break
}
cmd = strings.TrimRightFunc(cmd, unicode.IsSpace)
if cmd == "" {
continue
}
if strings.ToLower(cmd) == "quit" {
break
}
sendError(conn, SmtpBadSequenceOfCommands, "bad sequence of commands")
}
}
func main() {
listener, err := net.Listen("tcp", ":8025")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to bind to port 25: %s\n",
err)
os.Exit(1)
}
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
fmt.Fprintf(os.Stderr,
"error accepting connection: %s\n",
err)
break
}
go handleConnection(conn)
}
os.Exit(0)
}