123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- using GenHTTP.Api.Content;
- using GenHTTP.Api.Infrastructure;
- using GenHTTP.Api.Protocol;
- using GenHTTP.Engine;
- using GenHTTP.Modules.Practices;
- using InABox.API;
- using InABox.Core;
- using NPOI.SS.Formula.Functions;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- using System.Threading.Tasks;
- using System.Timers;
- using Timer = System.Timers.Timer;
- namespace PRSServices;
- public abstract class Handler<TProperties> : IHandler
- {
- public IHandler Parent { get; set; }
- public abstract void Init(TProperties properties);
- public abstract ValueTask<IResponse?> HandleAsync(IRequest request);
- public ValueTask PrepareAsync()
- {
- return new ValueTask();
- }
- public IEnumerable<ContentElement> GetContent(IRequest request)
- {
- return Enumerable.Empty<ContentElement>();
- }
- }
- public class HandlerBuilder<THandler, TProperties> : IHandlerBuilder<HandlerBuilder<THandler, TProperties>>
- where THandler : Handler<TProperties>, new()
- {
- private readonly List<IConcernBuilder> _Concerns = new();
- public HandlerBuilder(TProperties properties)
- {
- Properties = properties;
- }
- private TProperties Properties;
- public HandlerBuilder<THandler, TProperties> Add(IConcernBuilder concern)
- {
- _Concerns.Add(concern);
- return this;
- }
- public IHandler Build(IHandler parent)
- {
- return Concerns.Chain(parent, _Concerns, p => {
- var handler = new THandler { Parent = p };
- handler.Init(Properties);
- return handler;
- });
- }
- }
- public class Listener<THandler, TProperties>
- where THandler : Handler<TProperties>, new()
- where TProperties : notnull
- {
- private CertificateManager CertificateManager;
- private ushort Port;
- private IServerHost host;
- private TProperties Properties;
- public Listener(TProperties properties)
- {
- CertificateManager = new CertificateManager();
- CertificateManager.UpdateCertificate += CertificateManager_UpdateCertificate;
- CertificateManager.CertificateExpiring += CertificateManager_CertificateExpiring;
- CertificateManager.CertificateExpired += CertificateManager_CertificateExpired;
- Init(properties);
- }
- private void CertificateManager_CertificateExpired()
- {
- Logger.Send(LogType.Information, "", "Expiry of certificate reached; restarting HTTPS listener...");
- Restart();
- }
- private void CertificateManager_CertificateExpiring(DateTime expiry)
- {
- string message;
- if (expiry.Date == DateTime.Now.Date)
- {
- message = $"HTTPS Certificate will expire today at {expiry.TimeOfDay:hh\\:mm}";
- }
- else
- {
- message = $"HTTPS Certificate will expire in {(expiry - DateTime.Now).Days} at {expiry:dd/MM/yyyy hh:mm}";
- }
- Logger.Send(LogType.Information, "", message);
- }
- private void CertificateManager_UpdateCertificate(X509Certificate2 certificate)
- {
- Logger.Send(LogType.Information, "", "HTTPS Certificate with greater expiry date found; restarting HTTPS listener...");
- Restart();
- }
- [MemberNotNull("host", "Properties")]
- public void Init(TProperties properties)
- {
- Properties = properties;
- host = Host.Create()
- .Console()
- .Handler(new HandlerBuilder<THandler, TProperties>(properties))
- .Defaults();
- }
- private void InitCertificate(ushort port, X509Certificate2 certificate)
- {
- host.Bind(IPAddress.Any, port, certificate);
- }
- public void InitPort(ushort port)
- {
- Port = port;
- host.Bind(IPAddress.Any, port);
- }
- public void InitHTTPS(ushort port, string certificateFile)
- {
- Port = port;
- CertificateManager.SetFile(certificateFile);
- var status = CertificateManager.GetCertificate(out var certificate);
- switch (status)
- {
- case CertificateStatus.Valid:
- var names = certificate!.GetNameInfo(X509NameType.DnsName, false);
- Logger.Send(LogType.Information, "", $"Certificate valid for {names}");
- InitCertificate(port, certificate);
- break;
- case CertificateStatus.Expired:
- Logger.Send(LogType.Error, "", "HTTPS Certificate has expired, using HTTP instead");
- InitPort(port);
- break;
- case CertificateStatus.NotFound:
- InitPort(port);
- break;
- case CertificateStatus.Error:
- Logger.Send(LogType.Error, "", "Error validating HTTPS Certificate, using HTTP instead");
- InitPort(port);
- break;
- }
- }
- public void Start()
- {
- host.Start();
- }
- public void Stop()
- {
- host.Stop();
- CertificateManager.Stop();
- }
- private void Restart()
- {
- Clear();
- Init(Properties);
- if(CertificateManager.CertificateFile != null)
- {
- InitHTTPS(Port, CertificateManager.CertificateFile);
- }
- else
- {
- InitPort(Port);
- }
- Start();
- }
- /// <summary>
- /// Clears certificate and host information, and stops the listener.
- /// </summary>
- private void Clear()
- {
- host?.Stop();
- CertificateManager.Clear();
- }
- }
|