Un-static part 1

Replace TrayManager.getInstance() with constructor
Pass Main to TrayManager
Create WindowHandler in TrayManager
Pass WindowHandler to RegisteredFrames
Only create ItoopieHostnameVerifier once, in gui/Main
Pass gui/Main to SettingsFrame
Pass gui/Main to ItoopieHostnameVerifier and CertificateGui
Sync on lock in ItoopieHostnameVerifier.clearRecentlyDenied()
Comment out some main() and other test code
Make some fields non-static
Remove some static initializers
Start gui/Main before the tray icon
Fixed the tray icon on Gnome with a delay in TrayManager
This commit is contained in:
zzz
2022-01-11 11:05:00 -05:00
parent 0498ea69ea
commit 25dd896d26
10 changed files with 98 additions and 97 deletions

View File

@ -10,7 +10,6 @@ import java.util.Timer;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import javax.net.ssl.HttpsURLConnection;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException; import javax.swing.UnsupportedLookAndFeelException;
@ -23,7 +22,6 @@ import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import net.i2p.itoopie.configuration.ConfigurationManager; import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.gui.GUIHelper; import net.i2p.itoopie.gui.GUIHelper;
import net.i2p.itoopie.gui.TrayManager; import net.i2p.itoopie.gui.TrayManager;
import net.i2p.itoopie.gui.WindowHandler;
import net.i2p.itoopie.i2pcontrol.InvalidParametersException; import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException; import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.JSONRPC2Interface; import net.i2p.itoopie.i2pcontrol.JSONRPC2Interface;
@ -39,7 +37,6 @@ import net.i2p.itoopie.i2pcontrol.methods.SetI2PControl;
import net.i2p.itoopie.i2pcontrol.methods.SetNetworkSetting; import net.i2p.itoopie.i2pcontrol.methods.SetNetworkSetting;
import net.i2p.itoopie.i2pcontrol.methods.SetRouterManager; import net.i2p.itoopie.i2pcontrol.methods.SetRouterManager;
import net.i2p.itoopie.maintenance.ReseedMonitor; import net.i2p.itoopie.maintenance.ReseedMonitor;
import net.i2p.itoopie.security.ItoopieHostnameVerifier;
/** /**
* The main class of the application. * The main class of the application.
@ -47,32 +44,35 @@ import net.i2p.itoopie.security.ItoopieHostnameVerifier;
public class Main { public class Main {
///Manages the lifetime of the tray icon. ///Manages the lifetime of the tray icon.
private TrayManager trayManager = null; private TrayManager trayManager;
private static ConfigurationManager _conf; private final ConfigurationManager _conf;
private static Timer reseedMonitor; private static Timer reseedMonitor;
private static Log _log; private final Log _log;
public Main() {
_conf = ConfigurationManager.getInstance();
_log = LogFactory.getLog(Main.class);
}
/** /**
* Start the tray icon code (loads tray icon in the tray area). * Start the tray icon code (loads tray icon in the tray area).
* @throws Exception * @throws Exception
*/ */
public void startUp() throws Exception { public void startUp() throws Exception {
trayManager = TrayManager.getInstance(); trayManager = new TrayManager(this);
trayManager.startManager(); trayManager.startManager();
} }
public static void main(String[] args) { public static void main(String[] args) {
beginStartup(args); Main main = new Main();
main.beginStartup(args);
} }
/** /**
* Main method launching the application. * Main method launching the application.
*/ */
public static void beginStartup(String[] args) { public void beginStartup(String[] args) {
System.setProperty("java.awt.headless", "false"); System.setProperty("java.awt.headless", "false");
_conf = ConfigurationManager.getInstance();
_log = LogFactory.getLog(Main.class);
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier());
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
@ -84,16 +84,13 @@ public class Main {
} }
final Main main = new Main(); launchForeverLoop();
main.launchForeverLoop();
try { try {
main.startUp(); startUp();
} catch (Exception e) { } catch (Exception e) {
_log.error("Error during TrayManager launch.", e); _log.error("Error during TrayManager launch.", e);
} }
// Popup Main window.
WindowHandler.toggleFrames();
// Start running periodic task after 2 minutes, run periodically every 10th minute. // Start running periodic task after 2 minutes, run periodically every 10th minute.
@ -104,7 +101,7 @@ public class Main {
} }
@SuppressWarnings("static-access") @SuppressWarnings("static-access")
public static void beginShutdown(){ public void beginShutdown(){
_conf.writeConfFile(); _conf.writeConfFile();
System.exit(0); System.exit(0);
} }
@ -131,6 +128,7 @@ public class Main {
} }
/*
private static void testStuff(){ private static void testStuff(){
_conf.parseConfigStr("server.hostname=127.0.0.1"); _conf.parseConfigStr("server.hostname=127.0.0.1");
_conf.parseConfigStr("server.port=5555"); _conf.parseConfigStr("server.port=5555");
@ -138,7 +136,6 @@ public class Main {
// Try port switching // Try port switching
/*
System.out.println("\nI2PControl - Port Switch"); System.out.println("\nI2PControl - Port Switch");
try { try {
HashMap<I2P_CONTROL, String> hm = new HashMap<I2P_CONTROL, String>(); HashMap<I2P_CONTROL, String> hm = new HashMap<I2P_CONTROL, String>();
@ -268,7 +265,6 @@ public class Main {
System.out.println("Bad parameters sent.."); System.out.println("Bad parameters sent..");
} }
*/
// Test reading all router info // Test reading all router info
System.out.println("\nGetRouterInfo"); System.out.println("\nGetRouterInfo");
try { try {
@ -287,7 +283,6 @@ public class Main {
// Test restart - worked at one point :) Possibly now as well. // Test restart - worked at one point :) Possibly now as well.
/*
System.out.println("\nSetRouterRunner: Restart"); System.out.println("\nSetRouterRunner: Restart");
try { try {
SetRouterRunner.execute(ROUTER_RUNNER.RESTART); SetRouterRunner.execute(ROUTER_RUNNER.RESTART);
@ -295,10 +290,9 @@ public class Main {
System.out.println("Invalid password.."); System.out.println("Invalid password..");
} catch (JSONRPC2SessionException e) { } catch (JSONRPC2SessionException e) {
System.out.println("Connection failed.."); System.out.println("Connection failed..");
}*/ }
// Test restart graceful - worked at one point :) Possibly now as well. // Test restart graceful - worked at one point :) Possibly now as well.
/*
System.out.println("\nSetRouterRunner: Restart Graceful"); System.out.println("\nSetRouterRunner: Restart Graceful");
try { try {
SetRouterRunner.execute(ROUTER_RUNNER.RESTART_GRACEFUL); SetRouterRunner.execute(ROUTER_RUNNER.RESTART_GRACEFUL);
@ -306,9 +300,8 @@ public class Main {
System.out.println("Invalid password.."); System.out.println("Invalid password..");
} catch (JSONRPC2SessionException e) { } catch (JSONRPC2SessionException e) {
System.out.println("Connection failed.."); System.out.println("Connection failed..");
}*/ }
/*
// Test shutdown - worked at one point :) Possibly now as well. // Test shutdown - worked at one point :) Possibly now as well.
System.out.println("\nSetRouterRunner: Shutdown "); System.out.println("\nSetRouterRunner: Shutdown ");
try { try {
@ -318,7 +311,6 @@ public class Main {
} catch (JSONRPC2SessionException e) { } catch (JSONRPC2SessionException e) {
System.out.println("Connection failed.."); System.out.println("Connection failed..");
} }
}
*/ */
} }
}

