mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 13:47:41 +00:00
Do the kdeconnect handshake in a new thread
Some Android versions seem to hang if calling sslSocket.getOutputStream() from within the HandshakeCompleted callback (maybe because calling it in on a socket that hasn't finished the SSL handshake is supposed to trigger the SSL handshake). BUG: 501241
This commit is contained in:
parent
f80e29538a
commit
69adfbfbc2
@ -255,37 +255,40 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
final boolean clientMode = (connectionStarted == LanLink.ConnectionStarted.Locally);
|
final boolean clientMode = (connectionStarted == LanLink.ConnectionStarted.Locally);
|
||||||
final SSLSocket sslSocket = SslHelper.convertToSslSocket(context, socket, deviceId, deviceTrusted, clientMode);
|
final SSLSocket sslSocket = SslHelper.convertToSslSocket(context, socket, deviceId, deviceTrusted, clientMode);
|
||||||
sslSocket.addHandshakeCompletedListener(event -> {
|
sslSocket.addHandshakeCompletedListener(event -> {
|
||||||
String mode = clientMode ? "client" : "server";
|
// Start a new thread because some Android versions don't allow calling sslSocket.getOutputStream() from the callback
|
||||||
try {
|
ThreadHelper.execute(() -> {
|
||||||
NetworkPacket secureIdentityPacket;
|
String mode = clientMode ? "client" : "server";
|
||||||
if (protocolVersion >= 8) {
|
try {
|
||||||
DeviceInfo myDeviceInfo = DeviceHelper.getDeviceInfo(context);
|
NetworkPacket secureIdentityPacket;
|
||||||
NetworkPacket myIdentity = myDeviceInfo.toIdentityPacket();
|
if (protocolVersion >= 8) {
|
||||||
OutputStream writer = sslSocket.getOutputStream();
|
DeviceInfo myDeviceInfo = DeviceHelper.getDeviceInfo(context);
|
||||||
writer.write(myIdentity.serialize().getBytes(Charsets.UTF_8));
|
NetworkPacket myIdentity = myDeviceInfo.toIdentityPacket();
|
||||||
writer.flush();
|
OutputStream writer = sslSocket.getOutputStream();
|
||||||
String line = readSingleLine(sslSocket);
|
writer.write(myIdentity.serialize().getBytes(Charsets.UTF_8));
|
||||||
// Do not trust the identity packet we received unencrypted
|
writer.flush();
|
||||||
secureIdentityPacket = NetworkPacket.unserialize(line);
|
String line = readSingleLine(sslSocket);
|
||||||
if (!DeviceInfo.isValidIdentityPacket(secureIdentityPacket)) {
|
// Do not trust the identity packet we received unencrypted
|
||||||
throw new JSONException("Invalid identity packet");
|
secureIdentityPacket = NetworkPacket.unserialize(line);
|
||||||
|
if (!DeviceInfo.isValidIdentityPacket(secureIdentityPacket)) {
|
||||||
|
throw new JSONException("Invalid identity packet");
|
||||||
|
}
|
||||||
|
int newProtocolVersion = secureIdentityPacket.getInt("protocolVersion");
|
||||||
|
if (newProtocolVersion != protocolVersion) {
|
||||||
|
Log.w("KDE/LanLinkProvider", "Protocol version changed half-way through the handshake: " + protocolVersion + " ->" + newProtocolVersion);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
secureIdentityPacket = identityPacket;
|
||||||
}
|
}
|
||||||
int newProtocolVersion = secureIdentityPacket.getInt("protocolVersion");
|
Certificate certificate = event.getPeerCertificates()[0];
|
||||||
if (newProtocolVersion != protocolVersion) {
|
DeviceInfo deviceInfo = DeviceInfo.fromIdentityPacketAndCert(secureIdentityPacket, certificate);
|
||||||
Log.w("KDE/LanLinkProvider", "Protocol version changed half-way through the handshake: " + protocolVersion + " ->" + newProtocolVersion);
|
Log.i("KDE/LanLinkProvider", "Handshake as " + mode + " successful with " + deviceName + " secured with " + event.getCipherSuite());
|
||||||
}
|
addOrUpdateLink(sslSocket, deviceInfo);
|
||||||
} else {
|
} catch (JSONException e) {
|
||||||
secureIdentityPacket = identityPacket;
|
Log.e("KDE/LanLinkProvider", "Remote device doesn't correctly implement protocol version 8", e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.e("KDE/LanLinkProvider", "Handshake as " + mode + " failed with " + deviceName, e);
|
||||||
}
|
}
|
||||||
Certificate certificate = event.getPeerCertificates()[0];
|
});
|
||||||
DeviceInfo deviceInfo = DeviceInfo.fromIdentityPacketAndCert(secureIdentityPacket, certificate);
|
|
||||||
Log.i("KDE/LanLinkProvider", "Handshake as " + mode + " successful with " + deviceName + " secured with " + event.getCipherSuite());
|
|
||||||
addOrUpdateLink(sslSocket, deviceInfo);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
Log.e("KDE/LanLinkProvider", "Remote device doesn't correctly implement protocol version 8", e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e("KDE/LanLinkProvider", "Handshake as " + mode + " failed with " + deviceName, e);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//Handshake is blocking, so do it on another thread and free this thread to keep receiving new connection
|
//Handshake is blocking, so do it on another thread and free this thread to keep receiving new connection
|
||||||
|
Loading…
x
Reference in New Issue
Block a user