-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1741 from ruhullahshah/add_support_for_proxy_feature
Add support for custom proxy credentials using the same policy as adopted by Paket
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.0
- 6.0.0-beta001
- 6.0.0-alpha004
- 6.0.0-alpha003
- 6.0.0-alpha002
- 6.0.0-alpha001
- 5.23.1
- 5.23.0
- 5.23.0-alpha002
- 5.23.0-alpha001
- 5.22.0
- 5.21.1
- 5.21.0
- 5.21.0-alpha004
- 5.21.0-alpha003
- 5.20.4
- 5.20.4-alpha.1658
- 5.20.4-alpha.1642
- 5.20.3
- 5.20.2
- 5.20.1
- 5.20.0
- 5.20.0-alpha.1584
- 5.20.0-alpha.1583
- 5.20.0-alpha.1580
- 5.20.0-alpha.1566
- 5.20.0-alpha.1545
- 5.19.2-alpha.1526
- 5.19.1
- 5.19.0
- 5.19.0-alpha.1473
- 5.19.0-alpha.1439
- 5.18.3
- 5.18.2
- 5.18.1
- 5.18.0
- 5.17.0
- 5.16.2-alpha.1304
- 5.16.1
- 5.16.0
- 5.16.0-alpha.1228
- 5.15.4
- 5.15.3
- 5.15.2
- 5.15.1
- 5.15.1-alpha.1104
- 5.15.0
- 5.14.1
- 5.14.0
- 5.14.0-alpha.1085
- 5.13.7
- 5.13.5
- 5.13.3
- 5.13.2
- 5.13.1
- 5.13.0
- 5.13.0-alpha.987
- 5.13.0-alpha.975
- 5.12.6
- 5.12.4
- 5.12.1
- 5.12.0
- 5.11.1
- 5.11.0
- 5.10.1
- 5.9.3
- 5.9.2
- 5.9.1
- 5.9.0
- 5.8.5
- 5.8.4
- 5.7.2
- 5.7.0
- 5.6.2-alpha.494
- 5.6.2-alpha.491
- 5.6.1
- 5.6.0
- 5.5.1-alpha.403
- 5.5.0
- 5.4.1
- 5.4.0
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 5.0.0-rc018.248
- 5.0.0-rc018-244
- 5.0.0-rc017.237
- 5.0.0-rc016.225
- 5.0.0-rc015.196
- 5.0.0-rc014.167
- 5.0.0-rc013.141
- 5.0.0-rc012.95
- 5.0.0-rc012.95+vsts.617e5c3e3ac66e15fd822f03d531fe4dce04dd85
- 5.0.0-rc011
- 5.0.0-rc010
- 5.0.0-rc009
- 5.0.0-rc008
- 5.0.0-rc007
- 5.0.0-rc006
- 5.0.0-rc005
- 5.0.0-rc004
- 5.0.0-rc001
- 5.0.0-beta029
- 5.0.0-beta028
- 5.0.0-beta027
- 5.0.0-beta026
- 5.0.0-beta025
- 5.0.0-beta024
- 5.0.0-beta023
- 5.0.0-beta022
- 5.0.0-beta021
- 5.0.0-beta020
- 5.0.0-beta019
- 5.0.0-beta018
- 5.0.0-beta017
- 5.0.0-beta016
- 5.0.0-beta015
- 5.0.0-beta015-real
- 5.0.0-beta014
- 5.0.0-beta013
- 5.0.0-beta012
- 5.0.0-beta011
Showing
3 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
module Fake.Utils | ||
|
||
open System | ||
open System.Net | ||
|
||
//Using the same code as https://github.com/fsprojects/Paket for Memoization & Proxy handling | ||
let inline internal memoizeByExt (getKey : 'a -> 'key) (f: 'a -> 'b) : ('a -> 'b) * ('key * 'b -> unit) = | ||
let cache = System.Collections.Concurrent.ConcurrentDictionary<'key, 'b>() | ||
(fun (x: 'a) -> | ||
cache.GetOrAdd(getKey x, fun _ -> f x)), | ||
(fun (key, c) -> | ||
cache.TryAdd(key, c) |> ignore) | ||
|
||
let inline internal memoizeBy (getKey : 'a -> 'key) (f: 'a -> 'b) : ('a -> 'b) = | ||
memoizeByExt getKey f |> fst | ||
|
||
let inline internal memoize (f: 'a -> 'b) : 'a -> 'b = memoizeBy id f | ||
|
||
let envProxies () = | ||
let getEnvValue (name:string) = | ||
let v = Environment.GetEnvironmentVariable(name.ToUpperInvariant()) | ||
// under mono, env vars are case sensitive | ||
if isNull v then Environment.GetEnvironmentVariable(name.ToLowerInvariant()) else v | ||
let bypassList = | ||
let noproxyString = getEnvValue "NO_PROXY" | ||
let noproxy = if not (String.IsNullOrEmpty (noproxyString)) then System.Text.RegularExpressions.Regex.Escape(noproxyString).Replace(@"*", ".*") else noproxyString | ||
|
||
if String.IsNullOrEmpty noproxy then [||] else | ||
noproxy.Split([| ',' |], StringSplitOptions.RemoveEmptyEntries) | ||
let getCredentials (uri:Uri) = | ||
let userPass = uri.UserInfo.Split([| ':' |], 2) | ||
if userPass.Length <> 2 || userPass.[0].Length = 0 then None else | ||
let credentials = NetworkCredential(Uri.UnescapeDataString userPass.[0], Uri.UnescapeDataString userPass.[1]) | ||
Some credentials | ||
|
||
let getProxy (scheme:string) = | ||
let envVarName = sprintf "%s_PROXY" (scheme.ToUpperInvariant()) | ||
let envVarValue = getEnvValue envVarName | ||
if isNull envVarValue then None else | ||
match Uri.TryCreate(envVarValue, UriKind.Absolute) with | ||
| true, envUri -> | ||
#if NETSTANDARD1_6 | ||
Some | ||
{ new IWebProxy with | ||
member __.Credentials | ||
with get () = (Option.toObj (getCredentials envUri)) :> ICredentials | ||
and set value = () | ||
member __.GetProxy _ = | ||
Uri (sprintf "http://%s:%d" envUri.Host envUri.Port) | ||
member __.IsBypassed (host : Uri) = | ||
Array.contains (string host) bypassList | ||
} | ||
#else | ||
let proxy = WebProxy (Uri (sprintf "http://%s:%d" envUri.Host envUri.Port)) | ||
proxy.Credentials <- Option.toObj (getCredentials envUri) | ||
proxy.BypassProxyOnLocal <- true | ||
proxy.BypassList <- bypassList | ||
Some proxy | ||
#endif | ||
| _ -> None | ||
|
||
let addProxy (map:Map<string, WebProxy>) scheme = | ||
match getProxy scheme with | ||
| Some p -> Map.add scheme p map | ||
| _ -> map | ||
|
||
[ "http"; "https" ] | ||
|> List.fold addProxy Map.empty | ||
|
||
let calcEnvProxies = lazy (envProxies()) | ||
|
||
let getDefaultProxyForUrl = | ||
memoize | ||
(fun (url:string) -> | ||
let uri = Uri url | ||
let getDefault () = | ||
#if NETSTANDARD1_6 | ||
let result = WebRequest.DefaultWebProxy | ||
#else | ||
let result = WebRequest.GetSystemWebProxy() | ||
#endif | ||
#if NETSTANDARD1_6 | ||
let proxy = result | ||
#else | ||
let address = result.GetProxy uri | ||
if address = uri then null else | ||
let proxy = WebProxy address | ||
proxy.BypassProxyOnLocal <- true | ||
#endif | ||
proxy.Credentials <- CredentialCache.DefaultCredentials | ||
proxy | ||
|
||
match calcEnvProxies.Force().TryFind uri.Scheme with | ||
| Some p -> if p.GetProxy uri <> uri then p else getDefault() | ||
| None -> getDefault()) |