public NewRuleBlockingDialog(IServiceProvider serviceProvider, InboundFeature rewriteFeature)
            : base(serviceProvider)
        {
            InitializeComponent();
            cbMode.SelectedIndex     = 1;
            cbInput.SelectedIndex    = 0;
            cbMatch.SelectedIndex    = 0;
            cbResponse.SelectedIndex = 1;

            var container = new CompositeDisposable();

            FormClosed += (sender, args) => container.Dispose();

            container.Add(
                Observable.FromEventPattern <EventArgs>(cbInput, "SelectedIndexChanged")
                .Merge(Observable.FromEventPattern <EventArgs>(cbMode, "SelectedIndexChanged"))
                .ObserveOn(System.Threading.SynchronizationContext.Current)
                .Subscribe(evt =>
            {
                switch (cbInput.SelectedIndex)
                {
                case 0:
                    lblPattern.Text = "Pattern (URL Path):";
                    lblExample.Text = cbMode.SelectedIndex == 1 ? "Example: IMG*.jpg" : "Example: ^IMG.*\\.jpg$";
                    break;

                case 1:
                    lblPattern.Text = "Pattern (User-agent Header):";
                    lblExample.Text = cbMode.SelectedIndex == 1 ? "Example: Mozilla/4*" : "Example: ^Mozilla/[1234].*";
                    break;

                case 2:
                    lblPattern.Text = "Pattern (IP Address):";
                    lblExample.Text = cbMode.SelectedIndex == 1 ? "Exmaple: 192.168.1.*" : "Example: 192\\.168\\.1\\.[1-9]";
                    break;

                case 3:
                    lblPattern.Text = "Pattern (Query String):";
                    lblExample.Text = cbMode.SelectedIndex == 1 ? "Example: *id=*&p=*" : "Example: id=[0-9]+&p=[a-z]+";
                    break;

                case 4:
                    lblPattern.Text = "Pattern (Referer):";
                    lblExample.Text = cbMode.SelectedIndex == 1 ? "Example: http://*.consoto.com" : "Example: http://(?:www\\.)?contoso\\.com$";
                    break;

                case 5:
                    lblPattern.Text = "Pattern (Host Header):";
                    lblExample.Text = cbMode.SelectedIndex == 1 ? "Example: *.consoto.com" : "Example: (?:www\\.)?contoso\\.com$";
                    break;
                }
            }));

            container.Add(
                Observable.FromEventPattern <EventArgs>(txtPattern, "TextChanged")
                .Sample(TimeSpan.FromSeconds(0.5))
                .ObserveOn(System.Threading.SynchronizationContext.Current)
                .Subscribe(evt =>
            {
                btnOK.Enabled = !string.IsNullOrWhiteSpace(txtPattern.Text);
            }));

            container.Add(
                Observable.FromEventPattern <EventArgs>(btnOK, "Click")
                .ObserveOn(System.Threading.SynchronizationContext.Current)
                .Subscribe(evt =>
            {
                var inputType = cbInput.SelectedIndex;
                var pattern   = txtPattern.Text;
                var match     = cbMatch.SelectedIndex;
                var response  = cbResponse.SelectedIndex;
                var mode      = cbMode.SelectedIndex;

                int index = 0;
                string name;
                do
                {
                    index++;
                    name = string.Format("RequestBlockingRule{0}", index);
                }while (rewriteFeature.Items.All(item => item.Name != name));
                var newRule               = new InboundRule(null);
                newRule.Name              = name;
                newRule.Input             = "URL Path";
                newRule.Enabled           = true;
                newRule.PatternSyntax     = mode == 0 ? 0L : 1L;
                newRule.PatternUrl        = mode == 0 ? ".*" : "*";
                newRule.Type              = response == 3 ? 4L : 3L;
                newRule.ActionUrl         = "{C:1}";
                newRule.RedirectType      = 301;
                newRule.StatusCode        = GetStatusCode(response);
                newRule.SubStatusCode     = 0;
                newRule.StatusReason      = GetReason(response);
                newRule.StatusDescription = GetMessage(response);
                newRule.Conditions.Add(
                    new ConditionItem(null)
                {
                    Input      = GetInput(inputType),
                    MatchType  = match == 0 ? 4 : 5,
                    Pattern    = pattern,
                    IgnoreCase = true
                });
                rewriteFeature.AddItem(newRule);
                DialogResult = DialogResult.OK;
            }));
        }
Пример #2
0
        public NewRuleWithRewriteMapsDialog(IServiceProvider serviceProvider, InboundFeature feature)
            : base(serviceProvider)
        {
            InitializeComponent();
            var service      = (IConfigurationService)GetService(typeof(IConfigurationService));
            var rulesSection = service.GetSection("system.webServer/rewrite/rewriteMaps");
            ConfigurationElementCollection rulesCollection = rulesSection.GetCollection();

            foreach (ConfigurationElement ruleElement in rulesCollection)
            {
                cbMap.Items.Add(ruleElement["name"]);
            }

            cbAction.SelectedIndex = 0;

            var container = new CompositeDisposable();

            FormClosed += (sender, args) => container.Dispose();

            container.Add(
                Observable.FromEventPattern <EventArgs>(cbMap, "SelectedIndexChanged")
                .ObserveOn(System.Threading.SynchronizationContext.Current)
                .Subscribe(evt =>
            {
                btnOK.Enabled = cbMap.SelectedIndex > -1;
            }));

            container.Add(
                Observable.FromEventPattern <EventArgs>(btnOK, "Click")
                .ObserveOn(System.Threading.SynchronizationContext.Current)
                .Subscribe(evt =>
            {
                var action = cbAction.Text;
                var map    = cbMap.Text;

                int index = 0;
                string name;
                do
                {
                    index++;
                    name = string.Format("{0} rule{1} for {2}", action, index, map);
                }while (feature.Items.All(item => item.Name != name));
                var rule           = new InboundRule(null);
                rule.Name          = name;
                rule.Input         = "URL Path";
                rule.PatternSyntax = 0L;
                rule.PatternUrl    = ".*";
                rule.Type          = action == "Rewrite" ? 1L : 2L;
                rule.ActionUrl     = "{C:1}";
                rule.RedirectType  = 301;
                rule.Conditions.Add(
                    new ConditionItem(null)
                {
                    Input      = string.Format("{{{0}:{{REQUEST_URI}}}}", map),
                    MatchType  = 4,
                    Pattern    = "(.+)",
                    IgnoreCase = true
                });

                feature.AddItem(rule);
                DialogResult = DialogResult.OK;
            }));

            container.Add(
                Observable.FromEventPattern <EventArgs>(cbAction, "SelectedIndexChanged")
                .ObserveOn(System.Threading.SynchronizationContext.Current)
                .Subscribe(evt =>
            {
                lblDescription.Text = cbAction.SelectedIndex == 0 ? s_rewriteText : s_redirectText;
            }));
        }