Changed certificates to be specific not only to host but also to port.

Rewrote settings dialog to verify settings in a more sane way.
This commit is contained in:
dev
2011-07-14 07:29:07 +00:00
parent 568d2ab389
commit c33b6c1188
3 changed files with 91 additions and 76 deletions

View File

@ -152,6 +152,14 @@ public class Settings extends RegisteredFrame{
populateSettings(); populateSettings();
} }
@Override
public void setVisible(boolean isVisible){
if (isVisible){
populateSettings();
}
super.setVisible(isVisible);
}
private void populateSettings(){ private void populateSettings(){
textFieldRouterIP.setText(_conf.getConf("server.hostname", "127.0.0.1")); textFieldRouterIP.setText(_conf.getConf("server.hostname", "127.0.0.1"));
textFieldRouterPort.setText(_conf.getConf("server.port", 7560)+""); textFieldRouterPort.setText(_conf.getConf("server.port", 7560)+"");
@ -160,89 +168,96 @@ public class Settings extends RegisteredFrame{
@SuppressWarnings("static-access") @SuppressWarnings("static-access")
private int saveSettings(){ private int saveSettings(){
boolean newSettings = false; String oldIP = _conf.getConf("server.hostname", "127.0.0.1");
int oldPort = _conf.getConf("server.port", 7560);
String oldPW = _conf.getConf("server.password", "itoopie");
String ipText = textFieldRouterIP.getText(); String ipText = textFieldRouterIP.getText();
if (!ipText.equals(_conf.getConf("server.hostname", "127.0.0.1"))){ try {
System.out.println("Password changed: \""+_conf.getConf("server.hostname","127.0.0.1")+"\"->\"" + ipText + "\""); InetAddress.getByName(ipText);
try { } catch (UnknownHostException e) {
InetAddress.getByName(ipText); JOptionPane.showConfirmDialog(
newSettings = true; this,
_conf.setConf("server.hostname", ipText); Transl._(ipText + " can not be interpreted as an ip address.\r\n" +
} catch (UnknownHostException e) { "\r\nTry entering the ip address of the machine running i2p."),
JOptionPane.showConfirmDialog( Transl._("Invalid ip address."),
this, JOptionPane.DEFAULT_OPTION,
Transl._(ipText + " can not be interpreted as an ip address.\r\n" + JOptionPane.ERROR_MESSAGE);
"\r\nTry entering the ip address of the machine running i2p."), return SAVE_ERROR;
Transl._("Invalid ip address."),
JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE);
return SAVE_ERROR;
}
} }
String portText = textFieldRouterPort.getText(); String portText = textFieldRouterPort.getText();
if (!portText.equals(_conf.getConf("server.port",7560)+"")){ int port = 0;
System.out.println("Password changed: \""+_conf.getConf("server.port",7560)+"\"->\"" + portText + "\""); try {
try { port = Integer.parseInt(portText);
int nbr = Integer.parseInt(portText); if (port > 65535 || port <= 0)
if (nbr > 65535 || nbr <= 0) throw new NumberFormatException();
throw new NumberFormatException(); } catch (NumberFormatException e){
newSettings = true; JOptionPane.showConfirmDialog(
_conf.setConf("server.port", nbr); this,
} catch (NumberFormatException e){ Transl._(portText + " can not be interpreted as a port.\r\n" +
JOptionPane.showConfirmDialog( "\r\nA port has to be a number in the range 1-65535."),
this, Transl._("Invalid port."),
Transl._(portText + " can not be interpreted as a port.\r\n" + JOptionPane.DEFAULT_OPTION,
"\r\nA port has to be a number in the range 1-65535."), JOptionPane.ERROR_MESSAGE);
Transl._("Invalid port."), return SAVE_ERROR;
JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE);
return SAVE_ERROR;
}
} }
String pwText = new String(passwordField.getPassword()); String pwText = new String(passwordField.getPassword());
String oldPW = _conf.getConf("server.password", "itoopie"); try {
if (!pwText.equals(oldPW)){ _conf.setConf("server.hostname", ipText);
try { _conf.setConf("server.port", port);
System.out.println("Password changed: \""+oldPW+"\"->\"" + pwText + "\""); _conf.setConf("server.password", pwText);
_conf.setConf("server.password", pwText); JSONRPC2Interface.testSettings();
JSONRPC2Interface.testSettings(); } catch (InvalidPasswordException e) {
newSettings = true; _conf.setConf("server.password", oldPW);
} catch (InvalidPasswordException e) { JOptionPane.showConfirmDialog(
_conf.setConf("server.password", oldPW); this,
JOptionPane.showConfirmDialog( Transl._("The password was not accepted as valid by the specified host.\r\n" +
this, "\r\n(by default the password is, \"itoopie\")"),
Transl._("The password was not accepted as valid by the specified host.\r\n" + Transl._("Rejected password."),
"\r\nPassword was reset to the default password, \"itoopie\"."), JOptionPane.DEFAULT_OPTION,
Transl._("Rejected password."), JOptionPane.ERROR_MESSAGE);
JOptionPane.DEFAULT_OPTION, return SAVE_ERROR;
JOptionPane.ERROR_MESSAGE); } catch (JSONRPC2SessionException e) {
return SAVE_ERROR; _conf.setConf("server.hostname", oldIP);
} catch (JSONRPC2SessionException e) { _conf.setConf("server.port", oldPort);
e.printStackTrace(); _conf.setConf("server.password", oldPW);
JOptionPane.showConfirmDialog( JOptionPane.showConfirmDialog(
this, this,
Transl._("The remote host at the ip and port did not respond.\r\n" + Transl._("The remote host at the ip and port did not respond.\r\n" +
"\r\nMaybe I2PControl is not running on the remote I2P router, \r\n" + "\r\nMaybe I2PControl is not running on the remote I2P router, \r\n" +
"maybe the I2P router is not started."), "maybe the I2P router is not started."),
Transl._("Connection failed."), Transl._("Connection failed."),
JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
return SAVE_ERROR; return SAVE_ERROR;
}
_conf.setConf("server.hostname", ipText);
_conf.setConf("server.port", port);
_conf.setConf("server.password", pwText);
System.out.println("Ip old->new: \""+_conf.getConf("server.hostname","127.0.0.1")+"\"->\"" + ipText + "\"");
System.out.println("Port old->new: \""+_conf.getConf("server.port",7560)+"\"->\"" + portText + "\"");
System.out.println("Password old->new: \""+oldPW+"\"->\"" + pwText + "\"");
StatusHandler.setStatus("Settings saved");
(new Thread() {
@Override
public void run(){
_conf.writeConfFile();
} }
} }).start();
if (newSettings){
StatusHandler.setStatus("Settings saved");
(new Thread() {
@Override
public void run(){
_conf.writeConfFile();
}
}).start();
}
return SAVE_OK; return SAVE_OK;
} }
/**
* Used to signify that a setting was not accepted.
* @author hottuna
*
*/
private class BadSettingsException extends Exception{ }
} }

View File

@ -130,7 +130,7 @@ public class JSONRPC2Interface {
} }
} }
return resp; return resp;
}catch (FailedRequestException e) { } catch (FailedRequestException e) {
return sendReq(req, ++tryNbr); return sendReq(req, ++tryNbr);
} }
} }

View File

@ -116,7 +116,7 @@ public class CertificateHelper {
return new HostnameVerifier(){ return new HostnameVerifier(){
public boolean verify(String urlHostName, SSLSession session) { public boolean verify(String urlHostName, SSLSession session) {
String serverHost = session.getPeerHost(); String serverHost = session.getPeerHost() + ":" + session.getPeerPort();
try { try {
javax.security.cert.X509Certificate[] certs = session.getPeerCertificateChain(); javax.security.cert.X509Certificate[] certs = session.getPeerCertificateChain();