package us.d8u.pfGUI;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class Main {
public static BufferedWriter writer = null;
private static Vector<Short> keys;
public static void writeln(String line) {
try {
writer.write(String.format("%s\\n", line));
} catch (IOException e) {
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("PF GUI configurator");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
FileReader etcServicesReader_ = null;
try {
etcServicesReader_ = new FileReader("/etc/services");
} catch (FileNotFoundException e) {
System.err.println("You don\'t have an /etc/services file -- not supported");
System.exit(-1);
}
BufferedReader etcServicesReader = new BufferedReader(etcServicesReader_);
String line = " ";
Vector<Vector<String>> values = new Vector<>();
keys = new Vector<>();
try {
while ((line = etcServicesReader.readLine()) != null) {
if (line.startsWith("#"))
continue;
if (line.contains("/udp"))
continue;
Matcher match = Pattern.compile("([a-z-]+)[^0-9]*([0-9]+)").matcher(line);
if (!match.find()) {
System.err.println("Failed to match " + line);
continue;
}
String value__ = match.group(1);
Vector<String> value_ = new Vector<>();
value_.add(value__);
String key__ = match.group(2);
value_.add(key__);
Short key_ = null;
try {
key_ = Short.valueOf(key__);
} catch (NumberFormatException e) {
continue;
}
keys.add(key_);
values.add(value_);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
etcServicesReader.close();
} catch (IOException e) {
}
Vector<String> headers = new Vector<>();
headers.add("Service (select to block)");
headers.add("Port #");
final JTable tbl = new JTable(new DefaultTableModel(values, headers));
frame.addWindowListener(new WindowAdapter() {
@Override
@SuppressWarnings("unused")
public void windowClosing(WindowEvent e) {
String path = JOptionPane.showInputDialog("Where\'s your pf configuration file?");
try {
writer = new BufferedWriter(new FileWriter(path));
} catch (IOException e1) {
path = System.getProperty("java.io.tmpdir") + "/pf.conf";
try {
writer = new BufferedWriter(new FileWriter(path));
} catch (IOException e_) {
e_.printStackTrace();
}
}
try {
writer.write("# Generated by pfGUI\\n");
} catch (IOException e1) {
e1.printStackTrace();
}
try {
writer.write("# Written by Hasan Diwan <hd1@jsc.d8u.us>\\n");
} catch (IOException e6) {
e6.printStackTrace();
}
try {
writer.write("# Provided under the BSD License -- a copy of which may be found at http://www.netbsd.org/about/redistribution.html\\n");
} catch (IOException e5) {
e5.printStackTrace();
}
try {
writer.write("block in all\\n");
} catch (IOException e4) {
e4.printStackTrace();
}
try {
writer.write("pass out all\\n");
} catch (IOException e3) {
e3.printStackTrace();
}
try {
writer.write("pass in all keep state\\n");
} catch (IOException e2) {
e2.printStackTrace();
}
int[] selectedRows = tbl.getSelectedRows();
TableModel model = tbl.getModel();
for (int r = 0; r != tbl.getSelectedRowCount(); r++) {
Short port = Short.valueOf((String) model.getValueAt(tbl.getSelectedRows()[r], 1));
String line2 = String
.format("pass in proto tcp from any port = %d to any\\npass in proto udp from any port = %d to any\\n",
port, port);
try {
writer.write(line2 + "\\n");
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
frame.getContentPane().add(new JScrollPane(tbl));
frame.pack();
frame.setVisible(true);
}
}