diff --git a/src/chocolatey/chocolatey.csproj b/src/chocolatey/chocolatey.csproj index d9419cea7e..b8f781becc 100644 --- a/src/chocolatey/chocolatey.csproj +++ b/src/chocolatey/chocolatey.csproj @@ -1,4 +1,4 @@ - + Debug @@ -135,6 +135,7 @@ + diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs index 53851701a0..e4d218f0b1 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyPinCommand.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2025 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -146,7 +146,7 @@ public virtual void noop(ChocolateyConfiguration configuration) public virtual void run(ChocolateyConfiguration configuration) { var packageManager = NugetCommon.GetPackageManager(configuration, _nugetLogger, - new PackageDownloader(), + new ChocolateyPackageDownloader(), installSuccessAction: null, uninstallSuccessAction: null, addUninstallHandler: false); diff --git a/src/chocolatey/infrastructure.app/nuget/ChocolateyNugetCredentialProvider.cs b/src/chocolatey/infrastructure.app/nuget/ChocolateyNugetCredentialProvider.cs index 4a246db552..b0568af803 100644 --- a/src/chocolatey/infrastructure.app/nuget/ChocolateyNugetCredentialProvider.cs +++ b/src/chocolatey/infrastructure.app/nuget/ChocolateyNugetCredentialProvider.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2025 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -17,9 +17,9 @@ namespace chocolatey.infrastructure.app.nuget { using System; + using System.Collections.Concurrent; using System.Linq; using System.Net; - using System.Text.RegularExpressions; using commandline; using NuGet; using configuration; @@ -29,6 +29,7 @@ namespace chocolatey.infrastructure.app.nuget public sealed class ChocolateyNugetCredentialProvider : ICredentialProvider { + private static readonly ConcurrentDictionary _resolveDownloadUrls = new ConcurrentDictionary(); private readonly ChocolateyConfiguration _config; private const string INVALID_URL = "http://somewhere123zzaafasd.invalid"; @@ -38,6 +39,28 @@ public ChocolateyNugetCredentialProvider(ChocolateyConfiguration config) _config = config; } + public static void add_download_url_from_package(IPackageName package) + { + if (package == null) + { + return; + } + + var servicePackage = package as DataServicePackage; + + if (servicePackage == null) + { + return; + } + + var sourceUri = get_source_url(servicePackage); + + if (servicePackage != null && sourceUri != null && servicePackage.DownloadUrl != null && !servicePackage.DownloadUrl.ToString().StartsWith(sourceUri.ToString(), StringComparison.InvariantCultureIgnoreCase)) + { + _resolveDownloadUrls.TryAdd(servicePackage.DownloadUrl, sourceUri); + } + } + public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType credentialType, bool retrying) { if (uri == null) @@ -50,6 +73,12 @@ public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType cred this.Log().Warn("Invalid credentials specified."); } + Uri resolveUri; + if (_resolveDownloadUrls.TryGetValue(uri, out resolveUri)) + { + return GetCredentials(resolveUri, proxy, credentialType, retrying); + } + var configSourceUri = new Uri(INVALID_URL); this.Log().Debug(ChocolateyLoggers.Verbose, "Attempting to gather credentials for '{0}'".format_with(uri.OriginalString)); @@ -179,6 +208,26 @@ public ICredentials get_credentials_from_user(Uri uri, IWebProxy proxy, Credenti return credentials; } + + private static Uri get_source_url(IPackage package) + { + var type = package.GetType(); + var property = type.GetProperty("Context", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + + if (property == null) + { + return null; + } + + var context = property.GetValue(package, new object[0]) as IDataServiceContext; + + if (context == null) + { + return null; + } + + return context.BaseUri; + } } // ReSharper restore InconsistentNaming diff --git a/src/chocolatey/infrastructure.app/nuget/ChocolateyPackageDownloader.cs b/src/chocolatey/infrastructure.app/nuget/ChocolateyPackageDownloader.cs new file mode 100644 index 0000000000..27a1a7c985 --- /dev/null +++ b/src/chocolatey/infrastructure.app/nuget/ChocolateyPackageDownloader.cs @@ -0,0 +1,39 @@ +// Copyright © 2017 - 2025 Chocolatey Software, Inc +// Copyright © 2011 - 2017 RealDimensions Software, LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +namespace chocolatey.infrastructure.app.nuget +{ + using System; + using System.IO; + using NuGet; + + internal class ChocolateyPackageDownloader : PackageDownloader + { + public override void DownloadPackage(Uri uri, IPackageMetadata package, Stream targetStream) + { + ChocolateyNugetCredentialProvider.add_download_url_from_package(package); + + base.DownloadPackage(uri, package, targetStream); + } + + public override void DownloadPackage(IHttpClient downloadClient, IPackageName package, Stream targetStream) + { + ChocolateyNugetCredentialProvider.add_download_url_from_package(package); + + base.DownloadPackage(downloadClient, package, targetStream); + } + } +} diff --git a/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs b/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs index 169265df00..e23fe79e1b 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetCommon.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2025 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -173,7 +173,7 @@ public static IPackageRepository GetRemoteRepository(ChocolateyConfiguration con // keep this here for the licensed edition for now public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, Action installSuccessAction, Action uninstallSuccessAction, bool addUninstallHandler) { - return GetPackageManager(configuration, nugetLogger, new PackageDownloader(), installSuccessAction, uninstallSuccessAction, addUninstallHandler); + return GetPackageManager(configuration, nugetLogger, new ChocolateyPackageDownloader(), installSuccessAction, uninstallSuccessAction, addUninstallHandler); } public static IPackageManager GetPackageManager(ChocolateyConfiguration configuration, ILogger nugetLogger, IPackageDownloader packageDownloader, Action installSuccessAction, Action uninstallSuccessAction, bool addUninstallHandler) diff --git a/src/chocolatey/infrastructure.app/nuget/NugetList.cs b/src/chocolatey/infrastructure.app/nuget/NugetList.cs index ee55cc7b1b..e1703b6cf7 100644 --- a/src/chocolatey/infrastructure.app/nuget/NugetList.cs +++ b/src/chocolatey/infrastructure.app/nuget/NugetList.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2025 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -40,7 +40,7 @@ public static int GetCount(ChocolateyConfiguration configuration, ILogger nugetL private static IQueryable execute_package_search(ChocolateyConfiguration configuration, ILogger nugetLogger) { - var packageRepository = NugetCommon.GetRemoteRepository(configuration, nugetLogger, new PackageDownloader()); + var packageRepository = NugetCommon.GetRemoteRepository(configuration, nugetLogger, new ChocolateyPackageDownloader()); var searchTermLower = configuration.Input.to_lower(); // Whether or not the package is remote determines two things: diff --git a/src/chocolatey/infrastructure.app/registration/ChocolateyRegistrationModule.cs b/src/chocolatey/infrastructure.app/registration/ChocolateyRegistrationModule.cs index 8436e7e31f..119c5bd32f 100644 --- a/src/chocolatey/infrastructure.app/registration/ChocolateyRegistrationModule.cs +++ b/src/chocolatey/infrastructure.app/registration/ChocolateyRegistrationModule.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2022 Chocolatey Software, Inc +// Copyright © 2017 - 2025 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -44,7 +44,7 @@ public void register_dependencies(IContainerRegistrator registrator, ChocolateyC //nuget registrator.register_service(); registrator.register_service(); - registrator.register_service(); + registrator.register_service(); registrator.register_service(); registrator.register_service(); registrator.register_service(); diff --git a/src/chocolatey/infrastructure.app/services/TemplateService.cs b/src/chocolatey/infrastructure.app/services/TemplateService.cs index 9e6d2355d2..db91736937 100644 --- a/src/chocolatey/infrastructure.app/services/TemplateService.cs +++ b/src/chocolatey/infrastructure.app/services/TemplateService.cs @@ -1,4 +1,4 @@ -// Copyright © 2017 - 2021 Chocolatey Software, Inc +// Copyright © 2017 - 2025 Chocolatey Software, Inc // Copyright © 2011 - 2017 RealDimensions Software, LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -221,7 +221,7 @@ public void list_noop(ChocolateyConfiguration configuration) public void list(ChocolateyConfiguration configuration) { var packageManager = NugetCommon.GetPackageManager(configuration, _nugetLogger, - new PackageDownloader(), + new ChocolateyPackageDownloader(), installSuccessAction: null, uninstallSuccessAction: null, addUninstallHandler: false);