Esempio n. 1
0
        /// <summary>
        /// Get full url to download NGrok on this platform
        /// </summary>
        /// <exception cref="NGrokUnsupportedException">Throws if platform not supported by NGrok</exception>
        /// <returns></returns>
        private string GetDownloadPath()
        {
            const string cdn     = "https://bin.equinox.io";
            const string cdnPath = "c/4VmDzA7iaHb/NGrok-stable";

            return($"{cdn}/{cdnPath}-{RuntimeExtensions.GetOsArchitectureString()}.zip");
        }
Esempio n. 2
0
        /// <summary>
        /// Download NGrok from equinox.io CDN
        /// </summary>
        /// <exception cref="NGrokUnsupportedException">Throws if platform not supported by NGrok</exception>
        /// <exception cref="HttpRequestException">Throws if failed to download from CDN</exception>
        /// <returns></returns>
        public async Task DownloadExecutableAsync(CancellationToken cancellationToken)
        {
            var downloadUrl = GetDownloadPath();
            var fileName    = $"{RuntimeExtensions.GetOsArchitectureString()}.zip";
            var filePath    = $"{Path.Combine(Directory.GetCurrentDirectory(), fileName)}";

            if (File.Exists(filePath))
            {
                return;
            }

            var downloadResponse = await _httpClient.GetAsync(downloadUrl, cancellationToken);

            downloadResponse.EnsureSuccessStatusCode();

            // Download Zip
            var downloadStream = await downloadResponse.Content.ReadAsStreamAsync();

            await using (var writer = File.Create(filePath))
            {
                await downloadStream.CopyToAsync(writer, cancellationToken);
            }

            // Extract zip
            ZipFile.ExtractToDirectory(filePath, Directory.GetCurrentDirectory());

            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                GrantNGrokFileExecutablePermissions();
            }
        }