Skip to content

Commit

Permalink
speedup / refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Aug 27, 2024
1 parent c2a6ff9 commit 4dae993
Show file tree
Hide file tree
Showing 29 changed files with 1,608 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,14 @@ You should also get your employer (if you work as a programmer) or school,
*/
package com.jdimension.jlawyer.client;

import com.jdimension.jlawyer.security.Crypto;
import com.jdimension.jlawyer.security.CachingCrypto;
import com.jdimension.jlawyer.security.CryptoProvider;
import java.awt.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.StringReader;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -686,15 +688,17 @@ public class ConnectionProfiles {
private static ConnectionProfiles instance = null;

private final String connectionDir = System.getProperty("user.home") + File.separator + ".j-lawyer-client" + File.separator + "connections";
private CachingCrypto crypto=null;

private ConnectionProfiles() {
private ConnectionProfiles() throws GeneralSecurityException {
File dir = new File(connectionDir);
if (!dir.exists()) {
dir.mkdirs();
}
this.crypto=CryptoProvider.newCrypto(System.getProperty("user.name").toCharArray());
}

public static synchronized ConnectionProfiles getInstance() {
public static synchronized ConnectionProfiles getInstance() throws GeneralSecurityException {
if (instance == null) {
instance = new ConnectionProfiles();
}
Expand Down Expand Up @@ -756,7 +760,7 @@ private ConnectionProfile loadProfile(Properties props, Component caller) {
String pwd = props.getProperty("sshpwd").trim();
try {
if (pwd.length() > 0) {
pwd = Crypto.decrypt(pwd, System.getProperty("user.name").toCharArray());
pwd = getCrypto().decrypt(pwd);
}
} catch (Throwable t) {
log.error("Unable to decrypt tunnel SSH password", t);
Expand Down Expand Up @@ -851,7 +855,7 @@ private Properties toProperties(ConnectionProfile profile) throws Exception {
props.setProperty("securitymode", profile.getSecurityMode());
props.setProperty("server", profile.getServer());
props.setProperty("sshhost", profile.getSshHost());
String pwd=Crypto.encrypt(profile.getSshPassword(), System.getProperty("user.name").toCharArray());
String pwd=getCrypto().encrypt(profile.getSshPassword());
props.setProperty("sshpwd", pwd);
props.setProperty("sshport", profile.getSshPort());
props.setProperty("sshtargetport", profile.getSshTargetPort());
Expand All @@ -860,4 +864,11 @@ private Properties toProperties(ConnectionProfile profile) throws Exception {
return props;
}

/**
* @return the crypto
*/
public CachingCrypto getCrypto() {
return crypto;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -670,15 +670,18 @@ You should also get your employer (if you work as a programmer) or school,
public class ImportConnectionProfileDialog extends javax.swing.JDialog {

private ConnectionProfile profile=null;

ConnectionProfiles connections=null;

/**
* Creates new form ImportConnectionProfileDialog
* @param parent
* @param modal
*/
public ImportConnectionProfileDialog(java.awt.Frame parent, boolean modal) {
public ImportConnectionProfileDialog(java.awt.Frame parent, boolean modal, ConnectionProfiles connections) {
super(parent, modal);
this.connections=connections;
initComponents();

}

/**
Expand Down Expand Up @@ -754,8 +757,7 @@ private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
}//GEN-LAST:event_cmdCancelActionPerformed

private void cmdImportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdImportActionPerformed
ConnectionProfiles connections=ConnectionProfiles.getInstance();
this.profile=connections.fromPropertiesString(this.jTextArea1.getText(), this);
this.profile=this.connections.fromPropertiesString(this.jTextArea1.getText(), this);
this.setVisible(false);
this.dispose();
}//GEN-LAST:event_cmdImportActionPerformed
Expand Down Expand Up @@ -793,7 +795,7 @@ public static void main(String args[]) {

/* Create and display the dialog */
java.awt.EventQueue.invokeLater(() -> {
ImportConnectionProfileDialog dialog = new ImportConnectionProfileDialog(new javax.swing.JFrame(), true);
ImportConnectionProfileDialog dialog = new ImportConnectionProfileDialog(new javax.swing.JFrame(), true, null);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
Expand Down
45 changes: 30 additions & 15 deletions j-lawyer-client/src/com/jdimension/jlawyer/client/LoginDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@
import com.jdimension.jlawyer.client.utils.ThreadUtils;
import com.jdimension.jlawyer.client.utils.VersionUtils;
import com.jdimension.jlawyer.jlawyerbox.BoxAccess;
import com.jdimension.jlawyer.security.Crypto;
import com.jdimension.jlawyer.services.JLawyerServiceLocator;
import com.jdimension.jlawyer.services.SecurityServiceRemote;
import com.jdimension.jlawyer.services.SystemManagementRemote;
Expand Down Expand Up @@ -710,6 +709,8 @@ public class LoginDialog extends javax.swing.JFrame {

private StartupSplashFrame splash = null;
private String initialStatus = null;

private ConnectionProfiles connections=null;

/**
* Creates new form LoginDialog
Expand All @@ -730,6 +731,14 @@ public LoginDialog(String initialStatus, String cmdHost, String cmdPort, String
initComponents();

this.lblAutoUpdate.setText(" ");

try {
this.connections=ConnectionProfiles.getInstance();
} catch (Throwable t) {
log.error("Could not instantiate cryptography", t);
JOptionPane.showMessageDialog(null, "Fehler im Kryptographiesystem:" + System.lineSeparator() + t.getMessage(), "Fehler", JOptionPane.ERROR_MESSAGE);
System.exit(-1);
}

Color highlightColor = new Color(DefaultColorTheme.COLOR_DARK_GREY.getRed(), DefaultColorTheme.COLOR_DARK_GREY.getGreen(), DefaultColorTheme.COLOR_DARK_GREY.getBlue(), 170);
this.jPanel5.setBackground(highlightColor);
Expand Down Expand Up @@ -843,7 +852,6 @@ public void focusGained(java.awt.event.FocusEvent evt) {
this.initialStatus = initialStatus;

ClientSettings settings = ClientSettings.getInstance();
ConnectionProfiles connections = ConnectionProfiles.getInstance();
String lastConnectionName = settings.getConfiguration(ClientSettings.CONF_LASTCONNECTION, null);
if (lastConnectionName == null) {
// migration and initialization logic - first call of the client or first call after an update
Expand All @@ -856,7 +864,7 @@ public void focusGained(java.awt.event.FocusEvent evt) {
String p = settings.getConfiguration(ClientSettings.CONF_LASTSSHPWD, "");
try {
if (p.length() > 0) {
p = Crypto.decrypt(p, System.getProperty("user.name").toCharArray());
p = this.connections.getCrypto().decrypt(p);
}
} catch (Throwable t) {
log.error("Unable to decrypt tunnel SSH password", t);
Expand All @@ -873,7 +881,7 @@ public void focusGained(java.awt.event.FocusEvent evt) {
}
}

List<ConnectionProfile> allProfiles = connections.getAllProfiles();
List<ConnectionProfile> allProfiles = this.connections.getAllProfiles();
DefaultComboBoxModel allConnections1 = new DefaultComboBoxModel();
DefaultComboBoxModel allConnections2 = new DefaultComboBoxModel();
for (ConnectionProfile p : allProfiles) {
Expand Down Expand Up @@ -1668,6 +1676,8 @@ private void loginPerformed(boolean saveProfile) {
launching = true;
this.cmdLogin.setEnabled(false);

log.info("login: initiated");
long loginStart=System.currentTimeMillis();
ClientSettings settings = ClientSettings.getInstance();
int sourcePort = -1;
if (this.rdSecTunnel.isSelected()) {
Expand Down Expand Up @@ -1701,6 +1711,7 @@ private void loginPerformed(boolean saveProfile) {
}
}

log.info("login: saving last settings");
// BEGIN: required because backup configuration dialog needs it for constructing URL for ad hoc backups
settings.setConfiguration(ClientSettings.CONF_LASTPORTDYN, this.txtPort.getText());
settings.setConfiguration(ClientSettings.CONF_LASTSERVER, this.txtServer.getText());
Expand All @@ -1714,6 +1725,7 @@ private void loginPerformed(boolean saveProfile) {
}
// END

log.info("login: initiating lookups");
Properties properties = new Properties();
if (this.rdSecSsl.isSelected()) {
//properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
Expand Down Expand Up @@ -1756,6 +1768,7 @@ private void loginPerformed(boolean saveProfile) {

settings.setLookupProperties(properties);

log.info("login: logging in...");
// try connecting to see whether the loginContext.login succeeded
try {
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());
Expand Down Expand Up @@ -1784,13 +1797,16 @@ private void loginPerformed(boolean saveProfile) {
return;
}

log.info("login: logged in.");
settings.setConfiguration(ClientSettings.CONF_LASTCONNECTION, this.cmbCurrentConnection.getSelectedItem().toString());

if (saveProfile) {
log.info("login: saving profile");
// save profile in case user changed the user name
this.cmdSaveProfileActionPerformed(null);
}

log.info("login: version check");
String serverVersion = VersionUtils.getServerVersion();
String currentClientVersion = VersionUtils.getFullClientVersion();
String latestClientVersion = VersionUtils.getLatestClientVersionForServer(serverVersion);
Expand Down Expand Up @@ -1863,6 +1879,7 @@ private void loginPerformed(boolean saveProfile) {

}

log.info("login: checking client-server compatibility");
if (!VersionUtils.isCompatible(serverVersion, currentClientVersion)) {
int response = JOptionPane.showConfirmDialog(this, java.text.MessageFormat.format(java.util.ResourceBundle.getBundle("com/jdimension/jlawyer/client/LoginDialog").getString("msg.compatibilitycheck.failed"), new Object[]{currentClientVersion, serverVersion, System.getProperty("line.separator")}), java.util.ResourceBundle.getBundle("com/jdimension/jlawyer/client/LoginDialog").getString("msg.compatibilitycheck"), JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.NO_OPTION) {
Expand All @@ -1874,6 +1891,7 @@ private void loginPerformed(boolean saveProfile) {

this.setVisible(false);

log.info("login: initiating splash frame");
splash = new StartupSplashFrame();

FrameUtils.centerFrame(splash, null);
Expand All @@ -1886,6 +1904,7 @@ private void loginPerformed(boolean saveProfile) {
splash.addStatus(System.getProperty("line.separator"));
splash.repaint();

log.info("login: launching splash thread, login procedure took " + (System.currentTimeMillis()-loginStart));
new Thread(new SplashThread(splash, settings, this)).start();
}

Expand All @@ -1902,8 +1921,7 @@ private void rdSecTunnelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-F
}//GEN-LAST:event_rdSecTunnelActionPerformed

private void cmbProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbProfileActionPerformed
ConnectionProfiles connections = ConnectionProfiles.getInstance();
ConnectionProfile profile = connections.getProfile(this.cmbProfile.getSelectedItem().toString());
ConnectionProfile profile = this.connections.getProfile(this.cmbProfile.getSelectedItem().toString());
if (profile != null) {

this.txtPort.setText(profile.getPort());
Expand Down Expand Up @@ -1938,8 +1956,7 @@ private void txtServerFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:even
}//GEN-LAST:event_txtServerFocusLost

private void cmdSaveProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSaveProfileActionPerformed
ConnectionProfiles profiles = ConnectionProfiles.getInstance();
ConnectionProfile profile = profiles.getProfile(this.cmbProfile.getSelectedItem().toString());
ConnectionProfile profile = this.connections.getProfile(this.cmbProfile.getSelectedItem().toString());
if (profile != null) {
profile.setPort(this.txtPort.getText());
profile.setSecurityMode(SECMODE_STANDARD);
Expand All @@ -1957,7 +1974,7 @@ private void cmdSaveProfileActionPerformed(java.awt.event.ActionEvent evt) {//GE
profile.setSshUser(this.txtSshUser.getText());
profile.setUser(this.txtUser.getText());
try {
profiles.saveProfile(profile);
this.connections.saveProfile(profile);
} catch (Exception ex) {
log.error("Unable to save profile", ex);
JOptionPane.showMessageDialog(this, "Profil konnte nicht gespeichert werden", "Profil speichern", JOptionPane.ERROR_MESSAGE);
Expand All @@ -1966,9 +1983,8 @@ private void cmdSaveProfileActionPerformed(java.awt.event.ActionEvent evt) {//GE
}//GEN-LAST:event_cmdSaveProfileActionPerformed

private void cmdDeleteProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteProfileActionPerformed
ConnectionProfiles profiles = ConnectionProfiles.getInstance();
String selected = this.cmbProfile.getSelectedItem().toString();
profiles.removeProfile(selected);
this.connections.removeProfile(selected);
((DefaultComboBoxModel) this.cmbProfile.getModel()).removeElement(selected);
((DefaultComboBoxModel) this.cmbCurrentConnection.getModel()).removeElement(selected);
this.cmbCurrentConnection.setSelectedItem(this.cmbCurrentConnection.getSelectedItem());
Expand All @@ -1980,14 +1996,13 @@ private void cmdAddProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN
return;
}

ConnectionProfiles profiles = ConnectionProfiles.getInstance();
ConnectionProfile newProfile = new ConnectionProfile();
newProfile.setName(newNameObject.toString());
newProfile.setServer("localhost");
newProfile.setPort("8080");
newProfile.setSecurityMode(SECMODE_STANDARD);
try {
profiles.addProfile(newProfile);
this.connections.addProfile(newProfile);
((DefaultComboBoxModel) this.cmbProfile.getModel()).addElement(newNameObject.toString());
((DefaultComboBoxModel) this.cmbCurrentConnection.getModel()).addElement(newNameObject.toString());
this.cmbCurrentConnection.setSelectedItem(newNameObject.toString());
Expand All @@ -1999,14 +2014,14 @@ private void cmdAddProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN
}//GEN-LAST:event_cmdAddProfileActionPerformed

private void cmdImportProfileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdImportProfileActionPerformed
ImportConnectionProfileDialog dlg = new ImportConnectionProfileDialog(this, true);
ImportConnectionProfileDialog dlg = new ImportConnectionProfileDialog(this, true,this.connections);
dlg.setTitle("Profil aus Zwischenablage einfügen");
FrameUtils.centerDialog(dlg, this);
dlg.setVisible(true);
ConnectionProfile profile = dlg.getProfile();
if (profile != null) {
try {
ConnectionProfiles.getInstance().addProfile(profile);
this.connections.addProfile(profile);
((DefaultComboBoxModel) this.cmbProfile.getModel()).addElement(profile.getName());
((DefaultComboBoxModel) this.cmbCurrentConnection.getModel()).addElement(profile.getName());
this.cmbCurrentConnection.setSelectedItem(profile.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@
import com.jdimension.jlawyer.persistence.ArchiveFileBean;
import com.jdimension.jlawyer.persistence.ArchiveFileDocumentsBean;
import com.jdimension.jlawyer.persistence.CaseFolder;
import com.jdimension.jlawyer.security.Crypto;
import com.jdimension.jlawyer.security.CryptoProvider;
import com.jdimension.jlawyer.services.AddressServiceRemote;
import com.jdimension.jlawyer.services.ArchiveFileServiceRemote;
import com.jdimension.jlawyer.services.JLawyerServiceLocator;
Expand Down Expand Up @@ -893,7 +893,7 @@ private void initWithCertificate() throws BeaWrapperException {
AppUserBean cu = UserSettings.getInstance().getCurrentUser();
String pwd = null;
try {
pwd = Crypto.decrypt(cu.getBeaCertificatePassword());
pwd = CryptoProvider.defaultCrypto().decrypt(cu.getBeaCertificatePassword());
} catch (GeneralSecurityException | IOException ge) {
log.error("Unable to decrypt beA certificate password");
throw new BeaWrapperException(ge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@
import com.jdimension.jlawyer.client.settings.UserSettings;
import com.jdimension.jlawyer.client.utils.ThreadUtils;
import com.jdimension.jlawyer.persistence.AppUserBean;
import com.jdimension.jlawyer.security.Crypto;
import com.jdimension.jlawyer.security.CryptoProvider;
import java.awt.Color;
import javax.swing.JDialog;
import javax.swing.JFrame;
Expand Down Expand Up @@ -941,7 +941,7 @@ private void cmdCertificateLoginActionPerformed(java.awt.event.ActionEvent evt)
lblCertLogin.setText("Verbinde zum beA... einloggen...");

AppUserBean cu = UserSettings.getInstance().getCurrentUser();
BeaAccess bea = BeaAccess.getInstance(cu.getBeaCertificate(), Crypto.decrypt(cu.getBeaCertificatePassword()));
BeaAccess bea = BeaAccess.getInstance(cu.getBeaCertificate(), CryptoProvider.defaultCrypto().decrypt(cu.getBeaCertificatePassword()));
bea.login();
lblCertLogin.setText("Verbinde zum beA... laden...");
if (callback != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ You should also get your employer (if you work as a programmer) or school,

import com.jdimension.jlawyer.client.utils.FileUtils;
import com.jdimension.jlawyer.persistence.AppUserBean;
import com.jdimension.jlawyer.security.Crypto;
import com.jdimension.jlawyer.security.CryptoProvider;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -734,7 +734,7 @@ public static CloudInstance getInstance(AppUserBean user) {

String pwd=null;
try {
pwd=Crypto.decrypt(user.getCloudPassword());
pwd=CryptoProvider.defaultCrypto().decrypt(user.getCloudPassword());
} catch(Throwable t) {
log.error("Unable to decrypt Nextcloud password", t);
return null;
Expand Down
Loading

0 comments on commit 4dae993

Please sign in to comment.