Domain Expiration Task Added
This commit is contained in:
parent
133de4b747
commit
bb0b92e565
20 changed files with 834 additions and 87 deletions
|
@ -4723,12 +4723,12 @@ namespace WebsitePanel.EnterpriseServer
|
|||
|
||||
#region MX|NX Services
|
||||
|
||||
public static IDataReader GetAllPackagesIds()
|
||||
public static IDataReader GetAllPackages()
|
||||
{
|
||||
return SqlHelper.ExecuteReader(
|
||||
ConnectionString,
|
||||
CommandType.StoredProcedure,
|
||||
"GetAllPackageIds"
|
||||
"GetAllPackages"
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -4776,7 +4776,28 @@ namespace WebsitePanel.EnterpriseServer
|
|||
"DeleteDomainDnsRecord",
|
||||
new SqlParameter("@Id", id)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateDomainCreationDate(int domainId, DateTime date)
|
||||
{
|
||||
UpdateDomainDate(domainId, "UpdateDomainCreationDate", date);
|
||||
}
|
||||
|
||||
public static void UpdateDomainExpirationDate(int domainId, DateTime date)
|
||||
{
|
||||
UpdateDomainDate(domainId, "UpdateDomainExpirationDate", date);
|
||||
}
|
||||
|
||||
private static void UpdateDomainDate(int domainId, string stroredProcedure, DateTime date)
|
||||
{
|
||||
SqlHelper.ExecuteReader(
|
||||
ConnectionString,
|
||||
CommandType.StoredProcedure,
|
||||
stroredProcedure,
|
||||
new SqlParameter("@DomainId", domainId),
|
||||
new SqlParameter("@Date", date)
|
||||
);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
|
@ -0,0 +1,137 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using WebsitePanel.Providers.DomainLookup;
|
||||
using Whois.NET;
|
||||
|
||||
namespace WebsitePanel.EnterpriseServer
|
||||
{
|
||||
public class DomainExpirationTask: SchedulerTask
|
||||
{
|
||||
private static readonly string TaskId = "SCHEDULE_TASK_DOMAIN_EXPIRATION";
|
||||
|
||||
// Input parameters:
|
||||
private static readonly string DaysBeforeNotify = "DAYS_BEFORE";
|
||||
private static readonly string MailToParameter = "MAIL_TO";
|
||||
private static readonly string EnableNotification = "ENABLE_NOTIFICATION";
|
||||
|
||||
|
||||
private static readonly string MailBodyTemplateParameter = "MAIL_BODY";
|
||||
private static readonly string MailBodyDomainRecordTemplateParameter = "MAIL_DOMAIN_RECORD";
|
||||
|
||||
public override void DoWork()
|
||||
{
|
||||
BackgroundTask topTask = TaskManager.TopTask;
|
||||
var domainUsers = new Dictionary<int, UserInfo>();
|
||||
|
||||
// get input parameters
|
||||
int daysBeforeNotify;
|
||||
bool sendEmailNotifcation = Convert.ToBoolean( topTask.GetParamValue(EnableNotification));
|
||||
|
||||
// check input parameters
|
||||
if (String.IsNullOrEmpty((string)topTask.GetParamValue("MAIL_TO")))
|
||||
{
|
||||
TaskManager.WriteWarning("The e-mail message has not been sent because 'Mail To' is empty.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
int.TryParse((string)topTask.GetParamValue(DaysBeforeNotify), out daysBeforeNotify);
|
||||
|
||||
var user = UserController.GetUser(topTask.EffectiveUserId);
|
||||
|
||||
var packages = GetUserPackages(user.UserId, user.Role);
|
||||
|
||||
var expiredDomains = new List<DomainInfo>();
|
||||
|
||||
foreach (var package in packages)
|
||||
{
|
||||
var domains = ServerController.GetDomains(package.PackageId);
|
||||
|
||||
domains = domains.Where(x => !x.IsSubDomain && !x.IsDomainPointer).ToList(); //Selecting top-level domains
|
||||
|
||||
domains = domains.Where(x => x.CreationDate == null || x.ExpirationDate == null ? true : CheckDomainExpiration(x.ExpirationDate, daysBeforeNotify)).ToList(); // selecting expired or with empty expire date domains
|
||||
|
||||
var domainUser = UserController.GetUser(package.UserId);
|
||||
|
||||
if (!domainUsers.ContainsKey(package.PackageId))
|
||||
{
|
||||
domainUsers.Add(package.PackageId, domainUser);
|
||||
}
|
||||
|
||||
foreach (var domain in domains)
|
||||
{
|
||||
ServerController.UpdateDomainRegistrationData(domain);
|
||||
|
||||
if (CheckDomainExpiration(domain.ExpirationDate, daysBeforeNotify))
|
||||
{
|
||||
expiredDomains.Add(domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expiredDomains = expiredDomains.GroupBy(p => p.DomainId).Select(g => g.First()).ToList();
|
||||
|
||||
if (expiredDomains.Count > 0 && sendEmailNotifcation)
|
||||
{
|
||||
SendMailMessage(user, expiredDomains, domainUsers);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<PackageInfo> GetUserPackages(int userId,UserRole userRole)
|
||||
{
|
||||
var packages = new List<PackageInfo>();
|
||||
|
||||
switch (userRole)
|
||||
{
|
||||
case UserRole.Administrator:
|
||||
{
|
||||
packages = ObjectUtils.CreateListFromDataReader<PackageInfo>(DataProvider.GetAllPackages());
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
packages = PackageController.GetMyPackages(userId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return packages;
|
||||
}
|
||||
|
||||
private bool CheckDomainExpiration(DateTime? date, int daysBeforeNotify)
|
||||
{
|
||||
if (date == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return (date.Value - DateTime.Now).Days < daysBeforeNotify;
|
||||
}
|
||||
|
||||
private void SendMailMessage(UserInfo user, IEnumerable<DomainInfo> domains, Dictionary<int, UserInfo> domainUsers)
|
||||
{
|
||||
BackgroundTask topTask = TaskManager.TopTask;
|
||||
|
||||
var bodyTemplate = ObjectUtils.FillObjectFromDataReader<ScheduleTaskEmailTemplate>(DataProvider.GetScheduleTaskEmailTemplate(TaskId, MailBodyTemplateParameter));
|
||||
|
||||
// input parameters
|
||||
string mailFrom = "wsp-scheduler@noreply.net";
|
||||
string mailTo = (string)topTask.GetParamValue("MAIL_TO");
|
||||
string mailSubject = "Domain expiration notification";
|
||||
|
||||
Hashtable items = new Hashtable();
|
||||
|
||||
items["user"] = user;
|
||||
items["Domains"] = domains.Select(x => new { DomainName = x.DomainName, ExpirationDate = x.ExpirationDate, Customer = string.Format("{0} {1}", domainUsers[x.PackageId].FirstName, domainUsers[x.PackageId].LastName) });
|
||||
|
||||
var mailBody = PackageController.EvaluateTemplate(bodyTemplate.Value, items);
|
||||
|
||||
// send mail message
|
||||
MailHelper.SendMessage(mailFrom, mailTo, mailSubject, mailBody, true);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -36,7 +36,7 @@ namespace WebsitePanel.EnterpriseServer
|
|||
|
||||
var dnsServers = dnsServersString.Split(';');
|
||||
|
||||
var packages = ObjectUtils.CreateListFromDataReader<PackageInfo>(DataProvider.GetAllPackagesIds());
|
||||
var packages = ObjectUtils.CreateListFromDataReader<PackageInfo>(DataProvider.GetAllPackages());
|
||||
|
||||
foreach (var package in packages)
|
||||
{
|
||||
|
|
|
@ -39,6 +39,8 @@ using WebsitePanel.Server;
|
|||
using WebsitePanel.Providers.ResultObjects;
|
||||
using WebsitePanel.Providers.Web;
|
||||
using WebsitePanel.Providers.HostedSolution;
|
||||
using Whois.NET;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace WebsitePanel.EnterpriseServer
|
||||
{
|
||||
|
@ -49,6 +51,24 @@ namespace WebsitePanel.EnterpriseServer
|
|||
{
|
||||
private const string LOG_SOURCE_SERVERS = "SERVERS";
|
||||
|
||||
private static List<string> _createdDatePatterns = new List<string> { @"Creation Date:(.+)", // base
|
||||
@"created:(.+)",
|
||||
@"Created On:(.+)",
|
||||
@"Domain Registration Date:(.+)",
|
||||
@"Domain Create Date:(.+)",
|
||||
@"Registered on:(.+)"};
|
||||
|
||||
private static List<string> _expiredDatePatterns = new List<string> { @"Expiration Date:(.+)", // base
|
||||
@"Registry Expiry Date:(.+)", //.org
|
||||
@"paid-till:(.+)", //.ru
|
||||
@"Expires On:(.+)", //.name
|
||||
@"Domain Expiration Date:(.+)", //.us
|
||||
@"renewal date:(.+)", //.pl
|
||||
@"Expiry date:(.+)", //.uk
|
||||
@"anniversary:(.+)", //.fr
|
||||
@"expires:(.+)" //.fi
|
||||
};
|
||||
|
||||
#region Servers
|
||||
public static List<ServerInfo> GetAllServers()
|
||||
{
|
||||
|
@ -1787,6 +1807,8 @@ namespace WebsitePanel.EnterpriseServer
|
|||
ServerController.AddServiceDNSRecords(packageId, ResourceGroups.VPS, domain, "");
|
||||
ServerController.AddServiceDNSRecords(packageId, ResourceGroups.VPSForPC, domain, "");
|
||||
}
|
||||
|
||||
UpdateDomainRegistrationData(domain);
|
||||
}
|
||||
|
||||
// add instant alias
|
||||
|
@ -2637,6 +2659,47 @@ namespace WebsitePanel.EnterpriseServer
|
|||
TaskManager.CompleteTask();
|
||||
}
|
||||
}
|
||||
|
||||
public static DomainInfo UpdateDomainRegistrationData(DomainInfo domain)
|
||||
{
|
||||
var whoisResult = WhoisClient.Query(domain.DomainName);
|
||||
|
||||
var createdDate = GetDomainInfoDate(whoisResult.Raw, _createdDatePatterns);
|
||||
var expiredDate = GetDomainInfoDate(whoisResult.Raw, _expiredDatePatterns);
|
||||
|
||||
if (createdDate != null)
|
||||
{
|
||||
domain.CreationDate = createdDate;
|
||||
DataProvider.UpdateDomainCreationDate(domain.DomainId, createdDate.Value);
|
||||
}
|
||||
|
||||
if (expiredDate != null)
|
||||
{
|
||||
domain.ExpirationDate = expiredDate;
|
||||
DataProvider.UpdateDomainExpirationDate(domain.DomainId, expiredDate.Value);
|
||||
}
|
||||
|
||||
return domain;
|
||||
}
|
||||
|
||||
private static DateTime? GetDomainInfoDate(string raw, IEnumerable<string> patterns)
|
||||
{
|
||||
foreach (var createdRegex in patterns)
|
||||
{
|
||||
var regex = new Regex(createdRegex, RegexOptions.IgnoreCase);
|
||||
|
||||
foreach (Match match in regex.Matches(raw))
|
||||
{
|
||||
if (match.Success && match.Groups.Count == 2)
|
||||
{
|
||||
return DateTime.Parse(match.Groups[1].ToString().Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region DNS Zones
|
||||
|
|
|
@ -35,6 +35,9 @@
|
|||
<Reference Include="Ionic.Zip.Reduced">
|
||||
<HintPath>..\..\Lib\Ionic.Zip.Reduced.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="IPAddressRange">
|
||||
<HintPath>..\..\Lib\References\Whois.NET\IPAddressRange.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.Web.Services3">
|
||||
<HintPath>..\..\Lib\Microsoft.Web.Services3.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
|
@ -62,6 +65,9 @@
|
|||
<Reference Include="WebsitePanel.Server.Client">
|
||||
<HintPath>..\..\Bin\WebsitePanel.Server.Client.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="WhoisClient">
|
||||
<HintPath>..\..\Lib\References\Whois.NET\WhoisClient.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Comments\CommentsController.cs" />
|
||||
|
@ -140,6 +146,7 @@
|
|||
<Compile Include="SchedulerTasks\CalculatePackagesDiskspaceTask.cs" />
|
||||
<Compile Include="SchedulerTasks\CancelOverdueInvoicesTask.cs" />
|
||||
<Compile Include="SchedulerTasks\CheckWebSiteTask.cs" />
|
||||
<Compile Include="SchedulerTasks\DomainExpirationTask.cs" />
|
||||
<Compile Include="SchedulerTasks\FTPFilesTask.cs" />
|
||||
<Compile Include="SchedulerTasks\GenerateInvoicesTask.cs" />
|
||||
<Compile Include="SchedulerTasks\HostedSolutionReport.cs" />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue