You are on page 1of 5

package GUI; import import import import import import import import import import import import java.awt.

Dimension; java.awt.Rectangle; java.io.File; java.net.NetworkInterface; java.util.Enumeration; javax.swing.JButton; javax.swing.JCheckBox; javax.swing.JComboBox; javax.swing.JFrame; javax.swing.JLabel; javax.swing.JOptionPane; javax.swing.JPanel;

import system.MasqueradingRule; public class IPMasquerading { private private private private private private private private private private JFrame masqueFrame = null; JPanel MasquePanel = null; JComboBox gwIFList = null; JLabel gwIFLabel = null; JComboBox inIFList = null; JLabel inIFLabel = null; JCheckBox gateway = null; JCheckBox forwIF = null; JCheckBox ipForw = null; JButton applyMasqueButton = null;

/** * This method initializes jFrame1 * * @return javax.swing.JFrame */ public JFrame getMasqueFrame() { if (masqueFrame == null) { masqueFrame = new JFrame(); masqueFrame.setSize(new Dimension(309, 165)); masqueFrame.setLocationRelativeTo(null); masqueFrame.setContentPane(getMasquePanel()); masqueFrame.setTitle("IP Masquerading"); masqueFrame.setResizable(false); } return masqueFrame; } private JPanel getMasquePanel() { if (MasquePanel == null) { gwIFLabel = new JLabel(); gwIFLabel.setBounds(new Rectangle(20, 15, 114, 15)); gwIFLabel.setText("Gateway Interface"); inIFLabel = new JLabel(); inIFLabel.setBounds(new Rectangle(143, 15, 120, 15)); inIFLabel.setText("Incoming Interface"); MasquePanel = new JPanel(); MasquePanel.setLayout(null); MasquePanel.add(gwIFLabel, null); MasquePanel.add(getGWIFList()); MasquePanel.add(inIFLabel, null);

MasquePanel.add(getINIFList()); MasquePanel.add(getGateway(), null); MasquePanel.add(getForwIF(), null); MasquePanel.add(getIpForw(), null); MasquePanel.add(getApplyMasqueButton(), null); } return MasquePanel; } /** * This method initializes gwIFList * * @return javax.swing.JComboBox */ @SuppressWarnings("unchecked") private JComboBox getGWIFList() { if (gwIFList == null) { gwIFList = new JComboBox(); gwIFList.setBounds(new Rectangle(19, 44, 79, 24)); try { NetworkInterface ifElement1 = null; Enumeration ifEnum1 = NetworkInterface.getNetwor kInterfaces(); while (ifEnum1.hasMoreElements()) { ifElement1 = (NetworkInterface) ifEnum1. nextElement(); if (ifElement1.getName().equals("lo")) { // Don't display loopback interf ace } else { gwIFList.addItem(ifElement1.getD isplayName()); } } } catch (Exception ex) { ex.printStackTrace(); } } return gwIFList; } /** * This method initializes inIFList * * @return javax.swing.JComboBox */ @SuppressWarnings("unchecked") private JComboBox getINIFList() { if (inIFList == null) { inIFList = new JComboBox(); inIFList.setBounds(new Rectangle(143, 44, 79, 24)); try { NetworkInterface ifElement1 = null; Enumeration ifEnum1 = NetworkInterface.getNetwor

kInterfaces(); while (ifEnum1.hasMoreElements()) { ifElement1 = (NetworkInterface) ifEnum1. nextElement(); if (ifElement1.getName().equals("lo")) { // Don't display loopback interf ace } else { inIFList.addItem(ifElement1.getD isplayName()); } } } catch (Exception ex) { ex.printStackTrace(); } } return inIFList; } /** * This method initializes gateway * * @return javax.swing.JCheckBox */ private JCheckBox getGateway() { if (gateway == null) { gateway = new JCheckBox(); gateway.setBounds(new Rectangle(130, 75, 155, 21)); gateway.setText("Gateway"); } return gateway; } /** * This method initializes forwIF * * @return javax.swing.JCheckBox */ private JCheckBox getForwIF() { if (forwIF == null) { forwIF = new JCheckBox(); forwIF.setBounds(new Rectangle(130, 95, 180, 21)); forwIF.setText("Interface forwarding"); } return forwIF; } /** * This method initializes ipForw * * @return javax.swing.JCheckBox */ private JCheckBox getIpForw() { if (ipForw == null) { ipForw = new JCheckBox(); ipForw.setBounds(new Rectangle(130, 115, 155, 21)); ipForw.setText("IP forwarding"); }

return ipForw; } /** * This method initializes applyMasqueButton * * @return javax.swing.JButton */ private JButton getApplyMasqueButton() { if (applyMasqueButton == null) { applyMasqueButton = new JButton(); applyMasqueButton.setBounds(new Rectangle(17, 95, 85, 21 )); applyMasqueButton.setText("Apply"); applyMasqueButton .addActionListener(new java.awt.event.Ac tionListener() { public void actionPerformed(java .awt.event.ActionEvent e) { if (Application.rule.che ckRoot()) { if ((gateway.isS elected() == false) && (forwIF.isSelected() == false) && (ipForw.isSelected() == false)) { JOptionP ane .showMessageDialog( null, "You have to select an option first", "MASQUERADING", JOptionPane.ERROR_MESSAGE); } else { if (gate way.isSelected()) { gwRule(); } if (ipFo rw.isSelected()) { ipForwRule(); } if (forw IF.isSelected()) { ifForwRule(); } } }

} }); } return applyMasqueButton; } private void gwRule() { MasqueradingRule rl = new MasqueradingRule(gwIFList.getSelectedI tem() .toString(), inIFList.getSelectedItem().toString ()); Application.rule.rules.add(rl); Application.outputScreen.append("\n" + rl.displayRule()); } private void ifForwRule() { if (gwIFList.getSelectedItem().toString().equals( inIFList.getSelectedItem().toString())) { JOptionPane .showMessageDialog( null, "The gateway interface c an't be the same with the incoming inteface", "GATEWAY", JOptionPane.E RROR_MESSAGE); } else { MasqueradingRule rl = new MasqueradingRule(gwIFList .getSelectedItem().toString(), inIFList. getSelectedItem() .toString()); Application.rule.rules.add(rl); Application.outputScreen.append("\n" + rl.displayRule()) ; } } private void ipForwRule() { try { File ipForward = new File("/opt/jipfire/sh/ipForward"); if (ipForward.exists()) { Runtime.getRuntime().exec("/opt/jipfire/sh/ipFor ward"); } else { Runtime.getRuntime().exec("./sh/ipForward"); } Application.outputScreen.append("\necho 1 > /proc/sys/ne t/ip4v/ip_forward"); } catch (Exception ex) { ex.printStackTrace(); } } }

You might also like