Ejemplo n.º 1
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete the selected rule(s)?", "Confirm Delete!", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }
            //return;
            int count = 0;

            foreach (ListViewItem item in listRules.SelectedItems)
            {
                ProxyRule rule = new ProxyRule();
                rule.Direction     = item.SubItems[0].Text;
                rule.Listenaddress = item.SubItems[1].Text;
                rule.Listenport    = item.SubItems[2].Text;

                if (proxyDal.DeleteRule(rule))
                {
                    int index = boundRules.IndexOf(rule);
                    boundRules.RemoveAt(index);
                    count++;
                }
                else
                {
                    MessageBox.Show("Failed to remove rule " + rule.ToShortString());
                }
            }
            if (count > 0)
            {
                ListViewReload();
            }
        }
Ejemplo n.º 2
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool valid = ValidateListenaddress() &&
                         ValidateListenport() &&
                         ValidateConnectaddress() &&
                         ValidateConnectport();

            if (!valid)
            {
                return;
            }
            ProxyRule rule = new ProxyRule();

            rule.Direction      = cmbDirection.Text;
            rule.Listenaddress  = txtListenaddress.Text;
            rule.Listenport     = txtListenport.Text;
            rule.Connectaddress = txtConnectaddress.Text;
            rule.Connectport    = txtConnectport.Text;
            rule.Protocol       = cmbProtocol.Text;
            string errorMessage = AddOrChangeRule(rule);

            if (string.IsNullOrEmpty(errorMessage))
            {
                ListViewReload();
            }
            else
            {
                MessageBox.Show(errorMessage);
            }
        }
Ejemplo n.º 3
0
        private string AddOrChangeRule(ProxyRule rule)
        {
            if (rule.Listenaddress == rule.Connectaddress && rule.Listenport == rule.Connectport)
            {
                return("Connect address:port cannot be the same as listen address:port. for rule\r\n    " + rule.ToString());
            }
            int index = boundRules.IndexOf(rule);

            if (index == -1)
            {
                if (proxyDal.AddRule(rule))
                {
                    boundRules.Add(rule);
                    return("");
                }
                else
                {
                    return("Failed to add rule " + rule.ToString());
                }
            }
            else
            {
                if (proxyDal.SetRule(rule))
                {
                    boundRules[index] = rule;
                    return("");
                }
                else
                {
                    return("Failed to update rule " + rule.ToShortString());
                }
            }
        }
Ejemplo n.º 4
0
        public override bool Equals(Object obj)
        {
            ProxyRule that = obj as ProxyRule;

            if (that == null)
            {
                return(false);
            }
            return(Direction == that.Direction && Listenaddress == that.Listenaddress && Listenport == that.Listenport);
        }
Ejemplo n.º 5
0
        public static ProxyRule FromSubitems(string[] items)
        {
            ProxyRule rule = new ProxyRule();

            rule.Direction      = items[0];
            rule.Listenaddress  = items[1];
            rule.Listenport     = items[2];
            rule.Connectaddress = items[3];
            rule.Connectport    = items[4];
            rule.Protocol       = items[5];
            return(rule);
        }
Ejemplo n.º 6
0
 public static void WriteRuleElem(ProxyRule rule, XmlTextWriter writer)
 {
     writer.WriteStartElement("rule");
     writer.WriteAttributeString("direction", rule.Direction);
     writer.WriteAttributeString("protocol", rule.Protocol);
     writer.WriteStartElement("listen");
     writer.WriteAttributeString("address", rule.Listenaddress);
     writer.WriteAttributeString("port", rule.Listenport);
     writer.WriteEndElement();
     writer.WriteStartElement("connect");
     writer.WriteAttributeString("address", rule.Connectaddress);
     writer.WriteAttributeString("port", rule.Connectport);
     writer.WriteEndElement();
     writer.WriteEndElement();
 }
Ejemplo n.º 7
0
        private void listRules_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnDelete.Enabled = listRules.SelectedItems.Count > 0;
            if (listRules.SelectedItems.Count == 0)
            {
                return;
            }
            ListViewItem item = listRules.SelectedItems[0];

            string[]  arr  = ToStringArray(item.SubItems);
            ProxyRule rule = ProxyRule.FromSubitems(arr);

            SelectByValue(cmbDirection, rule.Direction);
            txtListenaddress.Text  = rule.Listenaddress;
            txtListenport.Text     = rule.Listenport;
            txtConnectaddress.Text = rule.Connectaddress;
            txtConnectport.Text    = rule.Connectport;
            SelectByValue(cmbProtocol, rule.Protocol);
            errorProvider1.Clear();
        }
Ejemplo n.º 8
0
        public List <ProxyRule> ReadRulesFromFile(string pathname)
        {
            List <ProxyRule> rules  = new List <ProxyRule>();
            XmlDocument      xmlDoc = new XmlDocument();

            xmlDoc.Load(pathname);
            XmlNodeList ruleNodes = xmlDoc.DocumentElement.SelectNodes("/portproxy/rule");

            foreach (XmlNode ruleNode in ruleNodes)
            {
                ProxyRule rule = new ProxyRule();
                rule.Direction = ruleNode.Attributes["direction"].Value;
                rule.Protocol  = ruleNode.Attributes["protocol"].Value;
                XmlNode listenElem = ruleNode.SelectSingleNode("listen");
                rule.Listenaddress = listenElem.Attributes["address"].Value;
                rule.Listenport    = listenElem.Attributes["port"].Value;
                XmlNode connectElem = ruleNode.SelectSingleNode("connect");
                rule.Connectaddress = connectElem.Attributes["address"].Value;
                rule.Connectport    = connectElem.Attributes["port"].Value;
                rules.Add(rule);
            }
            return(rules);
        }
Ejemplo n.º 9
0
        public List <ProxyRule> GetRules()
        {
            List <ProxyRule> rules = new List <ProxyRule>();

            foreach (string direction in directions)
            {
                ExecResult result = ExecCommand("netsh", "interface portproxy show " + direction);
                Match      m      = regex.Match(result.output);
                while (m.Success)
                {
                    ProxyRule rule = new ProxyRule();
                    rule.Direction      = direction;
                    rule.Listenaddress  = m.Groups[1].Captures[0].Value;
                    rule.Listenport     = m.Groups[2].Captures[0].Value;
                    rule.Connectaddress = m.Groups[3].Captures[0].Value;
                    rule.Connectport    = m.Groups[4].Captures[0].Value;
                    rule.Protocol       = "tcp";
                    rules.Add(rule);
                    Console.WriteLine(rule.ToString());
                    m = m.NextMatch();
                }
            }
            return(rules);
        }
Ejemplo n.º 10
0
        public bool SetRule(ProxyRule rule)
        {
            ExecResult result = ExecCommand("netsh", "interface portproxy set " + rule.ToString());

            return(result.code == 0);
        }
Ejemplo n.º 11
0
        public bool DeleteRule(ProxyRule rule)
        {
            ExecResult result = ExecCommand("netsh", "interface portproxy delete " + rule.ToShortString());

            return(result.code == 0);
        }