コード例 #1
0
        /// <summary>
        /// Check if ngrok present in current directory or Windows PATH variable. If not, download from CDN, and throw exception if download fails
        /// </summary>
        /// <exception cref="NgrokUnsupportedException">Throws if platform not supported by ngrok</exception>
        /// <exception cref="NgrokNotFoundException">Throws if ngrok not found and failed to download from CDN</exception>
        /// <returns></returns>
        public async Task <string> EnsureNgrokInstalled(NgrokOptions options)
        {
            bool fileExists = File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "ngrok.exe"));

            if (fileExists)
            {
                return(Path.Combine(Directory.GetCurrentDirectory(), "ngrok.exe"));
            }

            bool fileInPath = false;             // TODO parse this from windows PATH variable if on windows

            if (fileInPath)
            {
                return("");                // TODO get actual path from PATH variable if on windows
            }

            bool fileInOptions = !string.IsNullOrWhiteSpace(options.NgrokPath) && File.Exists(options.NgrokPath);

            if (fileInOptions)
            {
                return(options.NgrokPath);
            }

            if (!(fileExists || fileInPath || fileInOptions))
            {
                await DownloadNgrokAsync();

                return("ngrok.exe");
            }
            throw new NgrokNotFoundException();
        }
コード例 #2
0
        public static void AddNgrok(this IServiceCollection services, NgrokOptions options = null)
        {
            services.TryAddSingleton <NgrokProcess>();

            services.AddHttpClient <NgrokDownloader>();
            services.AddHttpClient <NgrokLocalApiClient>();

            services.TryAddSingleton <NgrokOptions>(options ?? new NgrokOptions());

            services.AddLogging();
        }
コード例 #3
0
        public NgrokLocalApiClient(HttpClient httpClient, NgrokProcess ngrokProcess, NgrokOptions options, ILogger <NgrokLocalApiClient> logger)
        {
            _ngrokApi     = httpClient;
            _options      = options;
            _ngrokProcess = ngrokProcess;
            _logger       = logger;

            // TODO some of this can be moved to the DI registration
            _ngrokApi.BaseAddress = new Uri("http://localhost:4040");
            _ngrokApi.DefaultRequestHeaders.Accept.Clear();
            _ngrokApi.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
コード例 #4
0
        /// <summary>
        /// Check if ngrok present in current directory or Windows PATH variable. If not, download from CDN, and throw exception if download fails
        /// </summary>
        /// <exception cref="NgrokUnsupportedException">Throws if platform not supported by ngrok</exception>
        /// <exception cref="NgrokNotFoundException">Throws if ngrok not found and failed to download from CDN</exception>
        /// <returns></returns>
        public async Task <string> EnsureNgrokInstalled(NgrokOptions options)
        {
            // Search options
            bool fileInOptions = !string.IsNullOrWhiteSpace(options.NgrokPath) && File.Exists(options.NgrokPath);

            if (fileInOptions)
            {
                return(options.NgrokPath);
            }

            // Search execution directory
            bool fileExists = File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "ngrok.exe"));

            if (fileExists)
            {
                return(Path.Combine(Directory.GetCurrentDirectory(), "ngrok.exe"));
            }

            // Search Windows PATH
            var envFullPath = PathExtensions.GetFullPathFromEnvPath("ngrok.exe");

            if (!string.IsNullOrWhiteSpace(envFullPath) && File.Exists(envFullPath))
            {
                return(envFullPath);
            }

            // Throw exception if not found yet and downloading is disabled
            if (!options.DownloadNgrok)
            {
                throw new NgrokNotFoundException();
            }

            await DownloadNgrokAsync();

            return("ngrok.exe");
        }
コード例 #5
0
        public static async Task <IEnumerable <Tunnel> > StartNgrokAsyncWorker(IServiceProvider services, NgrokOptions options)
        {
            // Start Ngrok
            var ngrokClient = services.GetRequiredService <NgrokLocalApiClient>();

            return(await ngrokClient.StartTunnelsAsync(options.NgrokPath));
        }