();
if (dnsResponse.EDNS is not null)
{
foreach (EDnsOption option in dnsResponse.EDNS.Options)
{
if (option.Code == EDnsOptionCode.EXTENDED_DNS_ERROR)
{
EDnsExtendedDnsErrorOptionData ede = option.Data as EDnsExtendedDnsErrorOptionData;
options.Add(ede);
}
}
}
options.AddRange(dnsResponse.DnsClientExtendedErrors);
foreach (EDnsExtendedDnsErrorOptionData option in options)
{
if (blockingInfoHtmlContent is null)
blockingInfoHtmlContent = " Detailed Info
" + option.InfoCode.ToString() + (option.ExtraText is null ? "" : ": " + option.ExtraText);
else
blockingInfoHtmlContent += "
" + option.InfoCode.ToString() + (option.ExtraText is null ? "" : ": " + option.ExtraText);
}
if (blockingInfoHtmlContent is not null)
blockingInfoHtmlContent += "
";
}
}
catch (Exception ex)
{
_dnsServer.WriteLog(ex);
}
if (blockingInfoHtmlContent is null)
blockPageContent = blockPageContent.Replace("{BLOCKING-INFO}", "");
else
blockPageContent = blockPageContent.Replace("{BLOCKING-INFO}", blockingInfoHtmlContent);
}
byte[] finalBlockPageContent = Encoding.UTF8.GetBytes(blockPageContent);
HttpResponse response = context.Response;
response.StatusCode = StatusCodes.Status200OK;
response.ContentType = "text/html; charset=utf-8";
response.ContentLength = finalBlockPageContent.Length;
using (Stream s = context.Response.Body)
{
await s.WriteAsync(finalBlockPageContent);
}
}
#endregion
#region public
public async Task InitializeAsync(JsonElement jsonWebServerConfig)
{
bool enableWebServer = jsonWebServerConfig.GetPropertyValue("enableWebServer", true);
if (!enableWebServer)
{
await StopWebServerAsync();
return;
}
_webServerLocalAddresses = WebUtilities.GetValidKestralLocalAddresses(jsonWebServerConfig.ReadArray("webServerLocalAddresses", IPAddress.Parse));
if (jsonWebServerConfig.TryGetProperty("webServerUseSelfSignedTlsCertificate", out JsonElement jsonWebServerUseSelfSignedTlsCertificate))
_webServerUseSelfSignedTlsCertificate = jsonWebServerUseSelfSignedTlsCertificate.GetBoolean();
else
_webServerUseSelfSignedTlsCertificate = true;
_webServerTlsCertificateFilePath = jsonWebServerConfig.GetProperty("webServerTlsCertificateFilePath").GetString();
_webServerTlsCertificatePassword = jsonWebServerConfig.GetProperty("webServerTlsCertificatePassword").GetString();
_webServerRootPath = jsonWebServerConfig.GetProperty("webServerRootPath").GetString();
if (!Path.IsPathRooted(_webServerRootPath))
_webServerRootPath = Path.Combine(_dnsServer.ApplicationFolder, _webServerRootPath);
_serveBlockPageFromWebServerRoot = jsonWebServerConfig.GetProperty("serveBlockPageFromWebServerRoot").GetBoolean();
string blockPageTitle = jsonWebServerConfig.GetProperty("blockPageTitle").GetString();
string blockPageHeading = jsonWebServerConfig.GetProperty("blockPageHeading").GetString();
string blockPageMessage = jsonWebServerConfig.GetProperty("blockPageMessage").GetString();
_includeBlockingInfo = jsonWebServerConfig.GetPropertyValue("includeBlockingInfo", true);
_blockPageContent = @"
" + (blockPageTitle is null ? "" : blockPageTitle) + @"
" + (blockPageHeading is null ? "" : " " + blockPageHeading + "
") + @"
" + (blockPageMessage is null ? "" : " " + blockPageMessage + "
") + @"
" + (_includeBlockingInfo ? "{BLOCKING-INFO}" : "") + @"
";
try
{
await StopWebServerAsync();
string selfSignedCertificateFilePath = Path.Combine(_dnsServer.ApplicationFolder, "self-signed-cert.pfx");
if (_webServerUseSelfSignedTlsCertificate)
{
string oldSelfSignedCertificateFilePath = Path.Combine(_dnsServer.ApplicationFolder, "cert.pfx");
if (!oldSelfSignedCertificateFilePath.Equals(_webServerTlsCertificateFilePath, Environment.OSVersion.Platform == PlatformID.Win32NT ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) && File.Exists(oldSelfSignedCertificateFilePath) && !File.Exists(selfSignedCertificateFilePath))
File.Move(oldSelfSignedCertificateFilePath, selfSignedCertificateFilePath);
if (!File.Exists(selfSignedCertificateFilePath))
{
RSA rsa = RSA.Create(2048);
CertificateRequest req = new CertificateRequest("cn=" + _dnsServer.ServerDomain, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
X509Certificate2 cert = req.CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddYears(5));
await File.WriteAllBytesAsync(selfSignedCertificateFilePath, cert.Export(X509ContentType.Pkcs12, null as string));
}
}
else
{
File.Delete(selfSignedCertificateFilePath);
}
if (string.IsNullOrEmpty(_webServerTlsCertificateFilePath))
{
await StopTlsCertificateUpdateTimerAsync();
if (_webServerUseSelfSignedTlsCertificate)
{
LoadWebServiceTlsCertificate(selfSignedCertificateFilePath, null);
}
else
{
//disable HTTPS
_webServerTlsCertificateCollection = null;
}
}
else
{
LoadWebServiceTlsCertificate(_webServerTlsCertificateFilePath, _webServerTlsCertificatePassword);
StartTlsCertificateUpdateTimer();
}
await StartWebServerAsync();
}
catch (Exception ex)
{
_dnsServer.WriteLog(ex);
}
}
#endregion
#region properties
public string Name
{ get { return _name; } }
#endregion
}
}
}