예제 #1
0
        private static void ConfigGradle(UrlTemplate urlTemplate)
        {
            // Configure gradle
            if (Msdos.IsInstalled("gradle"))
            {
                var gradlePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));

                if (urlTemplate.ClearData)
                {
                    logger.Info("Clearing proxy for gradle");
                    StringBuilder sb = new StringBuilder();
                    File.WriteAllText(Path.Combine(gradlePath, ".gradle.properties"), sb.ToString());
                }
                else
                {
                    logger.Info("Setting proxy for gradle");

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine($"systemProp.http.proxyHost=={urlTemplate.GetHost()}");
                    sb.AppendLine($"systemProp.http.proxyPort={urlTemplate.GetPort()}");
                    sb.AppendLine($"systemProp.http.nonProxyHosts={urlTemplate.GetProxyExceptions()}");
                    sb.AppendLine($"systemProp.https.proxyHost=={urlTemplate.GetHost()}");
                    sb.AppendLine($"systemProp.https.proxyPort={urlTemplate.GetPort()}");
                    sb.AppendLine($"systemProp.https.nonProxyHosts={urlTemplate.GetProxyExceptions()}");

                    File.WriteAllText(Path.Combine(gradlePath, ".gradle.properties"), sb.ToString());
                }
            }
        }
예제 #2
0
        private static void ConfigBower(UrlTemplate urlTemplate)
        {
            // Configure bower
            if (Msdos.IsInstalled("bower.cmd"))
            {
                // bowerrc user file
                var bowerrc = Path.Combine(Windows.GetCurrentUserFolder(), ".bowerrc");

                if (urlTemplate.ClearData)
                {
                    logger.Info("Clearing proxy for bower");
                    Msdos.Run("npm.cmd", $@"config delete proxy");
                    Msdos.Run("npm.cmd", $@"config delete https-proxy");

                    if (File.Exists(bowerrc))
                    {
                        System.IO.File.Delete(bowerrc);
                    }
                }
                else
                {
                    logger.Info("Setting proxy for bower");
                    Msdos.Run("npm.cmd", $@"npm config set proxy {urlTemplate.GetProxy()}");
                    Msdos.Run("npm.cmd", $@"npm config set https-proxy {urlTemplate.GetProxySsl()}");

                    if (!File.Exists(bowerrc))
                    {
                        // create settings
                        var obj = new JObject();
                        obj.Add("proxy", urlTemplate.GetProxy());
                        obj.Add("https-proxy", urlTemplate.GetProxySsl());
                        obj.Add("no-proxy", urlTemplate.GetProxyExceptions());

                        System.IO.File.WriteAllText(bowerrc, obj.ToString());
                    }
                    else
                    {
                        // update settings
                        var content = System.IO.File.ReadAllText(bowerrc);
                        var obj     = JObject.Parse(content);
                        obj["proxy"]       = urlTemplate.GetProxy();
                        obj["https-proxy"] = urlTemplate.GetProxySsl();
                        obj["no-proxy"]    = urlTemplate.GetProxyExceptions();

                        System.IO.File.WriteAllText(bowerrc, obj.ToString());
                    }
                }
            }
        }
예제 #3
0
 private static void ConfigYarn(UrlTemplate urlTemplate)
 {
     // Configure yarn
     if (Msdos.IsInstalled("yarn.cmd"))
     {
         if (urlTemplate.ClearData)
         {
             logger.Info("Clearing proxy for yarn");
             Msdos.Run("yarn.cmd", $@"config delete proxy");
             Msdos.Run("yarn.cmd", $@"config delete https-proxy");
         }
         else
         {
             logger.Info("Setting proxy for yarn");
             Msdos.Run("yarn.cmd", $@"npm config set proxy {urlTemplate.GetProxy()}");
             Msdos.Run("yarn.cmd", $@"npm config set https-proxy {urlTemplate.GetProxySsl()}");
         }
     }
 }