View File

@ -15,12 +15,14 @@ import net.i2p.itoopie.security.CertificateManager;
public class CertificateGUI { public class CertificateGUI {
/*
public static void main(String[] args){ public static void main(String[] args){
System.out.println("Save new cert: " + saveNewCert(null,null)); System.out.println("Save new cert: " + saveNewCert(null,null));
System.out.println("Overwrite cert: " + overwriteCert(null,null)); System.out.println("Overwrite cert: " + overwriteCert(null,null));
} }
*/
public static synchronized boolean saveNewCert(String hostname, X509Certificate cert){ public static synchronized boolean saveNewCert(Main main, String hostname, X509Certificate cert){
JFrame frame = new JFrame(); JFrame frame = new JFrame();
frame.setLayout(new BorderLayout()); frame.setLayout(new BorderLayout());
JButton bt = new JButton(); JButton bt = new JButton();
@ -58,7 +60,7 @@ public class CertificateGUI {
if (n == JOptionPane.YES_OPTION){ if (n == JOptionPane.YES_OPTION){
CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert)); CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert));
updateUI(); updateUI(main);
return true; return true;
} else { } else {
return false; return false;
@ -66,7 +68,7 @@ public class CertificateGUI {
} }
public static boolean overwriteCert(String hostname, X509Certificate cert){ public static boolean overwriteCert(Main main, String hostname, X509Certificate cert){
JFrame frame = new JFrame(); JFrame frame = new JFrame();
String title = Transl._t("Warning, new remote host detected"); String title = Transl._t("Warning, new remote host detected");
@ -108,7 +110,7 @@ public class CertificateGUI {
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
if (n == JOptionPane.YES_OPTION){ if (n == JOptionPane.YES_OPTION){
CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert)); CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert));
updateUI(); updateUI(main);
return true; // Confirmation positive return true; // Confirmation positive
} else { } else {
return false; // Confirmation negative return false; // Confirmation negative
@ -121,7 +123,7 @@ public class CertificateGUI {
/** /**
* Upon new cert accepted it is probable a good idea to show it by updating the GUI. * Upon new cert accepted it is probable a good idea to show it by updating the GUI.
*/ */
private static void updateUI(){ private static void updateUI(final Main main) {
// Sleep before updating. // Sleep before updating.
(new Thread(){ (new Thread(){
@Override @Override
@ -133,7 +135,7 @@ public class CertificateGUI {
@Override @Override
public void run() { public void run() {
Main.fireNewChange(); main.fireNewChange();
} }
}); });
} }

View File

@ -3,6 +3,7 @@ package net.i2p.itoopie.gui;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.EventQueue; import java.awt.EventQueue;
import javax.net.ssl.HttpsURLConnection;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
import java.awt.BorderLayout; import java.awt.BorderLayout;
@ -19,6 +20,7 @@ import net.i2p.itoopie.gui.component.RegisteredFrame;
import net.i2p.itoopie.gui.component.TabLogoPanel; import net.i2p.itoopie.gui.component.TabLogoPanel;
import net.i2p.itoopie.gui.component.util.TabChangeListener; import net.i2p.itoopie.gui.component.util.TabChangeListener;
import net.i2p.itoopie.i18n.Transl; import net.i2p.itoopie.i18n.Transl;
import net.i2p.itoopie.security.ItoopieHostnameVerifier;
import net.i2p.itoopie.util.IconLoader; import net.i2p.itoopie.util.IconLoader;
import javax.swing.border.Border; import javax.swing.border.Border;
@ -31,8 +33,9 @@ import java.awt.FlowLayout;
public class Main { public class Main {
private static JFrame frame; private JFrame frame;
private static JTabbedPane tabbedPane; private JTabbedPane tabbedPane;
private final WindowHandler windowHandler;
public final static int FRAME_WIDTH = 550; public final static int FRAME_WIDTH = 550;
public final static int FRAME_HEIGHT = 400; public final static int FRAME_HEIGHT = 400;
public final static int TABBED_PANE_HEIGHT = FRAME_HEIGHT - 66; public final static int TABBED_PANE_HEIGHT = FRAME_HEIGHT - 66;
@ -40,6 +43,7 @@ public class Main {
/** /**
* Launch the application. * Launch the application.
*/ */
/*
public static void main(String[] args) { public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
@ -52,11 +56,14 @@ public class Main {
} }
}); });
} }
*/
/** /**
* Create the application. * Create the application.
*/ */
public Main() { public Main(WindowHandler wh) {
windowHandler = wh;
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier(this));
initialize(); initialize();
} }
@ -67,7 +74,7 @@ public class Main {
GUIHelper.setDefaultStyle(); GUIHelper.setDefaultStyle();
//GUIHelper.setTabLooks(); //GUIHelper.setTabLooks();
frame = new RegisteredFrame("itoopie"); frame = new RegisteredFrame("itoopie", windowHandler);
frame.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT); frame.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
frame.setResizable(false); frame.setResizable(false);
frame.setBackground(GUIHelper.VERY_LIGHT); frame.setBackground(GUIHelper.VERY_LIGHT);
@ -75,7 +82,7 @@ public class Main {
root.setLayout(null); root.setLayout(null);
//root.setBorder(BorderFactory.createLineBorder(GUIHelper.MEDIUM)); //root.setBorder(BorderFactory.createLineBorder(GUIHelper.MEDIUM));
WindowHandler.registerMain(frame); windowHandler.registerMain(frame);
tabbedPane = new JTabbedPane(JTabbedPane.TOP); tabbedPane = new JTabbedPane(JTabbedPane.TOP);
root.add(tabbedPane); root.add(tabbedPane);
@ -93,7 +100,7 @@ public class Main {
// pass overview tab to settingsframe to reset the charts on change // pass overview tab to settingsframe to reset the charts on change
TabLogoPanel settingsTab = new SettingsFrame("itoopie-opaque12", overviewTab); TabLogoPanel settingsTab = new SettingsFrame("itoopie-opaque12", this, overviewTab);
tabbedPane.addTab(' ' + Transl._t("Settings") + ' ', icon, settingsTab, null); tabbedPane.addTab(' ' + Transl._t("Settings") + ' ', icon, settingsTab, null);
tabbedPane.addChangeListener(new TabChangeListener(settingsTab)); tabbedPane.addChangeListener(new TabChangeListener(settingsTab));
@ -129,7 +136,7 @@ public class Main {
/** /**
* Used to manually trigger updates for the tab being shown. * Used to manually trigger updates for the tab being shown.
*/ */
public static void fireNewChange(){ public void fireNewChange() {
for (ChangeListener ch : tabbedPane.getChangeListeners()){ for (ChangeListener ch : tabbedPane.getChangeListeners()){
ch.stateChanged(new ChangeEvent(tabbedPane)); ch.stateChanged(new ChangeEvent(tabbedPane));
} }

View File

@ -90,15 +90,17 @@ public class SettingsFrame extends TabLogoPanel {
private final ConfigurationManager _conf; private final ConfigurationManager _conf;
private final OverviewTab _otab; private final OverviewTab _otab;
private final Main _main;
/** /**
* Create the application. * Create the application.
*/ */
public SettingsFrame(String imageName, OverviewTab otab) { public SettingsFrame(String imageName, Main m, OverviewTab otab) {
super(imageName); super(imageName);
setLayout(null); setLayout(null);
_conf = ConfigurationManager.getInstance(); _conf = ConfigurationManager.getInstance();
_otab = otab; _otab = otab;
_main = m;
initialize(); initialize();
} }
@ -200,7 +202,7 @@ public class SettingsFrame extends TabLogoPanel {
newAddressStatus != REMOTE_SAVE_STATUS.SAVE_FAILED_LOCALLY && newAddressStatus != REMOTE_SAVE_STATUS.SAVE_FAILED_REMOTELY newAddressStatus != REMOTE_SAVE_STATUS.SAVE_FAILED_LOCALLY && newAddressStatus != REMOTE_SAVE_STATUS.SAVE_FAILED_REMOTELY
*/ */
) { ) {
Main.fireNewChange(); _main.fireNewChange();
} }
} }
}); });

View File

@ -28,45 +28,44 @@ import net.i2p.itoopie.util.IsJar;
*/ */
public class TrayManager { public class TrayManager {
private static TrayManager instance = null;
///The tray area, or null if unsupported ///The tray area, or null if unsupported
protected SystemTray tray = null; protected SystemTray tray;
///Our tray icon, or null if unsupported ///Our tray icon, or null if unsupported
protected TrayIcon trayIcon = null; protected TrayIcon trayIcon;
private final net.i2p.itoopie.Main main;
/** /**
* Instantiate tray manager. * Instantiate tray manager.
*/ */
protected TrayManager() {} public TrayManager(net.i2p.itoopie.Main m) {
main = m;
public static synchronized TrayManager getInstance() {
if(instance == null) {
instance = new TrayManager();
}
return instance;
} }
/** /**
* Add the tray icon to the system tray and start everything up. * Add the tray icon to the system tray and start everything up.
*/ */
public synchronized void startManager() { public synchronized void startManager() {
final WindowHandler windowHandler = new WindowHandler();
windowHandler.toggleFrames();
// so the tray icon works right on Gnome
try { Thread.sleep(500); } catch (InterruptedException ie) {}
SwingUtilities.invokeLater(new Runnable(){ SwingUtilities.invokeLater(new Runnable(){
public void run(){ public void run(){
if(SystemTray.isSupported()) { if(SystemTray.isSupported()) {
final Image img = IconLoader.getTrayImage();
tray = SystemTray.getSystemTray(); tray = SystemTray.getSystemTray();
trayIcon = new TrayIcon(IconLoader.getTrayImage(), "itoopie", getMainMenu()); trayIcon = new TrayIcon(img, "itoopie", getMainMenu());
trayIcon.setImageAutoSize(true); //Resize image to fit the system tray trayIcon.setImageAutoSize(true); //Resize image to fit the system tray
try {
tray.add(trayIcon);
} catch (AWTException e) { e.printStackTrace(); }
trayIcon.addMouseListener(new MouseAdapter(){ trayIcon.addMouseListener(new MouseAdapter(){
@Override @Override
public void mouseClicked(MouseEvent arg0) { public void mouseClicked(MouseEvent arg0) {
WindowHandler.toggleFrames(); windowHandler.toggleFrames();
} }
}); });
try {
tray.add(trayIcon);
} catch (AWTException e) {}
} }
} }
}); });
@ -106,7 +105,7 @@ public class TrayManager {
@Override @Override
protected Object doInBackground() throws Exception { protected Object doInBackground() throws Exception {
Main.beginShutdown(); main.beginShutdown();
return null; return null;
} }
}.execute(); }.execute();

View File

@ -6,19 +6,19 @@ import java.util.HashSet;
import javax.swing.JFrame; import javax.swing.JFrame;
public class WindowHandler { public class WindowHandler {
private static final HashSet<JFrame> _frames = new HashSet<JFrame>(); private final HashSet<JFrame> _frames = new HashSet<JFrame>();
private static JFrame mainFrame; private JFrame mainFrame;
private static boolean areFramesShown = false; private boolean areFramesShown;
public static void register(JFrame frame){ public void register(JFrame frame){
_frames.add(frame); _frames.add(frame);
} }
public static void registerMain(JFrame frame){ public void registerMain(JFrame frame){
mainFrame = frame; mainFrame = frame;
} }
public static void deRegister(JFrame frame){ public void deRegister(JFrame frame){
// don't remove the main frame when // don't remove the main frame when
// the user clicks on the X, so we have the updated // the user clicks on the X, so we have the updated
// graph when the user clicks on the icon again // graph when the user clicks on the icon again
@ -28,7 +28,7 @@ public class WindowHandler {
_frames.remove(frame); _frames.remove(frame);
} }
public static void hideFrames(){ public void hideFrames(){
for (JFrame frame : _frames){ for (JFrame frame : _frames){
frame.setVisible(false); frame.setVisible(false);
} }
@ -38,7 +38,7 @@ public class WindowHandler {
areFramesShown = false; areFramesShown = false;
} }
public static void showFrames(){ public void showFrames(){
for (JFrame frame : _frames){ for (JFrame frame : _frames){
frame.setVisible(true); frame.setVisible(true);
} }
@ -48,9 +48,9 @@ public class WindowHandler {
areFramesShown = true; areFramesShown = true;
} }
public static void toggleFrames(){ public void toggleFrames(){
if (_frames.isEmpty()){ if (_frames.isEmpty()){
new Main(); new Main(this);
} else { } else {
if (areFramesShown){ if (areFramesShown){
hideFrames(); hideFrames();

View File

@ -10,25 +10,18 @@ import net.i2p.itoopie.util.IconLoader;
public class RegisteredFrame extends JFrame implements WindowListener{ public class RegisteredFrame extends JFrame implements WindowListener{
private static final long serialVersionUID = 3351260168651061327L; private static final long serialVersionUID = 3351260168651061327L;
private final WindowHandler windowHandler;
public RegisteredFrame(String name) { public RegisteredFrame(String name, WindowHandler wh) {
super(name); super(name);
super.addWindowListener(this); super.addWindowListener(this);
WindowHandler.register(this); windowHandler = wh;
windowHandler.register(this);
this.setIconImage(IconLoader.getIcon("itoopie", 128)); this.setIconImage(IconLoader.getIcon("itoopie", 128));
} }
public RegisteredFrame() {
super();
super.addWindowListener(this);
WindowHandler.register(this);
this.setIconImage(IconLoader.getIcon("itoopie", 128));
}
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
WindowHandler.deRegister(this); windowHandler.deRegister(this);
this.dispose(); this.dispose();
} }

View File

@ -73,7 +73,8 @@ public class JSONRPC2Interface {
} }
public static void testSettings() throws InvalidPasswordException, JSONRPC2SessionException{ public static void testSettings() throws InvalidPasswordException, JSONRPC2SessionException{
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier()); // set in gui/Main
//HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier());
setupSession(); setupSession();
Authenticate.execute(); Authenticate.execute();
} }

View File

@ -32,11 +32,7 @@ import net.i2p.itoopie.security.ItoopieHostnameVerifier;
*/ */
public class ReseedMonitor extends TimerTask{ public class ReseedMonitor extends TimerTask{
private static final Long MIN_KNOWN_PEERS = new Long(30); private static final Long MIN_KNOWN_PEERS = new Long(30);
private static final Log _log; private final Log _log = LogFactory.getLog(ReseedMonitor.class);
static {
_log = LogFactory.getLog(ReseedMonitor.class);
}
@Override @Override
public void run(){ public void run(){
@ -60,6 +56,7 @@ public class ReseedMonitor extends TimerTask{
} }
/*
public static void main(String[] args){ public static void main(String[] args){
System.out.println("Reading config file.."); System.out.println("Reading config file..");
ConfigurationManager _conf = ConfigurationManager.getInstance(); ConfigurationManager _conf = ConfigurationManager.getInstance();
@ -105,5 +102,5 @@ public class ReseedMonitor extends TimerTask{
e.printStackTrace(); e.printStackTrace();
} }
} }
*/
} }

View File

@ -11,11 +11,17 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import net.i2p.itoopie.gui.CertificateGUI; import net.i2p.itoopie.gui.CertificateGUI;
import net.i2p.itoopie.gui.Main;
public class ItoopieHostnameVerifier implements HostnameVerifier { public class ItoopieHostnameVerifier implements HostnameVerifier {
private static final Log _log = LogFactory.getLog(ItoopieHostnameVerifier.class); private final Log _log = LogFactory.getLog(ItoopieHostnameVerifier.class);
private static final HashSet<String> recentlyDeniedHosts = new HashSet<String>(); private static final HashSet<String> recentlyDeniedHosts = new HashSet<String>();
private static final Object _uiLock = new Object(); private static final Object _uiLock = new Object();
private final Main _main;
public ItoopieHostnameVerifier(Main main) {
_main = main;
}
public boolean verify(String urlHostName, SSLSession session) { public boolean verify(String urlHostName, SSLSession session) {
String serverHost = session.getPeerHost() + ":" + session.getPeerPort(); String serverHost = session.getPeerHost() + ":" + session.getPeerPort();
@ -32,7 +38,7 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
return true; // Remote host has provided valid certificate that is stored locally. return true; // Remote host has provided valid certificate that is stored locally.
} else { } else {
// Remote host has provided a certificate that != the stored certificate for this host // Remote host has provided a certificate that != the stored certificate for this host
if (CertificateGUI.overwriteCert(serverHost, certs[0])) { if (CertificateGUI.overwriteCert(_main, serverHost, certs[0])) {
return true; return true;
} else { } else {
recentlyDeniedHosts.add(session.getPeerHost() + ":" + session.getPeerPort()); recentlyDeniedHosts.add(session.getPeerHost() + ":" + session.getPeerPort());
@ -41,7 +47,7 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
} }
} else { } else {
// GUI, Add new host! new host // GUI, Add new host! new host
if (CertificateGUI.saveNewCert(serverHost, certs[0])) { if (CertificateGUI.saveNewCert(_main, serverHost, certs[0])) {
return true; return true;
} else { } else {
recentlyDeniedHosts.add(session.getPeerHost() + ":" + session.getPeerPort()); recentlyDeniedHosts.add(session.getPeerHost() + ":" + session.getPeerPort());
@ -60,6 +66,8 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
* host that has had it's certificate denied recently. * host that has had it's certificate denied recently.
*/ */
public static void clearRecentlyDenied() { public static void clearRecentlyDenied() {
synchronized (_uiLock) {
recentlyDeniedHosts.clear(); recentlyDeniedHosts.clear();
} }
} }
}