public static ExecResult ExecCommand(string cmd, string args) { Process p = new Process(); p.StartInfo.FileName = cmd; p.StartInfo.Arguments = args; p.StartInfo.CreateNoWindow = true; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.Start(); string outstr = p.StandardOutput.ReadToEnd(); string errstr = p.StandardError.ReadToEnd(); p.WaitForExit(); int exitCode = p.ExitCode; ExecResult result = new ExecResult(p.ExitCode, outstr, errstr); return(result); }
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); }
public bool SetRule(ProxyRule rule) { ExecResult result = ExecCommand("netsh", "interface portproxy set " + rule.ToString()); return(result.code == 0); }
public bool Reset() { ExecResult result = ExecCommand("netsh", "interface portproxy reset"); return(result.code == 0); }
public bool DeleteRule(ProxyRule rule) { ExecResult result = ExecCommand("netsh", "interface portproxy delete " + rule.ToShortString()); return(result.code == 0); }