-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLinuxMacConnection.java
125 lines (113 loc) · 3.9 KB
/
LinuxMacConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package org.keepassxc;
import org.apache.commons.lang3.SystemUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.purejava.KeepassProxyAccessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.UnixDomainSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;
public class LinuxMacConnection extends Connection {
private static final Logger log = LoggerFactory.getLogger(LinuxMacConnection.class);
private final int BUFFER_SIZE = 1024;
private SocketChannel socket;
private final UnixDomainSocketAddress socketAddress;
private final ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
private final Charset charset = StandardCharsets.UTF_8;
private final CharsetDecoder charsetDecoder = charset.newDecoder();
private final CharBuffer charBuffer = CharBuffer.allocate(BUFFER_SIZE);
public LinuxMacConnection() {
var socketPath = getSocketPath();
this.socketAddress = UnixDomainSocketAddress.of(socketPath + "/" + PROXY_NAME);
}
/**
* Connect to the KeePassXC proxy via a Unix Domain Sockets (AF_UNIX)
* the proxy has opened.
*
* @throws IOException Connecting to the proxy failed due to technical reasons or the proxy wasn't started.
*/
@Override
public void connect() throws IOException {
try {
socket = SocketChannel.open(socketAddress);
} catch (IOException e) {
log.error("Cannot connect to proxy. Is KeepassXC started?");
throw e;
}
try {
lauchMessagePublisher();
changePublicKeys();
} catch (KeepassProxyAccessException e) {
log.error(e.toString(), e.getCause());
}
}
@Override
protected void sendCleartextMessage(String msg) throws IOException {
log.trace("Sending message: {}", msg);
socket.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8)));
}
@Override
protected JSONObject getCleartextResponse() {
var raw = new StringBuilder();
while (true) {
try {
if (socket.read(buffer) == -1) break;
} catch (IOException e) {
log.error(e.toString(), e.getCause());
return new JSONObject();
}
buffer.flip();
charsetDecoder.decode(buffer, charBuffer, true);
charBuffer.flip();
raw.append(charBuffer);
buffer.compact();
if (charBuffer.toString().contains("}")) {
charBuffer.clear();
break;
} else {
charBuffer.clear();
}
}
log.trace("Reading message: {}", raw);
try {
return new JSONObject(raw.toString());
} catch (JSONException e) {
log.error("Message corrupted. Received: {}", raw);
return new JSONObject();
}
}
/**
* Get the os-specific directory, where runtime files and sockets are kept.
*
* @return The socket path.
*/
private String getSocketPath() {
if (SystemUtils.IS_OS_LINUX) {
var path = System.getenv("XDG_RUNTIME_DIR");
if (null == path) path = System.getenv("TMPDIR");
return (null == path) ? "/tmp" : path;
}
if (SystemUtils.IS_OS_MAC_OSX) {
return System.getenv("TMPDIR");
} else {
// unknown OS
return "-";
}
}
@Override
protected boolean isConnected() {
return null!= socket && socket.isOpen();
}
@Override
public void close() throws Exception {
messagePublisher.doStop();
executorService.shutdown();
socket.close();
}
}