Skip to content

Commit

Permalink
try apply proxy only when proxy configured.
Browse files Browse the repository at this point in the history
  • Loading branch information
TingluoHuang committed Sep 7, 2017
1 parent 943b39e commit d03cb64
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
100 changes: 50 additions & 50 deletions src/Agent.Worker/Handlers/LegacyPowerShellHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,11 @@ public async Task RunAsync()
AddPrependPathToEnvironment();

// Add proxy setting to LegacyVSTSPowerShellHost.exe.config
AddProxySetting();
var agentProxy = HostContext.GetService<IVstsAgentWebProxy>();
if (!string.IsNullOrEmpty(agentProxy.ProxyAddress))
{
AddProxySetting(agentProxy);
}

// Invoke the process.
using (var processInvoker = HostContext.CreateService<IProcessInvoker>())
Expand Down Expand Up @@ -376,7 +380,7 @@ protected virtual void AddLegacyHostEnvironmentVariables(string scriptFile, stri
}
}

private void AddProxySetting()
private void AddProxySetting(IVstsAgentWebProxy agentProxy)
{
string appConfig = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.LegacyPSHost), _appConfigFileName);
ArgUtil.File(appConfig, _appConfigFileName);
Expand All @@ -395,68 +399,64 @@ private void AddProxySetting()
File.Copy(appConfigRestore, appConfig);
}

var agentProxy = HostContext.GetService<IVstsAgentWebProxy>();
if (!string.IsNullOrEmpty(agentProxy.ProxyAddress))
XmlDocument psHostAppConfig = new XmlDocument();
using (var appConfigStream = new FileStream(appConfig, FileMode.Open, FileAccess.Read))
{
XmlDocument psHostAppConfig = new XmlDocument();
using (var appConfigStream = new FileStream(appConfig, FileMode.Open, FileAccess.Read))
{
psHostAppConfig.Load(appConfigStream);
}
psHostAppConfig.Load(appConfigStream);
}

var configuration = psHostAppConfig.SelectSingleNode("configuration");
ArgUtil.NotNull(configuration, "configuration");
var configuration = psHostAppConfig.SelectSingleNode("configuration");
ArgUtil.NotNull(configuration, "configuration");

var exist_defaultProxy = psHostAppConfig.SelectSingleNode("configuration/system.net/defaultProxy");
if (exist_defaultProxy == null)
var exist_defaultProxy = psHostAppConfig.SelectSingleNode("configuration/system.net/defaultProxy");
if (exist_defaultProxy == null)
{
var system_net = psHostAppConfig.SelectSingleNode("configuration/system.net");
if (system_net == null)
{
var system_net = psHostAppConfig.SelectSingleNode("configuration/system.net");
if (system_net == null)
{
Trace.Verbose("Create system.net section in appconfg.");
system_net = psHostAppConfig.CreateElement("system.net");
}
Trace.Verbose("Create system.net section in appconfg.");
system_net = psHostAppConfig.CreateElement("system.net");
}

Trace.Verbose("Create defaultProxy section in appconfg.");
var defaultProxy = psHostAppConfig.CreateElement("defaultProxy");
defaultProxy.SetAttribute("useDefaultCredentials", "true");
Trace.Verbose("Create defaultProxy section in appconfg.");
var defaultProxy = psHostAppConfig.CreateElement("defaultProxy");
defaultProxy.SetAttribute("useDefaultCredentials", "true");

Trace.Verbose("Create proxy section in appconfg.");
var proxy = psHostAppConfig.CreateElement("proxy");
proxy.SetAttribute("proxyaddress", agentProxy.ProxyAddress);
proxy.SetAttribute("bypassonlocal", "true");
Trace.Verbose("Create proxy section in appconfg.");
var proxy = psHostAppConfig.CreateElement("proxy");
proxy.SetAttribute("proxyaddress", agentProxy.ProxyAddress);
proxy.SetAttribute("bypassonlocal", "true");

if (agentProxy.ProxyBypassList != null && agentProxy.ProxyBypassList.Count > 0)
if (agentProxy.ProxyBypassList != null && agentProxy.ProxyBypassList.Count > 0)
{
Trace.Verbose("Create bypasslist section in appconfg.");
var bypass = psHostAppConfig.CreateElement("bypasslist");
foreach (string proxyBypass in agentProxy.ProxyBypassList)
{
Trace.Verbose("Create bypasslist section in appconfg.");
var bypass = psHostAppConfig.CreateElement("bypasslist");
foreach (string proxyBypass in agentProxy.ProxyBypassList)
{
Trace.Verbose($"Create bypasslist.add section for {proxyBypass} in appconfg.");
var add = psHostAppConfig.CreateElement("add");
add.SetAttribute("address", proxyBypass);
bypass.AppendChild(add);
}

defaultProxy.AppendChild(bypass);
Trace.Verbose($"Create bypasslist.add section for {proxyBypass} in appconfg.");
var add = psHostAppConfig.CreateElement("add");
add.SetAttribute("address", proxyBypass);
bypass.AppendChild(add);
}

defaultProxy.AppendChild(proxy);
system_net.AppendChild(defaultProxy);
configuration.AppendChild(system_net);
defaultProxy.AppendChild(bypass);
}

using (var appConfigStream = new FileStream(appConfig, FileMode.Open, FileAccess.ReadWrite))
{
psHostAppConfig.Save(appConfigStream);
}
defaultProxy.AppendChild(proxy);
system_net.AppendChild(defaultProxy);
configuration.AppendChild(system_net);

ExecutionContext.Debug("Add Proxy setting in LegacyVSTSPowerShellHost.exe.config file.");
}
else
using (var appConfigStream = new FileStream(appConfig, FileMode.Open, FileAccess.ReadWrite))
{
//proxy setting exist.
ExecutionContext.Debug("Proxy setting already exist in LegacyVSTSPowerShellHost.exe.config file.");
psHostAppConfig.Save(appConfigStream);
}

ExecutionContext.Debug("Add Proxy setting in LegacyVSTSPowerShellHost.exe.config file.");
}
else
{
//proxy setting exist.
ExecutionContext.Debug("Proxy setting already exist in LegacyVSTSPowerShellHost.exe.config file.");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.VisualStudio.Services.Agent/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum OSPlatform

public static class Agent
{
public static readonly string Version = "2.122.0";
public static readonly string Version = "2.122.1";

#if OS_LINUX
public static readonly OSPlatform Platform = OSPlatform.Linux;
Expand Down

0 comments on commit d03cb64

Please sign in to comment.