public static void Run(CoreDB coreDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; // Удаляем из базы старые IP адреса if (!memoryCache.TryGetValue("Cron-WhitePtrIP_ClearIP", out byte _)) { memoryCache.Set("Cron-WhitePtrIP_ClearIP", (byte)1, TimeSpan.FromHours(1)); SqlToMode.SetMode(SqlMode.Read); foreach (var whiteIP in coreDB.WhitePtrIPs.AsNoTracking()) { if (DateTime.Now > whiteIP.Expires) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.WhitePtrIPs), whiteIP.Id)); } } SqlToMode.SetMode(SqlMode.ReadOrWrite); // Раз в час GC.Collect(GC.MaxGeneration); } IsRun = false; }
public static void UpdateCacheToUserAgent() { string oldUserAgentRegex = UserAgentRegex; // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Подключаемся к базе using (CoreDB coreDB = Service.Get <CoreDB>()) { List <string> mass = new List <string>(); foreach (var blockedIP in coreDB.BlockedsIP.AsNoTracking()) { if (blockedIP.BlockingTime > DateTime.Now && blockedIP.typeBlockIP == TypeBlockIP.UserAgent) { mass.Add(blockedIP.IP); Trigger.OnAddUserAgent((blockedIP.IP, blockedIP.Description, blockedIP.BlockingTime)); } } // Клеим данные в строчку UserAgentRegex = mass.Count == 0 ? "^$" : $"({string.Join("|", mass)})"; } // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); Trigger.OnUpdateCacheToUserAgent((oldUserAgentRegex, UserAgentRegex)); }
public static void Cron(object ob) { try { using (CoreDB coreDB = Service.Get <CoreDB>()) { JsonDB jsonDB = Service.Get <JsonDB>(); var memoryCache = Service.Get <IMemoryCache>(); Engine.Cron.Project.Run(coreDB, jsonDB, memoryCache); // Получение новостей и списка изменений Engine.Cron.UpdateAV.Run(coreDB, jsonDB, memoryCache); // Обновление антивируса Engine.Cron.BlockedIP.Run(coreDB, memoryCache); // Удаляем из базы старые IP адреса Engine.Cron.WhitePtrIP.Run(coreDB, memoryCache); // Удаляем из базы старые IP адреса Engine.RequestsFilter.Access.AccessIP.Clear(); // Очистка списка IP с разрешенным доступом Engine.Cron.Home.Run(coreDB, memoryCache); // Очистка журнала посещений Engine.Cron.Auth.Run(coreDB, memoryCache); // Очистка сессий Engine.Cron.AntiDdos.Run(coreDB, jsonDB, memoryCache); // Сбор статистики && очистка базы и правил IPTables Engine.Cron.AntiDdos.Run(coreDB, jsonDB, memoryCache); // Сбор статистики && очистка базы и правил IPTables Engine.Cron.Monitoring.Run(coreDB, memoryCache); // Статистика "/isp/monitoring" Engine.Base.SqlAndCache.WriteLogTo.CloseFiles(); // Закрытие потоков на файлах которые не используются Engine.Base.SqlAndCache.WriteLogTo.ZipFiles(); // Сжатие логов Engine.Cron.Notifications.Run(coreDB, jsonDB, memoryCache); // Уведомления Engine.Cron.AntiVirus.Run(coreDB); // Проверка доменов антивирусом Engine.Cron.SyncBackup.Run(coreDB, memoryCache); // SyncBackup } } catch (Exception ex) { File.AppendAllText(Folders.File.SystemErrorLog, ex.ToString() + "\n\n=======================================================================\n\n"); } }
public static bool CheckUserAgent(string userAgent) { if (userAgentsRegex != null) { return(Regex.IsMatch(userAgent, userAgentsRegex, RegexOptions.IgnoreCase)); } // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Подключаемся к базе using (CoreDB coreDB = Service.Get <CoreDB>()) { List <string> mass = new List <string>(); foreach (var blockedIP in coreDB.BlockedsIP.AsNoTracking()) { if (blockedIP.BlockingTime > DateTime.Now && blockedIP.typeBlockIP == TypeBlockIP.UserAgent) { mass.Add(blockedIP.IP); } } // Клеим данные в строчку userAgentsRegex = mass.Count == 0 ? "^$" : $"({string.Join("|", mass)})"; } // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Результат return(Regex.IsMatch(userAgent, userAgentsRegex, RegexOptions.IgnoreCase)); }
public static void Run(CoreDB coreDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; // Очистка сессий if (!memoryCache.TryGetValue("Cron-Auth_Session", out _)) { memoryCache.Set("Cron-Auth_Session", (byte)1, TimeSpan.FromHours(3)); SqlToMode.SetMode(SqlMode.Read); foreach (var session in coreDB.Auth_Sessions.AsNoTracking()) { // Удаляем старые записи if (DateTime.Now > session.Expires) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.Auth_Sessions), session.Id)); } } SqlToMode.SetMode(SqlMode.ReadOrWrite); } IsRun = false; }
public Time(CoreDB db, IProduct productRepository, ICampaign campaignRepository, IOrder orderRepository) { _campaignRepository = campaignRepository; _orderRepository = orderRepository; _productRepository = productRepository; _db = db; }
public static void Run(CoreDB coreDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; // Очистка журнала посещений if (!memoryCache.TryGetValue("Cron-Home_Jurnals", out _)) { memoryCache.Set("Cron-Home_Jurnals", (byte)1, TimeSpan.FromHours(12)); SqlToMode.SetMode(SqlMode.Read); var expires = DateTime.Now.AddDays(-90); // Пропускаем последние 60 записей foreach (var jurn in coreDB.Home_Jurnals.AsNoTracking().AsEnumerable().Reverse().Skip(60)) { // Удаляем старые записи if (expires > jurn.Time) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.Home_Jurnals), jurn.Id)); } } SqlToMode.SetMode(SqlMode.ReadOrWrite); // Раз в 12 часов GC.Collect(GC.MaxGeneration); } IsRun = false; }
public Table loadTable(string tableName) { Table table = new Table(IO.LoadXMLTable(tableName, CoreDB.getPath()), tableName); Tables.Add(table); return(table); }
internal void addTableNameToList(string tableName) { IncreaseHead(); xTable.Root.Add(new XElement("Table", tableName)); Names.Add(tableName); new TableIO().Save("table_list", CoreDB.getPath(), xTable); }
public static void Run(CoreDB coreDB, JsonDB jsonDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; // Очистка сессий if (!memoryCache.TryGetValue("Cron-Auth_Session", out _)) { memoryCache.Set("Cron-Auth_Session", (byte)1, TimeSpan.FromMinutes(30)); SqlToMode.SetMode(SqlMode.Read); foreach (var session in coreDB.Auth_Sessions.AsNoTracking()) { // Удаляем старые записи // Если включена авторизация 2FA и сессии больше 20 минут if (DateTime.Now > session.Expires || (jsonDB.Base.EnableTo2FA && !session.Confirm2FA && DateTime.Now.AddMinutes(-20) > session.CreateTime)) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.Auth_Sessions), session.Id)); } } SqlToMode.SetMode(SqlMode.ReadOrWrite); } IsRun = false; }
/// <summary> /// /// </summary> /// <param name="navPage">Навигация</param> /// <param name="ajax">ajax запрос</param> /// <param name="jsonDB">База Json</param> /// <param name="coreDB">База SQL</param> /// <param name="memoryCache">Кеш</param> public PageToView(NavPage <T> navPage, bool ajax, JsonDB jsonDB, CoreDB coreDB, IMemoryCache memoryCache) { this.Page = navPage; this.ajax = ajax; this.jsonDB = jsonDB; this.coreDB = coreDB; this.memoryCache = memoryCache; }
public override IHttpActionResult Patch(Guid id, [FromBody] JObject model) { using (CoreDB db = new CoreDB(UserK)) { db.SaveChanges(); } return(Ok()); }
internal void removeReferencePrefix(string tableName) { try { xTable.Root.Elements().First(element => element.Value == tableName).Attribute("FOREIGN_KEY").Remove(); new TableIO().Save("table_list", CoreDB.getPath(), xTable); } catch { } }
public Table(XDocument xdoc, string tableName) { Columns = new List <Column>(); this._xDocument = xdoc; path = CoreDB.getPath(); this.tableName = tableName; entity = new Entity(_xDocument.Root.Element("Entity")); InitializeColumns(); InitializeLines(); InitializeColumnLine(); }
/// <summary> /// Удалить запись /// </summary> /// <typeparam name="T">Тип данных</typeparam> /// <param name="db">Таблица</param> /// <param name="coreDB">База</param> /// <param name="Id">Id записи</param> public static bool RemoveAttach <T>(this DbSet <T> db, CoreDB coreDB, int Id) where T : class, IId, new() { try { // Получаем данные var item = new T() { Id = Id }; db.Attach(item); // Удаляем данные db.Remove(item); // Сохраняем базу coreDB.SaveChanges(); // Отдаем результат return(true); } catch { return(false); } }
public static void Run(CoreDB coreDB, JsonDB jsonDB, IMemoryCache memoryCache) { // Очистка базы от старых уведомлений if (memoryCache.TryGetValue("CronNoteClearDB", out DateTime CronDate)) { // Если дата отличается от текущей if (CronDate.Day != DateTime.Now.Day) { // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Создаем кеш memoryCache.Set("CronNoteClearDB", DateTime.Now); // Удаляем записи старше 180 дней foreach (var item in coreDB.Notations.AsNoTracking()) { if (DateTime.Now > item.Time.AddDays(180)) { // Удаляем заметку coreDB.Notations.RemoveAttach(coreDB, item.Id); } } // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Раз в день GC.Collect(GC.MaxGeneration); } } else { // Создаем кеш задним числом memoryCache.Set("CronNoteClearDB", DateTime.Now.AddDays(-1)); } }
public Product(CoreDB db) { _db = db; }
internal void addReferencePrefix(string tableName) { xTable.Root.Elements().First(element => element.Value == tableName).Add(new XAttribute("FOREIGN_KEY", "")); new TableIO().Save("table_list", CoreDB.getPath(), xTable); }
public Order(CoreDB db, IProduct productRepository) { _db = db; _productRepository = productRepository; }
public static void Run(CoreDB coreDB, JsonDB jsonDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; if (!memoryCache.TryGetValue("Cron-ProjectInfo", out _)) { memoryCache.Set("Cron-ProjectInfo", (byte)0, Startup.AbsoluteExpirationToAPI); #region Получаем новости проекта try { HttpClient client = new HttpClient(); var newsAPI = JsonConvert.DeserializeObject <List <ProjectNews> >(client.GetStringAsync("http://api.core-system.org/isp/news").Result); if (newsAPI.Count > 0) { jsonDB.ProjectNews = newsAPI; jsonDB.Save(); } } catch { } #endregion #region Получаем список изменений проекта try { HttpClient client = new HttpClient(); var ProjectChange = JsonConvert.DeserializeObject <List <ProjectChange> >(client.GetStringAsync("http://api.core-system.org/isp/change").Result); if (ProjectChange.Count > 0) { jsonDB.ProjectChange = ProjectChange; jsonDB.Save(); } } catch { } #endregion #region Сравниваем версию ISPCore try { HttpClient client = new HttpClient(); var result = JsonConvert.DeserializeObject <LatestVersion>(client.GetStringAsync("http://api.core-system.org/isp/LatestVersion").Result); // Сверяем версии ISPCore if (result.Version > 0 && (result.Version > Startup.version.Version || (result.Version == Startup.version.Version && result.Patch > Startup.version.Patch))) { #region Автоматическое обновление ISPCore if (jsonDB.Base.AutoUpdate && (Platform.Get == PlatformOS.Docker || Platform.Get == PlatformOS.Unix)) { // Если метод auto-update.sh еще не вызывался для этой версии ISPCore if (!File.Exists($"{Folders.AutoUpdate}/{Startup.version.ToString()}.ok")) { // Проверяем можно ли текущею версию обновлять if (client.GetStringAsync($"http://api.core-system.org/isp/UpdateSupport?Version={Startup.version.Version}&Patch={Startup.version.Patch}&os={PlatformOS.Unix.ToString()}").Result == "ok") { // Записываем версию ISPCore с которой был вызван auto-update.sh File.WriteAllText($"{Folders.AutoUpdate}/{Startup.version.ToString()}.ok", string.Empty); // Обновляем Bash bash = new Bash(); string os = Platform.Get == PlatformOS.Docker ? "docker" : "linux"; bash.Run($"curl -fsSL http://cdn.core-system.org/isp/{os}/auto-update.sh | sh"); return; } } } #endregion // Уведомление var note = new Notation() { Category = "Система", Msg = jsonDB.Base.AutoUpdate ? "Доступна новая версия ISPCore, требуется ручное обновление" : "Доступна новая версия ISPCore", Time = DateTime.Now, More = new List <More>() { new More("Текущая версия", Startup.version.ToString()), new More("Доступна версия", result.ToString()) } }; // HashData note.HashData = Notation.CreateHashData(note); // Если в базе нету HashData if (coreDB.Notations.AsNoTracking().FirstOrDefault(it => it.HashData == note.HashData) == null) { // Добовляем в базу coreDB.Notations.Add(note); // Сохраняем базу coreDB.SaveChanges(); // Обновляем CountNotification jsonDB.Base.CountNotification++; jsonDB.Save(); } } } catch { } #endregion } IsRun = false; }
public static void ISPCore() { using (CoreDB coreDB = Service.Get <CoreDB>()) { // Версия базы if (coreDB.Version.AsNoTracking().LastOrDefault() is LatestVersion vSql) { // Версия базы совпадает if (vSql.ToString() == Startup.vSql.ToString()) { return; } #region Миграция SQL switch (vSql.Version) { #region Первая версия базы case 1.9: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_Aliases] ADD [Folder] TEXT NULL ;"); vSql.Patch = 1; goto case 0.0; } #endregion #region case 0.0 case 0.0: { switch (vSql.Patch) { case 1: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_ConfToLog] ADD [Jurn200] BIGINT DEFAULT 0 NOT NULL ;"); goto case 2; } #region case 2 case 2: { #region Журнал 200 coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [RequestsFilter_Jurnals200] ( [Id] INTEGER NOT NULL , [City] text NULL , [Country] text NULL , [FormData] text NULL , [Host] text NULL , [IP] text NULL , [Method] text NULL , [Referer] text NULL , [Region] text NULL , [Time] text NOT NULL , [Uri] text NULL , [UserAgent] text NULL , [typeJurn] bigint NOT NULL , CONSTRAINT [sqlite_master_PK_RequestsFilter_Jurnals200] PRIMARY KEY ([Id]) );"); #endregion // Удаляем старые базы coreDB.Database.ExecuteSqlCommand("DROP TABLE [RequestsFilter_NumberOfRequestDay];"); coreDB.Database.ExecuteSqlCommand("DROP TABLE [RequestsFilter_NumberOfRequestMonth];"); #region Статистика запросов - сутки coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [RequestsFilter_NumberOfRequestDay] ( [Id] INTEGER NOT NULL , [Count200] bigint NOT NULL , [Count2FA] bigint NOT NULL , [Count303] bigint NOT NULL , [Count401] bigint NOT NULL , [Count403] bigint NOT NULL , [Count500] bigint NOT NULL , [Host] text NULL , [Time] text NOT NULL , CONSTRAINT [sqlite_master_PK_RequestsFilter_NumberOfRequestDay] PRIMARY KEY ([Id]) );"); #endregion #region Статистика запросов - месяц coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [RequestsFilter_NumberOfRequestMonth] ( [Id] INTEGER NOT NULL , [Count200] bigint NOT NULL , [Count2FA] bigint NOT NULL , [Count303] bigint NOT NULL , [Count401] bigint NOT NULL , [Count403] bigint NOT NULL , [Count500] bigint NOT NULL , [Time] text NOT NULL , [allRequests] bigint NOT NULL , CONSTRAINT [sqlite_master_PK_RequestsFilter_NumberOfRequestMonth] PRIMARY KEY ([Id]) );"); #endregion // Успех goto case 3; } #endregion #region case 3 case 3: { coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [WhitePtrIPs] ( [Id] INTEGER NOT NULL , [Expires] text NOT NULL , [IPv4Or6] text NULL , CONSTRAINT [sqlite_master_PK_WhitePtrIPs] PRIMARY KEY ([Id]) );"); goto case 4; } #endregion case 4: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_LimitRequest] ADD [UseGlobalConf] BIGINT DEFAULT 0 NOT NULL ;"); goto case 5; } case 5: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domains] ADD [Auth2faToAccess] BIGINT DEFAULT 0 NOT NULL ;"); goto case 6; } #region case 6 case 6: { #region Новая таблица AntiBot coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [RequestsFilter_Domain_AntiBot] ( [Id] INTEGER NOT NULL , [AddCodeToHtml] text NULL , [DomainId] bigint NOT NULL , [UseGlobalConf] bigint NOT NULL , [FirstSkipToBot] bigint NOT NULL , [HourCacheToBot] bigint NOT NULL , [HourCacheToUser] bigint NOT NULL , [RewriteToOriginalDomain] bigint NOT NULL , [WaitUser] bigint NOT NULL , [type] bigint NOT NULL , CONSTRAINT [sqlite_master_PK_RequestsFilter_Domain_AntiBot] PRIMARY KEY ([Id]) , FOREIGN KEY ([DomainId]) REFERENCES [RequestsFilter_Domains] ([Id]) ON DELETE CASCADE ON UPDATE NO ACTION ); CREATE UNIQUE INDEX [IX_RequestsFilter_Domain_AntiBot_DomainId] ON [RequestsFilter_Domain_AntiBot] ([DomainId] ASC); "); #endregion #region Удаляем поле AntiBot в таблице RequestsFilter_Domain coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [table_temp_05032018] ( [Id] INTEGER NOT NULL , [Protect] bigint NOT NULL , [StopBruteForce] bigint NOT NULL , [host] text NULL , [typeBlockIP] bigint NOT NULL , [Auth2faToAccess] bigint DEFAULT 0 NOT NULL , CONSTRAINT [sqlite_master_PK_RequestsFilter_Domains] PRIMARY KEY ([Id]) ); INSERT INTO [table_temp_05032018] SELECT [id], [Protect], [StopBruteForce], [host], [typeBlockIP], [Auth2faToAccess] FROM [RequestsFilter_Domains]; ALTER TABLE [RequestsFilter_Domains] RENAME TO [migrate_05032018]; ALTER TABLE [table_temp_05032018] RENAME TO [RequestsFilter_Domains]; "); #endregion #region Делаем привязку к таблице AntiBot int ColumnId = 1; foreach (var domain in coreDB.RequestsFilter_Domains.AsNoTracking()) { coreDB.Database.ExecuteSqlCommand($@"INSERT INTO [RequestsFilter_Domain_AntiBot] ([Id] ,[AddCodeToHtml] ,[DomainId] ,[UseGlobalConf] ,[FirstSkipToBot] ,[HourCacheToBot] ,[HourCacheToUser] ,[RewriteToOriginalDomain] ,[WaitUser] ,[type]) VALUES ({ColumnId},null,{domain.Id},0,1,216,12,1,2800,0); "); ColumnId++; } #endregion goto case 7; } #endregion #region case 7 case 7: { coreDB.Database.ExecuteSqlCommand("UPDATE SQLITE_SEQUENCE SET SEQ=200 WHERE NAME='RequestsFilter_Domains';"); coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [RequestsFilter_Domain_RuleReplaces] ( [Id] INTEGER NOT NULL , [ContentType] text NULL , [DomainId] bigint NOT NULL , [GetArgs] text NULL , [IsActive] bigint NOT NULL , [PostArgs] text NULL , [RegexWhite] text NULL , [ResponceUri] text NULL , [TypeResponse] bigint NOT NULL , [kode] text NULL , [uri] text NULL , CONSTRAINT [sqlite_master_PK_RequestsFilter_Domain_RuleReplaces] PRIMARY KEY ([Id]) , FOREIGN KEY ([DomainId]) REFERENCES [RequestsFilter_Domains] ([Id]) ON DELETE CASCADE ON UPDATE NO ACTION ); CREATE INDEX [IX_RequestsFilter_Domain_RuleReplaces_DomainId] ON [RequestsFilter_Domain_RuleReplaces] ([DomainId] ASC); "); goto case 8; } #endregion #region case 8 case 8: { coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [RequestsFilter_Template_RuleReplaces] ( [Id] INTEGER NOT NULL , [ContentType] text NULL , [GetArgs] text NULL , [IsActive] bigint NOT NULL , [PostArgs] text NULL , [RegexWhite] text NULL , [ResponceUri] text NULL , [TemplateId] bigint NOT NULL , [TypeResponse] bigint NOT NULL , [kode] text NULL , [uri] text NULL , CONSTRAINT [sqlite_master_PK_RequestsFilter_Template_RuleReplaces] PRIMARY KEY ([Id]) , FOREIGN KEY ([TemplateId]) REFERENCES [RequestsFilter_Templates] ([Id]) ON DELETE CASCADE ON UPDATE NO ACTION ); CREATE INDEX [IX_RequestsFilter_Template_RuleReplaces_TemplateId] ON [RequestsFilter_Template_RuleReplaces] ([TemplateId] ASC); "); coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [RequestsFilter_Template_RuleOverrides] ( [Id] INTEGER NOT NULL , [IsActive] bigint NOT NULL , [Method] bigint NOT NULL , [TemplateId] bigint NOT NULL , [order] bigint NOT NULL , [rule] text NULL , CONSTRAINT [sqlite_master_PK_RequestsFilter_Template_RuleOverrides] PRIMARY KEY ([Id]) , FOREIGN KEY ([TemplateId]) REFERENCES [RequestsFilter_Templates] ([Id]) ON DELETE CASCADE ON UPDATE NO ACTION ); CREATE INDEX [IX_RequestsFilter_Template_RuleOverrides_TemplateId] ON [RequestsFilter_Template_RuleOverrides] ([TemplateId] ASC); "); goto case 9; } #endregion case 9: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_AntiBot] ADD [BackgroundCheck] BIGINT DEFAULT 0 NOT NULL ;"); coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_AntiBot] ADD [CountBackgroundRequest] BIGINT DEFAULT 2 NOT NULL ;"); coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_AntiBot] ADD [BackgroundCheckToAddExtensions] TEXT NULL ;"); break; } } // Миграция на 0.1.* vSql.Patch = 0; goto case 0.1; } #endregion case 0.1: { switch (vSql.Patch) { case 0: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_AntiBot] ADD [BackgroundHourCacheToIP] BIGINT DEFAULT 0 NOT NULL ;"); goto case 1; } case 1: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_LimitRequest] ADD [BlockType] BIGINT DEFAULT 0 NOT NULL ;"); coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_LimitRequest] ADD [MaxRequestToAgainСheckingreCAPTCHA] BIGINT DEFAULT 300 NOT NULL ;"); coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domains] ADD [Auth2faToPasswd] TEXT NULL ;"); goto case 2; } case 2: { coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [Auth_Sessions] ( [Id] INTEGER NOT NULL , [Confirm2FA] bigint NOT NULL , [Expires] text NOT NULL , [HashPasswdToRoot] text NULL , [IP] text NULL , [Session] text NULL , CONSTRAINT [sqlite_master_PK_Auth_Sessions] PRIMARY KEY ([Id]) ); "); goto case 3; } case 3: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [Auth_Sessions] ADD [CreateTime] TEXT DEFAULT [2018-04-24 10:57:30.9735464] NOT NULL ;"); goto case 4; } #region case 4 case 4: { coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [SyncBackup_db_Tasks] ( [Id] INTEGER NOT NULL , [Description] text NULL , [JobStatus] bigint NOT NULL , [LastSync] text NOT NULL , [SuncTime] bigint NOT NULL , [TypeDb] bigint NOT NULL , CONSTRAINT [sqlite_master_PK_SyncBackup_db_Tasks] PRIMARY KEY ([Id]) ); "); coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [SyncBackup_db_Task_Conf] ( [Id] INTEGER NOT NULL , [AddBackupTime] bigint NOT NULL , [Compression] bigint NOT NULL , [DumpDatabases] text NULL , [IgnoreDatabases] text NULL , [TaskId] bigint NOT NULL , [Whence] text NULL , CONSTRAINT [sqlite_master_PK_SyncBackup_db_Task_Conf] PRIMARY KEY ([Id]) , FOREIGN KEY ([TaskId]) REFERENCES [SyncBackup_db_Tasks] ([Id]) ON DELETE CASCADE ON UPDATE NO ACTION ); CREATE UNIQUE INDEX [IX_SyncBackup_db_Task_Conf_TaskId] ON [SyncBackup_db_Task_Conf] ([TaskId] ASC); "); coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [SyncBackup_db_Task_MySQL] ( [Id] INTEGER NOT NULL , [Host] text NULL , [Password] text NULL , [Port] bigint NOT NULL , [TaskId] bigint NOT NULL , [User] text NULL , CONSTRAINT [sqlite_master_PK_SyncBackup_db_Task_MySQL] PRIMARY KEY ([Id]) , FOREIGN KEY ([TaskId]) REFERENCES [SyncBackup_db_Tasks] ([Id]) ON DELETE CASCADE ON UPDATE NO ACTION ); CREATE UNIQUE INDEX [IX_SyncBackup_db_Task_MySQL_TaskId] ON [SyncBackup_db_Task_MySQL] ([TaskId] ASC); "); coreDB.Database.ExecuteSqlCommand(@"CREATE TABLE [SyncBackup_db_Reports] ( [Id] INTEGER NOT NULL , [Category] text NULL , [ErrorMsg] text NULL , [Msg] text NULL , [Status] text NULL , [TaskId] bigint NOT NULL , [Time] text NOT NULL , CONSTRAINT [sqlite_master_PK_SyncBackup_db_Reports] PRIMARY KEY ([Id]) ); "); goto case 5; } #endregion case 5: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE SyncBackup_db_Task_MySQL RENAME TO SyncBackup_db_Task_ConnectionConf;"); coreDB.Database.ExecuteSqlCommand("ALTER TABLE SyncBackup_db_Task_Conf RENAME TO SyncBackup_db_Task_DumpConf;"); goto case 6; } case 6: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [WhitePtrIPs] ADD [PTR] TEXT NULL ;"); goto case 7; } case 7: { coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_NumberOfRequestDay] ADD [CountIPtables] BIGINT DEFAULT 0 NOT NULL ;"); coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_NumberOfRequestMonth] ADD [CountIPtables] BIGINT DEFAULT 0 NOT NULL ;"); coreDB.Database.ExecuteSqlCommand("ALTER TABLE [RequestsFilter_Domain_AntiBot] ADD [HashKey] TEXT NULL ;"); goto case 8; } case 8: { // Миграция на 9 //goto case 9; break; } } break; } } #endregion // Сохраняем версию базы coreDB.Version.Add(Startup.vSql); coreDB.SaveChanges(); } } }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime applicationLifetime, IMemoryCache memoryCache) { #region Системные настройки loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } #endregion #region Добовляем порт в белый список if (Platform.Get == PlatformOS.Unix) { if (string.IsNullOrWhiteSpace(new Bash().Run("iptables -L INPUT -v -n 2>/dev/null | grep 8793"))) { new Bash().Run("iptables -I INPUT -p tcp --dport 8793 -j ACCEPT"); } } #endregion #region Unix Socket try { applicationLifetime.ApplicationStarted.Register(() => { if (Platform.Get == PlatformOS.Unix || Platform.Get == PlatformOS.Docker) { ThreadPool.QueueUserWorkItem((s) => { while (true) { // Ждем 3 секунды Thread.Sleep(1000 * 3); // Меняем права доступа if (File.Exists("/var/run/ispcore.sock")) { new Bash().Run("chmod 666 /var/run/ispcore.sock"); return; } } }); } }); } catch { } #endregion // Создаем сервис Service.Create(memoryCache); // Регистрируем триггеры RegisteredTriggers.Initialize(); #region Загружаем список BlockedIP в кеш using (CoreDB coreDB = Service.Get <CoreDB>()) { // IP который нужно удалить string unlockip = string.Empty; if (File.Exists($"{Folders.Tmp}/unlockip.root")) { unlockip = File.ReadAllText($"{Folders.Tmp}/unlockip.root").Trim(); } // Загружаем IP адреса foreach (var item in coreDB.BlockedsIP.AsNoTracking()) { if (item.BlockingTime > DateTime.Now && item.typeBlockIP != TypeBlockIP.UserAgent) { // Белый IP if (item.IP.Contains(unlockip)) { continue; } // Модель var data = new ModelIPtables(item.Description, item.BlockingTime); IPtables.AddIPv4Or6(item.IP, data, item.typeBlockIP, item.BlockedHost); } } } #endregion #region Загружаем список WhitePtrIP в кеш using (CoreDB coreDB = Service.Get <CoreDB>()) { // Загружаем IP адреса foreach (var item in coreDB.WhitePtrIPs.AsNoTracking()) { // Добовляем IP в кеш if (item.Expires > DateTime.Now) { memoryCache.Set(KeyToMemoryCache.WhitePtrIP(item.IPv4Or6), (byte)0, item.Expires); } } } #endregion #region Загружаем список "Разрешенные доступы" в кеш foreach (var item in AccessIP.List()) { // IP для кеша string ipCache = item.IP.Replace(".*", "").Replace(":*", ""); switch (item.accessType) { case AccessType.all: memoryCache.Set(KeyToMemoryCache.CheckLinkWhitelistToAll(item.host, ipCache), (byte)1, item.Expires); break; case AccessType.Is2FA: memoryCache.Set(KeyToMemoryCache.CheckLinkWhitelistTo2FA(item.host, ipCache), (byte)1, item.Expires); break; case AccessType.allDomain: memoryCache.Set(KeyToMemoryCache.CheckLinkWhitelistToAllDomain(ipCache), (byte)1, item.Expires); break; } } #endregion #region Кеш WhiteList / IPtables WhiteUserList.UpdateCache(); Engine.Security.IPtables.UpdateCacheToUserAgent(); #endregion #region Статичиские файлы var provider = new FileExtensionContentTypeProvider(); provider.Mappings[".jgz"] = "application/javascript"; app.UseStaticFiles(new StaticFileOptions { ContentTypeProvider = provider }); #endregion #region IP-адрес клиента app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto, KnownProxies = { IPAddress.Parse("172.17.42.1"), IPAddress.Parse("127.0.0.1") }, }); #endregion // Страницы ошибок app.UseMvc(routes => { routes.MapRoute("ErrorPage-404", "404", new { controller = "Error", action = "_404" }); }); // Блокировка IP app.UseIPtablesMiddleware(); #region Core API // Доступ запрещен с порта панели 8793 app.UseCoreMiddleware(); // Core CheckRequest app.Map("/core/check/request", ap => ap.Run(context => { if (Startup.cmd.Timeout.core == 0) { return(Engine.core.Check.Request.Check(context)); } // Состояние потока bool RanToCompletion = false; // Завершаем подключение через ** секунд var token = new CancellationTokenSource(1000 * Startup.cmd.Timeout.core).Token; token.Register(() => { if (!RanToCompletion) { context.Abort(); } }); // Проверка запроса var task = Engine.core.Check.Request.Check(context); RanToCompletion = task.Status == TaskStatus.RanToCompletion; // Успех return(task); })); // Core API app.UseMvc(routes => { routes.MapRoute(null, "core/unlock/2fa", new { controller = "CoreUnlock2FA", action = "Index" }); routes.MapRoute(null, "core/check/cookie", new { controller = "CoreCheckCookie", action = "Index" }); routes.MapRoute(null, "core/check/recaptcha", new { controller = "CoreCheckRecaptcha", action = "Base" }); routes.MapRoute(null, "core/check/recaptcha/limitrequest", new { controller = "CoreCheckRecaptcha", action = "LimitRequest" }); }); // AntiBotHub app.UseSignalR(routes => routes.MapHub <AntiBotHub>("/core/AntiBotHub")); // Генерация скриптов app.UseMvc(routes => { routes.MapRoute(null, "core/gen/antibot.js", new { controller = "CoreGenToAntiBot", action = "Index" }); }); // Заглушка для "Core API" app.Map("/core", ap => ap.Run(context => context.Response.WriteAsync("404 Not Found"))); #endregion #region Открытый API { // Проверка авторизации app.UseAuthApiMiddleware(); #region API - List app.UseMvc(routes => { routes.MapRoute(null, "api/list/home/jurnal", new { controller = "ApiListHome", action = "Jurnal" }); routes.MapRoute(null, "api/list/notifications", new { controller = "ApiListNotifications", action = "Jurnal" }); routes.MapRoute(null, "api/list/jsondb", new { controller = "ApiListJsonDB", action = "Get" }); routes.MapRoute(null, "api/list/whitelist/system", new { controller = "ApiListWhitelistTo", action = "Systems" }); routes.MapRoute(null, "api/list/security/anti-ddos/stats/day", new { controller = "ApiListAntiDdos", action = "StatsDay" }); routes.MapRoute(null, "api/list/security/anti-ddos/stats/month", new { controller = "ApiListAntiDdos", action = "StatsMonth" }); routes.MapRoute(null, "api/list/security/anti-ddos/jurnal", new { controller = "ApiListAntiDdos", action = "Jurnal" }); routes.MapRoute(null, "api/list/security/iptables/blockedip", new { controller = "ApiListIptables", action = "BlockedsIP" }); routes.MapRoute(null, "api/list/security/av/report", new { controller = "ApiListAntiVirus", action = "Report" }); routes.MapRoute(null, "api/list/requests-filter/domain", new { controller = "ApiListDomain", action = "Domain" }); routes.MapRoute(null, "api/list/requests-filter/domains", new { controller = "ApiListDomain", action = "Domains" }); routes.MapRoute(null, "api/list/requests-filter/template", new { controller = "ApiListTemplate", action = "Template" }); routes.MapRoute(null, "api/list/requests-filter/templates", new { controller = "ApiListTemplate", action = "Templates" }); routes.MapRoute(null, "api/list/requests-filter/access", new { controller = "ApiListAccess", action = "Get" }); routes.MapRoute(null, "api/list/requests-filter/monitoring/stats/day", new { controller = "ApiListMonitoring", action = "StatsDay" }); routes.MapRoute(null, "api/list/requests-filter/monitoring/stats/month", new { controller = "ApiListMonitoring", action = "StatsMonth" }); routes.MapRoute(null, "api/list/requests-filter/monitoring/jurnal", new { controller = "ApiListMonitoring", action = "Jurnal" }); routes.MapRoute(null, "api/list/backup/io/task", new { controller = "ApiListBackupFiles", action = "Task" }); routes.MapRoute(null, "api/list/backup/io/tasks", new { controller = "ApiListBackupFiles", action = "Tasks" }); routes.MapRoute(null, "api/list/backup/io/operations", new { controller = "ApiListBackupFiles", action = "Operation" }); routes.MapRoute(null, "api/list/backup/db/task", new { controller = "ApiListBackupDatabase", action = "Task" }); routes.MapRoute(null, "api/list/backup/db/tasks", new { controller = "ApiListBackupDatabase", action = "Tasks" }); routes.MapRoute(null, "api/list/backup/db/operations", new { controller = "ApiListBackupDatabase", action = "Operation" }); }); #endregion #region API - Add app.UseMvc(routes => { routes.MapRoute(null, "api/add/whitelist", new { controller = "ApiAddWhiteList", action = "Base" }); routes.MapRoute(null, "api/add/security/iptables", new { controller = "ApiAddIptables", action = "Base" }); routes.MapRoute(null, "api/add/backup/io/task", new { controller = "ApiAddBackupFiles", action = "Task" }); routes.MapRoute(null, "api/add/backup/io/ignore", new { controller = "ApiAddBackupFiles", action = "Ignore" }); routes.MapRoute(null, "api/add/backup/db/task", new { controller = "ApiAddBackupDatabase", action = "Task" }); routes.MapRoute(null, "api/add/requests-filter/template", new { controller = "ApiAddTemplate", action = "Base" }); routes.MapRoute(null, "api/add/requests-filter/template/rule", new { controller = "ApiAddRule", action = "RuleTemplate" }); routes.MapRoute(null, "api/add/requests-filter/domain", new { controller = "ApiAddDomain", action = "Domain" }); routes.MapRoute(null, "api/add/requests-filter/aliases", new { controller = "ApiAddDomain", action = "Aliases" }); routes.MapRoute(null, "api/add/requests-filter/domain/rule", new { controller = "ApiAddRule", action = "RuleDomain" }); routes.MapRoute(null, "api/add/requests-filter/domain/template", new { controller = "ApiAddDomain", action = "TemplatesId" }); routes.MapRoute(null, "api/add/requests-filter/domain/ignore", new { controller = "ApiAddDomain", action = "IgnoreLogs" }); routes.MapRoute(null, "api/add/requests-filter/access", new { controller = "ApiAddAccess", action = "Base" }); }); #endregion #region API - Remove app.UseMvc(routes => { routes.MapRoute(null, "api/remove/whitelist/user", new { controller = "ApiRemoveWhiteList", action = "Users" }); routes.MapRoute(null, "api/remove/whitelist/system", new { controller = "ApiRemoveWhiteList", action = "Systems" }); routes.MapRoute(null, "api/remove/security/iptables", new { controller = "ApiRemoveIptables", action = "BlockedsIP" }); routes.MapRoute(null, "api/remove/security/antivirus", new { controller = "ApiRemoveAntivirus", action = "Base" }); routes.MapRoute(null, "api/remove/backup/io/task", new { controller = "ApiRemoveBackupFiles", action = "Task" }); routes.MapRoute(null, "api/remove/backup/io/ignore", new { controller = "ApiRemoveBackupFiles", action = "Ignore" }); routes.MapRoute(null, "api/remove/backup/db/task", new { controller = "ApiRemoveBackupDatabase", action = "Task" }); routes.MapRoute(null, "api/remove/requests-filter/rules/base", new { controller = "ApiRemoveRules", action = "Rule" }); routes.MapRoute(null, "api/remove/requests-filter/rules/replace", new { controller = "ApiRemoveRules", action = "RuleReplace" }); routes.MapRoute(null, "api/remove/requests-filter/rules/override", new { controller = "ApiRemoveRules", action = "RuleOverride" }); routes.MapRoute(null, "api/remove/requests-filter/rules/arg", new { controller = "ApiRemoveRules", action = "RuleArg" }); routes.MapRoute(null, "api/remove/requests-filter/alias", new { controller = "ApiRemoveDomain", action = "Alias" }); routes.MapRoute(null, "api/remove/requests-filter/domain", new { controller = "ApiRemoveDomain", action = "Base" }); routes.MapRoute(null, "api/remove/requests-filter/domain/template", new { controller = "ApiRemoveDomain", action = "Template" }); routes.MapRoute(null, "api/remove/requests-filter/domain/ignore", new { controller = "ApiRemoveDomain", action = "Ignore" }); routes.MapRoute(null, "api/remove/requests-filter/template", new { controller = "ApiRemoveTemplate", action = "Base" }); routes.MapRoute(null, "api/remove/requests-filter/access", new { controller = "ApiRemoveAccess", action = "Base" }); }); #endregion #region API - Common app.UseMvc(routes => { routes.MapRoute(null, "api/common/av/start", new { controller = "ApiCommonAV", action = "Start" }); routes.MapRoute(null, "api/common/av/stop", new { controller = "ApiCommonAV", action = "Stop" }); routes.MapRoute(null, "api/common/template/export", new { controller = "ApiCommonTemplate", action = "Export" }); routes.MapRoute(null, "api/common/template/import", new { controller = "ApiCommonTemplate", action = "Import" }); routes.MapRoute(null, "api/common/backup/io/clearing/cache", new { controller = "ApiCommonBackupFiles", action = "ClearingCache" }); routes.MapRoute(null, "api/common/backup/io/recovery", new { controller = "ApiCommonBackupFiles", action = "Recovery" }); }); #endregion #region API - Edit app.UseMvc(routes => { routes.MapRoute(null, "api/edit/settings/base", new { controller = "ApiEditSettingsToBase", action = "Base" }); routes.MapRoute(null, "api/edit/settings/cache", new { controller = "ApiEditSettingsToBase", action = "Cache" }); routes.MapRoute(null, "api/edit/settings/api", new { controller = "ApiEditSettingsToBase", action = "API" }); routes.MapRoute(null, "api/edit/settings/security", new { controller = "ApiEditSettingsToBase", action = "Security" }); routes.MapRoute(null, "api/edit/settings/passwd", new { controller = "ApiEditSettingsToBase", action = "Passwd" }); routes.MapRoute(null, "api/edit/settings/brute-force", new { controller = "ApiEditSettingsToBase", action = "BruteForce" }); routes.MapRoute(null, "api/edit/settings/anti-ddos", new { controller = "ApiEditSettingsToBase", action = "AntiDdos" }); routes.MapRoute(null, "api/edit/settings/antivirus", new { controller = "ApiEditSettingsToBase", action = "AntiVirus" }); routes.MapRoute(null, "api/edit/settings/service/email", new { controller = "ApiEditSettingsToService", action = "Email" }); routes.MapRoute(null, "api/edit/settings/service/telegram", new { controller = "ApiEditSettingsToService", action = "Telegram" }); routes.MapRoute(null, "api/edit/settings/service/sms", new { controller = "ApiEditSettingsToService", action = "SMS" }); routes.MapRoute(null, "api/edit/antibot/base", new { controller = "ApiEditAntiBot", action = "Base" }); routes.MapRoute(null, "api/edit/antibot/limit", new { controller = "ApiEditAntiBot", action = "Limit" }); routes.MapRoute(null, "api/edit/domain/base", new { controller = "ApiEditDomain", action = "Base" }); routes.MapRoute(null, "api/edit/domain/log", new { controller = "ApiEditDomain", action = "LogSettings" }); routes.MapRoute(null, "api/edit/domain/av", new { controller = "ApiEditDomain", action = "AntiVirus" }); routes.MapRoute(null, "api/edit/domain/antibot", new { controller = "ApiEditDomain", action = "AntiBot" }); routes.MapRoute(null, "api/edit/domain/limit/request", new { controller = "ApiEditDomain", action = "LimitRequest" }); routes.MapRoute(null, "api/edit/template", new { controller = "ApiEditTemplate", action = "Base" }); routes.MapRoute(null, "api/edit/backup/io/task", new { controller = "ApiEditBackupFiles", action = "Task" }); routes.MapRoute(null, "api/edit/backup/io/ftp", new { controller = "ApiEditBackupFiles", action = "FTP" }); routes.MapRoute(null, "api/edit/backup/io/webdav", new { controller = "ApiEditBackupFiles", action = "WebDav" }); routes.MapRoute(null, "api/edit/backup/io/onedrive", new { controller = "ApiEditBackupFiles", action = "OneDrive" }); routes.MapRoute(null, "api/edit/backup/db/task", new { controller = "ApiEditBackupDatabase", action = "Task" }); routes.MapRoute(null, "api/edit/backup/db/task/dumpconf", new { controller = "ApiEditBackupDatabase", action = "DumpConf" }); routes.MapRoute(null, "api/edit/backup/db/connectionconf", new { controller = "ApiEditBackupDatabase", action = "ConnectionConf" }); }); #endregion } #endregion #region Авторизация // Страница авторизации app.UseMvc(routes => { routes.MapRoute(null, "auth", new { controller = "AuthToBase", action = "Index" }); routes.MapRoute(null, "auth/unlock", new { controller = "AuthToBase", action = "Unlock" }); routes.MapRoute(null, "auth/signout", new { controller = "AuthToBase", action = "SignOut" }); }); // Проверка авторизации app.UseAuthMiddleware(); // Страница 2FA app.UseMvc(routes => { routes.MapRoute(null, "auth/confirm", new { controller = "AuthToConfirm", action = "Index" }); routes.MapRoute(null, "auth/confirm/unlock", new { controller = "AuthToConfirm", action = "Unlock" }); }); #endregion // Главная страница app.UseMvc(routes => { routes.MapRoute(null, "", new { controller = "Home", action = "Index" }); }); #region API // FAQ app.UseMvc(routes => routes.MapRoute(null, "api/faq", new { controller = "ApiFaq", action = "Index" })); // Заглушка для "API" app.Map("/api", ap => ap.Run(context => context.Response.WriteAsync("404 Not Found"))); #endregion #region Настройки app.UseMvc(routes => { routes.MapRoute(null, "settings", new { controller = "SettingsToBase", action = "Index" }); routes.MapRoute(null, "settings/save/base", new { controller = "SettingsToBase", action = "Save" }); routes.MapRoute(null, "settings/whitelist", new { controller = "SettingsToUserWhiteList", action = "Index" }); routes.MapRoute(null, "settings/save/whitelist", new { controller = "SettingsToUserWhiteList", action = "Save" }); routes.MapRoute(null, "settings/remove/whitelist", new { controller = "SettingsToUserWhiteList", action = "Remove" }); routes.MapRoute(null, "settings/whitelist/export", new { controller = "SettingsToUserWhiteList", action = "Export" }); routes.MapRoute(null, "settings/whitelist/import", new { controller = "SettingsToUserWhiteList", action = "Import" }); routes.MapRoute(null, "settings/whitelist/system", new { controller = "SettingsToSystemWhiteList", action = "Index" }); routes.MapRoute(null, "settings/remove/whitelist/system", new { controller = "SettingsToSystemWhiteList", action = "Remove" }); routes.MapRoute(null, "settings/service", new { controller = "SettingsToService", action = "Index" }); routes.MapRoute(null, "settings/save/service", new { controller = "SettingsToService", action = "Save" }); }); #endregion #region Безопастность системы // Брандмауэр app.UseMvc(routes => { routes.MapRoute(null, "security/iptables", new { controller = "SecurityToIPtables", action = "Index" }); routes.MapRoute(null, "security/iptables/remove", new { controller = "SecurityToIPtables", action = "Remove" }); routes.MapRoute(null, "security/iptables/add", new { controller = "SecurityToIPtables", action = "Add" }); }); // Антивирус app.UseMvc(routes => { routes.MapRoute(null, "security/antivirus", new { controller = "SecurityToAntiVirus", action = "Index" }); routes.MapRoute(null, "security/antivirus/save", new { controller = "SecurityToAntiVirus", action = "Save" }); routes.MapRoute(null, "security/antivirus/remove", new { controller = "SecurityToAntiVirus", action = "Remove" }); routes.MapRoute(null, "security/antivirus/start", new { controller = "SecurityToAntiVirus", action = "Start" }); routes.MapRoute(null, "security/antivirus/stop", new { controller = "SecurityToAntiVirus", action = "Stop" }); }); // AntiDdos if (Platform.Get == PlatformOS.Unix) { app.UseMvc(routes => { routes.MapRoute(null, "security/anti-ddos", new { controller = "SecurityToAntiDdos", action = "Index" }); routes.MapRoute(null, "security/anti-ddos/save", new { controller = "SecurityToAntiDdos", action = "Save" }); }); } // Anti-Bot app.UseMvc(routes => { routes.MapRoute(null, "security/antibot", new { controller = "SecurityToAntiBot", action = "Index" }); routes.MapRoute(null, "security/antibot/save", new { controller = "SecurityToAntiBot", action = "Save" }); }); #endregion #region Фильтрация запросов // Views app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/domains", new { controller = "RequestsFilterToDomains", action = "Index" }); routes.MapRoute(null, "requests-filter/templates", new { controller = "RequestsFilterToTemplates", action = "Index" }); routes.MapRoute(null, "requests-filter/monitoring", new { controller = "RequestsFilterToMonitoring", action = "Index" }); }); // Common app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/common/remove/rule", new { controller = "RequestsFilterToCommon", action = "RemoveToRule" }); routes.MapRoute(null, "requests-filter/common/remove/rulereplace", new { controller = "RequestsFilterToCommon", action = "RemoveToRuleReplace" }); routes.MapRoute(null, "requests-filter/common/remove/ruleoverride", new { controller = "RequestsFilterToCommon", action = "RemoveToRuleOverride" }); routes.MapRoute(null, "requests-filter/common/remove/rulearg", new { controller = "RequestsFilterToCommon", action = "RemoveToRuleArg" }); routes.MapRoute(null, "requests-filter/common/remove/alias", new { controller = "RequestsFilterToCommon", action = "RemoveToAlias" }); }); // Шаблон app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/template", new { controller = "RequestsFilterToTemplate", action = "Index" }); routes.MapRoute(null, "requests-filter/template/save", new { controller = "RequestsFilterToTemplate", action = "Save" }); routes.MapRoute(null, "requests-filter/template/remove", new { controller = "RequestsFilterToTemplate", action = "Remove" }); routes.MapRoute(null, "requests-filter/template/import", new { controller = "RequestsFilterToTemplate", action = "Import" }); routes.MapRoute(null, "requests-filter/template/export", new { controller = "RequestsFilterToTemplate", action = "Export" }); }); // Разрешенные доступы app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/access", new { controller = "RequestsFilterToAccess", action = "Index" }); routes.MapRoute(null, "requests-filter/access/open", new { controller = "RequestsFilterToAccess", action = "Open" }); routes.MapRoute(null, "requests-filter/access/remove", new { controller = "RequestsFilterToAccess", action = "Remove" }); }); #endregion #region Фильтрация запросов - Домен // Домен - Главная/FAQ app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/domain/base", new { controller = "RequestsFilterToDomainBase", action = "Index" }); routes.MapRoute(null, "requests-filter/domain/faq", new { controller = "RequestsFilterToDomainBase", action = "Faq" }); routes.MapRoute(null, "requests-filter/domain/remove", new { controller = "RequestsFilterToDomainBase", action = "Remove" }); routes.MapRoute(null, "requests-filter/domain/save/base", new { controller = "RequestsFilterToDomainBase", action = "Save" }); routes.MapRoute(null, "requests-filter/domain/import", new { controller = "RequestsFilterToDomainBase", action = "Import" }); routes.MapRoute(null, "requests-filter/domain/export", new { controller = "RequestsFilterToDomainBase", action = "Export" }); }); // Домен - Алиасы app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/domain/aliases", new { controller = "RequestsFilterToDomainAliases", action = "Index" }); routes.MapRoute(null, "requests-filter/domain/save/aliases", new { controller = "RequestsFilterToDomainAliases", action = "Save" }); }); // Домен - Правила app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/domain/rules", new { controller = "RequestsFilterToDomainRules", action = "Index" }); routes.MapRoute(null, "requests-filter/domain/save/rules", new { controller = "RequestsFilterToDomainRules", action = "Save" }); }); // Домен - Настройки журнала app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/domain/logsettings", new { controller = "RequestsFilterToDomainLogSettings", action = "Index" }); routes.MapRoute(null, "requests-filter/domain/save/logsettings", new { controller = "RequestsFilterToDomainLogSettings", action = "Save" }); }); // Домен - Антивирус app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/domain/av", new { controller = "RequestsFilterToDomainAv", action = "Index" }); routes.MapRoute(null, "requests-filter/domain/save/av", new { controller = "RequestsFilterToDomainAv", action = "Save" }); }); // Домен - AntiBot app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/domain/antibot", new { controller = "RequestsFilterToDomainAntiBot", action = "Index" }); routes.MapRoute(null, "requests-filter/domain/save/antibot", new { controller = "RequestsFilterToDomainAntiBot", action = "Save" }); }); // Домен - Лимит запросов app.UseMvc(routes => { routes.MapRoute(null, "requests-filter/domain/limitrequest", new { controller = "RequestsFilterToDomainLimitRequest", action = "Index" }); routes.MapRoute(null, "requests-filter/domain/save/limitrequest", new { controller = "RequestsFilterToDomainLimitRequest", action = "Save" }); }); #endregion #region SyncBackup - IO // Views app.UseMvc(routes => { routes.MapRoute(null, "backup/io/tasks", new { controller = "SyncBackupFilesToTasks", action = "Index" }); routes.MapRoute(null, "backup/io/operation", new { controller = "SyncBackupFilesToOperation", action = "Index" }); }); // Задание app.UseMvc(routes => { routes.MapRoute(null, "backup/io/task", new { controller = "SyncBackupFilesToTask", action = "Index" }); routes.MapRoute(null, "backup/io/task/remove", new { controller = "SyncBackupFilesToTask", action = "Remove" }); routes.MapRoute(null, "backup/io/task/save", new { controller = "SyncBackupFilesToTask", action = "Save" }); routes.MapRoute(null, "backup/io/task/clearing-cache", new { controller = "SyncBackupFilesToTask", action = "ClearingCache" }); }); // Улиты app.UseMvc(routes => { routes.MapRoute(null, "backup/io/tools", new { controller = "SyncBackupFilesToTools", action = "Index" }); routes.MapRoute(null, "backup/io/tools/recover", new { controller = "SyncBackupFilesToTools", action = "Recovery" }); }); // Получение токенов app.UseMvc(routes => { routes.MapRoute(null, "backup/io/authorize/onedrive", new { controller = "SyncBackupFilesToAuthorize", action = "OneDrive" }); }); #endregion #region SyncBackup - DB // Views app.UseMvc(routes => { routes.MapRoute(null, "backup/db/tasks", new { controller = "SyncBackupDatabaseToTasks", action = "Index" }); routes.MapRoute(null, "backup/db/operation", new { controller = "SyncBackupDatabaseToOperation", action = "Index" }); }); // Задание app.UseMvc(routes => { routes.MapRoute(null, "backup/db/task", new { controller = "SyncBackupDatabaseToTask", action = "Index" }); routes.MapRoute(null, "backup/db/task/remove", new { controller = "SyncBackupDatabaseToTask", action = "Remove" }); routes.MapRoute(null, "backup/db/task/save", new { controller = "SyncBackupDatabaseToTask", action = "Save" }); }); #endregion #region Файловый менеджер app.UseMvc(routes => { routes.MapRoute(null, "file-manager", new { controller = "ToolsToFileManager", action = "Index" }); routes.MapRoute(null, "file-manager/target/{targetfile}", new { controller = "ToolsToFileManager", action = "Target" }); routes.MapRoute(null, "file-manager/connector", new { controller = "ToolsToFileManager", action = "Connector" }); routes.MapRoute(null, "file-manager/thumb/{hash}", new { controller = "ToolsToFileManager", action = "Thumbs" }); }); #endregion #region Триггеры app.UseMvc(routes => { routes.MapRoute(null, "triggers", new { controller = "ToolsToTriggers", action = "Index" }); routes.MapRoute(null, "trigger", new { controller = "ToolsToTriggerSettings", action = "Index" }); routes.MapRoute(null, "trigger/faq", new { controller = "ToolsToTriggerSettings", action = "FAQ" }); routes.MapRoute(null, "trigger/save", new { controller = "ToolsToTriggerSettings", action = "Save" }); routes.MapRoute(null, "trigger/remove", new { controller = "ToolsToTriggerSettings", action = "Remove" }); routes.MapRoute(null, "trigger/export", new { controller = "ToolsToTriggerSettings", action = "Export" }); routes.MapRoute(null, "trigger/import", new { controller = "ToolsToTriggerSettings", action = "Import" }); routes.MapRoute(null, "trigger/nodes", new { controller = "ToolsToTriggerNodes", action = "Index" }); routes.MapRoute(null, "trigger/nodes/save", new { controller = "ToolsToTriggerNodes", action = "Save" }); }); #endregion // Уведомления app.UseMvc(routes => { routes.MapRoute(null, "notifications", new { controller = "Notifications", action = "Index" }); }); // Ошибка 404 app.Run(async(context) => { await RewriteTo.Local(context, "404" + (context.Request.QueryString.Value.Contains("ajax=true") ? "?ajax=true" : "")); }); }
public static void Run(CoreDB coreDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; #region Очистка базы 'Операции' и папки 'ReportSync' if (memoryCache.TryGetValue("CronSyncBackupIO:ClearDB", out DateTime CronSyncBackupClearDB)) { // Если дата отличается от текущей if (CronSyncBackupClearDB.Day != DateTime.Now.Day) { // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Обновляем кеш memoryCache.Set("CronSyncBackupIO:ClearDB", DateTime.Now); // Чистим базу foreach (var note in coreDB.SyncBackup_Notations.AsNoTracking()) { // Если записи больше 90 дней if ((DateTime.Now - note.Time).TotalDays > 90) { // Удаляем заметку coreDB.SyncBackup_Notations.RemoveAttach(coreDB, note.Id); } } // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Удаляем старые файлы foreach (var intFile in Directory.GetFiles(Folders.ReportSync, "*.*")) { try { if ((DateTime.Now - File.GetLastWriteTime(intFile)).TotalDays > 90) { File.Delete(intFile); } } catch { } } // Раз в сутки GC.Collect(GC.MaxGeneration); } } else { // Создаем кеш задним числом memoryCache.Set("CronSyncBackupIO:ClearDB", DateTime.Now.AddDays(-1)); } #endregion // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Получаем весь список заданий var Alltasks = coreDB.SyncBackup_Tasks.Include(f => f.FTP).Include(o => o.OneDrive).Include(dav => dav.WebDav).Include(ignr => ignr.IgnoreFileOrFolders).ToList(); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Проходим задания foreach (var task in Alltasks) { // Пропускаем задания которые не требуют выполнения if (task.JobStatus != JobStatus.on || task.LastSync > DateTime.Now.AddMinutes(-task.SuncTime)) { continue; } // Кеш DateTime NewCacheSync = DateTime.Now; bool IsOk = false; #region Добовляем задание в WorkNote CancellationToken cancellationToken = new CancellationToken(); var WorkNoteNotation = new Notation() { TaskId = task.Id, Category = "Бэкап", Msg = $"Задание: {task.Description}", Time = DateTime.Now, More = new List <More>() { new More("Состояние", "Выполняется") } }; CoreDB.SyncBackupWorkNote.Add(WorkNoteNotation, cancellationToken); #endregion // Обновляем CacheSync if (task.CacheSync > task.CacheExpires) { SqlToMode.SetMode(SqlMode.Read); task.CacheSync = default(DateTime); task.CacheExpires = DateTime.Now.AddDays(12); coreDB.SaveChanges(); SqlToMode.SetMode(SqlMode.ReadOrWrite); } // Trigger.OnStartJob((task.Id, task.TypeSunc)); // Создание отчета по ошибкам Report report = new Report(task); // Выполняем задание Sync(task, new RemoteServer(task.TypeSunc, task.FTP, task.WebDav, task.OneDrive, report, out string NewRefreshTokenToOneDrive), WorkNoteNotation, out List <More> ResponseNameAndValue, ref IsOk); // Сохраняем отчет об ошибках (если есть ошибки) report.SaveAndDispose(ref ResponseNameAndValue); // Чистим WorkNote CoreDB.SyncBackupWorkNote.Take(cancellationToken); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Добовляем задание в список завершеных операций coreDB.SyncBackup_Notations.Add(new Notation() { TaskId = task.Id, Category = "Бэкап", Msg = $"Задание: {task.Description}", Time = DateTime.Now, More = ResponseNameAndValue, }); // Завершаем задание if (IsOk) { task.LastSync = DateTime.Now; task.CacheSync = NewCacheSync; } // Меняем токен if (!string.IsNullOrWhiteSpace(NewRefreshTokenToOneDrive)) { task.OneDrive.RefreshToken = NewRefreshTokenToOneDrive; } coreDB.SaveChanges(); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Trigger.OnStopJob((task.Id, task.TypeSunc, IsOk)); } IsRun = false; }
public JsonResult Recovery(Task task, TypeRecovery typeRecovery, IDictionary <string, string> nameAndValue, bool IsAPI = false) { #region Демо режим if (Platform.IsDemo) { return(Json(new Text("Операция недоступна в демо-режиме"))); } #endregion DateTime DateRecovery = default(DateTime); #region Проверка данных if (string.IsNullOrWhiteSpace(task.Description)) { return(Json(new Text("Имя задания не может быть пустым"))); } if (string.IsNullOrWhiteSpace(task.Whence)) { return(Json(new Text("Локальный каталог не может быть пустым"))); } if (string.IsNullOrWhiteSpace(task.Where)) { return(Json(new Text("Удаленный каталог не может быть пустым"))); } switch (task.TypeSunc) { case TypeSunc.SFTP: case TypeSunc.FTP: { if (string.IsNullOrWhiteSpace(task.FTP.HostOrIP) || string.IsNullOrWhiteSpace(task.FTP.Login) || string.IsNullOrWhiteSpace(task.FTP.Passwd)) { return(Json(new Text("Настройки 'FTP/SFTP' имеют недопустимое значение"))); } break; } case TypeSunc.WebDav: { if (string.IsNullOrWhiteSpace(task.WebDav.url) || string.IsNullOrWhiteSpace(task.WebDav.Login) || string.IsNullOrWhiteSpace(task.WebDav.Passwd)) { return(Json(new Text("Настройки 'WebDav' имеют недопустимое значение"))); } break; } case TypeSunc.OneDrive: { if (string.IsNullOrWhiteSpace(task.OneDrive.ApplicationId) || string.IsNullOrWhiteSpace(task.OneDrive.RefreshToken)) { return(Json(new Text("Настройки 'OneDrive' имеют недопустимое значение"))); } break; } } if (task.EncryptionAES && string.IsNullOrWhiteSpace(task.PasswdAES)) { return(Json(new Text("Пароль для шифрования файлов не может быть пустым"))); } if (typeRecovery == TypeRecovery.Date && nameAndValue.TryGetValue("TypeRecoveryToDate", out string DateRecoveryTostring) && !DateTime.TryParse(DateRecoveryTostring, out DateRecovery)) { return(Json(new Text("Отметка бэкапа имеет неправильный формат"))); } #endregion // Искать файлы только в текущем каталоге bool SearchToCurrentDirectory = false; if (nameAndValue.TryGetValue("SearchOption", out string SearchOption) && SearchOption == "CurrentDirectory") { SearchToCurrentDirectory = true; } // Выполняем задание в потоке ThreadPool.QueueUserWorkItem(ob => { #region Добовляем задание в WorkNote CancellationToken cancellationToken = new CancellationToken(); var WorkNoteNotation = new Notation() { TaskId = task.Id, Category = "Восстановление", Msg = $"Задание: {task.Description}", Time = DateTime.Now, More = new List <More>() { new More("Состояние", "Выполняется поиск всех папок") } }; CoreDB.SyncBackupWorkNote.Add(WorkNoteNotation, cancellationToken); #endregion // Создание отчета по ошибкам Report report = new Report(task); // Выполняем задание Tools.Recovery(task, new RemoteServer(task.TypeSunc, task.FTP, task.WebDav, task.OneDrive, report, out _), WorkNoteNotation, out List <More> ResponseNameAndValue, typeRecovery, DateRecovery, SearchToCurrentDirectory); // Сохраняем отчет об ошибках (если есть ошибки) report.SaveAndDispose(ref ResponseNameAndValue); // Чистим WorkNote CoreDB.SyncBackupWorkNote.Take(cancellationToken); #region Сохраняем данные задание в базе SqlToMode.SetMode(SqlMode.Read); using (CoreDB coreDB = Service.Get <CoreDB>()) { // Добовляем задание в список завершеных операций coreDB.SyncBackup_Notations.Add(new Notation() { TaskId = task.Id, Category = "Восстановление", Msg = $"Задание: {task.Description}", Time = DateTime.Now, More = ResponseNameAndValue, }); // Сохраняем базу coreDB.SaveChanges(); } SqlToMode.SetMode(SqlMode.ReadOrWrite); #endregion }); // Отдаем ответ if (IsAPI) { return(Json(new TrueOrFalse(true))); } return(Json(new Text("Задание добавлено на обработку"))); }
public static void Run(CoreDB coreDB, JsonDB jsonDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; if (!memoryCache.TryGetValue("Cron-UpdateAV", out _)) { memoryCache.Set("Cron-UpdateAV", (byte)1, Startup.AbsoluteExpirationToAPI); try { HttpClient client = new HttpClient(); string vers = client.GetStringAsync("http://cdn.core-system.org/isp/av/vers.txt").Result; string old_vers = File.ReadAllText($"{Folders.AV}/vers.txt"); if (Regex.IsMatch(vers, "^[0-9]+-[0-9]+-[0-9]+([\n\r]+)?$") && vers != old_vers) { if (Download("ai-bolit.php", "new_ai-bolit.php") && Download("AIBOLIT-WHITELIST.db", "new_AIBOLIT-WHITELIST.db")) { if (File.Exists($"{Folders.AV}/ai-bolit.php")) { File.Delete($"{Folders.AV}/ai-bolit.php"); } if (File.Exists($"{Folders.AV}/AIBOLIT-WHITELIST.db")) { File.Delete($"{Folders.AV}/AIBOLIT-WHITELIST.db"); } File.Move($"{Folders.AV}/new_ai-bolit.php", $"{Folders.AV}/ai-bolit.php"); File.Move($"{Folders.AV}/new_AIBOLIT-WHITELIST.db", $"{Folders.AV}/AIBOLIT-WHITELIST.db"); File.WriteAllText($"{Folders.AV}/vers.txt", vers); // Добовляем данные в базу SqlToMode.SetMode(SqlMode.Read); coreDB.Notations.Add(new Notation() { Category = "Обновления", Msg = "Обновлен антивирус AI-Bolit", Time = DateTime.Now, More = new List <More>() { new More("Предыдущая версия", old_vers.Replace('-', '.')), new More("Текущая версия", vers.Replace('-', '.')) } }); // Сохраняем базу coreDB.SaveChanges(); SqlToMode.SetMode(SqlMode.ReadOrWrite); // Обновляем CountNotification jsonDB.Base.CountNotification++; jsonDB.Save(); } } } catch { } } IsRun = false; }
public static void Run(CoreDB coreDB) { if (IsRun) { return; } IsRun = true; // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Получаем весь список заданий var Alltasks = coreDB.RequestsFilter_Domains.Where(i => i.av.JobStatus == JobStatus.on && DateTime.Now.AddMinutes(-i.av.CheckEveryToMinute) > i.av.LastRun).Include(i => i.av).Include(i => i.Aliases).ToList(); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Проходим задания foreach (var task in Alltasks) { // Нету PHP if (!File.Exists(task.av.php)) { continue; } #region Локальный метод - "RunAV" void RunAV(string progress_id) { #region Создаем команду StringBuilder comand = new StringBuilder(); comand.Append($"--path={task.av.path} "); if (!string.IsNullOrWhiteSpace(task.av.skip)) { comand.Append($"--skip={task.av.skip} "); } if (!string.IsNullOrWhiteSpace(task.av.scan)) { comand.Append($"--scan={task.av.scan} "); } comand.Append($"--mode={task.av.mode} "); comand.Append($"--memory={task.av.memory}M "); comand.Append($"--size={task.av.size}K "); comand.Append($"--delay={task.av.delay} "); #endregion // Имя отчета string report = $"{Models.Security.AntiVirus.name}_{Models.Security.AntiVirus.vers}_{DateTime.Now.ToString("HH-mm_dd-MM-yyy")}{task.av.path.Replace("/", "_-_")}"; // Запускаем процесс bash Bash bash = new Bash(); bash.Run($"{task.av.php} {Folders.AV}/ai-bolit.php {comand.ToString()} --progress={Folders.AV}/progress_id-{progress_id}.json --report={Folders.ReportsAV}/{report}.html >/dev/null 2>/dev/null"); } #endregion // Проверяем папки алиасов foreach (var alias in task.Aliases) { if (string.IsNullOrWhiteSpace(alias.Folder)) { continue; } RunAV($"{task.Id}.{alias.Id}"); } // Задание самого домена RunAV(task.Id.ToString()); // Обновляем LastRun SqlToMode.SetMode(SqlMode.Read); task.av.LastRun = DateTime.Now; coreDB.SaveChanges(); SqlToMode.SetMode(SqlMode.ReadOrWrite); } IsRun = false; }
public static void Run(CoreDB coreDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; #region Очистка базы - "Отчеты" if (memoryCache.TryGetValue("CronSyncBackupDB:ClearDB", out DateTime CronSyncBackupClearDB)) { // Если дата отличается от текущей if (CronSyncBackupClearDB.Day != DateTime.Now.Day) { // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Обновляем кеш memoryCache.Set("CronSyncBackupDB:ClearDB", DateTime.Now); // Чистим базу foreach (var note in coreDB.SyncBackup_db_Reports.AsNoTracking()) { // Если записи больше 90 дней if ((DateTime.Now - note.Time).TotalDays > 90) { // Удаляем отчет coreDB.SyncBackup_db_Reports.RemoveAttach(coreDB, note.Id); } } // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Раз в сутки GC.Collect(GC.MaxGeneration); } } else { // Создаем кеш задним числом memoryCache.Set("CronSyncBackupDB:ClearDB", DateTime.Now.AddDays(-1)); } #endregion // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Получаем весь список заданий var Alltasks = coreDB.SyncBackup_db_Tasks.Include(i => i.DumpConf).Include(i => i.ConnectionConf).ToList(); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Проходим задания foreach (Task task in Alltasks) { // Пропускаем задания которые не требуют выполнения if (task.JobStatus != JobStatus.on || task.LastSync > DateTime.Now.AddMinutes(-task.SuncTime)) { continue; } // Trigger.OnStartJob((task.Id, task.TypeDb)); // Выполняем задание Dump(task, out bool IsOk, out string ErrorMsg); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Добовляем задание в список завершеных операций coreDB.SyncBackup_db_Reports.Add(new Report() { TaskId = task.Id, Category = $"{task.TypeDb.ToString()}", Msg = $"Задание: {task.Description}", Time = DateTime.Now, Status = IsOk ? "Задание выполнено без ошибок" : "Задание выполнено с ошибками", ErrorMsg = ErrorMsg, }); // Завершаем задание task.LastSync = DateTime.Now; coreDB.SaveChanges(); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Trigger.OnStopJob((task.Id, task.TypeDb, IsOk, ErrorMsg)); } IsRun = false; }
public static void Run(CoreDB coreDB, IMemoryCache memoryCache) { if (IsRun) { return; } IsRun = true; #region IspNumberOfRequestToHour // Если есть кеш за прошлый час var TimeIspNumberOfRequestDay = DateTime.Now.AddHours(-1); if (memoryCache.TryGetValue(KeyToMemoryCache.IspNumberOfRequestToHour(TimeIspNumberOfRequestDay), out IDictionary <string, NumberOfRequestHour> DataNumberOfRequestToHour)) { SqlToMode.SetMode(SqlMode.Read); coreDB.ChangeTracker.AutoDetectChangesEnabled = false; // Записываем данные в базу foreach (var item in DataNumberOfRequestToHour) { coreDB.RequestsFilter_NumberOfRequestDay.Add(new NumberOfRequestDay() { Host = item.Key, Time = TimeIspNumberOfRequestDay, Count200 = item.Value.Count200, Count303 = item.Value.Count303, Count401 = item.Value.Count401, Count403 = item.Value.Count403, Count500 = item.Value.Count500, Count2FA = item.Value.Count2FA, }); } // Сохраняем базу coreDB.SaveChanges(); // Разрешаем записывать данные в SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); coreDB.ChangeTracker.AutoDetectChangesEnabled = true; // Сносим кеш (статистика за час) memoryCache.Remove(KeyToMemoryCache.IspNumberOfRequestToHour(TimeIspNumberOfRequestDay)); // Раз в час GC.Collect(GC.MaxGeneration); } #endregion #region Очистка баз + перенос NumberOfRequestDay в NumberOfRequestMonth if (memoryCache.TryGetValue("CronIspClearDB", out DateTime CronIspClearDB)) { // Если дата отличается от текущей if (CronIspClearDB.Day != DateTime.Now.Day) { SqlToMode.SetMode(SqlMode.Read); // Обновляем кеш memoryCache.Set("CronIspClearDB", DateTime.Now); #region Очищаем NumberOfRequestMonth foreach (var item in coreDB.RequestsFilter_NumberOfRequestMonth.AsNoTracking()) { // Если записи больше 30 дней if ((DateTime.Now - item.Time).TotalDays > 30) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.RequestsFilter_NumberOfRequestMonth), item.Id)); } } #endregion #region Очищаем Jurnals200 foreach (var item in coreDB.RequestsFilter_Jurnals200.AsNoTracking()) { // Если записи больше 30 дней if ((DateTime.Now - item.Time).TotalDays > 30) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.RequestsFilter_Jurnals200), item.Id)); } } #endregion #region Очищаем Jurnals303 foreach (var item in coreDB.RequestsFilter_Jurnals303.AsNoTracking()) { // Если записи больше 30 дней if ((DateTime.Now - item.Time).TotalDays > 30) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.RequestsFilter_Jurnals303), item.Id)); } } #endregion #region Очищаем Jurnals403 foreach (var item in coreDB.RequestsFilter_Jurnals403.AsNoTracking()) { // Если записи больше 30 дней if ((DateTime.Now - item.Time).TotalDays > 30) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.RequestsFilter_Jurnals403), item.Id)); } } #endregion #region Очищаем Jurnals401 foreach (var item in coreDB.RequestsFilter_Jurnals401.AsNoTracking()) { // Если записи больше 30 дней if ((DateTime.Now - item.Time).TotalDays > 30) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.RequestsFilter_Jurnals401), item.Id)); } } #endregion #region Очищаем Jurnals2FA foreach (var item in coreDB.RequestsFilter_Jurnals2FA.AsNoTracking()) { // Если записи больше 30 дней if ((DateTime.Now - item.Time).TotalDays > 30) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.RequestsFilter_Jurnals2FA), item.Id)); } } #endregion #region Очищаем Jurnals500 foreach (var item in coreDB.RequestsFilter_Jurnals500.AsNoTracking()) { // Если записи больше 30 дней if ((DateTime.Now - item.Time).TotalDays > 30) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.RequestsFilter_Jurnals500), item.Id)); } } #endregion #region Переносим NumberOfRequestDay в NumberOfRequestMonth // Хранимм дату и значение Dictionary <int, NumberOfRequestBase> NumberOfRequestMonth = new Dictionary <int, NumberOfRequestBase>(); // Собираем статистику за прошлые дни foreach (var item in coreDB.RequestsFilter_NumberOfRequestDay.AsNoTracking()) { // Пропускаем статистику за сегодня if (item.Time.Day == DateTime.Now.Day && item.Time.Month == DateTime.Now.Month) { continue; } #region Переносим значения в NumberOfRequestMonth if (NumberOfRequestMonth.TryGetValue(item.Time.Day, out NumberOfRequestBase it)) { it.Count200 += item.Count200; it.Count303 += item.Count303; it.Count403 += item.Count403; it.Count401 += item.Count401; it.Count500 += item.Count500; it.Count2FA += item.Count2FA; } else { NumberOfRequestMonth.Add(item.Time.Day, item); } #endregion // Удаляем значения из базы coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.RequestsFilter_NumberOfRequestDay), item.Id)); } // Переносим временные данные с NumberOfRequestMonth в базу foreach (var item in NumberOfRequestMonth) { // Добовляем в базу coreDB.RequestsFilter_NumberOfRequestMonth.Add(new NumberOfRequestMonth() { Time = item.Value.Time, allRequests = item.Value.Count200 + item.Value.Count303 + item.Value.Count403 + item.Value.Count500 + item.Value.Count401 + item.Value.Count2FA, Count200 = item.Value.Count200, Count303 = item.Value.Count303, Count401 = item.Value.Count401, Count403 = item.Value.Count403, Count500 = item.Value.Count500, Count2FA = item.Value.Count2FA, }); } #endregion // Сохраняем базу coreDB.SaveChanges(); SqlToMode.SetMode(SqlMode.ReadOrWrite); // Раз в день GC.Collect(GC.MaxGeneration); } } else { // Создаем кеш задним числом memoryCache.Set("CronIspClearDB", DateTime.Now.AddDays(-1)); } #endregion IsRun = false; }
public Product(IServiceProvider services) { _scope = services.CreateScope(); _db = _scope.ServiceProvider.GetRequiredService <CoreDB>(); }
public static void Run(CoreDB coreDB, JsonDB jsonDB, IMemoryCache memoryCache) { if (IsRun || !jsonDB.AntiDdos.IsActive || Platform.Get != PlatformOS.Unix) { return; } IsRun = true; #region Переносим данные TCP/UPD с кеша в базу (за прошлый час) var TimeAntiDdosNumberOfRequestDay = DateTime.Now.AddHours(-1); if (memoryCache.TryGetValue(KeyToMemoryCache.AntiDdosNumberOfRequestDay(TimeAntiDdosNumberOfRequestDay), out NumberOfRequestDay dataLastHour)) { // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Записываем данные в базу coreDB.AntiDdos_NumberOfRequestDays.Add(dataLastHour); // Сохраняем базу coreDB.SaveChanges(); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Сносим кеш (статистика за час) memoryCache.Remove(KeyToMemoryCache.AntiDdosNumberOfRequestDay(TimeAntiDdosNumberOfRequestDay)); } #endregion #region Очистка баз + перенос NumberOfRequestDay в NumberOfRequestMonth if (memoryCache.TryGetValue("CronAntiDdosClearDB", out DateTime CronClearDB)) { // Если дата отличается от текущей if (CronClearDB.Day != DateTime.Now.Day) { // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.Read); // Обновляем кеш memoryCache.Set("CronAntiDdosClearDB", DateTime.Now); #region Очищаем NumberOfRequestMonth foreach (var item in coreDB.AntiDdos_NumberOfRequestMonths.AsNoTracking()) { // Если записи больше 90 дней if ((DateTime.Now - item.Time).TotalDays > 90) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.AntiDdos_NumberOfRequestMonths), item.Id)); } } #endregion #region Очищаем Jurnals foreach (var item in coreDB.AntiDdos_Jurnals.AsNoTracking()) { // Если записи больше 90 дней if ((DateTime.Now - item.Time).TotalDays > 90) { coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.AntiDdos_Jurnals), item.Id)); } } #endregion #region Очищаем NumberOfRequestDay + Переносим NumberOfRequestDay в NumberOfRequestMonth // Хранимм дату и значение var NumberOfRequestMonth = new Dictionary <int, (DateTime time, long value, int CountBlocked)>(); // Собираем статистику за прошлые дни foreach (var item in coreDB.AntiDdos_NumberOfRequestDays.AsNoTracking()) { // Пропускаем статистику за сегодня if (item.Time.Day == DateTime.Now.Day && item.Time.Month == DateTime.Now.Month) { continue; } #region Переносим значения в NumberOfRequestMonth if (NumberOfRequestMonth.TryGetValue(item.Time.Day, out (DateTime time, long value, int CountBlocked)it)) { NumberOfRequestMonth[item.Time.Day] = (it.time, (item.value > it.value ? item.value : it.value), (item.CountBlocked + it.CountBlocked)); } else { NumberOfRequestMonth.Add(item.Time.Day, (item.Time, item.value, item.CountBlocked)); } #endregion // Удаляем значения из базы coreDB.Database.ExecuteSqlCommand(ComandToSQL.Delete(nameof(coreDB.AntiDdos_NumberOfRequestDays), item.Id)); } // Переносим временные данные с NumberOfRequestMonth в базу foreach (var item in NumberOfRequestMonth) { // Добовляем в базу coreDB.AntiDdos_NumberOfRequestMonths.Add(new NumberOfRequestMonth() { Time = item.Value.time, value = item.Value.value, CountBlocked = item.Value.CountBlocked }); } #endregion // Сохраняем базу coreDB.SaveChanges(); // Меняем режим доступа к SQL SqlToMode.SetMode(SqlMode.ReadOrWrite); // Раз в сутки GC.Collect(GC.MaxGeneration); } } else { // Создаем кеш задним числом memoryCache.Set("CronAntiDdosClearDB", DateTime.Now.AddDays(-1)); } #endregion #region Очистка IPTables/IP6Tables if (jsonDB.AntiDdos.BlockToIPtables) { Bash bash = new Bash(); foreach (var comandTables in "iptables,ip6tables".Split(',')) { // Список IP foreach (var line in bash.Run(comandTables + " -L INPUT -v --line-numbers | awk '{print $1,$2,$9,$12}'").Split('\n').Reverse()) { // Разбираем строку var gr = new Regex("^([0-9]+) ([^ ]+) [^ ]+ ISPCore_([^\n\r]+)$").Match(line).Groups; if (string.IsNullOrWhiteSpace(gr[1].Value) || !DateTime.TryParse(gr[3].Value, out DateTime time)) { continue; } // Если время блокировки истекло if (DateTime.Now > time) { if (jsonDB.AntiDdos.ActiveLockMode) { if (gr[2].Value == "0") { bash.Run($"{comandTables} -D INPUT {gr[1].Value}"); } else { bash.Run($"{comandTables} -Z INPUT {gr[1].Value}"); } } else { bash.Run($"{comandTables} -D INPUT {gr[1].Value}"); } } } } } #endregion IsRun = false; }