From 88c6e286ee6a90e78b17a3e14ed7ee3d01ed0c7d Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 31 May 2019 17:55:36 +0800 Subject: [PATCH] Resolve #1160 Change ports of templates to randoms on template creation. --- .../Templates/Mvc/MvcTemplate.cs | 9 ++ .../Templates/MvcModule/MvcModuleTemplate.cs | 8 ++ .../Templates/TemplateRandomSslPortStep.cs | 107 ++++++++++++++++++ .../Properties/launchSettings.json | 6 +- .../Properties/launchSettings.json | 6 +- .../Properties/launchSettings.json | 6 +- .../Properties/launchSettings.json | 6 +- .../Properties/launchSettings.json | 8 +- .../appsettings.json | 2 +- .../Properties/launchSettings.json | 8 +- .../appsettings.json | 2 +- .../Properties/launchSettings.json | 6 +- .../appsettings.json | 6 +- .../Properties/launchSettings.json | 6 +- .../appsettings.json | 4 +- 15 files changed, 157 insertions(+), 33 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/TemplateRandomSslPortStep.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/Mvc/MvcTemplate.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/Mvc/MvcTemplate.cs index 069c5fbf03..1b94062ce6 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/Mvc/MvcTemplate.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/Mvc/MvcTemplate.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using Volo.Abp.Cli.ProjectBuilding.Building; using Volo.Abp.Cli.ProjectBuilding.Building.Steps; +using Volo.Abp.Cli.ProjectBuilding.Templates.MvcModule; namespace Volo.Abp.Cli.ProjectBuilding.Templates.Mvc { @@ -24,6 +25,14 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.Mvc SwitchDatabaseProvider(context, steps); DeleteUnrelatedProjects(context, steps); + steps.Add(new TemplateRandomSslPortStep(new List + { + "https://localhost:44300", + "https://localhost:44301", + "https://localhost:44302", + "https://localhost:44303" + })); + return steps; } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/MvcModule/MvcModuleTemplate.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/MvcModule/MvcModuleTemplate.cs index e3258ec8a6..0c8b9b9180 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/MvcModule/MvcModuleTemplate.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/MvcModule/MvcModuleTemplate.cs @@ -23,6 +23,14 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.MvcModule DeleteUnrelatedProjects(context, steps); + steps.Add(new TemplateRandomSslPortStep(new List + { + "https://localhost:44300", + "https://localhost:44301", + "https://localhost:44302", + "https://localhost:44303" + })); + return steps; } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/TemplateRandomSslPortStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/TemplateRandomSslPortStep.cs new file mode 100644 index 0000000000..f363e2c31b --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/TemplateRandomSslPortStep.cs @@ -0,0 +1,107 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using Volo.Abp.Cli.ProjectBuilding.Building; + +namespace Volo.Abp.Cli.ProjectBuilding.Templates +{ + public class TemplateRandomSslPortStep : ProjectBuildPipelineStep + { + private readonly int _minSslPort; + + private readonly int _maxSslPort; + + private readonly List _buildInSslUrls; + + public TemplateRandomSslPortStep(List buildInSslSslUrls, int minSslPort = 44300, + int maxSslPort = 44399) + { + _buildInSslUrls = buildInSslSslUrls; + + _minSslPort = minSslPort; + _maxSslPort = maxSslPort; + } + + public override void Execute(ProjectBuildContext context) + { + var launchSettings = context.Files.Where(x => + !x.IsDirectory && x.Name.EndsWith("launchSettings.json", StringComparison.InvariantCultureIgnoreCase)) + .ToList(); + + var appSettings = context.Files.Where(x => + !x.IsDirectory && x.Name.EndsWith("appSettings.json", StringComparison.InvariantCultureIgnoreCase)) + .ToList(); + + + var excludePorts = new List(); + excludePorts.AddRange(_buildInSslUrls.Select(GetUrlPort).ToList()); + + foreach (var buildInUrl in _buildInSslUrls) + { + var newPort = GetRandomPort(excludePorts); + excludePorts.Add(newPort); + + var buildInUrlWithoutPort = GetUrlWithoutPort(buildInUrl); + var buildInUrlPort = GetUrlPort(buildInUrl); + + foreach (var launchSetting in launchSettings) + { + launchSetting.NormalizeLineEndings(); + + var launchSettingLines = launchSetting.GetLines(); + for (var i = 0; i < launchSettingLines.Length; i++) + { + if (launchSettingLines[i].Contains($"{buildInUrl}")) + { + launchSettingLines[i] = launchSettingLines[i].Replace($"{buildInUrl}", $"{buildInUrlWithoutPort}:{newPort}"); + } + + if (launchSettingLines[i].Contains($"\"sslPort\": {buildInUrlPort}")) + { + launchSettingLines[i] = launchSettingLines[i] + .Replace($"\"sslPort\": {buildInUrlPort}", $"\"sslPort\": {newPort}"); + } + } + launchSetting.SetLines(launchSettingLines); + + foreach (var appSetting in appSettings) + { + appSetting.NormalizeLineEndings(); + + var appSettingLines = appSetting.GetLines(); + for (var i = 0; i < appSettingLines.Length; i++) + { + if (appSettingLines[i].Contains(buildInUrl)) + { + appSettingLines[i] = appSettingLines[i].Replace(buildInUrl, $"{buildInUrlWithoutPort}:{newPort}"); + } + } + appSetting.SetLines(appSettingLines); + } + } + } + } + + private string GetRandomPort(IReadOnlyCollection excludePort = null) + { + var ports = Enumerable.Range(_minSslPort, _maxSslPort).Select(p => p.ToString()); + + return RandomHelper.GetRandomOfList(excludePort != null + ? ports.Except(excludePort).ToList() + : ports.ToList()); + } + + private string GetUrlPort(string url) + { + var match = Regex.Match(url, @":(\d+)"); + return match.Success ? match.Groups[1].Value : ""; + } + + private string GetUrlWithoutPort(string url) + { + var match = Regex.Match(url, @"(^.+):"); + return match.Success ? match.Groups[1].Value : ""; + } + } +} \ No newline at end of file diff --git a/templates/mvc-module/host/MyCompanyName.MyProjectName.HttpApi.Host/Properties/launchSettings.json b/templates/mvc-module/host/MyCompanyName.MyProjectName.HttpApi.Host/Properties/launchSettings.json index 469b617f37..71506f734d 100644 --- a/templates/mvc-module/host/MyCompanyName.MyProjectName.HttpApi.Host/Properties/launchSettings.json +++ b/templates/mvc-module/host/MyCompanyName.MyProjectName.HttpApi.Host/Properties/launchSettings.json @@ -3,8 +3,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:57992", - "sslPort": 44300 //44300 to 44399 (inclusive) + "applicationUrl": "https://localhost:44300", + "sslPort": 44300 } }, "profiles": { @@ -18,7 +18,7 @@ "MyCompanyName.MyProjectName.DemoApp": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:44300", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/templates/mvc-module/host/MyCompanyName.MyProjectName.IdentityServer/Properties/launchSettings.json b/templates/mvc-module/host/MyCompanyName.MyProjectName.IdentityServer/Properties/launchSettings.json index d1d5c8a40c..b054d4c6bc 100644 --- a/templates/mvc-module/host/MyCompanyName.MyProjectName.IdentityServer/Properties/launchSettings.json +++ b/templates/mvc-module/host/MyCompanyName.MyProjectName.IdentityServer/Properties/launchSettings.json @@ -3,8 +3,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:61517", - "sslPort": 44301 //44300 to 44399 (inclusive) + "applicationUrl": "https://localhost:44301", + "sslPort": 44301 } }, "profiles": { @@ -18,7 +18,7 @@ "IdentityServerHost": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:44301", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/templates/mvc-module/host/MyCompanyName.MyProjectName.Web.Host/Properties/launchSettings.json b/templates/mvc-module/host/MyCompanyName.MyProjectName.Web.Host/Properties/launchSettings.json index 8f09dd974b..a0f21d8d9c 100644 --- a/templates/mvc-module/host/MyCompanyName.MyProjectName.Web.Host/Properties/launchSettings.json +++ b/templates/mvc-module/host/MyCompanyName.MyProjectName.Web.Host/Properties/launchSettings.json @@ -3,8 +3,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:55953", - "sslPort": 44302 //44300 to 44399 (inclusive) + "applicationUrl": "https://localhost:44302", + "sslPort": 44302 } }, "profiles": { @@ -18,7 +18,7 @@ "MyCompanyName.MyProjectName.Web.Host": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:44302", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/templates/mvc-module/host/MyCompanyName.MyProjectName.Web.Unified/Properties/launchSettings.json b/templates/mvc-module/host/MyCompanyName.MyProjectName.Web.Unified/Properties/launchSettings.json index e19ee82834..6b5081f583 100644 --- a/templates/mvc-module/host/MyCompanyName.MyProjectName.Web.Unified/Properties/launchSettings.json +++ b/templates/mvc-module/host/MyCompanyName.MyProjectName.Web.Unified/Properties/launchSettings.json @@ -3,8 +3,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:57891", - "sslPort": 44303 //44300 to 44399 (inclusive) + "applicationUrl": "https://localhost:44303", + "sslPort": 44303 } }, "profiles": { @@ -18,7 +18,7 @@ "MyCompanyName.MyProjectName.DemoApp": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:44303", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/Properties/launchSettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/Properties/launchSettings.json index 55293a31cd..c3ea010654 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/Properties/launchSettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/Properties/launchSettings.json @@ -1,10 +1,10 @@ { "iisSettings": { "windowsAuthentication": false, - "anonymousAuthentication": true, + "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:50904", - "sslPort": 44395 + "applicationUrl": "https://localhost:44300", + "sslPort": 44300 } }, "profiles": { @@ -18,7 +18,7 @@ "MyCompanyName.MyProjectName.HttpApi.Host": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:44300", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json index a4df214f53..2f6399ac58 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json @@ -6,6 +6,6 @@ "Configuration": "127.0.0.1" }, "AuthServer": { - "Authority": "https://localhost:44348" + "Authority": "https://localhost:44301" } } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/Properties/launchSettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/Properties/launchSettings.json index 755641f9d7..0fb6f3a3b1 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/Properties/launchSettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/Properties/launchSettings.json @@ -1,10 +1,10 @@ { "iisSettings": { "windowsAuthentication": false, - "anonymousAuthentication": true, + "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:55993", - "sslPort": 44348 + "applicationUrl": "https://localhost:44301", + "sslPort": 44301 } }, "profiles": { @@ -18,7 +18,7 @@ "MyCompanyName.MyProjectName.IdentityServer": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:44301", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json index e16f493489..036a7fae3e 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json @@ -1,5 +1,5 @@ { - "AppSelfUrl": "https://localhost:44348", + "AppSelfUrl": "https://localhost:44301", "ConnectionStrings": { "Default": "Server=localhost;Database=MyProjectName;Trusted_Connection=True;MultipleActiveResultSets=true" }, diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/Properties/launchSettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/Properties/launchSettings.json index 510c9321f1..a0f21d8d9c 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/Properties/launchSettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/Properties/launchSettings.json @@ -3,8 +3,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:55953", - "sslPort": 44314 + "applicationUrl": "https://localhost:44302", + "sslPort": 44302 } }, "profiles": { @@ -18,7 +18,7 @@ "MyCompanyName.MyProjectName.Web.Host": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:44302", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/appsettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/appsettings.json index f0d2e40d57..3e8f88f825 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/appsettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Web.Host/appsettings.json @@ -1,15 +1,15 @@ { - "AppSelfUrl": "https://localhost:44314", + "AppSelfUrl": "https://localhost:44302", "RemoteServices": { "Default": { - "BaseUrl": "https://localhost:44395/" + "BaseUrl": "https://localhost:44300/" } }, "Redis": { "Configuration": "127.0.0.1" }, "AuthServer": { - "Authority": "https://localhost:44348", + "Authority": "https://localhost:44301", "ClientId": "MyProjectName_Web", "ClientSecret": "1q2w3e*" } diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Web/Properties/launchSettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.Web/Properties/launchSettings.json index a03511dbe8..1d21b27b65 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Web/Properties/launchSettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Web/Properties/launchSettings.json @@ -3,8 +3,8 @@ "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { - "applicationUrl": "http://localhost:53929/", - "sslPort": 44361 + "applicationUrl": "https://localhost:44303/", + "sslPort": 44303 } }, "profiles": { @@ -21,7 +21,7 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, - "applicationUrl": "http://localhost:53932/" + "applicationUrl": "https://localhost:44303/" } } } \ No newline at end of file diff --git a/templates/mvc/src/MyCompanyName.MyProjectName.Web/appsettings.json b/templates/mvc/src/MyCompanyName.MyProjectName.Web/appsettings.json index 5b48dc3d38..99415d6a8f 100644 --- a/templates/mvc/src/MyCompanyName.MyProjectName.Web/appsettings.json +++ b/templates/mvc/src/MyCompanyName.MyProjectName.Web/appsettings.json @@ -1,10 +1,10 @@ { - "AppSelfUrl": "https://localhost:44361", + "AppSelfUrl": "https://localhost:44303", "ConnectionStrings": { "Default": "Server=localhost;Database=MyProjectName;Trusted_Connection=True;MultipleActiveResultSets=true" }, "AuthServer": { - "Authority": "https://localhost:44361" + "Authority": "https://localhost:44301" }, "IdentityServer": { "Clients": {