1 package com.jbidwatcher.ui.config;
2 /*
3  * Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved.
4  *
5  * Developed by mrs (Morgan Schweers)
6  */
7 
8 import com.jbidwatcher.util.config.JConfig;
9 
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.event.*;
14 
15 public class JConfigFirewallTab extends JConfigTab {
16   private JRadioButton noFirewall;
17   private JRadioButton socksFirewall;
18   private JRadioButton webProxy;
19   private JCheckBox proxyHttps;
20 
21   String holdProxyHost=null, holdProxyPort=null;
22   String holdHTTPSProxyHost=null, holdHTTPSProxyPort=null;
23   String holdProxyUser=null, holdProxyPass=null;
24   String holdFireHost=null, holdFirePort=null;
25 
26   private JTextField proxyHost, proxyPort;
27   private JTextField httpsProxyHost, httpsProxyPort;
28   private JTextField proxyUser;
29   private JPasswordField proxyPass;
30   private JTextField firewallHost, firewallPort;
31   private DocumentListener firewallTextFieldListener;
32 
33   private class radioAction implements ActionListener {
actionPerformed(ActionEvent ae)34     public void actionPerformed(ActionEvent ae) {
35       boolean proxyP=false, firewallP=false;
36 
37       if(noFirewall.isSelected()) {
38         firewallP = false;
39         proxyP = false;
40       } else if(socksFirewall.isSelected()) {
41         firewallP = true;
42         proxyP = false;
43       } else if(webProxy.isSelected()) {
44         firewallP = false;
45         proxyP = true;
46       }
47 
48       updateProxyFirewall(proxyP, firewallP, proxyHttps.isSelected());
49     }
50   }
51 
52   private class textAction implements DocumentListener {
anyEvent()53     private void anyEvent() {
54       if(proxyHost.isEnabled()) {
55         holdProxyHost = proxyHost.getText();
56       }
57       if(proxyPort.isEnabled()) {
58         holdProxyPort = proxyPort.getText();
59       }
60       if(proxyUser.isEnabled()) {
61         holdProxyUser = proxyUser.getText();
62       }
63       if(proxyPass.isEnabled()) {
64         holdProxyPass = new String(proxyPass.getPassword());
65       }
66       if(firewallHost.isEnabled()) {
67         holdFireHost = firewallHost.getText();
68       }
69       if(firewallPort.isEnabled()) {
70         holdFirePort = firewallPort.getText();
71       }
72       if(httpsProxyHost.isEnabled()) {
73         holdHTTPSProxyHost = httpsProxyHost.getText();
74       }
75       if(httpsProxyPort.isEnabled()) {
76         holdHTTPSProxyPort = httpsProxyPort.getText();
77       }
78     }
insertUpdate(DocumentEvent de)79     public void insertUpdate(DocumentEvent de) {
80       anyEvent();
81     }
changedUpdate(DocumentEvent de)82     public void changedUpdate(DocumentEvent de) {
83       anyEvent();
84     }
removeUpdate(DocumentEvent de)85     public void removeUpdate(DocumentEvent de) {
86       anyEvent();
87     }
88   }
89 
JConfigFirewallTab()90   public JConfigFirewallTab() {
91     super();
92     JPanel topPanes = new JPanel();
93     JPanel bottomPanes = new JPanel();
94 
95     firewallTextFieldListener = new textAction();
96 
97     this.setLayout(new BorderLayout());
98 
99     topPanes.setLayout(new GridLayout(1,2));
100     topPanes.add(panelPack(buildRadioButtons()));
101     topPanes.add(panelPack(buildFirewallPrompt()));
102     this.add(topPanes, "North");
103 
104     bottomPanes.setLayout(new GridLayout(1, 2));
105     bottomPanes.add(panelPack(buildProxyPanel()));
106     bottomPanes.add(panelPack(buildHTTPSProxyPanel()));
107     //this.add(panelPack(buildProxyPanel()), "Center");
108     this.add(bottomPanes, "Center");
109 
110     updateValues();
111   }
112 
113   //  This is how the main configuration menu knows what to name this
114   //  tab.
getTabName()115   public String getTabName() { return "Firewall"; }
116 
117   //
118   //  Cancel all modifications.
119   //  Reverts to stored configuration values.
120   //
cancel()121   public void cancel() {
122     holdProxyHost = holdProxyPort = holdProxyUser = holdProxyPass =
123       holdFireHost = holdFirePort = holdHTTPSProxyHost = holdHTTPSProxyPort = null;
124     updateValues();
125   }
126 
127   //
128   //  Apply all changes made to the firewall options.  This does NOT
129   //  immediately open a SOCKS server, or a web proxy for all future
130   //  transactions.  It should.  HACKHACK -- mrs: 14-August-2001 01:44
131   //
apply()132   public void apply() {
133     String firewallState = "none";
134 
135     if(noFirewall.isSelected()) {
136       firewallState = "none";
137     } else if(socksFirewall.isSelected()) {
138       firewallState = "firewall";
139     } else if(webProxy.isSelected()) {
140       firewallState = "proxy";
141     }
142 
143     JConfig.setConfiguration("proxyfirewall", firewallState);
144     if(holdProxyHost != null) {
145       JConfig.setConfiguration("proxy.host", holdProxyHost);
146     }
147     if(holdProxyPort != null) {
148       JConfig.setConfiguration("proxy.port", holdProxyPort);
149     }
150     if(holdProxyUser != null) {
151       JConfig.setConfiguration("proxy.user", holdProxyUser);
152     }
153     if(holdProxyPass != null) {
154       JConfig.setConfiguration("proxy.pass", holdProxyPass);
155     }
156     if(holdFireHost != null) {
157       JConfig.setConfiguration("firewall.host", holdFireHost);
158     }
159     if(holdFirePort != null) {
160       JConfig.setConfiguration("firewall.port", holdFirePort);
161     }
162     if(proxyHttps.isSelected()) {
163       JConfig.setConfiguration("proxy.https.set", "true");
164       if(holdHTTPSProxyHost != null) {
165         JConfig.setConfiguration("proxy.https.host", holdHTTPSProxyHost);
166       }
167       if(holdHTTPSProxyPort != null) {
168         JConfig.setConfiguration("proxy.https.port", holdHTTPSProxyPort);
169       }
170     } else {
171       JConfig.setConfiguration("proxy.https.set", "false");
172     }
173   }
174 
175   //
176   //  If the radio button is selected, return either the default
177   //  value, or if that value is null, the correct value from the
178   //  configuration file, or if THAT value is also null, an empty
179   //  string.
180   //
181   //  If the radio button is NOT selected, return "<disabled>", so
182   //  there's some marker in the text field that it's not editable
183   //  right now.
184   //
getConfigValue(JToggleButton jrb, String configValue, String defaultValue)185   private String getConfigValue(JToggleButton jrb, String configValue, String defaultValue) {
186     String outputValue;
187 
188     if(jrb.isSelected()) {
189       if(defaultValue == null) {
190         outputValue = JConfig.queryConfiguration(configValue);
191         if(outputValue == null) {
192           return "";
193         } else {
194           return outputValue;
195         }
196       }
197       return defaultValue;
198     } else {
199       return "<disabled>";
200     }
201   }
202 
setAllProxyText()203   private void setAllProxyText() {
204     proxyHost.setText(getConfigValue(webProxy, "proxy.host", holdProxyHost));
205     proxyPort.setText(getConfigValue(webProxy, "proxy.port", holdProxyPort));
206     proxyUser.setText(getConfigValue(webProxy, "proxy.user", holdProxyUser));
207     proxyPass.setText(getConfigValue(webProxy, "proxy.pass", holdProxyPass));
208   }
209 
setAllProxyStatus(boolean proxyP)210   private void setAllProxyStatus(boolean proxyP) {
211     proxyHost.setEnabled(proxyP);
212     proxyPort.setEnabled(proxyP);
213     proxyUser.setEnabled(proxyP);
214     proxyPass.setEnabled(proxyP);
215   }
216 
setAllFirewallText()217   private void setAllFirewallText() {
218     firewallHost.setText(getConfigValue(socksFirewall, "firewall.host", holdFireHost));
219     firewallPort.setText(getConfigValue(socksFirewall, "firewall.port", holdFirePort));
220   }
221 
setAllFirewallStatus(boolean firewallP)222   private void setAllFirewallStatus(boolean firewallP) {
223     firewallHost.setEnabled(firewallP);
224     firewallPort.setEnabled(firewallP);
225   }
226 
setAllHTTPSText()227   private void setAllHTTPSText() {
228     httpsProxyHost.setText(getConfigValue(proxyHttps, "proxy.https.host", holdHTTPSProxyHost));
229     httpsProxyPort.setText(getConfigValue(proxyHttps, "proxy.https.port", holdHTTPSProxyPort));
230   }
231 
setAllHTTPSStatus(boolean httpsP)232   private void setAllHTTPSStatus(boolean httpsP) {
233     httpsProxyHost.setEnabled(httpsP);
234     httpsProxyPort.setEnabled(httpsP);
235   }
236 
updateProxyFirewall(boolean proxyP, boolean firewallP, boolean httpsP)237   private void updateProxyFirewall(boolean proxyP, boolean firewallP, boolean httpsP) {
238     if(!proxyP) {
239       setAllProxyStatus(proxyP);
240       setAllProxyText();
241     } else {
242       setAllProxyText();
243       setAllProxyStatus(proxyP);
244     }
245 
246     if(!firewallP) {
247       setAllFirewallStatus(firewallP);
248       setAllFirewallText();
249     } else {
250       setAllFirewallText();
251       setAllFirewallStatus(firewallP);
252     }
253 
254     if(!httpsP) {
255       setAllHTTPSStatus(httpsP);
256       setAllHTTPSText();
257     } else {
258       setAllHTTPSText();
259       setAllHTTPSStatus(httpsP);
260     }
261   }
262 
updateValues()263   public final void updateValues() {
264     String proxyFirewall = JConfig.queryConfiguration("proxyfirewall");
265     boolean proxyP, firewallP;
266 
267     if(proxyFirewall == null) {
268       noFirewall.setSelected(true);
269       firewallP = false;
270       proxyP = false;
271     } else if(proxyFirewall.equals("proxy")) {
272       webProxy.setSelected(true);
273       firewallP = false;
274       proxyP = true;
275     } else if(proxyFirewall.equals("firewall")) {
276       socksFirewall.setSelected(true);
277       firewallP = true;
278       proxyP = false;
279     } else {
280       //  HACKHACK --  Should make a note that it's an invalid value.
281       noFirewall.setSelected(true);
282       firewallP = false;
283       proxyP = false;
284     }
285 
286     if(JConfig.queryConfiguration("proxy.https.set", "false").equals("true")) {
287       proxyHttps.setSelected(true);
288     }
289 
290     updateProxyFirewall(proxyP, firewallP, proxyHttps.isSelected());
291   }
292 
buildRadioButtons()293   private JPanel buildRadioButtons() {
294     ActionListener rad = new radioAction();
295     JPanel buttonPanel = new JPanel();
296     Box buttonBox = Box.createVerticalBox();
297 
298     noFirewall = new JRadioButton("No firewall or proxy");
299     socksFirewall = new JRadioButton("SOCKS 4/5 Firewall");
300     webProxy = new JRadioButton("HTTP Web Proxy");
301 
302     ButtonGroup allButtons = new ButtonGroup();
303     allButtons.add(socksFirewall);
304     allButtons.add(webProxy);
305     allButtons.add(noFirewall);
306     socksFirewall.addActionListener(rad);
307     webProxy.addActionListener(rad);
308     noFirewall.addActionListener(rad);
309 
310     buttonPanel.setBorder(BorderFactory.createTitledBorder("Firewall/Proxy"));
311     buttonPanel.setLayout(new BorderLayout());
312 
313     buttonBox.add(socksFirewall);
314     buttonBox.add(webProxy);
315     buttonBox.add(noFirewall);
316 
317     buttonPanel.add(buttonBox, "North");
318 
319     return buttonPanel;
320   }
321 
buildFirewallPrompt()322   private JPanel buildFirewallPrompt() {
323     JPanel firewallPanel = new JPanel();
324     Box updownBox;
325 
326     firewallPanel.setBorder(BorderFactory.createTitledBorder("SOCKS Settings"));
327     firewallPanel.setLayout(new BoxLayout(firewallPanel, BoxLayout.Y_AXIS));
328 
329     firewallHost = new JTextField();
330     firewallPort = new JTextField();
331 
332     setAllFirewallStatus(false);
333 
334     adjustField(firewallHost, "Host name or IP address of SOCKS firewall", firewallTextFieldListener);
335     adjustField(firewallPort, "Port number for SOCKS firewall", firewallTextFieldListener);
336 
337     updownBox = Box.createVerticalBox();
338     updownBox.add(makeLine(new JLabel("SOCKS Host: "), firewallHost));
339     updownBox.add(makeLine(new JLabel("SOCKS Port:  "), firewallPort));
340 
341     firewallPanel.add(updownBox);
342 
343     return firewallPanel;
344   }
345 
buildProxyPanel()346   private JPanel buildProxyPanel() {
347     JPanel proxyPanel = new JPanel();
348 
349     proxyPanel.setBorder(BorderFactory.createTitledBorder("HTTP/Web Proxy Settings"));
350     proxyPanel.setLayout(new BoxLayout(proxyPanel, BoxLayout.Y_AXIS));
351 
352     proxyHost = new JTextField();
353     proxyPort = new JTextField();
354     proxyUser = new JTextField();
355     proxyPass = new JPasswordField();
356 
357     setAllProxyStatus(false);
358     adjustField(proxyHost, "Host name or IP address of web proxy server", firewallTextFieldListener);
359     adjustField(proxyPort, "Port number that a web proxy server runs on", firewallTextFieldListener);
360     adjustField(proxyUser, "Username (if needed) for web proxy server", firewallTextFieldListener);
361     adjustField(proxyPass, "Password (if needed) for web proxy server", firewallTextFieldListener);
362 
363     proxyPanel.add(makeLine(new JLabel("Host: "), proxyHost));
364     proxyPanel.add(makeLine(new JLabel("Port:  "), proxyPort));
365     proxyPanel.add(makeLine(new JLabel("Username: "), proxyUser));
366     proxyPanel.add(makeLine(new JLabel("Password:  "), proxyPass));
367 
368     return proxyPanel;
369   }
370 
buildHTTPSProxyPanel()371   private JPanel buildHTTPSProxyPanel() {
372     JPanel proxyPanel = new JPanel();
373     radioAction rad = new radioAction();
374 
375     proxyPanel.setBorder(BorderFactory.createTitledBorder("HTTPS/Secure Proxy Settings"));
376     proxyPanel.setLayout(new BoxLayout(proxyPanel, BoxLayout.Y_AXIS));
377 
378     httpsProxyHost = new JTextField();
379     httpsProxyPort = new JTextField();
380     setAllHTTPSStatus(false);
381     adjustField(httpsProxyHost, "Host name or IP address of HTTPS proxy server", firewallTextFieldListener);
382     adjustField(httpsProxyPort, "Port number that the HTTPS proxy server runs on", firewallTextFieldListener);
383 
384     proxyHttps = new JCheckBox("Enable HTTPS (secure http) proxy?");
385     proxyHttps.addActionListener(rad);
386     JPanel checkboxPanel = new JPanel(new BorderLayout());
387     checkboxPanel.add(proxyHttps, BorderLayout.WEST);
388     proxyPanel.add(checkboxPanel);
389 
390     proxyPanel.add(makeLine(new JLabel("HTTPS Host: "), httpsProxyHost));
391     proxyPanel.add(makeLine(new JLabel("HTTPS Port: "), httpsProxyPort));
392 
393     return proxyPanel;
394   }
395 }
396