Exemple #1
0
        private static async Task <bool> CompareHash(string pathFile, string pathArchive)
        {
            using FileStream stream1    = new(pathFile, FileMode.Open);
            using FileStream stream2    = new(pathArchive, FileMode.Open);
            using DeflateStream deflate = new(stream2, CompressionMode.Decompress);
            var hashTask1 = Sha256.ComputeHashAsync(stream1);
            var hashTask2 = Sha256.ComputeHashAsync(deflate);
            var hash1     = await hashTask1;
            var hash2     = await hashTask2;

            return(CompareArray <byte>(ref hash1, ref hash2));
        }
Exemple #2
0
        private async Task ValidateRunnerHash(string archiveFile, string packageHashValue)
        {
            var stopWatch = Stopwatch.StartNew();

            // Validate Hash Matches if it is provided
            using (FileStream stream = File.OpenRead(archiveFile))
            {
                if (!string.IsNullOrEmpty(packageHashValue))
                {
                    using (SHA256 sha256 = SHA256.Create())
                    {
                        byte[] srcHashBytes = await sha256.ComputeHashAsync(stream);

                        var hash = PrimitiveExtensions.ConvertToHexString(srcHashBytes);
                        if (hash != packageHashValue)
                        {
                            // Hash did not match, we can't recover from this, just throw
                            throw new Exception($"Computed runner hash {hash} did not match expected Runner Hash {packageHashValue} for {_targetPackage.Filename}");
                        }

                        stopWatch.Stop();
                        Trace.Info($"Validated Runner Hash matches {_targetPackage.Filename} : {packageHashValue}");
                        _updateTrace.Add($"ValidateHashTime: {stopWatch.ElapsedMilliseconds}ms");
                    }
                }
            }
        }
Exemple #3
0
        private static async Task <string> CreateFullHashFromFileAsync(string sourceFilePath)
        {
            using (SHA256 sha256Hash = SHA256.Create())
            {
                using (System.IO.FileStream fs = new System.IO.FileStream(sourceFilePath, System.IO.FileMode.Open))
                {
                    var result = await sha256Hash.ComputeHashAsync(fs);

                    return(result.ToHash());
                }
            }
        }
        private static async Task <string> GenerateTokenAsync(string sessionId, DateTime dateIssued, string hashKey)
        {
            // refer: https://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string
            byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes($"{sessionId}.{hashKey}.{dateIssued:ddMMyyyy}");

            using (SHA256 sha256 = SHA256.Create())
            {
                Stream stream    = new MemoryStream(plainTextBytes);
                byte[] hashValue = await sha256.ComputeHashAsync(stream);

                return(Convert.ToBase64String(hashValue));
            }
        }
        private static async Task <string> GenerateUserTOTPSecretAsync(Guid accountId, string hashKey)
        {
            // refer: https://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string
            string plainText = accountId.ToString();

            byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes($"{hashKey}.{plainText}.{hashKey}");

            using (SHA256 sha256 = SHA256.Create())
            {
                Stream stream    = new MemoryStream(plainTextBytes);
                byte[] hashValue = await sha256.ComputeHashAsync(stream);

                return(Base32Encoding.ToString(hashValue));
            }
        }
Exemple #6
0
        private async Task ShowFileDetailsDialog(FileItemView fv)
        {
            if (string.IsNullOrEmpty(fv.SHA256) || string.IsNullOrEmpty(fv.PreBytes))
            {
                try
                {
                    using FileStream fs = fv.FileInfo.Open(FileMode.Open);
                    if (string.IsNullOrEmpty(fv.PreBytes))
                    {
                        fs.Position = 0;
                        byte[] prebs = new byte[4];
                        await fs.ReadAsync(prebs, 0, 4);

                        fv.PreBytes = FilesHashComputer.ToHexString(prebs);
                    }
                    if (string.IsNullOrEmpty(fv.SHA256))
                    {
                        fs.Position = 0;
                        using (SHA256 sha256 = SHA256.Create())
                        {
                            fv.SHA256 = "Sha256正在计算中...";
                            byte[] hv = await sha256.ComputeHashAsync(fs);

                            fv.SHA256 = FilesHashComputer.ToHexString(hv);
                            fv.IsOK   = true;
                        }
                    }
                    fs.Close();
                }
                catch (UnauthorizedAccessException unex)
                {
                    fv.SHA256 = "未授权访问";
                    ShowErrorMsg(unex.Message);
                }
                catch (Exception ex)
                {
                    ShowErrorMsg(ex.Message);
                }
            }
            var fdd = new FileDetailsDialog(fv);

            fdd.ShowDialog();
        }
