Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(ISignToolService signToolService)
        {
            _signToolService = signToolService;
            _logBuilder      = new StringBuilder();

            _startServerCommand = new RelayCommand(StartServer, CanStartServer);
            _stopServerCommand  = new RelayCommand(StopServer, CanStopServer);

            MessengerInstance.Register <LogMessage>(
                this,
                message =>
            {
                _logBuilder.AppendLine(message.Message);
                RaisePropertyChanged(() => Log);
            });

            this.BaseAddress = Properties.Settings.Default.BaseAddress;

            string signToolPath;

            if (!_signToolService.TryToFindSignToolPath(out signToolPath))
            {
                Logger.Error(Properties.Resources.SignToolNotInstalled);
            }
        }
Exemple #2
0
        private async void StartServer()
        {
            if (_httpServer != null)
            {
                return;
            }

            StartOptions options = new StartOptions();

            options.Urls.Add(this.BaseAddress);

            try
            {
                string signToolPath;
                if (!_signToolService.TryToFindSignToolPath(out signToolPath))
                {
                    Logger.Error(Properties.Resources.SignToolNotInstalled);
                    throw new Exception(Properties.Resources.SignToolNotInstalled);
                }

                var folderToSignAtStartup = Properties.Settings.Default.FolderToSignAtStartup;
                if (!string.IsNullOrWhiteSpace(folderToSignAtStartup))
                {
                    var signResult = await _signToolService.Sign(signToolPath, " /a", folderToSignAtStartup);

                    if (signResult.ExitCode != 0)
                    {
                        Logger.Error(Properties.Resources.SigningAtStartupFailed);
                        Logger.Error(Properties.Resources.SignToolExitedWithCodeFormat, signResult.ExitCode);
                        Logger.Error(signResult.StandardError);

                        throw new Exception(Properties.Resources.SigningAtStartupFailed);
                    }
                }

                _httpServer  = WebApp.Start <Startup>(options);
                ServerStatus = Properties.Resources.Label_ServerIsRunning;
                _startServerCommand.RaiseCanExecuteChanged();
                _stopServerCommand.RaiseCanExecuteChanged();
                Logger.Info(Properties.Resources.ServerHasBeenStarted);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, "Failed to start server");
            }
        }
        public async Task <IHttpActionResult> Sign([FromBody] SignDto dto)
        {
            Logger.Info(Properties.Resources.StartSigningFiles);

            var archivePath = Path.Combine(UploadController.UploadDirectoryName, dto.ArchiveName);

            if (!File.Exists(archivePath))
            {
                Logger.Warn(Properties.Resources.ArchiveHasNotBeenFoundFormat, dto.ArchiveName);
                return(this.BadRequest(string.Format(Properties.Resources.ArchiveHasNotBeenFoundFormat, dto.ArchiveName)));
            }

            var extractionDirectoryName = Path.Combine(TempDirectoryName, Path.GetRandomFileName());

            Directory.CreateDirectory(extractionDirectoryName);
            Logger.Info(Properties.Resources.DirectoryCreatedFormat, extractionDirectoryName);

            using (ZipFile zip = new ZipFile(archivePath))
            {
                zip.ExtractAll(extractionDirectoryName);
                Logger.Info(Properties.Resources.FilesHaveBeenExtractedFormat, extractionDirectoryName);
            }

            string signToolPath;

            if (!_signToolService.TryToFindSignToolPath(out signToolPath))
            {
                Logger.Error(Properties.Resources.SignToolNotInstalled);
                return(this.InternalServerError(new FileNotFoundException(Properties.Resources.SignToolNotInstalled, "signtool.exe")));
            }

            var signResult = await _signToolService.Sign(signToolPath, dto.SignSubcommands, extractionDirectoryName);

            var signedArchiveName = string.Format("{0}_signed.zip", Path.GetFileNameWithoutExtension(dto.ArchiveName));

            if (signResult.ExitCode == 0)
            {
                Logger.Info(Properties.Resources.SignToolSuccessfullySignedFiles);
                using (ZipFile zip = new ZipFile())
                {
                    // Currently, we flatten the hierarchy of files for sake of simplicity
                    zip.AddFiles(Directory.GetFiles(extractionDirectoryName), false, string.Empty);
                    zip.Save(Path.Combine(UploadController.UploadDirectoryName, signedArchiveName));
                    Logger.Info(Properties.Resources.ArchiveWithSignedFilesCreated, signedArchiveName);
                }
            }
            else
            {
                Logger.Error(Properties.Resources.SignToolExitedWithCodeFormat, signResult.ExitCode);
                Logger.Error(signResult.StandardError);
            }

            var result = new SignResultDto()
            {
                ExitCode       = signResult.ExitCode,
                StandardOutput = signResult.StandardOutput,
                StandardError  = signResult.StandardError,
                DownloadUrl    = signResult.ExitCode == 0 ? this.Url.Link(UploadController.DownloadRouteName, new { fileName = Uri.EscapeUriString(signedArchiveName) }) : null
            };

            return(this.Ok(result));
        }