private Uri PrefixProxy(string url) { if (string.IsNullOrEmpty(ProxyUrl)) { return(new Uri(url, UriKind.RelativeOrAbsolute)); } string proxyUrl = ProxyUrl; if (!proxyUrl.Contains("?")) { if (!proxyUrl.EndsWith("?")) { proxyUrl = ProxyUrl + "?"; } } else { if (!proxyUrl.EndsWith("&")) { proxyUrl = ProxyUrl + "&"; } } if (ProxyUrl.StartsWith("~") || ProxyUrl.StartsWith("../")) //relative to xap root { string uri = Application.Current.Host.Source.AbsoluteUri; int count = proxyUrl.Split(new string[] { "../" }, StringSplitOptions.None).Length; for (int i = 0; i < count; i++) { uri = uri.Substring(0, uri.LastIndexOf("/")); } if (!uri.EndsWith("/")) { uri += "/"; } proxyUrl = uri + proxyUrl.Replace("~", "").Replace("../", ""); } else if (ProxyUrl.StartsWith("/")) //relative to domain root { proxyUrl = ProxyUrl.Replace("/", string.Format("{0}://{1}:{2}", Application.Current.Host.Source.Scheme, Application.Current.Host.Source.Host, Application.Current.Host.Source.Port)); } UriBuilder b = new UriBuilder(proxyUrl); b.Query = url; return(b.Uri); }
protected void reply(object data) { try { if (data == null) { return; } object replyData = (data is object[]) ? data : new object[] { data }; string replyStr = jsonSerializer.Serialize(replyData); string result = new WebClient().UploadString(ProxyUrl.ToString() + proxyReplyPath + "?sharedkey=" + _sharedKeyUrlEncoded, replyStr); } catch (Exception ex) { State = TournamentProxyState.Error; Program.logException(ex, Resources.ProxyReplyFailed); } }
public void ApplyProxySettings() { Trace.Entering(); if (_proxySettingsApplied) { return; } string proxyConfigFile = IOUtil.GetProxyConfigFilePath(); if (File.Exists(proxyConfigFile)) { // we expect the first line of the file is the proxy url Trace.Verbose($"Try read proxy setting from file: {proxyConfigFile}."); ProxyUrl = File.ReadLines(proxyConfigFile).FirstOrDefault() ?? string.Empty; ProxyUrl = ProxyUrl.Trim(); Trace.Verbose($"{ProxyUrl}"); } if (string.IsNullOrEmpty(ProxyUrl)) { Trace.Verbose("Try read proxy setting from environment variable: 'VSTS_HTTP_PROXY'."); ProxyUrl = Environment.GetEnvironmentVariable("VSTS_HTTP_PROXY") ?? string.Empty; ProxyUrl = ProxyUrl.Trim(); Trace.Verbose($"{ProxyUrl}"); } if (!string.IsNullOrEmpty(ProxyUrl) && !Uri.IsWellFormedUriString(ProxyUrl, UriKind.Absolute)) { Trace.Info($"The proxy url is not a well formed absolute uri string: {ProxyUrl}."); ProxyUrl = string.Empty; } if (!string.IsNullOrEmpty(ProxyUrl)) { Trace.Info($"Config proxy at: {ProxyUrl}."); string username = Environment.GetEnvironmentVariable("VSTS_HTTP_PROXY_USERNAME"); string password = Environment.GetEnvironmentVariable("VSTS_HTTP_PROXY_PASSWORD"); if (!string.IsNullOrEmpty(password)) { var secretMasker = HostContext.GetService <ISecretMasker>(); secretMasker.AddValue(password); } if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password)) { Trace.Info($"Config proxy use DefaultNetworkCredentials."); _proxyCredential = CredentialCache.DefaultNetworkCredentials; } else { Trace.Info($"Config authentication proxy as: {username}."); _proxyCredential = new NetworkCredential(username, password); } VssHttpMessageHandler.DefaultWebProxy = new WebProxy(new Uri(ProxyUrl)) { Credentials = _proxyCredential }; _proxySettingsApplied = true; } else { Trace.Info($"No proxy setting found."); } }