Skip to content

Commit 884fec9

Browse files
committed
Add method to manually close the connection to the KeePassXC database
1 parent 29ff6a6 commit 884fec9

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public void run() {
9292
if (errorCount > MAX_ERROR_COUNT) {
9393
log.info("Too much errors - stopping MessagePublisher");
9494
doStop();
95+
try {
96+
close();
97+
} catch (Exception e) {
98+
log.error(e.toString(), e.getCause());
99+
}
95100
reconnect();
96101
}
97102
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ private String getSocketPath() {
120120

121121
@Override
122122
protected boolean isConnected() {
123-
return null!= socket && socket.isOpen();
123+
return null != socket && socket.isOpen();
124124
}
125125

126126
@Override
127127
public void close() throws Exception {
128-
messagePublisher.doStop();
128+
if (null != messagePublisher) messagePublisher.doStop();
129129
executorService.shutdown();
130-
socket.close();
130+
if (isConnected()) socket.close();
131131
}
132132
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ protected JSONObject getCleartextResponse() {
8787

8888
@Override
8989
protected boolean isConnected() {
90-
return null != pipe;
90+
return null != pipe && pipe.isOpen();
9191
}
9292

9393
@Override
9494
public void close() throws Exception {
95-
messagePublisher.doStop();
95+
if (null != messagePublisher) messagePublisher.doStop();
9696
executorService.shutdown();
97-
pipe.close();
97+
if (isConnected()) pipe.close();
9898
}
9999
}

src/main/java/org/purejava/KeepassProxyAccess.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ public KeepassProxyAccess() {
5656
}
5757
scheduler = Executors.newSingleThreadScheduledExecutor();
5858
connection.addPropertyChangeListener(this);
59-
Runtime.getRuntime().addShutdownHook(new Thread(() ->
60-
connection.removePropertyChangeListener(this)
59+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
60+
connection.removePropertyChangeListener(this);
61+
try {
62+
closeConnection();
63+
} catch (Exception e) {
64+
log.error(e.toString(), e.getCause());
65+
}
66+
}
6167
));
6268
connection.setCredentials(loadCredentials());
6369
}
@@ -489,6 +495,21 @@ public String getAssociateId() {
489495
return connection.getAssociateId();
490496
}
491497

498+
/**
499+
* Close the connection to the socket (for Linux and Mac) or the named pip (for Windows) respectively.
500+
*
501+
* @return True, in case the connection was closed without an error, false otherwise.
502+
*/
503+
public boolean closeConnection() {
504+
try {
505+
connection.close();
506+
return true;
507+
} catch (Exception e) {
508+
log.error(e.toString(), e.getCause());
509+
return false;
510+
}
511+
}
512+
492513
/**
493514
* Getter for the ScheduledExecutorService in case the service needs to be shutdown from outside this library.
494515
*

src/test/java/org/purejava/KeepassProxyAccessTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.junit.jupiter.api.Test;
66

77
import static org.junit.jupiter.api.Assertions.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
89

910
/**
1011
* This test is designed to run within CI, where a KeePassXC database is not available.
@@ -17,5 +18,6 @@ public class KeepassProxyAccessTest {
1718
@DisplayName("Testing availability of a socket to KeePassXC")
1819
public void shouldHaveNoErrors() {
1920
assertFalse(kpa.connect());
21+
assertTrue(kpa.closeConnection());
2022
}
2123
}

src/test/java/org/purejava/LockedDatabaseTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public class LockedDatabaseTest {
1818
public void shouldHaveNoErrors() {
1919
assertTrue(kpa.connect());
2020
assertTrue(kpa.getDatabasehash().isEmpty());
21+
assertTrue(kpa.closeConnection());
2122
}
2223
}

src/test/java/org/purejava/UnlockedDatabaseTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class UnlockedDatabaseTest {
2323
@Test
2424
@Order(3)
2525
@DisplayName("Testing KeePassXC proxy functionality")
26-
public void shouldHaveNoErrors() throws InterruptedException {
26+
public void shouldHaveNoErrors() throws Exception {
2727
log.info("Please enter a name for the connection in the pop-up within 10 seconds");
2828
assertTrue(kpa.connect());
2929
// TODO:
@@ -52,5 +52,6 @@ public void shouldHaveNoErrors() throws InterruptedException {
5252
assertTrue(kpa.deleteEntry("2aafee1a89fd435c8bad7df12bbaaa3e"));
5353
log.info("Please deny to save changes");
5454
assertTrue(kpa.lockDatabase());
55+
assertTrue(kpa.closeConnection());
5556
}
5657
}

0 commit comments

Comments
 (0)