Exemplo n.º 1
0
        public static ISystemProcess ExecuteIptables(NetfilterSystem system, String command, String iptablesBinary)
        {
            ISystemProcess process = system.System.StartProcess(iptablesBinary, command);

            process.WaitForExit();

            //OK
            if (process.ExitCode == 0)
            {
                return(process);
            }

            //ERR: INVALID COMMAND LINE
            if (process.ExitCode == 2)
            {
                throw new IpTablesNetException("IPTables execution failed: Invalid Command Line - " + command);
            }

            //ERR: GENERAL ERROR
            if (process.ExitCode == 1)
            {
                throw new IpTablesNetException("IPTables execution failed: Error - " + command);
            }

            //ERR: UNKNOWN
            throw new IpTablesNetException("IPTables execution failed: Unknown Error - " + command);
        }
Exemplo n.º 2
0
        public override IpTablesChainSet ListRules(String table)
        {
            ISystemProcess process = StartProcess(_iptablesSaveBinary, String.Format("-c -t {0}", table));
            String         toEnd   = process.StandardOutput.ReadToEnd();

            process.WaitForExit();
            return(Helper.IPTablesSaveParser.GetRulesFromOutput(_system, toEnd, table, _ipVersion));
        }
Exemplo n.º 3
0
        public override IpTablesChainSet ListRules(String table)
        {
            ISystemProcess process = _system.System.StartProcess(_iptablesBinary + "-save", String.Format("-c -t {0}", table));
            String         output  = "";

            do
            {
                output += process.StandardOutput.ReadToEnd();
            } while (!process.HasExited);
            process.WaitForExit();
            return(Helper.IPTablesSaveParser.GetRulesFromOutput(_system, output, table, _ipVersion));
        }
Exemplo n.º 4
0
        public bool RestoreSets(IEnumerable <IpSetSet> sets)
        {
            //ipset save
            ISystemProcess process = _system.StartProcess(BinaryName, "restore");

            if (WriteSets(sets, process.StandardInput))
            {
                process.StandardInput.Flush();
                process.StandardInput.Close();
                process.WaitForExit();

                //OK
                if (process.ExitCode != 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 5
0
        private bool ExecuteTransaction()
        {
            //ipset save
            ISystemProcess process = _system.StartProcess(BinaryName, "restore");

            if (WriteStrings(_transactionCommands, process.StandardInput))
            {
                process.StandardInput.Flush();
                process.StandardInput.Close();
                process.WaitForExit();

                //OK
                if (process.ExitCode != 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Exemplo n.º 6
0
        public virtual IpSetSets SaveSets(IpTablesSystem iptables)
        {
            ISystemProcess process = _system.StartProcess(BinaryName, "save");

            IpSetSets sets = new IpSetSets(iptables);

            String[] all = process.StandardOutput.ReadToEnd().Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (String line in all)
            {
                if (String.IsNullOrEmpty(line))
                {
                    break;
                }
                var trimmed = line.Trim();
                if (trimmed.Length != 0)
                {
                    sets.Accept(trimmed, iptables);
                }
            }

            process.WaitForExit();

            return(sets);
        }
Exemplo n.º 7
0
        public static void ReadToEnd(ISystemProcess process, out String output, out String error, int timeout = DefaultTimeout)
        {
            String toutput = "";
            String terror = "";
            DataReceivedEventHandler errorEvent = null, outEvent = null;

            if (process.StartInfo.RedirectStandardError)
            {
                errorEvent = (a, b) => terror += b.Data + "\n";
                process.ErrorDataReceived += errorEvent;
                process.BeginErrorReadLine();
            }
            if (process.StartInfo.RedirectStandardOutput)
            {
                outEvent = (a, b) => toutput += b.Data + "\n";
                process.OutputDataReceived += outEvent;
                process.BeginOutputReadLine();
            }

            if (!process.WaitForExit(timeout * 1000))
            {
                throw new TimeoutException(String.Format("Timeout. Process did not complete executing within {0} seconds", timeout));
            }

            output = toutput;
            error = terror;

            if (errorEvent != null)
            {
                process.ErrorDataReceived -= errorEvent;
            }
            if (outEvent != null)
            {
                process.OutputDataReceived -= outEvent;
            }
        }
Exemplo n.º 8
0
        public override void EndTransactionCommit()
        {
            if (!_inTransaction)
            {
                return;
            }

            ISystemProcess process = StartProcess(_iptablesRestoreBinary, NoFlushOption + " " + NoClearOption);

            if (_builder.WriteOutput(process.StandardInput))
            {
                process.StandardInput.Flush();
                process.StandardInput.Close();
                process.WaitForExit();

                //OK
                if (process.ExitCode != 0)
                {
                    //ERR: INVALID COMMAND LINE
                    if (process.ExitCode == 2)
                    {
                        MemoryStream ms = new MemoryStream();
                        var          sw = new StreamWriter(ms);
                        _builder.WriteOutput(sw);
                        sw.Flush();
                        ms.Seek(0, SeekOrigin.Begin);
                        var sr = new StreamReader(ms);
                        Console.WriteLine(sr.ReadToEnd());
                        throw new IpTablesNetException("IpTables-Restore execution failed: Invalid Command Line - " + process.StandardError.ReadToEnd());
                    }

                    //ERR: GENERAL ERROR
                    if (process.ExitCode == 1)
                    {
                        String error = process.StandardError.ReadToEnd();
                        Console.WriteLine(error);

                        MemoryStream ms = new MemoryStream();
                        var          sw = new StreamWriter(ms);
                        _builder.WriteOutput(sw);
                        sw.Flush();
                        ms.Seek(0, SeekOrigin.Begin);
                        var sr    = new StreamReader(ms);
                        var rules = sr.ReadToEnd();

                        var r = new Regex("line ([0-9]+) failed");
                        if (r.IsMatch(error))
                        {
                            var m = r.Match(error);
                            var g = m.Groups[1];
                            var i = int.Parse(g.Value);

                            throw new IpTablesNetException("IpTables-Restore failed to parse rule: " +
                                                           rules.Split(new char[] { '\n' }).Skip(i - 1).FirstOrDefault());
                        }

                        throw new IpTablesNetException("IpTables-Restore execution failed: Error");
                    }

                    //ERR: UNKNOWN
                    throw new IpTablesNetException("IpTables-Restore execution failed: Unknown Error");
                }
            }

            try
            {
                process.Close();
            }
            catch
            {
            }


            _inTransaction = false;
        }