예제 #4
0
 private static void ConfigGit(UrlTemplate urlTemplate)
 {
     // Configure GIT
     if (Msdos.IsInstalled("git.exe", "version"))
     {
         if (urlTemplate.ClearData)
         {
             logger.Info("Clearing proxy for GIT");
             Msdos.Run("git.exe", $"config --global --unset http.proxy");
             Msdos.Run("git.exe", $"config --global --unset https.proxy");
         }
         else
         {
             logger.Info("Setting proxy for GIT");
             Msdos.Run("git.exe", $"config --global http.proxy {urlTemplate.GetProxy()}");
             Msdos.Run("git.exe", $"config --global https.proxy {urlTemplate.GetProxySsl()}");
         }
     }
 }
예제 #5
0
 private static void ConfigNuget(UrlTemplate urlTemplate)
 {
     // Configure nuget
     if (Msdos.IsInstalled("nuget.exe", "help"))
     {
         if (urlTemplate.ClearData)
         {
             logger.Info("Clearing proxy for nuget");
             Msdos.Run("nuget.exe", $@"config -set http_proxy=");
             Msdos.Run("nuget.exe", $@"config -set http_proxy.user="******"nuget.exe", $@"config -set http_proxy.password="******"Setting proxy for nuget");
             Msdos.Run("nuget.exe", $@"config -set http_proxy={urlTemplate.GetProxy(true)}");
             Msdos.Run("nuget.exe", $@"config -set http_proxy.user={urlTemplate.GetProxyUsername()}");
             Msdos.Run("nuget.exe", $@"config -set http_proxy.password={urlTemplate.GetProxyPassword(true)}");
         }
     }
 }
예제 #6
0
        private static void ConfigNpm(UrlTemplate urlTemplate)
        {
            // Configure npm
            if (Msdos.IsInstalled("npm.cmd", "version"))
            {
                if (urlTemplate.ClearData)
                {
                    logger.Info("Clearing proxy for npm");
                    Msdos.Run("npm.cmd", $@"config delete proxy");
                    Msdos.Run("npm.cmd", $@"config delete https-proxy");
                }
                else
                {
                    logger.Info("Setting proxy for npm");
                    Msdos.Run("npm.cmd", $@"config set proxy {urlTemplate.GetProxy()}");
                    Msdos.Run("npm.cmd", $@"config set https-proxy {urlTemplate.GetProxySsl()}");

                    var npmrc = Path.Combine(Windows.GetCurrentUserFolder(), ".npmrc");
                    // npmrc file is managed automatically
                }
            }
        }
예제 #7
0
        private static void ConfigChocolatey(UrlTemplate urlTemplate)
        {
            // Configure CHOCOLATEY
            if (Msdos.IsInstalled("choco.exe", "--version"))
            {
                if (urlTemplate.ClearData)
                {
                    logger.Info("Clearing proxy for chocolatey");
                    Msdos.Run("choco.exe", $@"config unset proxy");
                    Msdos.Run("choco.exe", $@"config unset proxyUser");
                    Msdos.Run("choco.exe", $@"config unset proxyPassword");
                    Msdos.Run("choco.exe", $@"config unset proxyBypassList");
                    Msdos.Run("choco.exe", $@"config unset proxyBypassOnLocal");
                }
                else
                {
                    logger.Info("Setting proxy for chocolatey");
                    Msdos.Run("choco.exe", $@"config set proxy {urlTemplate.GetProxy()}");

                    if (!string.IsNullOrEmpty(urlTemplate.GetProxyUsername()))
                    {
                        Msdos.Run("choco.exe", $@"config set proxyUser {urlTemplate.GetProxyUsername()}");
                    }

                    if (!string.IsNullOrEmpty(urlTemplate.GetProxyPassword()))
                    {
                        Msdos.Run("choco.exe", $@"config set proxyPassword {urlTemplate.GetProxyPassword()}");
                    }

                    if (!string.IsNullOrEmpty(urlTemplate.GetProxyExceptions()))
                    {
                        Msdos.Run("choco.exe", $@"config set proxyBypassList ""{urlTemplate.GetProxyExceptions()}");
                    }

                    Msdos.Run("choco.exe", $@"config set proxyBypassOnLocal true");
                }
            }
        }
