From 9c6518e246ce0ad7a836bf45d5f228d6cf4dee03 Mon Sep 17 00:00:00 2001 From: Koraniar Date: Fri, 1 May 2020 20:11:10 -0500 Subject: [PATCH] Use credentials for proxy server --- src/Core/WinSWCore/Download.cs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Core/WinSWCore/Download.cs b/src/Core/WinSWCore/Download.cs index f9caff3..897ea5c 100755 --- a/src/Core/WinSWCore/Download.cs +++ b/src/Core/WinSWCore/Download.cs @@ -36,7 +36,7 @@ namespace winsw public readonly string? Password; public readonly bool UnsecureAuth; public readonly bool FailOnError; - public readonly string? ProxyServer; + public readonly string? Proxy; public string ShortId => $"(download from {From})"; @@ -77,12 +77,12 @@ namespace winsw string? username = null, string? password = null, bool unsecureAuth = false, - string? proxyServer = null) + string? proxy = null) { From = from; To = to; FailOnError = failOnError; - ProxyServer = proxyServer; + Proxy = proxy; Auth = auth; Username = username; Password = password; @@ -101,7 +101,7 @@ namespace winsw // All arguments below are optional FailOnError = XmlHelper.SingleAttribute(n, "failOnError", false); - ProxyServer = XmlHelper.SingleAttribute(n, "proxyServer", null); + Proxy = XmlHelper.SingleAttribute(n, "proxy", null); Auth = XmlHelper.EnumAttribute(n, "auth", AuthType.none); Username = XmlHelper.SingleAttribute(n, "user", null); @@ -151,9 +151,27 @@ namespace winsw #endif { WebRequest request = WebRequest.Create(From); - if (!string.IsNullOrEmpty(ProxyServer)) + if (!string.IsNullOrEmpty(Proxy)) { - request.Proxy = new WebProxy(ProxyServer); + if (Proxy.Contains("@")) + { + // Extract proxy credentials + int credsFrom = Proxy.IndexOf("://") + 3; + int credsTo = Proxy.IndexOf("@"); + string completeCredsStr = Proxy.Substring(credsFrom, credsTo - credsFrom); + int credsSeparator = completeCredsStr.IndexOf(":"); + + string proxyAddress = Proxy.Replace(completeCredsStr + "@", ""); + string username = completeCredsStr.Substring(0, credsSeparator); + string password = completeCredsStr.Substring(credsSeparator + 1); + ICredentials credentials = new NetworkCredential(username, password); + + request.Proxy = new WebProxy(proxyAddress, false, null, credentials); + } + else + { + request.Proxy = new WebProxy(Proxy); + } } switch (Auth)