-
Notifications
You must be signed in to change notification settings - Fork 1
/
smtp_server.py
144 lines (117 loc) · 4.61 KB
/
smtp_server.py
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
import threading
import socket
from client import Forward, Recipient
from mailoffice import MailOffice
LOCAL_HOST = 'remsha.online'
def send_command(sock, command, buffer=1024):
"""
Отправляем команду SMTP-Serve-y получателя или клиенту.
:param sock:
:param command:
:param buffer:
:return: Прочитанный ответ
"""
sock.sendall(command + b'\n')
return sock.recv(buffer).decode()
class SMTPServerCore:
STATE_INIT = 0
STATE_HELO = 1
STATE_EHLO = 2
STATE_MAIL = 3
STATE_RCPT = 4
STATE_DATA = 5
STATE_QUIT = 6
STATE_LOGIN = 7
def __init__(self, conn):
self.state = SMTPServerCore.STATE_INIT
self.local_host = LOCAL_HOST
self.socket = conn
self.mail_office = MailOffice(LOCAL_HOST)
self.forward = Forward()
self.recipient = Recipient()
def session(self):
"""
Главный цикл общения сервера и клиента.
:return:
"""
self.socket.sendall(b'220 mail.remsha.online SMTP is glad to see you!\n')
while True:
data = bytearray()
complete_line = 0
while not complete_line:
part = self.socket.recv(1024)
if part:
data += part
if len(data) >= 2:
complete_line = 1
if self.state != SMTPServerCore.STATE_DATA:
code, keep_connection = self.do_command(data)
print(code)
else:
code = self.mail_office.do_data(data)
keep_connection = 1
if code is None:
continue
self.socket.sendall(code + b"\n")
if not keep_connection:
self.socket.close()
else:
# EOF
return
def do_command(self, data):
"""
Разбираем команду которую получили от клиента и меняем состояние сервера, возвращаем код операции.
:param data:
:return:
"""
data = data.decode()
cmd = data[0:4]
cmd = cmd.upper()
keep_connection = 1
info_log = ' {} '.format(cmd)
if cmd == 'HELO':
self.state = SMTPServerCore.STATE_HELO
elif cmd == 'EHLO':
self.state = SMTPServerCore.STATE_EHLO
elif cmd == 'AUTH':
self.state = SMTPServerCore.STATE_LOGIN
elif cmd == "MAIL":
self.forward.login = data[11:-2]
info_log += self.forward.login
''''Пока что без авторизации'''
# if self.state != SMTPServerCore.STATE_LOGIN:
# return (b"503 Bad command sequence", 1)
self.state = SMTPServerCore.STATE_MAIL
elif cmd == "RCPT":
if self.state != SMTPServerCore.STATE_MAIL:
return b"503 Bad command sequence", 0
self.recipient.login = data[9:-2]
info_log += self.recipient.login
self.state = SMTPServerCore.STATE_RCPT
elif cmd == "DATA":
if self.state != SMTPServerCore.STATE_RCPT:
return b"503 Bad command sequence", 0
self.state = SMTPServerCore.STATE_DATA
self.mail_office.forward = self.forward
self.mail_office.recipient = self.recipient
return b"354 OK, Enter data, terminated with a \\r\\n.\\r\\n", keep_connection
elif cmd == "QUIT":
return b"221 remsha.online Service closing ", 0
elif cmd == "RSET":
self.state = SMTPServerCore.STATE_INIT
elif cmd == "NOOP":
return b"250 I Can Fly", keep_connection
else:
return b"505 Bad command", 0
return b"250 OK" + info_log.encode(), keep_connection
class SMTPServer:
def __init__(self, port=25, listeners=5):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind(('localhost', port))
self.socket.listen(listeners)
def socket_accept(self):
while True:
conn, addr = self.socket.accept()
engine = SMTPServerCore(conn)
t = threading.Thread(target=engine.session())
t.start()