public async Task <IActionResult> Edit(BanFileMonitorDto model)
        {
            var banFileMonitorDto = await _banFileMonitorsRepository.GetBanFileMonitor(model.BanFileMonitorId);

            if (banFileMonitorDto == null)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                await AddGameServersViewData(model.ServerId);

                return(View(banFileMonitorDto));
            }

            var canEditBanFileMonitor = await _authorizationService.AuthorizeAsync(User, banFileMonitorDto, AuthPolicies.EditBanFileMonitor);

            if (!canEditBanFileMonitor.Succeeded)
            {
                return(Unauthorized());
            }

            banFileMonitorDto.FilePath = model.FilePath;

            await _banFileMonitorsRepository.UpdateBanFileMonitor(banFileMonitorDto);

            _logger.LogInformation(EventIds.Management, "User {User} has updated {BanFileMonitorId} against {ServerId}", User.Username(), banFileMonitorDto.BanFileMonitorId, banFileMonitorDto.ServerId);
            this.AddAlertSuccess($"The ban file monitor has been created for {banFileMonitorDto.GameServer.Title}");

            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 2
0
        public async Task ImportLatestBanFiles([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
        {
            log.LogDebug($"Start BanFileImportAndUpdate @ {DateTime.UtcNow}");

            var stopWatch = new Stopwatch();

            stopWatch.Start();

            var banFileMonitors = await _banFileMonitorsRepository.GetBanFileMonitors(new BanFileMonitorFilterModel());

            foreach (var banFileMonitor in banFileMonitors)
            {
                try
                {
                    _logger.LogDebug("Checking ban file for {server}", banFileMonitor.GameServer.Title);

                    var remoteFileSize = _ftpHelper.GetFileSize(
                        banFileMonitor.GameServer.FtpHostname,
                        banFileMonitor.FilePath,
                        banFileMonitor.GameServer.FtpUsername,
                        banFileMonitor.GameServer.FtpPassword);

                    var banFileSize = await _banFilesRepository.GetBanFileSizeForGame(banFileMonitor.GameServer.GameType);

                    if (remoteFileSize == 0)
                    {
                        _logger.LogInformation("Remote ban file on {server} at {path} is zero - updating file", banFileMonitor.GameServer.Title, banFileMonitor.FilePath);

                        var banFileStream = await _banFilesRepository.GetBanFileForGame(banFileMonitor.GameServer.GameType);

                        await _ftpHelper.UpdateRemoteFileFromStream(
                            banFileMonitor.GameServer.FtpHostname,
                            banFileMonitor.FilePath,
                            banFileMonitor.GameServer.FtpUsername,
                            banFileMonitor.GameServer.FtpPassword,
                            banFileStream);

                        banFileMonitor.RemoteFileSize = banFileSize;

                        await _banFileMonitorsRepository.UpdateBanFileMonitor(banFileMonitor);

                        continue;
                    }

                    if (remoteFileSize != banFileMonitor.RemoteFileSize)
                    {
                        _logger.LogInformation("Remote ban file on {server} at {path} has changed since last sync: {current} != {last}", banFileMonitor.GameServer.Title, banFileMonitor.FilePath, remoteFileSize, banFileMonitor.RemoteFileSize);

                        var remoteBanFileData = _ftpHelper.GetRemoteFileData(
                            banFileMonitor.GameServer.FtpHostname,
                            banFileMonitor.FilePath,
                            banFileMonitor.GameServer.FtpUsername,
                            banFileMonitor.GameServer.FtpPassword);

                        await _banFileIngest.IngestBanFileDataForGame(banFileMonitor.GameServer.GameType, remoteBanFileData);

                        banFileMonitor.RemoteFileSize = remoteFileSize;

                        await _banFileMonitorsRepository.UpdateBanFileMonitor(banFileMonitor);
                    }
                    else
                    {
                        _logger.LogDebug("Remote ban file on {server} at {path} has not been modified since last sync", banFileMonitor.GameServer.Title, banFileMonitor.FilePath);
                    }

                    if (remoteFileSize != banFileSize && remoteFileSize == banFileMonitor.RemoteFileSize)
                    {
                        _logger.LogInformation("Remote ban file on {server} at {path} is not the latest version", banFileMonitor.GameServer.Title, banFileMonitor.FilePath);

                        var banFileStream = await _banFilesRepository.GetBanFileForGame(banFileMonitor.GameServer.GameType);

                        await _ftpHelper.UpdateRemoteFileFromStream(
                            banFileMonitor.GameServer.FtpHostname,
                            banFileMonitor.FilePath,
                            banFileMonitor.GameServer.FtpUsername,
                            banFileMonitor.GameServer.FtpPassword,
                            banFileStream);

                        banFileMonitor.RemoteFileSize = banFileSize;

                        await _banFileMonitorsRepository.UpdateBanFileMonitor(banFileMonitor);
                    }
                    else
                    {
                        _logger.LogDebug("Remote ban file on {server} at {path} has the latest ban file", banFileMonitor.GameServer.Title, banFileMonitor.FilePath);
                    }

                    banFileMonitor.LastSync = DateTime.UtcNow;
                    await _banFileMonitorsRepository.UpdateBanFileMonitor(banFileMonitor);
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Failed to check ban file for {server}", banFileMonitor.GameServer.Title);
                }
            }

            stopWatch.Stop();
            log.LogDebug($"Stop BanFileImportAndUpdate @ {DateTime.UtcNow} after {stopWatch.ElapsedMilliseconds} milliseconds");
        }