public async Task <IActionResult> Index()
        {
            var filterModel = new BanFileMonitorFilterModel
            {
                Order = BanFileMonitorFilterModel.OrderBy.BannerServerListPosition
            }.ApplyAuth(User);
            var banFileMonitorDtos = await _banFileMonitorsRepository.GetBanFileMonitors(filterModel);

            return(View(banFileMonitorDtos));
        }
        public async Task <IActionResult> Details(Guid id)
        {
            var gameServerDto = await _gameServersRepository.GetGameServer(id);

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

            var canEditGameServer = await _authorizationService.AuthorizeAsync(User, gameServerDto, AuthPolicies.ViewGameServer);

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

            var banFileMonitorFilterModel = new BanFileMonitorFilterModel
            {
                Order    = BanFileMonitorFilterModel.OrderBy.BannerServerListPosition,
                ServerId = id
            }.ApplyAuth(User);
            var banFileMonitorDtos = await _banFileMonitorsRepository.GetBanFileMonitors(banFileMonitorFilterModel);

            var fileMonitorFilterModel = new FileMonitorFilterModel
            {
                Order    = FileMonitorFilterModel.OrderBy.BannerServerListPosition,
                ServerId = id
            }.ApplyAuth(User);
            var fileMonitorsDtos = await _fileMonitorsRepository.GetFileMonitors(fileMonitorFilterModel);

            var model = new GameServerDetailsViewModel
            {
                GameServerDto      = gameServerDto,
                BanFileMonitorDtos = banFileMonitorDtos,
                FileMonitorDtos    = fileMonitorsDtos
            };

            return(View(model));
        }
Exemplo n.º 3
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");
        }