7
7
import org .slf4j .Logger ;
8
8
import org .slf4j .LoggerFactory ;
9
9
10
+ import java .io .File ;
10
11
import java .io .IOException ;
11
12
import java .net .UnixDomainSocketAddress ;
12
13
import java .nio .ByteBuffer ;
@@ -22,12 +23,15 @@ public class LinuxMacConnection extends Connection {
22
23
23
24
private final int BUFFER_SIZE = 1024 ;
24
25
private SocketChannel socket ;
25
- private final UnixDomainSocketAddress socketAddress ;
26
+ private UnixDomainSocketAddress socketAddress ;
26
27
private final ByteBuffer buffer = ByteBuffer .allocate (BUFFER_SIZE );
27
28
private final Charset charset = StandardCharsets .UTF_8 ;
28
29
private final CharsetDecoder charsetDecoder = charset .newDecoder ();
29
30
private final CharBuffer charBuffer = CharBuffer .allocate (BUFFER_SIZE );
30
31
32
+ private static final String FLATPAK_PATH = "/app/org.keepassxc.KeePassXC" ;
33
+ private static final String SNAP_PATH = System .getProperty ("user.home" ) + "/snap/keepassxc/common" ;
34
+
31
35
public LinuxMacConnection () {
32
36
var socketPath = getSocketPath ();
33
37
this .socketAddress = UnixDomainSocketAddress .of (socketPath + "/" + PROXY_NAME );
@@ -100,15 +104,30 @@ protected JSONObject getCleartextResponse() {
100
104
}
101
105
102
106
/**
103
- * Get the os-specific directory, where runtime files and sockets are kept.
107
+ * Get the os-specific and KeePassXC installation-specific directory, where runtime files and sockets are kept.
104
108
*
105
109
* @return The socket path.
106
110
*/
107
111
private String getSocketPath () {
108
112
if (SystemUtils .IS_OS_LINUX ) {
109
- var path = System .getenv ("XDG_RUNTIME_DIR" );
110
- if (null == path ) path = System .getenv ("TMPDIR" );
111
- return (null == path ) ? "/tmp" : path ;
113
+ var type = KindOfKeePassXC .determineType ();
114
+ if (type .isEmpty ()) {
115
+ return getXDGPath ();
116
+ } else {
117
+ switch (type .get ()) {
118
+ case Repo , AppImage -> {
119
+ return getXDGPath ();
120
+ }
121
+ case Flatpak -> {
122
+ log .debug ("Using XDG_RUNTIME_DIR" + FLATPAK_PATH );
123
+ return System .getenv ("XDG_RUNTIME_DIR" ) + FLATPAK_PATH ;
124
+ }
125
+ case Snap -> {
126
+ log .debug ("Using " + SNAP_PATH );
127
+ return SNAP_PATH ;
128
+ }
129
+ }
130
+ }
112
131
}
113
132
if (SystemUtils .IS_OS_MAC_OSX ) {
114
133
return System .getenv ("TMPDIR" );
@@ -118,6 +137,34 @@ private String getSocketPath() {
118
137
}
119
138
}
120
139
140
+ /**
141
+ * Find the XDG_RUNTIME_DIR KeePassXC is using.
142
+ * When installed from a repo or run as an AppImage, it depends on the KeePassXC version, where it creates its socket:
143
+ * KeePassXC <2.7.2 create it in the XDG_RUNTIME_DIR
144
+ * KeePassXC 2.7.2+ create it in XDG_RUNTIME_DIR/app/org.keepassxc.KeePassXC/
145
+ *
146
+ * @return The correct socket path.
147
+ */
148
+ private String getXDGPath () {
149
+ var path = System .getenv ("XDG_RUNTIME_DIR" );
150
+ log .debug ("Checking if XDG_RUNTIME_DIR exists ..." );
151
+ if (null == path ) {
152
+ log .debug ("Unable to find XDG_RUNTIME_DIR" );
153
+ path = System .getenv ("TMPDIR" );
154
+ log .debug ("Using TEMPDIR" );
155
+ return (null == path ) ? "/tmp" : path ;
156
+ } else {
157
+ var flatpakPath = new File (path + FLATPAK_PATH );
158
+ if (flatpakPath .exists ()) {
159
+ log .debug ("Using XDG_RUNTIME_DIR" + FLATPAK_PATH );
160
+ return path + FLATPAK_PATH ;
161
+ } else {
162
+ log .debug ("Using XDG_RUNTIME_DIR" );
163
+ return path ;
164
+ }
165
+ }
166
+ }
167
+
121
168
@ Override
122
169
protected boolean isConnected () {
123
170
return null != socket && socket .isOpen ();
0 commit comments