generated from LeoFVO/go4hackers
/
listenner.go
179 lines (162 loc) · 5.48 KB
/
listenner.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package smtp
import (
"crypto/tls"
"net"
"strings"
log "github.com/sirupsen/logrus"
)
func (server Server) Listen() error {
// The ln variable represents the listener object, which is used to accept incoming connections.
ln, err := net.Listen("tcp", server.Hostname + ":" + server.Port)
if err != nil {
return err
}
log.Infof("Server started listening on %s:%s", server.Hostname, server.Port)
// The defer ln.Close() statement ensures that the listener is closed when the function exits.
defer ln.Close()
// Accept incoming connections
// When a new connection is accepted, the handleConnection() function is called to handle incoming messages:
for {
conn, err := ln.Accept()
if err != nil {
log.Errorln(err)
continue
}
// Handle incoming connection
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
// Send initial greeting message according to the RFC
conn.Write([]byte("220 localhost ESMTP Service Ready\r\n"))
log.Infof("Connection from %s to %s", conn.RemoteAddr().String(), conn.LocalAddr().String())
// Buffer for incoming messages
buf := make([]byte, 1024)
// Loop to handle incoming commands
for {
// Read incoming message
n, err := conn.Read(buf)
if err != nil {
log.Println(err)
break
}
// Parse incoming command
cmd := string(buf[:n]) // Convert bytes to string
parts := strings.Split(cmd, " ") // Split string into parts
command := strings.TrimSpace(parts[0]) // Extract command
args := parts[1:] // Extract arguments
// Handle incoming command
switch command {
case "EHLO", "HELO":
if len(args) < 1 {
conn.Write([]byte("501 Syntax: HELO hostname\r\n"))
continue
}
log.Tracef("Received command %s %+v", command, string(args[0]))
/*
* The list of features sent in the code includes:
*
* SIZE 31457280: This indicates that the server supports messages up to 31457280 bytes in size.
* 8BITMIME: This indicates that the server supports 8-bit MIME encoding.
* STARTTLS: This indicates that the server supports the STARTTLS command, which can be used to initiate a secure TLS connection.
* The server may support other features that can be included in the list. The purpose of sending the list of features is to allow the client to know what features are available and to negotiate which features to use.
*/
conn.Write([]byte("250 localhost\r\n"))
conn.Write([]byte("250-SIZE 31457280\r\n"))
conn.Write([]byte("250-8BITMIME\r\n"))
// conn.Write([]byte("250-STARTTLS\r\n"))
conn.Write([]byte("250-OK\r\n"))
case "MAIL":
if len(args) < 1 {
log.Tracef("Received command %s %+v", command, args)
conn.Write([]byte("501 Syntax: MAIL FROM:<address>\r\n"))
break
}
if !strings.HasPrefix(string(args[0]), "FROM:") {
log.Tracef("Received command %s %+v", command, args)
conn.Write([]byte("501 Syntax: MAIL FROM:<address>\r\n"))
break
}
log.Tracef("Received command %s %+v", command, string(args[0]))
conn.Write([]byte("250 OK\r\n"))
case "RCPT":
if len(args) < 1 {
log.Tracef("Received command %s %+v", command, args)
conn.Write([]byte("501 Syntax: RCPT TO:<address>\r\n"))
break
}
if !strings.HasPrefix(string(args[0]), "TO:") {
log.Tracef("Received command %s %+v", command, args)
conn.Write([]byte("501 Syntax: RCPT TO:<address>\r\n"))
break
}
log.Tracef("Received command %s %+v", command, string(string(args[0])))
conn.Write([]byte("250 OK\r\n"))
case "DATA":
if len(args) > 0 {
log.Tracef("Received command %s %+v", command, args)
conn.Write([]byte("501 Syntax: DATA\r\n"))
break
}
log.Tracef("Received command %s", command)
conn.Write([]byte("354 Start mail input; end with <CRLF>.<CRLF>\r\n"))
// Read incoming message data
var data []byte
for {
n, err := conn.Read(buf)
if err != nil {
log.Println(err)
break
}
data = append(data, buf[:n]...)
if strings.HasSuffix(string(data), "\r\n.\r\n") {
break
}
}
log.Debugf("Received message: \n%s\n", string(data))
conn.Write([]byte("250 OK\r\n"))
case "STARTTLS":
if len(args) > 0 {
log.Tracef("Received command %s %+v", command, args)
conn.Write([]byte("501 Syntax: STARTTLS\r\n"))
break
}
log.Tracef("Received command %s", command)
// Check if TLS is already established
if _, ok := conn.(*tls.Conn); ok {
conn.Write([]byte("503 Error: TLS already established\r\n"))
break
}
// Send response code indicating that TLS negotiation will begin
conn.Write([]byte("220 Ready to start TLS\r\n"))
conn.Write([]byte("220-Go ahead\r\n"))
// Upgrade the connection to TLS
tlsConfig := &tls.Config{
// Customize TLS configuration here
}
tlsConn := tls.Server(conn, tlsConfig)
err = tlsConn.Handshake()
if err != nil {
log.Errorln(err)
break
}
// Start new session with upgraded connection
handleConnection(tlsConn)
return
case "QUIT":
if len(args) > 0 {
log.Tracef("Received command %s %+v", command, args)
conn.Write([]byte("501 Syntax: QUIT\r\n"))
break
}
log.Tracef("Received command %s", command)
conn.Write([]byte("221 Bye\r\n"))
conn.Close()
log.Infof("Connection from %s closed\n", conn.RemoteAddr().String())
return
default:
log.Debugf("Received unknown command %s with args: %s", command, strings.Join(args, ","))
conn.Write([]byte("502 Command not implemented\r\n"))
}
}
}