-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmtpServer.cs
57 lines (49 loc) · 2.22 KB
/
SmtpServer.cs
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
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using TCPServer;
using TCPServer.Models;
using SMTPServer.Database;
using Microsoft.EntityFrameworkCore;
namespace SMTPServer
{
public partial class SmtpServer : TcpServer
{
public SmtpServerConfiguration Configuration { get; private set; }
private readonly SmtpConnectionHandler connectionHandler = new SmtpConnectionHandler();
private readonly SmtpCommandHandler commandHandler = new SmtpCommandHandler();
private readonly DatabaseServerService db = new DatabaseServerService();
public new void Start()
{
base.ConnectionBacklog = Configuration.ConnectionBacklog;
commandHandler.Initialize();
db.CreateDatabase();
base.Start();
db.AddSmtpServerInstance(Configuration.Name, Configuration.IPAddress, Configuration.Port, DateTime.Now);
Logger.Log($"SMTP Server {Configuration.Name} started on {Configuration.IPAddress}:{Configuration.Port}");
}
public SmtpServer(IPAddress ipAddress, int port, string name) : base(ipAddress, port)
{
Configuration = new SmtpServerConfiguration(this, ipAddress, port, name);
}
public override void OnConnectionClosed(TcpConnection tcpConnection, CloseReason closeReason)
{
connectionHandler.RemoveSmtpConnection(tcpConnection);
Logger.Log("Disconnected");
}
public override void OnConnectionEstablished(TcpConnection tcpConnection)
{
SmtpConnection smtpConnection = new SmtpConnection(tcpConnection, Configuration, commandHandler);
connectionHandler.AddSmtpConnection(tcpConnection, smtpConnection);
db.AddSmtpTcpConnection(tcpConnection.ConnectionInformation.RemoteEndpoint.Address,
tcpConnection.ConnectionInformation.RemoteEndpoint.Port,
tcpConnection.ConnectionInformation.Established);
smtpConnection.SendGreetingReply();
}
public override void OnDataReceived(TcpConnection tcpConnection, byte[] data)
{
connectionHandler.GetSmtpConnection(tcpConnection).ReceiveData(data);
}
}
}