Пример #1
0
        public static void InitializeVssClientSettings(IVstsAgentWebProxy proxySetting, IAgentCertificateManager certSetting)
        {
            var headerValues = new List <ProductInfoHeaderValue>();

            headerValues.Add(new ProductInfoHeaderValue($"VstsAgentCore-{BuildConstants.AgentPackage.PackageName}", Constants.Agent.Version));
            headerValues.Add(new ProductInfoHeaderValue($"({RuntimeInformation.OSDescription.Trim()})"));

            if (VssClientHttpRequestSettings.Default.UserAgent != null && VssClientHttpRequestSettings.Default.UserAgent.Count > 0)
            {
                headerValues.AddRange(VssClientHttpRequestSettings.Default.UserAgent);
            }

            VssClientHttpRequestSettings.Default.UserAgent = headerValues;
            VssClientHttpRequestSettings.Default.ClientCertificateManager = certSetting;
            VssHttpMessageHandler.DefaultWebProxy = proxySetting;
        }
Пример #2
0
        private void AddProxySetting(IVstsAgentWebProxy agentProxy)
        {
            string appConfig = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.LegacyPSHost), _appConfigFileName);

            ArgUtil.File(appConfig, _appConfigFileName);

            string appConfigRestore = Path.Combine(HostContext.GetDirectory(WellKnownDirectory.LegacyPSHost), _appConfigRestoreFileName);

            if (!File.Exists(appConfigRestore))
            {
                Trace.Info("Take snapshot of current appconfig for restore modified appconfig.");
                File.Copy(appConfig, appConfigRestore);
            }
            else
            {
                // cleanup any appconfig changes from previous build.
                ExecutionContext.Debug("Restore default LegacyVSTSPowerShellHost.exe.config.");
                IOUtil.DeleteFile(appConfig);
                File.Copy(appConfigRestore, appConfig);
            }

            XmlDocument psHostAppConfig = new XmlDocument();

            using (var appConfigStream = new FileStream(appConfig, FileMode.Open, FileAccess.Read))
            {
                psHostAppConfig.Load(appConfigStream);
            }

            var configuration = psHostAppConfig.SelectSingleNode("configuration");

            ArgUtil.NotNull(configuration, "configuration");

            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)
                {
                    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 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)
                {
                    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);
                }

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

                using (var appConfigStream = new FileStream(appConfig, FileMode.Open, FileAccess.ReadWrite))
                {
                    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.");
            }
        }