Un-static part 2: ConfigurationManager

This commit is contained in:
zzz
2022-01-11 13:08:48 -05:00
parent 25dd896d26
commit d8c85586b0
19 changed files with 119 additions and 127 deletions

View File

@ -16,10 +16,6 @@ import javax.net.ssl.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.i2p.itoopie.i2pcontrol.JSONRPC2Interface;
import net.i2p.itoopie.security.CertificateHelper;
import net.i2p.itoopie.security.CertificateManager;
import com.thetransactioncompany.jsonrpc2.*;

View File

@ -50,7 +50,7 @@ public class Main {
private final Log _log;
public Main() {
_conf = ConfigurationManager.getInstance();
_conf = new ConfigurationManager();
_log = LogFactory.getLog(Main.class);
}
@ -59,7 +59,8 @@ public class Main {
* @throws Exception
*/
public void startUp() throws Exception {
trayManager = new TrayManager(this);
JSONRPC2Interface.setupSession(_conf);
trayManager = new TrayManager(this, _conf);
trayManager.startManager();
}

View File

@ -24,7 +24,7 @@ import org.apache.commons.logging.LogFactory;
*/
public class ConfigurationManager {
private static final String DEFAULT_CONFIG_NAME = "itoopie.conf";
private static final Log _log = LogFactory.getLog(ConfigurationManager.class);
private final Log _log = LogFactory.getLog(ConfigurationManager.class);
private static final String APP_DIR_NAME = "itoopie";
/**
* For plugin
@ -32,25 +32,17 @@ public class ConfigurationManager {
public static final String PROP_CONF_DIR = "itoopie.confdir";
private static ConfigurationManager instance;
//Configurations with a String as value
private static Map<String, String> stringConfigurations = new HashMap<String, String>();
private Map<String, String> stringConfigurations = new HashMap<String, String>();
//Configurations with a Boolean as value
private static Map<String, Boolean> booleanConfigurations = new HashMap<String, Boolean>();
private Map<String, Boolean> booleanConfigurations = new HashMap<String, Boolean>();
//Configurations with an Integer as value
private static Map<String, Integer> integerConfigurations = new HashMap<String, Integer>();
private Map<String, Integer> integerConfigurations = new HashMap<String, Integer>();
private ConfigurationManager() {
public ConfigurationManager() {
readConfFile();
}
public synchronized static ConfigurationManager getInstance() {
if(instance == null) {
instance = new ConfigurationManager();
}
return instance;
}
/**
* Collects settingNameuments of the form --word, --word=otherword and -blah
* to determine user parameters.
@ -68,7 +60,7 @@ public class ConfigurationManager {
/**
* Reads configuration from file itoopie.conf, every line is parsed as key=value.
*/
public static void readConfFile(){
public void readConfFile(){
File f = new File(getAppConfDir(), DEFAULT_CONFIG_NAME);
try {
BufferedReader br = new BufferedReader(new FileReader(f));
@ -87,7 +79,7 @@ public class ConfigurationManager {
/**
* Write configuration into default config file.
*/
public static void writeConfFile(){
public void writeConfFile(){
TreeMap<String,String> tree = new TreeMap<String,String>();
for (Entry<String,String> e : stringConfigurations.entrySet()){
tree.put(e.getKey(), e.getValue());
@ -115,7 +107,7 @@ public class ConfigurationManager {
* where value will (in order) be parsed as integer/boolean/string.
* @param str
*/
public static void parseConfigStr(String str){
public void parseConfigStr(String str){
int eqIndex = str.indexOf('=');
if (eqIndex != -1){
String key = str.substring(0, eqIndex).trim().toLowerCase();
@ -219,7 +211,7 @@ public class ConfigurationManager {
* Get the file path to the configuration directory. If the directory does not yet exist, creates it.
* @return Application configuration directory.
*/
public static File getAppConfDir() {
public File getAppConfDir() {
String dir;
// for plugin
String override = System.getProperty(PROP_CONF_DIR);

View File

@ -1,6 +1,7 @@
package net.i2p.itoopie.gui;
import java.awt.BorderLayout;
import java.io.File;
import javax.security.cert.X509Certificate;
import javax.swing.BoxLayout;
@ -22,7 +23,7 @@ public class CertificateGUI {
}
*/
public static synchronized boolean saveNewCert(Main main, String hostname, X509Certificate cert){
public static synchronized boolean saveNewCert(Main main, File dir, String hostname, X509Certificate cert){
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JButton bt = new JButton();
@ -59,7 +60,7 @@ public class CertificateGUI {
JOptionPane.INFORMATION_MESSAGE);
if (n == JOptionPane.YES_OPTION){
CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert));
CertificateManager.forcePutServerCert(dir, hostname, CertificateHelper.convert(cert));
updateUI(main);
return true;
} else {
@ -68,7 +69,7 @@ public class CertificateGUI {
}
public static boolean overwriteCert(Main main, String hostname, X509Certificate cert){
public static boolean overwriteCert(Main main, File dir, String hostname, X509Certificate cert){
JFrame frame = new JFrame();
String title = Transl._t("Warning, new remote host detected");
@ -109,7 +110,7 @@ public class CertificateGUI {
JOptionPane.YES_NO_OPTION,
JOptionPane.ERROR_MESSAGE);
if (n == JOptionPane.YES_OPTION){
CertificateManager.forcePutServerCert(hostname, CertificateHelper.convert(cert));
CertificateManager.forcePutServerCert(dir, hostname, CertificateHelper.convert(cert));
updateUI(main);
return true; // Confirmation positive
} else {

View File

@ -16,6 +16,7 @@ import javax.swing.JRootPane;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.gui.component.RegisteredFrame;
import net.i2p.itoopie.gui.component.TabLogoPanel;
import net.i2p.itoopie.gui.component.util.TabChangeListener;
@ -36,6 +37,7 @@ public class Main {
private JFrame frame;
private JTabbedPane tabbedPane;
private final WindowHandler windowHandler;
private final ConfigurationManager _conf;
public final static int FRAME_WIDTH = 550;
public final static int FRAME_HEIGHT = 400;
public final static int TABBED_PANE_HEIGHT = FRAME_HEIGHT - 66;
@ -61,9 +63,10 @@ public class Main {
/**
* Create the application.
*/
public Main(WindowHandler wh) {
public Main(WindowHandler wh, ConfigurationManager conf) {
windowHandler = wh;
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier(this));
_conf = conf;
HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier(this, conf.getAppConfDir()));
initialize();
}
@ -88,7 +91,7 @@ public class Main {
root.add(tabbedPane);
tabbedPane.setBounds(0, 0, FRAME_WIDTH-9, TABBED_PANE_HEIGHT);
OverviewTab overviewTab = new OverviewTab("itoopie-opaque12");
OverviewTab overviewTab = new OverviewTab("itoopie-opaque12", _conf);
tabbedPane.addTab(' ' + Transl._t("Overview") + ' ', null, overviewTab, null);
tabbedPane.addChangeListener(new TabChangeListener(overviewTab));
@ -100,7 +103,7 @@ public class Main {
// pass overview tab to settingsframe to reset the charts on change
TabLogoPanel settingsTab = new SettingsFrame("itoopie-opaque12", this, overviewTab);
TabLogoPanel settingsTab = new SettingsFrame("itoopie-opaque12", this, _conf, overviewTab);
tabbedPane.addTab(' ' + Transl._t("Settings") + ' ', icon, settingsTab, null);
tabbedPane.addChangeListener(new TabChangeListener(settingsTab));

View File

@ -32,7 +32,7 @@ import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
import net.i2p.itoopie.util.DataHelper;
public class OverviewTab extends TabLogoPanel {
private static ConfigurationManager _conf = ConfigurationManager.getInstance();
private final ConfigurationManager _conf;
private final static int DEFAULT_INFO_UPDATE_INTERVAL = 30*1000; // Milliseconds.
private final JLabel lblI2P;
@ -47,12 +47,13 @@ public class OverviewTab extends TabLogoPanel {
private final BandwidthChart bwChart;
private final ParticipatingTunnelsChart partTunnelChart;
public OverviewTab(String imageName) {
public OverviewTab(String imageName, ConfigurationManager conf) {
super(imageName);
_conf = conf;
super.setLayout(null);
bwChart = new BandwidthChart();
partTunnelChart = new ParticipatingTunnelsChart();
bwChart = new BandwidthChart(_conf);
partTunnelChart = new ParticipatingTunnelsChart(_conf);
ChartPanel pt = new ChartPanel(partTunnelChart);
pt.setSize(300, 135);
pt.setLocation(5, 10);
@ -211,6 +212,7 @@ public class OverviewTab extends TabLogoPanel {
/**
* Launch the application.
*/
/*
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
@ -227,6 +229,6 @@ public class OverviewTab extends TabLogoPanel {
});
}
*/
}

View File

@ -95,10 +95,10 @@ public class SettingsFrame extends TabLogoPanel {
/**
* Create the application.
*/
public SettingsFrame(String imageName, Main m, OverviewTab otab) {
public SettingsFrame(String imageName, Main m, ConfigurationManager conf, OverviewTab otab) {
super(imageName);
setLayout(null);
_conf = ConfigurationManager.getInstance();
_conf = conf;
_otab = otab;
_main = m;
initialize();
@ -393,7 +393,7 @@ public class SettingsFrame extends TabLogoPanel {
_conf.setConf("server.hostname", ipText);
_conf.setConf("server.port", port);
_conf.setConf("server.password", pwText);
JSONRPC2Interface.testSettings();
JSONRPC2Interface.testSettings(_conf);
if (!oldIP.equals(ipText) || oldPort != port)
_otab.clearGraphs();
} catch (InvalidPasswordException e) {

View File

@ -19,6 +19,7 @@ import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import net.i2p.itoopie.Main;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.i18n.Transl;
import net.i2p.itoopie.util.IconLoader;
import net.i2p.itoopie.util.IsJar;
@ -33,19 +34,21 @@ public class TrayManager {
///Our tray icon, or null if unsupported
protected TrayIcon trayIcon;
private final net.i2p.itoopie.Main main;
private final ConfigurationManager _conf;
/**
* Instantiate tray manager.
*/
public TrayManager(net.i2p.itoopie.Main m) {
public TrayManager(net.i2p.itoopie.Main m, ConfigurationManager conf) {
main = m;
_conf = conf;
}
/**
* Add the tray icon to the system tray and start everything up.
*/
public synchronized void startManager() {
final WindowHandler windowHandler = new WindowHandler();
final WindowHandler windowHandler = new WindowHandler(_conf);
windowHandler.toggleFrames();
// so the tray icon works right on Gnome
try { Thread.sleep(500); } catch (InterruptedException ie) {}

View File

@ -5,10 +5,17 @@ import java.util.HashSet;
import javax.swing.JFrame;
import net.i2p.itoopie.configuration.ConfigurationManager;
public class WindowHandler {
private final HashSet<JFrame> _frames = new HashSet<JFrame>();
private JFrame mainFrame;
private boolean areFramesShown;
private final ConfigurationManager _conf;
public WindowHandler(ConfigurationManager conf) {
_conf = conf;
}
public void register(JFrame frame){
_frames.add(frame);
@ -50,7 +57,7 @@ public class WindowHandler {
public void toggleFrames(){
if (_frames.isEmpty()){
new Main(this);
new Main(this, _conf);
} else {
if (areFramesShown){
hideFrames();

View File

@ -30,7 +30,7 @@ import net.i2p.itoopie.i18n.Transl;
public class BandwidthChart extends Chart2D{
private static ConfigurationManager _conf = ConfigurationManager.getInstance();
private final ConfigurationManager _conf;
private final static int DEFAULT_UPDATE_INTERVAL = 10000; // Update every 2500th ms
private final static int DEFAULT_GRAPH_INTERVAL = 3600*1000; // The graph will cover a maximum of this time
private ObjRecorder2Trace2DAdapter bwInAdapter;
@ -38,9 +38,9 @@ public class BandwidthChart extends Chart2D{
private InboundBandwidthTracker bwInTracker;
private OutboundBandwidthTracker bwOutTracker;
public BandwidthChart(){
public BandwidthChart(ConfigurationManager conf) {
super();
_conf = conf;
int updateInterval = _conf.getConf("graph.updateinterval", DEFAULT_UPDATE_INTERVAL);
int graphInterval = _conf.getConf("graph.graphinterval", DEFAULT_GRAPH_INTERVAL);
@ -83,14 +83,14 @@ public class BandwidthChart extends Chart2D{
// force ranges:
getAxisY().setRangePolicy(new RangePolicyMinimumViewport(new Range(0, 5)));
bwInTracker = new InboundBandwidthTracker();
bwOutTracker = new OutboundBandwidthTracker();
bwInTracker = new InboundBandwidthTracker(updateInterval);
bwOutTracker = new OutboundBandwidthTracker(updateInterval);
bwInAdapter = new ObjRecorder2Trace2DAdapter(dataBWIn, bwInTracker, "m_value", updateInterval/2);
bwOutAdapter = new ObjRecorder2Trace2DAdapter(dataBWOut, bwOutTracker, "m_value", updateInterval/2);
}
/*
public static void main(final String[] args) {
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
@ -102,4 +102,5 @@ public class BandwidthChart extends Chart2D{
frame.setResizable(true);
frame.setVisible(true);
}
*/
}

View File

@ -29,14 +29,15 @@ import net.i2p.itoopie.i18n.Transl;
public class ParticipatingTunnelsChart extends Chart2D {
private static ConfigurationManager _conf = ConfigurationManager.getInstance();
private final ConfigurationManager _conf;
private final static int DEFAULT_UPDATE_INTERVAL = 10000; // Update every 1000th ms
private final static int DEFAULT_GRAPH_INTERVAL = 3600*1000; // The graph will cover a maximum of this time
private ParticipatingTunnelsTracker partTunnelTracker;
private ObjRecorder2Trace2DAdapter partTunnelAdapter;
public ParticipatingTunnelsChart(){
public ParticipatingTunnelsChart(ConfigurationManager conf) {
super();
_conf = conf;
int updateInterval = _conf.getConf("graph.updateinterval", DEFAULT_UPDATE_INTERVAL);
int graphInterval = _conf.getConf("graph.graphinterval", DEFAULT_GRAPH_INTERVAL);
@ -71,10 +72,11 @@ public class ParticipatingTunnelsChart extends Chart2D {
// force ranges:
getAxisY().setRangePolicy(new RangePolicyMinimumViewport(new Range(0, 20)));
partTunnelTracker = new ParticipatingTunnelsTracker();
partTunnelTracker = new ParticipatingTunnelsTracker(updateInterval);
partTunnelAdapter = new ObjRecorder2Trace2DAdapter(dataPartTunnels, partTunnelTracker, "m_value", updateInterval/2);
}
/*
public static void main(final String[] args) {
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
@ -86,4 +88,5 @@ public class ParticipatingTunnelsChart extends Chart2D {
frame.setResizable(true);
frame.setVisible(true);
}
*/
}

View File

@ -5,7 +5,6 @@ import java.util.HashMap;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.methods.GetRateStat;
@ -14,19 +13,15 @@ import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
public class InboundBandwidthTracker extends Thread implements Tracker {
private static ConfigurationManager _conf = ConfigurationManager.getInstance();
/** Last read bw */
private double m_value = 0;
/** Poll router for current ratestat every updateInterval seconds */
private final static int DEFAULT_UPDATE_INTERVAL = 100; // Update every 100th ms
private int updateInterval = _conf.getConf("graph.updateinterval", DEFAULT_UPDATE_INTERVAL);
private final int updateInterval;
/**
* Start daemon that checks to current inbound bandwidth of the router.
*/
public InboundBandwidthTracker() {
public InboundBandwidthTracker(int interval) {
updateInterval = interval;
this.setDaemon(true);
this.start();
}

View File

@ -5,7 +5,6 @@ import java.util.HashMap;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.methods.GetRateStat;
@ -14,19 +13,15 @@ import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
public class OutboundBandwidthTracker extends Thread implements Tracker {
private static ConfigurationManager _conf = ConfigurationManager.getInstance();
/** Last read bw */
private double m_value = 0;
/** Poll router for current ratestat every updateInterval seconds */
private final static int DEFAULT_UPDATE_INTERVAL = 100; // Update every 100th ms
private int updateInterval = _conf.getConf("graph.updateinterval", DEFAULT_UPDATE_INTERVAL);
private final int updateInterval;
/**
* Start daemon that checks to current inbound bandwidth of the router.
*/
public OutboundBandwidthTracker() {
public OutboundBandwidthTracker(int interval) {
updateInterval = interval;
this.setDaemon(true);
this.start();
}

View File

@ -14,19 +14,15 @@ import net.i2p.itoopie.i2pcontrol.methods.RouterInfo.ROUTER_INFO;
public class ParticipatingTunnelsTracker extends Thread implements Tracker {
private static ConfigurationManager _conf = ConfigurationManager.getInstance();
/** Last read bw */
private double m_value = 0;
/** Poll router for current ratestat every updateInterval seconds */
private final static int DEFAULT_UPDATE_INTERVAL = 100; // Update every 100th ms
private int updateInterval = _conf.getConf("graph.updateinterval", DEFAULT_UPDATE_INTERVAL);
private final int updateInterval;
/**
* Start daemon that checks to current inbound bandwidth of the router.
*/
public ParticipatingTunnelsTracker() {
public ParticipatingTunnelsTracker(int interval) {
updateInterval = interval;
this.setDaemon(true);
this.start();
}

View File

@ -2,21 +2,18 @@ package net.i2p.itoopie.gui.component.chart;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.i2pcontrol.InvalidParametersException;
import net.i2p.itoopie.i2pcontrol.InvalidPasswordException;
import net.i2p.itoopie.i2pcontrol.methods.GetRateStat;
/**
* Unused
*/
public class RateStatTracker extends Thread implements Tracker {
private static ConfigurationManager _conf = ConfigurationManager.getInstance();
/** Last read bw */
private double m_value = 0;
/** Poll router for current ratestat every updateInterval seconds */
private final static int DEFAULT_UPDATE_INTERVAL = 100; // Update every 100th ms
private int updateInterval = _conf.getConf("graph.updateinterval", DEFAULT_UPDATE_INTERVAL);
private final int updateInterval;
/** Which RateStat to measure from the router */
private String rateStat;
@ -27,9 +24,10 @@ public class RateStatTracker extends Thread implements Tracker {
/**
* Start daemon that checks to current inbound bandwidth of the router.
*/
public RateStatTracker(String rateStat, long period) {
public RateStatTracker(String rateStat, long period, int interval) {
this.rateStat = rateStat;
this.period = period;
updateInterval = interval;
this.setDaemon(true);
this.start();
}

View File

@ -10,8 +10,6 @@ import javax.net.ssl.HttpsURLConnection;
import net.i2p.itoopie.ItoopieVersion;
import net.i2p.itoopie.configuration.ConfigurationManager;
import net.i2p.itoopie.i2pcontrol.methods.Authenticate;
import net.i2p.itoopie.security.CertificateHelper;
import net.i2p.itoopie.security.ItoopieHostnameVerifier;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -24,31 +22,30 @@ import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
public class JSONRPC2Interface {
private static Log _log;
private static ConfigurationManager _conf;
private static int nonce;
private static final int MAX_NBR_RETRIES = 2;
private static JSONRPC2Session session;
private static String token;
private final static String DEFAULT_PASSWORD = "itoopie";
private static String pw = DEFAULT_PASSWORD;
static {
_log = LogFactory.getLog(JSONRPC2Interface.class);
_conf = ConfigurationManager.getInstance();
Random rnd = new Random();
nonce = rnd.nextInt();
setupSession();
}
public static synchronized int incrNonce() {
return ++nonce;
}
public static void setupSession() {
public static void setupSession(ConfigurationManager conf) {
URL srvURL = null;
String srvHost = _conf.getConf("server.hostname", "localhost");
String srvHost = conf.getConf("server.hostname", "localhost");
if (srvHost.contains(":"))
srvHost = '[' + srvHost + ']';
int srvPort = _conf.getConf("server.port", 7650);
String srvTarget = _conf.getConf("server.target", "jsonrpc");
int srvPort = conf.getConf("server.port", 7650);
String srvTarget = conf.getConf("server.target", "jsonrpc");
String method;
if (srvPort == 7657) {
// Use HTTP for the xmlrpc webapp in the HTTP router console
@ -68,15 +65,16 @@ public class JSONRPC2Interface {
_log.error("Bad URL: " + method + "://" + srvHost + ":" + srvPort + "/"
+ srvTarget, e);
}
pw = conf.getConf("server.password", DEFAULT_PASSWORD);
session = new JSONRPC2Session(srvURL);
session.trustAllCerts(true);
}
public static void testSettings() throws InvalidPasswordException, JSONRPC2SessionException{
public static void testSettings(ConfigurationManager conf) throws InvalidPasswordException, JSONRPC2SessionException{
// set in gui/Main
//HttpsURLConnection.setDefaultHostnameVerifier(new ItoopieHostnameVerifier());
setupSession();
Authenticate.execute();
setupSession(conf);
Authenticate.execute(pw);
}
public static JSONRPC2Response sendReq(JSONRPC2Request req)
@ -133,17 +131,17 @@ public class JSONRPC2Interface {
// break;
case -32002:
// No token
token = Authenticate.execute();
token = Authenticate.execute(pw);
throw new FailedRequestException();
// break;
case -32003:
// Invalid token
token = Authenticate.execute();
token = Authenticate.execute(pw);
throw new FailedRequestException();
//break;
case -32004:
// Token expired
token = Authenticate.execute();
token = Authenticate.execute(pw);
throw new FailedRequestException();
// break;
case -32005:

View File

@ -18,17 +18,13 @@ import com.thetransactioncompany.jsonrpc2.JSONRPC2Response;
import com.thetransactioncompany.jsonrpc2.client.JSONRPC2SessionException;
public class Authenticate {
private final static ConfigurationManager _conf = ConfigurationManager.getInstance();
private final static Log _log = LogFactory.getLog(Authenticate.class);
private final static String DEFAULT_PASSWORD = "itoopie";
public static String execute()
public static String execute(String password)
throws InvalidPasswordException, JSONRPC2SessionException {
JSONRPC2Request req = new JSONRPC2Request("Authenticate", JSONRPC2Interface.incrNonce());
Map outParams = new HashMap();
outParams.put("Password",
_conf.getConf("server.password", _conf.getConf("server.password", DEFAULT_PASSWORD)));
outParams.put("Password", password);
outParams.put("API", ItoopieVersion.I2PCONTROL_API_VERSION);
req.setParams(outParams);
@ -40,6 +36,7 @@ public class Authenticate {
}catch (UnrecoverableFailedRequestException e) {
return null; // Shouldn't normally happen.
} catch (InvalidParametersException e) {
Log _log = LogFactory.getLog(Authenticate.class);
_log.error("getNewToken() invalid parameters used");
}
return null;

View File

@ -22,8 +22,6 @@ import java.security.cert.X509Certificate;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import net.i2p.itoopie.configuration.ConfigurationManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -44,9 +42,9 @@ public class CertificateManager {
}
public static boolean verifyCert(String storedCertAlias, X509Certificate cert){
public static boolean verifyCert(File dir, String storedCertAlias, X509Certificate cert) {
try {
X509Certificate storedCert = (X509Certificate) getDefaultKeyStore().getCertificate(storedCertAlias);
X509Certificate storedCert = (X509Certificate) getDefaultKeyStore(dir).getCertificate(storedCertAlias);
storedCert.verify(cert.getPublicKey());
return true;
} catch (KeyStoreException e) {
@ -56,9 +54,9 @@ public class CertificateManager {
}
}
public static boolean contains(String certName) {
public static boolean contains(File dir, String certName) {
try {
return getDefaultKeyStore().containsAlias(certName);
return getDefaultKeyStore(dir).containsAlias(certName);
} catch (KeyStoreException e) {
_log.error("Error reading certificate with alias, " + certName + " from KeyStore", e);
}
@ -73,6 +71,7 @@ public class CertificateManager {
* @param cert - X509Certificate to store
* @return - True if store was successful, false in other cases.
*/
/*
public static boolean putServerCert(String name, X509Certificate cert) {
KeyStore ks = getDefaultKeyStore();
try {
@ -89,6 +88,7 @@ public class CertificateManager {
}
return false;
}
*/
/**
@ -98,11 +98,11 @@ public class CertificateManager {
* @param cert - X509Certificate to store
* @return - True if store was successful, false in other cases.
*/
public static boolean forcePutServerCert(String name, X509Certificate cert) {
KeyStore ks = getDefaultKeyStore();
public static boolean forcePutServerCert(File dir, String name, X509Certificate cert) {
KeyStore ks = getDefaultKeyStore(dir);
try {
ks.setCertificateEntry(name, cert);
saveKeyStore(ks);
saveKeyStore(ks, dir);
return true;
} catch (KeyStoreException e) {
e.printStackTrace();
@ -118,14 +118,14 @@ public class CertificateManager {
* @param cert - X509Certificate to overwrite
* @return - True if the overwrite was successful, false in other cases
*/
public static boolean overwriteServerCert(String name, X509Certificate cert){
KeyStore ks = getDefaultKeyStore();
public static boolean overwriteServerCert(File dir, String name, X509Certificate cert){
KeyStore ks = getDefaultKeyStore(dir);
try {
if (ks.containsAlias(name)){
return false;
} else {
getDefaultKeyStore().setCertificateEntry(name, cert);
saveKeyStore(ks);
getDefaultKeyStore(dir).setCertificateEntry(name, cert);
saveKeyStore(ks, dir);
return true;
}
} catch (KeyStoreException e) {
@ -138,6 +138,7 @@ public class CertificateManager {
* Get trustManagers for the currently loaded certificates
* @return - Returns trustmanagers for currently loaded certificates
*/
/*
public static TrustManager[] getTrustManagers(){
try {
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509","SunJSSE");
@ -152,17 +153,17 @@ public class CertificateManager {
}
return null;
}
*/
/**
* Get KeyStore containing server certs.
* @return - KeyStore used for keeping track of server.
*/
private static synchronized KeyStore getDefaultKeyStore(){
private static synchronized KeyStore getDefaultKeyStore(File dir) {
if (_ks == null){
try {
_ks = KeyStore.getInstance(DEFAULT_KEYSTORE_TYPE);
File f = new File(ConfigurationManager.getAppConfDir(), DEFAULT_KEYSTORE_LOCATION);
File f = new File(dir, DEFAULT_KEYSTORE_LOCATION);
if (f.exists()) {
InputStream is = new FileInputStream(f);
_ks.load(is, DEFAULT_KEYSTORE_PASSWORD.toCharArray());
@ -176,7 +177,7 @@ public class CertificateManager {
try {
_ks = KeyStore.getInstance(DEFAULT_KEYSTORE_TYPE);
_ks.load(null, DEFAULT_KEYSTORE_PASSWORD.toCharArray());
saveKeyStore(_ks);
saveKeyStore(_ks, dir);
return _ks;
} catch (Exception e){
// Log perhaps?
@ -187,9 +188,9 @@ public class CertificateManager {
}
}
private static void saveKeyStore(KeyStore ks){
private static void saveKeyStore(KeyStore ks, File dir) {
try {
ks.store(new FileOutputStream(new File(ConfigurationManager.getAppConfDir(), DEFAULT_KEYSTORE_LOCATION)), DEFAULT_KEYSTORE_PASSWORD.toCharArray());
ks.store(new FileOutputStream(new File(dir, DEFAULT_KEYSTORE_LOCATION)), DEFAULT_KEYSTORE_PASSWORD.toCharArray());
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();

View File

@ -1,5 +1,6 @@
package net.i2p.itoopie.security;
import java.io.File;
import java.util.HashSet;
import javax.net.ssl.HostnameVerifier;
@ -18,9 +19,11 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
private static final HashSet<String> recentlyDeniedHosts = new HashSet<String>();
private static final Object _uiLock = new Object();
private final Main _main;
private final File _dir;
public ItoopieHostnameVerifier(Main main) {
public ItoopieHostnameVerifier(Main main, File dir) {
_main = main;
_dir = dir;
}
public boolean verify(String urlHostName, SSLSession session) {
@ -33,12 +36,12 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
return false; // Deny recently denied hosts.
}
if (CertificateManager.contains(serverHost)) {
if (CertificateManager.verifyCert(serverHost, CertificateHelper.convert(certs[0]))) {
if (CertificateManager.contains(_dir, serverHost)) {
if (CertificateManager.verifyCert(_dir, serverHost, CertificateHelper.convert(certs[0]))) {
return true; // Remote host has provided valid certificate that is stored locally.
} else {
// Remote host has provided a certificate that != the stored certificate for this host
if (CertificateGUI.overwriteCert(_main, serverHost, certs[0])) {
if (CertificateGUI.overwriteCert(_main, _dir, serverHost, certs[0])) {
return true;
} else {
recentlyDeniedHosts.add(session.getPeerHost() + ":" + session.getPeerPort());
@ -47,7 +50,7 @@ public class ItoopieHostnameVerifier implements HostnameVerifier {
}
} else {
// GUI, Add new host! new host
if (CertificateGUI.saveNewCert(_main, serverHost, certs[0])) {
if (CertificateGUI.saveNewCert(_main, _dir, serverHost, certs[0])) {
return true;
} else {
recentlyDeniedHosts.add(session.getPeerHost() + ":" + session.getPeerPort());