MailSharp / MailSharp.WebManager / Controllers / StatusController.cs
Code · 133 lines · 3390 bytes
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
133using MailSharp.IMAP.Metrics;
using MailSharp.POP3.Metrics;
using MailSharp.SMTP.Metrics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace MailSharp.WebManager.Controllers;

[Authorize(Roles = "Administrator")]
[Route("~/api/[controller]")]
[ApiController]
public class StatusController(SmtpMetrics smtpMetrics, ImapMetrics imapMetrics, Pop3Metrics pop3Metrics) : ControllerBase
{
	[HttpGet("all")]
	public IActionResult StatusAll() => Ok(new
	{
		smtp = smtpMetrics.IsRunning,
		imap = imapMetrics.IsRunning,
		pop3 = pop3Metrics.IsRunning
	});

	[HttpGet("smtp")]
	public IActionResult SmtpStatus()
	{
		var topSenders = smtpMetrics.TopSenderDomains
			.OrderByDescending(x => x.Value).Take(10)
			.Select(x => new { domain = x.Key, count = x.Value });

		var topRecipients = smtpMetrics.TopRecipientDomains
			.OrderByDescending(x => x.Value).Take(10)
			.Select(x => new { domain = x.Key, count = x.Value });

		return Ok(new
		{
			isRunning = smtpMetrics.IsRunning,
			startTime = smtpMetrics.StartTime,
			uptimeSeconds = smtpMetrics.UptimeSeconds,
			connections = new
			{
				total = smtpMetrics.TotalConnections,
				active = smtpMetrics.ActiveSessions
			},
			messages = new
			{
				received = smtpMetrics.MessagesReceived,
				relayed = smtpMetrics.MessagesRelayed,
				bytesReceived = smtpMetrics.BytesReceived,
				lastReceivedAt = smtpMetrics.LastMessageReceivedAt
			},
			rejections = new
			{
				total = smtpMetrics.MessagesRejectedTotal,
				spf = smtpMetrics.MessagesRejectedSpf,
				dkim = smtpMetrics.MessagesRejectedDkim,
				dmarc = smtpMetrics.MessagesRejectedDmarc
			},
			auth = new
			{
				success = smtpMetrics.AuthSuccess,
				failed = smtpMetrics.AuthFailed
			},
			topSenders,
			topRecipients
		});
	}

	[HttpGet("imap")]
	public IActionResult ImapStatus()
	{
		var topUsers = imapMetrics.TopUsers
			.OrderByDescending(x => x.Value).Take(10)
			.Select(x => new { user = x.Key, count = x.Value });

		return Ok(new
		{
			isRunning = imapMetrics.IsRunning,
			startTime = imapMetrics.StartTime,
			uptimeSeconds = imapMetrics.UptimeSeconds,
			connections = new
			{
				total = imapMetrics.TotalConnections,
				active = imapMetrics.ActiveSessions
			},
			auth = new
			{
				success = imapMetrics.LoginSuccess,
				failed = imapMetrics.LoginFailed
			},
			messages = new
			{
				fetched = imapMetrics.MessagesFetched,
				bytesSent = imapMetrics.BytesSent,
				lastActivityAt = imapMetrics.LastActivityAt
			},
			commands = imapMetrics.CommandsProcessed,
			folderSelects = imapMetrics.FolderSelects,
			topUsers
		});
	}

	[HttpGet("pop3")]
	public IActionResult Pop3Status()
	{
		var topUsers = pop3Metrics.TopUsers
			.OrderByDescending(x => x.Value).Take(10)
			.Select(x => new { user = x.Key, count = x.Value });

		return Ok(new
		{
			isRunning = pop3Metrics.IsRunning,
			startTime = pop3Metrics.StartTime,
			uptimeSeconds = pop3Metrics.UptimeSeconds,
			connections = new
			{
				total = pop3Metrics.TotalConnections,
				active = pop3Metrics.ActiveSessions
			},
			auth = new
			{
				success = pop3Metrics.LoginSuccess,
				failed = pop3Metrics.LoginFailed
			},
			messages = new
			{
				retrieved = pop3Metrics.MessagesRetrieved,
				deleted = pop3Metrics.MessagesDeleted,
				bytesSent = pop3Metrics.BytesSent,
				lastActivityAt = pop3Metrics.LastActivityAt
			},
			topUsers
		});
	}
}