Exemple #7
0
        /// <summary>
        /// 计算文件Hash
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        private async Task <FileHashInfo> ComputeFileHash(FileInfo file)
        {
            using (SHA256 sha256 = SHA256.Create())
            {
                try
                {
                    using FileStream fileStream = file.Open(FileMode.Open);
                    fileStream.Position         = 0;
                    byte[] hv = await sha256.ComputeHashAsync(fileStream);

                    FileHashInfo fi = new FileHashInfo(file, hv);
                    fileStream.Close();
                    FileHashComputed?.Invoke(this, new FileHashInfoEventArgs(fi));
                    return(fi);
                }
                catch (Exception ex)
                {
                    FileHashError?.Invoke(this, new FileHashErrorEventArgs(new FileHashError(file, ex)));
                    return(new FileHashInfo(file, null));
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// _work
        ///     \_update
        ///            \bin
        ///            \externals
        ///            \run.sh
        ///            \run.cmd
        ///            \package.zip //temp download .zip/.tar.gz
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        private async Task DownloadLatestRunner(CancellationToken token)
        {
            string latestRunnerDirectory = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Work), Constants.Path.UpdateDirectory);

            IOUtil.DeleteDirectory(latestRunnerDirectory, token);
            Directory.CreateDirectory(latestRunnerDirectory);

            int    runnerSuffix      = 1;
            string archiveFile       = null;
            bool   downloadSucceeded = false;

            try
            {
                // Download the runner, using multiple attempts in order to be resilient against any networking/CDN issues
                for (int attempt = 1; attempt <= Constants.RunnerDownloadRetryMaxAttempts; attempt++)
                {
                    // Generate an available package name, and do our best effort to clean up stale local zip files
                    while (true)
                    {
                        if (_targetPackage.Platform.StartsWith("win"))
                        {
                            archiveFile = Path.Combine(latestRunnerDirectory, $"runner{runnerSuffix}.zip");
                        }
                        else
                        {
                            archiveFile = Path.Combine(latestRunnerDirectory, $"runner{runnerSuffix}.tar.gz");
                        }

                        try
                        {
                            // delete .zip file
                            if (!string.IsNullOrEmpty(archiveFile) && File.Exists(archiveFile))
                            {
                                Trace.Verbose("Deleting latest runner package zip '{0}'", archiveFile);
                                IOUtil.DeleteFile(archiveFile);
                            }

                            break;
                        }
                        catch (Exception ex)
                        {
                            // couldn't delete the file for whatever reason, so generate another name
                            Trace.Warning("Failed to delete runner package zip '{0}'. Exception: {1}", archiveFile, ex);
                            runnerSuffix++;
                        }
                    }

                    // Allow a 15-minute package download timeout, which is good enough to update the runner from a 1 Mbit/s ADSL connection.
                    if (!int.TryParse(Environment.GetEnvironmentVariable("GITHUB_ACTIONS_RUNNER_DOWNLOAD_TIMEOUT") ?? string.Empty, out int timeoutSeconds))
                    {
                        timeoutSeconds = 15 * 60;
                    }

                    Trace.Info($"Attempt {attempt}: save latest runner into {archiveFile}.");

                    using (var downloadTimeout = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSeconds)))
                        using (var downloadCts = CancellationTokenSource.CreateLinkedTokenSource(downloadTimeout.Token, token))
                        {
                            try
                            {
                                Trace.Info($"Download runner: begin download");

                                //open zip stream in async mode
                                using (HttpClient httpClient = new HttpClient(HostContext.CreateHttpClientHandler()))
                                {
                                    if (!string.IsNullOrEmpty(_targetPackage.Token))
                                    {
                                        Trace.Info($"Adding authorization token ({_targetPackage.Token.Length} chars)");
                                        httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _targetPackage.Token);
                                    }

                                    Trace.Info($"Downloading {_targetPackage.DownloadUrl}");

                                    using (FileStream fs = new FileStream(archiveFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
                                        using (Stream result = await httpClient.GetStreamAsync(_targetPackage.DownloadUrl))
                                        {
                                            //81920 is the default used by System.IO.Stream.CopyTo and is under the large object heap threshold (85k).
                                            await result.CopyToAsync(fs, 81920, downloadCts.Token);

                                            await fs.FlushAsync(downloadCts.Token);
                                        }
                                }

                                Trace.Info($"Download runner: finished download");
                                downloadSucceeded = true;
                                break;
                            }
                            catch (OperationCanceledException) when(token.IsCancellationRequested)
                            {
                                Trace.Info($"Runner download has been canceled.");
                                throw;
                            }
                            catch (Exception ex)
                            {
                                if (downloadCts.Token.IsCancellationRequested)
                                {
                                    Trace.Warning($"Runner download has timed out after {timeoutSeconds} seconds");
                                }

                                Trace.Warning($"Failed to get package '{archiveFile}' from '{_targetPackage.DownloadUrl}'. Exception {ex}");
                            }
                        }
                }

                if (!downloadSucceeded)
                {
                    throw new TaskCanceledException($"Runner package '{archiveFile}' failed after {Constants.RunnerDownloadRetryMaxAttempts} download attempts");
                }

                // If we got this far, we know that we've successfully downloaded the runner package
                // Validate Hash Matches if it is provided
                using (FileStream stream = File.OpenRead(archiveFile))
                {
                    if (!String.IsNullOrEmpty(_targetPackage.HashValue))
                    {
                        using (SHA256 sha256 = SHA256.Create())
                        {
                            byte[] srcHashBytes = await sha256.ComputeHashAsync(stream);

                            var hash = PrimitiveExtensions.ConvertToHexString(srcHashBytes);
                            if (hash != _targetPackage.HashValue)
                            {
                                // Hash did not match, we can't recover from this, just throw
                                throw new Exception($"Computed runner hash {hash} did not match expected Runner Hash {_targetPackage.HashValue} for {_targetPackage.Filename}");
                            }
                            Trace.Info($"Validated Runner Hash matches {_targetPackage.Filename} : {_targetPackage.HashValue}");
                        }
                    }
                }
                if (archiveFile.EndsWith(".zip", StringComparison.OrdinalIgnoreCase))
                {
                    ZipFile.ExtractToDirectory(archiveFile, latestRunnerDirectory);
                }
                else if (archiveFile.EndsWith(".tar.gz", StringComparison.OrdinalIgnoreCase))
                {
                    string tar = WhichUtil.Which("tar", trace: Trace);

                    if (string.IsNullOrEmpty(tar))
                    {
                        throw new NotSupportedException($"tar -xzf");
                    }

                    // tar -xzf
                    using (var processInvoker = HostContext.CreateService <IProcessInvoker>())
                    {
                        processInvoker.OutputDataReceived += new EventHandler <ProcessDataReceivedEventArgs>((sender, args) =>
                        {
                            if (!string.IsNullOrEmpty(args.Data))
                            {
                                Trace.Info(args.Data);
                            }
                        });

                        processInvoker.ErrorDataReceived += new EventHandler <ProcessDataReceivedEventArgs>((sender, args) =>
                        {
                            if (!string.IsNullOrEmpty(args.Data))
                            {
                                Trace.Error(args.Data);
                            }
                        });

                        int exitCode = await processInvoker.ExecuteAsync(latestRunnerDirectory, tar, $"-xzf \"{archiveFile}\"", null, token);

                        if (exitCode != 0)
                        {
                            throw new NotSupportedException($"Can't use 'tar -xzf' extract archive file: {archiveFile}. return code: {exitCode}.");
                        }
                    }
                }
                else
                {
                    throw new NotSupportedException($"{archiveFile}");
                }

                Trace.Info($"Finished getting latest runner package at: {latestRunnerDirectory}.");
            }
            finally
            {
                try
                {
                    // delete .zip file
                    if (!string.IsNullOrEmpty(archiveFile) && File.Exists(archiveFile))
                    {
                        Trace.Verbose("Deleting latest runner package zip: {0}", archiveFile);
                        IOUtil.DeleteFile(archiveFile);
                    }
                }
                catch (Exception ex)
                {
                    //it is not critical if we fail to delete the .zip file
                    Trace.Warning("Failed to delete runner package zip '{0}'. Exception: {1}", archiveFile, ex);
                }
            }

            // copy latest runner into runner root folder
            // copy bin from _work/_update -> bin.version under root
            string binVersionDir = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Root), $"{Constants.Path.BinDirectory}.{_targetPackage.Version}");

            Directory.CreateDirectory(binVersionDir);
            Trace.Info($"Copy {Path.Combine(latestRunnerDirectory, Constants.Path.BinDirectory)} to {binVersionDir}.");
            IOUtil.CopyDirectory(Path.Combine(latestRunnerDirectory, Constants.Path.BinDirectory), binVersionDir, token);

            // copy externals from _work/_update -> externals.version under root
            string externalsVersionDir = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Root), $"{Constants.Path.ExternalsDirectory}.{_targetPackage.Version}");

            Directory.CreateDirectory(externalsVersionDir);
            Trace.Info($"Copy {Path.Combine(latestRunnerDirectory, Constants.Path.ExternalsDirectory)} to {externalsVersionDir}.");
            IOUtil.CopyDirectory(Path.Combine(latestRunnerDirectory, Constants.Path.ExternalsDirectory), externalsVersionDir, token);

            // copy and replace all .sh/.cmd files
            Trace.Info($"Copy any remaining .sh/.cmd files into runner root.");
            foreach (FileInfo file in new DirectoryInfo(latestRunnerDirectory).GetFiles() ?? new FileInfo[0])
            {
                string destination = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.Root), file.Name);

                // Removing the file instead of just trying to overwrite it works around permissions issues on linux.
                // https://github.com/actions/runner/issues/981
                Trace.Info($"Copy {file.FullName} to {destination}");
                IOUtil.DeleteFile(destination);
                file.CopyTo(destination, true);
            }
        }