コード例 #1
0
ファイル: Rules.cs プロジェクト: ywy330/XWall
        //static void updateNewRuleSubmitToggleFile() {
        //    File.WriteAllText(App.AppDataDirectory + settings.ConfigsFolderName + settings.SubmitNewRuleToggleFileName, settings.SubmitNewRule.ToString().ToLower());
        //    Operation.GrantAccessControl(App.AppDataDirectory + settings.ConfigsFolderName + settings.SubmitNewRuleToggleFileName);
        //}

        public static void GenerateActionFile()
        {
            /* !DEBUG CODE
             * return;
             * //*/

            if (generatingActionFile)
            {
                return;
            }
            generatingActionFile = true;

            new Action(() => {
                string forwardSettings;

                switch (settings.ProxyType)
                {
                case "GA":
                    forwardSettings = "forward 127.0.0.1:" + settings.GaPort;
                    break;

                case "SSH":
                    forwardSettings = "forward-socks5 127.0.0.1:" + settings.SshSocksPort + " .";
                    break;

                case "HTTP":
                    forwardSettings = "forward " + (settings.HttpServer != "" ? settings.HttpServer + ":" : "127.0.0.1:") + settings.HttpPort;
                    break;

                case "SOCKS5":
                    forwardSettings = "forward-socks5 " + (settings.SocksServer != "" ? settings.SocksServer + ":" : "127.0.0.1:") + settings.SocksPort + " .";
                    break;

                default:
                    forwardSettings = "";
                    break;
                }

                //var defaultProxy = Operation.Proxies.DefaultProxy;
                var defaultForwardSettings = "forward " + (settings.UseIntranetProxy ? (String.IsNullOrEmpty(settings.IntranetProxyServer) ? "127.0.0.1" : settings.IntranetProxyServer) + ":" + settings.IntranetProxyPort : ".");

                string onlineForwardText = "";
                string onlineDefaultText = "";
                string customForwardText = "";
                string customDefaultText = "";

                if (enabled)
                {
                    if (settings.ForwardAll)
                    {
                        onlineForwardText =
                            "# Forward all\r\n" +
                            "{+forward-override{" + forwardSettings + "}}" + "\r\n" +
                            "/";
                    }
                    else
                    {
                        if (settings.UseOnlineRules && File.Exists(App.AppDataDirectory + settings.ConfigsFolderName + settings.OnlineRulesFileName))
                        {
                            var onlineRulesText = File.ReadAllText(App.AppDataDirectory + settings.ConfigsFolderName + settings.OnlineRulesFileName);
                            var parts           = onlineRulesText.Split(new string[] { "#-- separator --#" }, StringSplitOptions.None);
                            if (parts.Length == 2)
                            {
                                onlineForwardText = parts[0].Trim().Replace("$forward-settings$", forwardSettings);
                                onlineDefaultText = parts[1].Trim().Replace("$default-forward-settings$", defaultForwardSettings);
                            }
                        }

                        var matches = new Regex(@".+").Matches(settings.CustomRules);

                        var forwardRules      = new List <string>();
                        var doNotForwardRules = new List <string>();

                        foreach (Match match in matches)
                        {
                            if (match.Value.StartsWith("!"))
                            {
                                doNotForwardRules.Add(match.Value.Substring(1));
                            }
                            else
                            {
                                forwardRules.Add(match.Value);
                            }
                        }

                        if (forwardRules.Count > 0)
                        {
                            customForwardText =
                                "{+forward-override{" + forwardSettings + "}}" + "\r\n" +
                                String.Join("\r\n", forwardRules.ToArray());
                        }

                        customDefaultText =
                            "{+forward-override{" + defaultForwardSettings + "}}" + "\r\n" +
                            "null" + "\r\n" +
                            String.Join("\r\n", doNotForwardRules.ToArray());
                    }
                }

                File.WriteAllText(App.AppDataDirectory + settings.ConfigsFolderName + settings.PrivoxyOnlineForwardActionFileName, onlineForwardText);
                File.WriteAllText(App.AppDataDirectory + settings.ConfigsFolderName + settings.PrivoxyOnlineDefaultActionFileName, onlineDefaultText);
                File.WriteAllText(App.AppDataDirectory + settings.ConfigsFolderName + settings.PrivoxyCustomForwardActionFileName, customForwardText);
                File.WriteAllText(App.AppDataDirectory + settings.ConfigsFolderName + settings.PrivoxyCustomDefaultActionFileName, customDefaultText);

                Operation.GrantAccessControl(App.AppDataDirectory + settings.ConfigsFolderName + settings.PrivoxyOnlineForwardActionFileName);
                Operation.GrantAccessControl(App.AppDataDirectory + settings.ConfigsFolderName + settings.PrivoxyOnlineDefaultActionFileName);
                Operation.GrantAccessControl(App.AppDataDirectory + settings.ConfigsFolderName + settings.PrivoxyCustomForwardActionFileName);
                Operation.GrantAccessControl(App.AppDataDirectory + settings.ConfigsFolderName + settings.PrivoxyCustomDefaultActionFileName);

                generatingActionFile = false;
            }).BeginInvoke(null, null);
        }