Implemented ssl options for websites

This commit is contained in:
Marcel Baumgartner 2023-04-06 02:20:05 +02:00
parent 46f544b5f8
commit 907d9402aa
5 changed files with 93 additions and 24 deletions

View file

@ -16,12 +16,14 @@ public class WebsiteService
private readonly WebsiteRepository WebsiteRepository;
private readonly PleskServerRepository PleskServerRepository;
private readonly PleskApiHelper PleskApiHelper;
private readonly UserRepository UserRepository;
public WebsiteService(WebsiteRepository websiteRepository, PleskApiHelper pleskApiHelper, PleskServerRepository pleskServerRepository)
public WebsiteService(WebsiteRepository websiteRepository, PleskApiHelper pleskApiHelper, PleskServerRepository pleskServerRepository, UserRepository userRepository)
{
WebsiteRepository = websiteRepository;
PleskApiHelper = pleskApiHelper;
PleskServerRepository = pleskServerRepository;
UserRepository = userRepository;
}
public async Task<Website> Create(string baseDomain, User owner, PleskServer? ps = null)
@ -149,8 +151,6 @@ public class WebsiteService
{
var website = EnsureData(w);
var certs = new List<string>();
Logger.Debug("1");
var data = await ExecuteCli(website.PleskServer, "certificate", p =>
{
@ -158,9 +158,7 @@ public class WebsiteService
p.Add("-domain");
p.Add(w.BaseDomain);
});
Logger.Debug("2");
string[] lines = data.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in lines)
@ -169,11 +167,6 @@ public class WebsiteService
{
string[] parts = line.Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var part in parts)
{
Logger.Debug(part);
}
if(parts.Length > 6)
certs.Add($"{parts[4]} {parts[5]} {parts[6]}");
}
@ -187,9 +180,66 @@ public class WebsiteService
return certs.ToArray();
}
public async Task CreateSslCertificate()
public async Task CreateSslCertificate(Website w)
{
var website = EnsureData(w);
await ExecuteCli(website.PleskServer, "extension", p =>
{
p.Add("--exec");
p.Add("letsencrypt");
p.Add("cli.php");
p.Add("-d");
p.Add(website.BaseDomain);
p.Add("-m");
p.Add(website.Owner.Email);
});
}
public async Task DeleteSslCertificate(Website w, string name)
{
var website = EnsureData(w);
try
{
await ExecuteCli(website.PleskServer, "site", p =>
{
p.Add("-u");
p.Add(website.BaseDomain);
p.Add("-ssl");
p.Add("false");
});
try
{
await ExecuteCli(website.PleskServer, "certificate", p =>
{
p.Add("--remove");
p.Add(name);
p.Add("-domain");
p.Add(website.BaseDomain);
});
}
catch (Exception e)
{
Logger.Warn("Error removing ssl certificate");
Logger.Warn(e);
throw new DisplayException("An unknown error occured while removing ssl certificate");
}
}
catch (DisplayException)
{
// Redirect all display exception to soft error handler
throw;
}
catch (Exception e)
{
Logger.Warn("Error disabling ssl certificate");
Logger.Warn(e);
throw new DisplayException("An unknown error occured while disabling ssl certificate");
}
}
public async Task<FileAccess> CreateFileAccess(Website w)
@ -225,10 +275,11 @@ public class WebsiteService
private Website EnsureData(Website website)
{
if (website.PleskServer == null)
if (website.PleskServer == null || website.Owner == null)
return WebsiteRepository
.Get()
.Include(x => x.PleskServer)
.Include(x => x.Owner)
.First(x => x.Id == website.Id);
return website;

View file

@ -15,11 +15,20 @@
<div class="col-xl-8 mb-5 mb-xl-10">
<div class="card card-flush h-xl-100">
<div class="card-body pt-2">
<LazyLoader Load="Load">
<LazyLoader @ref="LazyLoader" Load="Load">
<div class="row mt-5">
<div class="card border">
<div class="card-header">
<span class="card-title"><TL>SSL certificates</TL></span>
<span class="card-title">
<TL>SSL certificates</TL>
</span>
<div class="card-toolbar">
<WButton Text="@(SmartTranslateService.Translate("Issue certificate"))"
WorkingText="@(SmartTranslateService.Translate("Working"))"
CssClasses="btn-success"
OnClick="CreateCertificate">
</WButton>
</div>
</div>
<div class="card-body">
@if (Certs.Any())
@ -47,11 +56,6 @@
<span class="text-dark fw-bold fs-6">@(cert)</span>
</td>
<td class="text-end">
<WButton Text="@(SmartTranslateService.Translate("Use"))"
WorkingText="@(SmartTranslateService.Translate("Working"))"
CssClasses="btn btn-light"
OnClick="() => UseCertificate(cert)">
</WButton>
<WButton Text="@(SmartTranslateService.Translate("Delete"))"
WorkingText="@(SmartTranslateService.Translate("Working"))"
CssClasses="btn btn-danger"
@ -66,7 +70,7 @@
else
{
<div class="alert alert-warning">
<TL>No SSL certificate found</TL>
<TL>No SSL certificates found</TL>
</div>
}
</div>
@ -85,17 +89,23 @@
private string[] Certs;
private LazyLoader LazyLoader;
private async Task Load(LazyLoader lazyLoader)
{
await lazyLoader.SetText("Loading certificates");
Certs = await WebsiteService.GetSslCertificates(CurrentWebsite);
}
private async Task UseCertificate(string name)
private async Task CreateCertificate()
{
await WebsiteService.CreateSslCertificate(CurrentWebsite);
await LazyLoader.Reload();
}
private async Task DeleteCertificate(string name)
{
await WebsiteService.DeleteSslCertificate(CurrentWebsite, name);
await LazyLoader.Reload();
}
}

View file

@ -1,6 +1,6 @@
@using Moonlight.App.Database.Entities
<div class="card card-body me-6">
<div class="card card-body">
<div class="row">
<div class="col-8">
<div class="d-flex align-items-center">

View file

@ -4,9 +4,11 @@
@using Moonlight.App.Services
@using Moonlight.Shared.Components.WebsiteControl
@using Microsoft.EntityFrameworkCore
@using Moonlight.App.Services.Interop
@inject WebsiteRepository WebsiteRepository
@inject WebsiteService WebsiteService
@inject ToastService ToastService
<LazyLoader Load="Load">
@if (CurrentWebsite == null)

View file

@ -500,3 +500,9 @@ Ftp Password;Ftp Password
Use;Use
SSL Certificates;SSL Certificates
SSL certificates;SSL certificates
Issue certificate;Issue certificate
New plesk server;New plesk server
Api url;Api url
Host system offline;Host system offline
The host system the website is running on is currently offline;The host system the website is running on is currently offline
No SSL certificates found;No SSL certificates found