Skip to content

Commit d38dee6

Browse files
committed
Code cleanups
1 parent bfdf7cf commit d38dee6

File tree

6 files changed

+76
-78
lines changed

6 files changed

+76
-78
lines changed

src/main/java/org/keepassxc/Connection.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
public abstract class Connection implements AutoCloseable {
2525

26-
private static final Logger log = LoggerFactory.getLogger(Connection.class);
26+
private static final Logger LOG = LoggerFactory.getLogger(Connection.class);
2727
private final PropertyChangeSupport support;
2828

2929
private TweetNaclFast.Box box;
@@ -48,10 +48,10 @@ public abstract class Connection implements AutoCloseable {
4848
private final int RESPONSE_TIMEOUT_S = 5;
4949

5050
protected final String PROXY_NAME = "org.keepassxc.KeePassXC.BrowserServer";
51-
private final String NOT_CONNECTED = "Not connected to KeePassXC. Call connect().";
52-
private final String KEYEXCHANGE_MISSING = "Public keys need to be exchanged. Call changePublicKeys().";
53-
private final String MISSING_CLASS = "Credentials have not been initialized";
54-
public final String EXCEPTION_INFO = "Delaying association dialog response lookup due to https://github.com/keepassxreboot/keepassxc/issues/7099";
51+
private static final String NOT_CONNECTED = "Not connected to KeePassXC. Call connect().";
52+
private static final String KEYEXCHANGE_MISSING = "Public keys need to be exchanged. Call changePublicKeys().";
53+
private static final String MISSING_CLASS = "Credentials have not been initialized";
54+
public static final String EXCEPTION_INFO = "Delaying association dialog response lookup due to https://github.com/keepassxreboot/keepassxc/issues/7099";
5555

5656
private static final Set<String> REQUESTS_WITHOUT_MANUAL_USER_INPUT = Set.of(
5757
"change-public-keys","get-databasehash","test-associate","get-database-groups"
@@ -88,24 +88,24 @@ public void run() {
8888
while (keepRunning()) {
8989
var response = getCleartextResponse();
9090
if (!response.isEmpty()) {
91-
if (!isSignal(response)) log.trace("Response added to queue: {}", response);
91+
if (!isSignal(response)) LOG.trace("Response added to queue: {}", response);
9292
queue.offer(response);
9393
errorCount = 0;
9494
} else {
9595
errorCount++;
9696
if (errorCount > MAX_ERROR_COUNT) {
97-
log.info("Too much errors - stopping MessagePublisher");
97+
LOG.info("Too much errors - stopping MessagePublisher");
9898
doStop();
9999
try {
100100
terminateConnection();
101101
} catch (IOException e) {
102-
log.error(e.toString(), e.getCause());
102+
LOG.error(e.toString(), e.getCause());
103103
}
104104
reconnect();
105105
}
106106
}
107107
}
108-
log.debug("MessagePublisher stopped");
108+
LOG.debug("MessagePublisher stopped");
109109
}
110110
}
111111

@@ -119,7 +119,6 @@ class MessageConsumer implements Callable<JSONObject> {
119119
*
120120
* @param action We are searching for a message with a certain action and
121121
* @param nonce a certain nonce.
122-
* @return The message that was looked up.
123122
*/
124123
public MessageConsumer(String action, byte[] nonce) {
125124
this.action = action;
@@ -140,22 +139,22 @@ public JSONObject call() throws Exception {
140139
}
141140
if (response.toString().equals("{}")) {
142141
queue.remove(response);
143-
log.trace("KeePassXC send an empty response: {}", response);
142+
LOG.trace("KeePassXC send an empty response: {}", response);
144143
continue;
145144
}
146145
for (JSONObject message : queue) {
147-
log.trace("Checking in queue message {}, looking for action '{}' and nonce {}", message, action, b64encode(incrementNonce(nonce)));
146+
LOG.trace("Checking in queue message {}, looking for action '{}' and nonce {}", message, action, b64encode(incrementNonce(nonce)));
148147
if (message.has("error") && message.getString("action").equals(action)) {
149148
queue.remove(message);
150-
log.trace("Found in and retrieved from queue: {}", message);
149+
LOG.trace("Found in and retrieved from queue: {}", message);
151150
return message;
152151
}
153152
if (message.has("action")
154153
&& message.getString("action").equals(action)
155154
&& message.has("nonce")
156155
&& message.getString("nonce").equals(b64encode(incrementNonce(nonce)))) {
157156
queue.remove(message);
158-
log.trace("Retrieved from queue: {}", message);
157+
LOG.trace("Retrieved from queue: {}", message);
159158
return message;
160159
}
161160
}
@@ -166,7 +165,7 @@ public JSONObject call() throws Exception {
166165

167166
void lauchMessagePublisher() {
168167
messagePublisher = new MessagePublisher();
169-
log.debug("MessagePublisher started");
168+
LOG.debug("MessagePublisher started");
170169
executorService.execute(messagePublisher);
171170
}
172171

@@ -255,7 +254,7 @@ private synchronized byte[] sendEncryptedMessage(Map<String, Object> msg) throws
255254
}
256255

257256
var strMsg = jsonTxt(msg);
258-
log.trace("Send - encrypting the following message: {}", strMsg);
257+
LOG.trace("Send - encrypting the following message: {}", strMsg);
259258

260259
box = new TweetNaclFast.Box(publicKey, keyPair.getSecretKey());
261260
nonce = ramdomGenerateNonce();
@@ -300,7 +299,7 @@ private synchronized JSONObject getEncryptedResponseAndDecrypt(String action, by
300299
} catch (TimeoutException toe) {
301300
throw new KeepassProxyAccessException("Timeout for action '" + action + "'");
302301
} catch (InterruptedException | ExecutionException e) {
303-
log.error(e.toString(), e.getCause());
302+
LOG.error(e.toString(), e.getCause());
304303
}
305304

306305
if (response.has("error")) {
@@ -315,7 +314,7 @@ private synchronized JSONObject getEncryptedResponseAndDecrypt(String action, by
315314
}
316315

317316
var decrypted = new String(bMessage, StandardCharsets.UTF_8);
318-
log.trace("Decrypted message: {}", decrypted);
317+
LOG.trace("Decrypted message: {}", decrypted);
319318
var decryptedResponse = new JSONObject(decrypted);
320319

321320
if (!decryptedResponse.has("success")) {
@@ -352,7 +351,7 @@ protected void changePublicKeys() throws IOException, KeepassProxyAccessExceptio
352351
try {
353352
response = executorService.submit(new MessageConsumer("change-public-keys", nonce)).get();
354353
} catch (InterruptedException | ExecutionException e) {
355-
log.error(e.toString(), e.getCause());
354+
LOG.error(e.toString(), e.getCause());
356355
}
357356

358357
if (!response.has("success")) {
@@ -400,7 +399,7 @@ public void associate() throws IOException, KeepassProxyAccessException {
400399
try {
401400
response = getEncryptedResponseAndDecrypt("associate", nonce);
402401
} catch (KeepassProxyAccessException e) {
403-
log.error(e.toString(), e.getCause());
402+
LOG.error(e.toString(), e.getCause());
404403
}
405404
assert response != null;
406405
credentials.orElseThrow(() -> new IllegalStateException(MISSING_CLASS)).setAssociateId(response.getString("id"));

src/main/java/org/keepassxc/KindOfKeePassXC.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@
66
import java.util.Optional;
77

88
public class KindOfKeePassXC {
9-
private static final Logger log = LoggerFactory.getLogger(KindOfKeePassXC.class);
9+
private static final Logger LOG = LoggerFactory.getLogger(KindOfKeePassXC.class);
1010

1111
static Optional<KeePassXCType> determineType() {
1212
var processHandle = ProcessHandle.allProcesses()
1313
.filter(ph -> ph.info().command().isPresent() && ph.info().command().get().contains("KeePassXC"))
1414
.findFirst();
1515
if (processHandle.isPresent() && processHandle.get().info().command().get().contains("AppImage")) {
16-
log.debug("Found running KeePassXC AppImage");
16+
LOG.debug("Found running KeePassXC AppImage");
1717
return Optional.of(KeePassXCType.AppImage);
1818
}
1919

2020
processHandle = ProcessHandle.allProcesses()
2121
.filter(ph -> ph.info().commandLine().isPresent() && ph.info().commandLine().get().contains("keepassxc"))
2222
.findFirst();
2323
if (processHandle.isPresent()) {
24-
log.debug("Found running KeePassXC installed from repository");
24+
LOG.debug("Found running KeePassXC installed from repository");
2525
return Optional.of(KeePassXCType.Repo);
2626
}
2727

2828
processHandle = ProcessHandle.allProcesses()
2929
.filter(ph -> ph.info().command().isPresent() && ph.info().command().get().contains("keepassxc"))
3030
.findFirst();
3131
if (processHandle.isPresent() && processHandle.get().info().command().get().contains("app")) {
32-
log.debug("Found running KeePassXC installed via Flatpak");
32+
LOG.debug("Found running KeePassXC installed via Flatpak");
3333
return Optional.of(KeePassXCType.Flatpak);
3434
}
3535

3636
if (processHandle.isPresent() && processHandle.get().info().command().get().contains("snap")) {
37-
log.debug("Found running KeePassXC installed via Snap");
37+
LOG.debug("Found running KeePassXC installed via Snap");
3838
return Optional.of(KeePassXCType.Snap);
3939
}
4040

41-
log.debug("Could not find running KeePassXC application");
41+
LOG.debug("Could not find running KeePassXC application");
4242
return Optional.empty();
4343
}
4444
}

src/main/java/org/keepassxc/LinuxMacConnection.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
public class LinuxMacConnection extends Connection {
2121

22-
private static final Logger log = LoggerFactory.getLogger(LinuxMacConnection.class);
22+
private static final Logger LOG = LoggerFactory.getLogger(LinuxMacConnection.class);
2323

2424
private final int BUFFER_SIZE = 1024;
2525
private SocketChannel socket;
26-
private UnixDomainSocketAddress socketAddress;
26+
private final UnixDomainSocketAddress socketAddress;
2727
private final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
2828
private final Charset charset = StandardCharsets.UTF_8;
2929
private final CharsetDecoder charsetDecoder = charset.newDecoder();
@@ -48,21 +48,21 @@ public void connect() throws IOException {
4848
try {
4949
socket = SocketChannel.open(socketAddress);
5050
} catch (IOException e) {
51-
log.error("Cannot connect to proxy. Is KeepassXC started?");
51+
LOG.error("Cannot connect to proxy. Is KeepassXC started?");
5252
throw e;
5353
}
5454
try {
5555
lauchMessagePublisher();
5656
changePublicKeys();
5757
} catch (KeepassProxyAccessException e) {
58-
log.error(e.toString(), e.getCause());
58+
LOG.error(e.toString(), e.getCause());
5959
}
6060
}
6161

6262
@Override
6363
protected void sendCleartextMessage(String msg) throws IOException {
6464
if (socket.isOpen()) {
65-
log.trace("Sending message: {}", msg);
65+
LOG.trace("Sending message: {}", msg);
6666
socket.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8)));
6767
} else {
6868
throw new IOException("Socket closed");
@@ -76,7 +76,7 @@ protected JSONObject getCleartextResponse() {
7676
try {
7777
if (socket.read(buffer) == -1) break;
7878
} catch (IOException e) {
79-
log.error(e.toString(), e.getCause());
79+
LOG.error(e.toString(), e.getCause());
8080
return new JSONObject();
8181
}
8282
buffer.flip();
@@ -91,14 +91,14 @@ protected JSONObject getCleartextResponse() {
9191
charBuffer.clear();
9292
}
9393
}
94-
log.trace("Reading message: {}", raw);
94+
LOG.trace("Reading message: {}", raw);
9595
try {
9696
var s = raw.toString();
9797
// Test, if we received more than one message with the last read
9898
if (s.length() - s.replace("}", "").length() > 1) throw new JSONException("");
9999
return new JSONObject(raw.toString());
100100
} catch (JSONException e) {
101-
log.error("Message corrupted. Received: {}", raw);
101+
LOG.error("Message corrupted. Received: {}", raw);
102102
return new JSONObject();
103103
}
104104
}
@@ -119,11 +119,11 @@ private String getSocketPath() {
119119
return getXDGPath();
120120
}
121121
case Flatpak -> {
122-
log.debug("Using XDG_RUNTIME_DIR" + FLATPAK_PATH);
122+
LOG.debug("Using XDG_RUNTIME_DIR" + FLATPAK_PATH);
123123
return System.getenv("XDG_RUNTIME_DIR") + FLATPAK_PATH;
124124
}
125125
case Snap -> {
126-
log.debug("Using " + SNAP_PATH);
126+
LOG.debug("Using " + SNAP_PATH);
127127
return SNAP_PATH;
128128
}
129129
}
@@ -147,19 +147,19 @@ private String getSocketPath() {
147147
*/
148148
private String getXDGPath() {
149149
var path = System.getenv("XDG_RUNTIME_DIR");
150-
log.debug("Checking if XDG_RUNTIME_DIR exists ...");
150+
LOG.debug("Checking if XDG_RUNTIME_DIR exists ...");
151151
if (null == path) {
152-
log.debug("Unable to find XDG_RUNTIME_DIR");
152+
LOG.debug("Unable to find XDG_RUNTIME_DIR");
153153
path = System.getenv("TMPDIR");
154-
log.debug("Using TEMPDIR");
154+
LOG.debug("Using TEMPDIR");
155155
return (null == path) ? "/tmp" : path;
156156
} else {
157157
var flatpakPath = new File(path + FLATPAK_PATH);
158158
if (flatpakPath.exists()) {
159-
log.debug("Using XDG_RUNTIME_DIR" + FLATPAK_PATH);
159+
LOG.debug("Using XDG_RUNTIME_DIR" + FLATPAK_PATH);
160160
return path + FLATPAK_PATH;
161161
} else {
162-
log.debug("Using XDG_RUNTIME_DIR");
162+
LOG.debug("Using XDG_RUNTIME_DIR");
163163
return path;
164164
}
165165
}

src/main/java/org/keepassxc/WindowsConnection.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
import java.nio.file.Paths;
1818
import java.nio.file.StandardOpenOption;
1919
import java.util.concurrent.ExecutionException;
20-
import java.util.concurrent.Future;
2120

2221
public class WindowsConnection extends Connection {
2322

24-
private static final Logger log = LoggerFactory.getLogger(WindowsConnection.class);
23+
private static final Logger LOG = LoggerFactory.getLogger(WindowsConnection.class);
2524

2625
private final int BUFFER_SIZE = 8192;
2726
private AsynchronousFileChannel pipe;
@@ -41,21 +40,21 @@ public void connect() throws IOException {
4140
Path path = Paths.get("\\\\.\\pipe\\" + PROXY_NAME + "_" + System.getenv("USERNAME"));
4241
pipe = AsynchronousFileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE);
4342
} catch (IOException e) {
44-
log.error("Cannot connect to proxy. Is KeepassXC started?");
43+
LOG.error("Cannot connect to proxy. Is KeepassXC started?");
4544
throw e;
4645
}
4746
try {
4847
lauchMessagePublisher();
4948
changePublicKeys();
5049
} catch (KeepassProxyAccessException e) {
51-
log.error(e.toString(), e.getCause());
50+
LOG.error(e.toString(), e.getCause());
5251
}
5352
}
5453

5554
@Override
5655
protected void sendCleartextMessage(String msg) throws IOException {
5756
if (pipe.isOpen()) {
58-
log.trace("Sending message: {}", msg);
57+
LOG.trace("Sending message: {}", msg);
5958
pipe.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8)), 0);
6059
} else {
6160
throw new IOException("Pipe closed");
@@ -69,22 +68,22 @@ protected JSONObject getCleartextResponse() {
6968
try {
7069
pipe.read(buffer, position).get();
7170
} catch (InterruptedException | ExecutionException e) {
72-
log.error(e.toString(), e.getCause());
71+
LOG.error(e.toString(), e.getCause());
7372
}
7473
buffer.flip();
7574
charsetDecoder.decode(buffer, charBuffer, true);
7675
charBuffer.flip();
7776
raw.append(charBuffer);
7877
buffer.compact();
7978
charBuffer.clear();
80-
log.trace("Reading message: {}", raw);
79+
LOG.trace("Reading message: {}", raw);
8180
try {
8281
var s = raw.toString();
8382
// Test, if we received more than one message with the last read
8483
if (s.length() - s.replace("}", "").length() > 1) throw new JSONException("");
8584
return new JSONObject(raw.toString());
8685
} catch (JSONException e) {
87-
log.error("Message corrupted. Received: {}", raw);
86+
LOG.error("Message corrupted. Received: {}", raw);
8887
return new JSONObject();
8988
}
9089
}

0 commit comments

Comments
 (0)