예제 #8
0
        public static void Main(string[] args)
        {
            if (!Windows.IsAdministrator())
            {
                logger.Error("You must run this with Administrator permission");
                Environment.Exit(1);
            }

            var            parser = new CommandLineParser.CommandLineParser();
            SwitchArgument clear  = new SwitchArgument('c', "clear", "Clear the configuration", false);

            parser.Arguments.Add(clear);
            parser.ParseCommandLine(args);

            logger.Info("Getting profile");
            var userSettings = GettingProfileUserData();

            if (!clear.Value)
            {
                if (userSettings == null)
                {
                    logger.Error("You must set user data settings using any bellow methods:");
                    logger.Info("APP ARGUMENTS => aplication arguments");
                    logger.Info("APP SETTINGS => ProxyMySystem.exe.config");
                    logger.Info("INI FILE => ProxyMySystem.ini");
                    logger.Info("JSON FILE => ProxyMySystem.json");
                    logger.Info("VARIABLE => environment variables");
                    logger.Info("The most important parameter is: PROXY_HOST then the first data settings that there are this value will be used");
                    Environment.Exit(2);
                }
                else
                {
                    logger.Info("Starting process to configure proxy: " + userSettings.ProxyHost);
                }
            }
            else
            {
                logger.Info("Starting clearing process");
            }

            var urlTemplate = new UrlTemplate(userSettings, clear.Value);

            ConfigInternetSettings(urlTemplate);
            ConfigWindowsCredential(urlTemplate);
            ConfigEnvironmentVariable(urlTemplate);
            ConfigNpm(urlTemplate);
            ConfigGit(urlTemplate);
            ConfigChocolatey(urlTemplate);
            //ConfigYarn(urlTemplate);
            ConfigNuget(urlTemplate);
            ConfigBower(urlTemplate);
            ConfigAndroidSdk(urlTemplate);
            ConfigGradle(urlTemplate);

            // Configure cntlm on localhost
            //var cntlmPath = Path.Combine(Environment.CurrentDirectory, "Lib", "cntlm-0.92.3");
            //var cntlmFiles = Directory.GetFiles(cntlmPath);
            //if (cntlmFiles.Contains(Path.Combine(cntlmPath, "cygrunsrv.exe")) && cntlmFiles.Contains(Path.Combine(cntlmPath, "cntlm.exe")))
            //{
            //    logger.Info("Setting cntlm on localhost");

            //    var existService = Msdos.Run(Path.Combine(cntlmPath, "cygrunsrv"), $@"--query cntlm");
            //    if (!existService.StandardError.Contains("The specified service does not exist as an installed service"))
            //    {
            //        // remove cntlm
            //        var remove = Msdos.Run(Path.Combine(cntlmPath, "cygrunsrv"), $@"--remove cntlm");
            //    }

            //    // Install
            //    var result = Msdos.Run(Path.Combine(cntlmPath, "cygrunsrv"), $@"--install cntlm --path {Path.Combine(cntlmPath, "cntlm.exe")}");
            //    var start = Msdos.Run(Path.Combine(cntlmPath, "cygrunsrv"), $@"--start cntlm");
            //}

            if (clear.Value)
            {
                logger.Info("The system has been cleared");
            }
            else
            {
                logger.Info("The system has been configured");
            }

            var refreshEnv = Msdos.IsInstalled(@"C:\Windows\DEV\Github\proxy-at-work\src\RefreshEnv.cmd");

            if (refreshEnv)
            {
                logger.Info("The environment variables was updated and the new settings were applied.");
            }
            else
            {
                logger.Info("Please, close all instances of Command Prompt or any program that use proxy and open again to reload newest settings");
            }

            Environment.Exit(0);
        }