document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package us.d8u.pfGUI;
  2.  
  3. import java.awt.event.WindowAdapter;
  4. import java.awt.event.WindowEvent;
  5. import java.io.BufferedReader;
  6. import java.io.BufferedWriter;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileReader;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.util.Vector;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14.  
  15. import javax.swing.JFrame;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JScrollPane;
  18. import javax.swing.JTable;
  19. import javax.swing.table.DefaultTableModel;
  20. import javax.swing.table.TableModel;
  21.  
  22. public class Main {
  23.     public static BufferedWriter writer = null;
  24.     private static Vector<Short> keys;
  25.  
  26.     public static void writeln(String line) {
  27.         try {
  28.             writer.write(String.format("%s\\n", line));
  29.         } catch (IOException e) {
  30.         }
  31.     }
  32.  
  33.     public static void main(String[] args) {
  34.         JFrame frame = new JFrame("PF GUI configurator");
  35.         frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  36.         FileReader etcServicesReader_ = null;
  37.         try {
  38.             etcServicesReader_ = new FileReader("/etc/services");
  39.         } catch (FileNotFoundException e) {
  40.             System.err.println("You don\'t have an /etc/services file -- not supported");
  41.             System.exit(-1);
  42.         }
  43.         BufferedReader etcServicesReader = new BufferedReader(etcServicesReader_);
  44.         String line = " ";
  45.  
  46.         Vector<Vector<String>> values = new Vector<>();
  47.         keys = new Vector<>();
  48.         try {
  49.             while ((line = etcServicesReader.readLine()) != null) {
  50.                 if (line.startsWith("#"))
  51.                     continue;
  52.                 if (line.contains("/udp"))
  53.                     continue;
  54.                 Matcher match = Pattern.compile("([a-z-]+)[^0-9]*([0-9]+)").matcher(line);
  55.                 if (!match.find()) {
  56.                     System.err.println("Failed to match " + line);
  57.                     continue;
  58.                 }
  59.                 String value__ = match.group(1);
  60.                 Vector<String> value_ = new Vector<>();
  61.                 value_.add(value__);
  62.  
  63.                 String key__ = match.group(2);
  64.                 value_.add(key__);
  65.                 Short key_ = null;
  66.                 try {
  67.                     key_ = Short.valueOf(key__);
  68.                 } catch (NumberFormatException e) {
  69.                     continue;
  70.                 }
  71.                 keys.add(key_);
  72.                 values.add(value_);
  73.             }
  74.         } catch (IOException e) {
  75.             e.printStackTrace();
  76.         }
  77.         try {
  78.             etcServicesReader.close();
  79.         } catch (IOException e) {
  80.         }
  81.  
  82.         Vector<String> headers = new Vector<>();
  83.         headers.add("Service (select to block)");
  84.         headers.add("Port #");
  85.         final JTable tbl = new JTable(new DefaultTableModel(values, headers));
  86.  
  87.         frame.addWindowListener(new WindowAdapter() {
  88.             @Override
  89.             @SuppressWarnings("unused")
  90.             public void windowClosing(WindowEvent e) {
  91.                 String path = JOptionPane.showInputDialog("Where\'s your pf configuration file?");
  92.                 try {
  93.                     writer = new BufferedWriter(new FileWriter(path));
  94.                 } catch (IOException e1) {
  95.                     path = System.getProperty("java.io.tmpdir") + "/pf.conf";
  96.                     try {
  97.                         writer = new BufferedWriter(new FileWriter(path));
  98.                     } catch (IOException e_) {
  99.                         e_.printStackTrace();
  100.                     }
  101.                 }
  102.                 try {
  103.                     writer.write("# Generated by pfGUI\\n");
  104.                 } catch (IOException e1) {
  105.                     e1.printStackTrace();
  106.                 }
  107.                 try {
  108.                     writer.write("# Written by Hasan Diwan <hd1@jsc.d8u.us>\\n");
  109.                 } catch (IOException e6) {
  110.                     e6.printStackTrace();
  111.                 }
  112.                 try {
  113.                     writer.write("# Provided under the BSD License -- a copy of which may be found at http://www.netbsd.org/about/redistribution.html\\n");
  114.                 } catch (IOException e5) {
  115.                     e5.printStackTrace();
  116.                 }
  117.                 try {
  118.                     writer.write("block in all\\n");
  119.                 } catch (IOException e4) {
  120.                     e4.printStackTrace();
  121.                 }
  122.                 try {
  123.                     writer.write("pass out all\\n");
  124.                 } catch (IOException e3) {
  125.                     e3.printStackTrace();
  126.                 }
  127.                 try {
  128.                     writer.write("pass in all keep state\\n");
  129.                 } catch (IOException e2) {
  130.                     e2.printStackTrace();
  131.                 }
  132.                 int[] selectedRows = tbl.getSelectedRows();
  133.                 TableModel model = tbl.getModel();
  134.                 for (int r = 0; r != tbl.getSelectedRowCount(); r++) {
  135.                     Short port = Short.valueOf((String) model.getValueAt(tbl.getSelectedRows()[r], 1));
  136.                     String line2 = String
  137.                             .format("pass in proto tcp from any port = %d to any\\npass in proto udp from any port = %d to any\\n",
  138.                                     port, port);
  139.                     try {
  140.                         writer.write(line2 + "\\n");
  141.                     } catch (IOException e1) {
  142.                         e1.printStackTrace();
  143.                     }
  144.                 }
  145.                 try {
  146.                     writer.close();
  147.                 } catch (IOException e1) {
  148.                     e1.printStackTrace();
  149.                 }
  150.             }
  151.         });
  152.         frame.getContentPane().add(new JScrollPane(tbl));
  153.         frame.pack();
  154.         frame.setVisible(true);
  155.     }
  156. }
');