private static void LoadOptionsFromAppSettingsJson(Project project, WebAppConfig webApp)
        {
            // Read the settings from the project's appsettings.json first
            foreach (ProjectItem item in project.ProjectItems)
            {
                if (item.Name.ToLower() != "appsettings.json")
                {
                    continue;
                }

                ReadOptionsFromJsonFile(item.FileNames[0], webApp);
            }

            // Override any additional settings from the secrets.json file if it exists
            var userSecretsId = project.Properties.OfType <Property>()
                                .FirstOrDefault(x => x.Name.Equals("UserSecretsId", StringComparison.OrdinalIgnoreCase))?.Value as String;

            if (string.IsNullOrEmpty(userSecretsId))
            {
                return;
            }

            var appdata     = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var secretsFile = Path.Combine(appdata, "Microsoft", "UserSecrets", userSecretsId, "secrets.json");

            ReadOptionsFromJsonFile(secretsFile, webApp);
        }
Example #2
0
        private async Task StartNgrokTunnelAsync(string projectName, WebAppConfig config)
        {
            var addr = config.NgrokAddress;

            if (!TunnelAlreadyExists(addr))
            {
                await CreateTunnelAsync(projectName, config, addr);
            }
        }
Example #3
0
        private async Task StartNgrokTunnelAsync(string projectName, WebAppConfig config)
        {
            var addr = $"localhost:{config.PortNumber}";

            if (!TunnelAlreadyExists(addr))
            {
                await CreateTunnelAsync(projectName, config, addr);
            }
        }
        private static void ReadOptionsFromJsonFile(string path, WebAppConfig webApp)
        {
            if (!File.Exists(path))
            {
                return;
            }

            var json        = File.ReadAllText(path);
            var appSettings = JsonConvert.DeserializeAnonymousType(json,
                                                                   new { IsEncrypted = false, Values = new Dictionary <string, string>() });

            if (appSettings.Values != null && appSettings.Values.TryGetValue(NgrokSubdomainSettingName, out var subdomain))
            {
                webApp.SubDomain = subdomain;
            }
        }
Example #5
0
        private static void LoadOptionsFromAppSettingsJson(Project project, WebAppConfig webApp)
        {
            foreach (ProjectItem item in project.ProjectItems)
            {
                if (item.Name.ToLower() != "appsettings.json")
                {
                    continue;
                }

                var json        = File.ReadAllText(item.FileNames[0]);
                var appSettings = JsonConvert.DeserializeAnonymousType(json,
                                                                       new { IsEncrypted = false, Values = new Dictionary <string, string>() });
                webApp.SubDomain = appSettings.Values?[NgrokSubdomainSettingName];
                break;
            }
        }
        private static void LoadOptionsFromWebConfig(Project project, WebAppConfig webApp)
        {
            foreach (ProjectItem item in project.ProjectItems)
            {
                if (item.Name.ToLower() != "web.config")
                {
                    continue;
                }

                var path        = item.FileNames[0];
                var webConfig   = XDocument.Load(path);
                var appSettings = webConfig.Descendants("appSettings").FirstOrDefault();
                webApp.SubDomain = appSettings?.Descendants("add")
                                   .FirstOrDefault(x => x.Attribute("key")?.Value == NgrokSubdomainSettingName)
                                   ?.Attribute("value")?.Value;
                break;
            }
        }
        private Dictionary <string, WebAppConfig> GetWebApps()
        {
            var webApps  = new Dictionary <string, WebAppConfig>();
            var projects = GetSolutionProjects();

            if (projects == null)
            {
                return(webApps);
            }

            foreach (Project project in projects)
            {
                if (project.Properties == null)
                {
                    continue;                             // Project not loaded yet
                }
                foreach (Property prop in project.Properties)
                {
                    DebugWriteProp(prop);
                    if (!PortPropertyNames.Contains(prop.Name))
                    {
                        continue;
                    }

                    WebAppConfig webApp;

                    if (prop.Name == "FileName")
                    {
                        if (prop.Value.ToString().EndsWith(".funproj"))
                        {
                            // Azure Functions app - use port 7071
                            webApp = new WebAppConfig("7071");
                            LoadOptionsFromAppSettingsJson(project, webApp);
                        }
                        else
                        {
                            continue;  // FileName property not relevant otherwise
                        }
                    }
                    else
                    {
                        webApp = new WebAppConfig(prop.Value.ToString());
                        if (!webApp.IsValid)
                        {
                            continue;
                        }
                        if (IsAspNetCoreProject(prop.Name))
                        {
                            LoadOptionsFromAppSettingsJson(project, webApp);
                        }
                        else
                        {
                            LoadOptionsFromWebConfig(project, webApp);
                        }
                    }

                    webApps.Add(project.Name, webApp);
                    break;
                }
            }
            return(webApps);
        }
Example #8
0
        private async Task CreateTunnelAsync(string projectName, WebAppConfig config, string addr, bool retry = false)
        {
            var request = new NgrokTunnelApiRequest
            {
                name        = projectName,
                addr        = addr,
                proto       = "http",
                host_header = StripProtocol(addr)
            };

            if (!string.IsNullOrEmpty(config.SubDomain))
            {
                request.subdomain = config.SubDomain;
            }

            Debug.WriteLine($"request: '{JsonConvert.SerializeObject(request)}'");
            var response = await _ngrokApi.PostAsJsonAsync("/api/tunnels", request);

            if (!response.IsSuccessStatusCode)
            {
                var errorText = await response.Content.ReadAsStringAsync();

                Debug.WriteLine($"{response.StatusCode} errorText: '{errorText}'");
                NgrokErrorApiResult error;

                try
                {
                    error = JsonConvert.DeserializeObject <NgrokErrorApiResult>(errorText);
                }
                catch (JsonReaderException)
                {
                    error = null;
                }

                if (error != null)
                {
                    await _showErrorFunc($"Could not create tunnel for {projectName} ({addr}): " +
                                         $"\n[{error.error_code}] {error.msg}" +
                                         $"\nDetails: {error.details.err.Replace("\\n", "\n")}");
                }
                else
                {
                    if (retry)
                    {
                        await _showErrorFunc($"Could not create tunnel for {projectName} ({addr}): " +
                                             $"\n{errorText}");
                    }
                    else
                    {
                        await Task.Delay(1000);  // wait for ngrok to spin up completely?
                        await CreateTunnelAsync(projectName, config, addr, true);
                    }
                }
                return;
            }

            var responseText = await response.Content.ReadAsStringAsync();

            Debug.WriteLine($"responseText: '{responseText}'");
            var tunnel = JsonConvert.DeserializeObject <Tunnel>(responseText);

            config.PublicUrl = tunnel.public_url;
            Debug.WriteLine(config.PublicUrl);
        }