mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-30 13:47:41 +00:00
Working :D
This commit is contained in:
parent
71dc713578
commit
8a413bb42e
@ -27,7 +27,6 @@ import android.util.Log;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.kde.kdeconnect.Backends.BaseLink;
|
import org.kde.kdeconnect.Backends.BaseLink;
|
||||||
import org.kde.kdeconnect.Backends.BasePairingHandler;
|
import org.kde.kdeconnect.Backends.BasePairingHandler;
|
||||||
import org.kde.kdeconnect.BackgroundService;
|
|
||||||
import org.kde.kdeconnect.Device;
|
import org.kde.kdeconnect.Device;
|
||||||
import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper;
|
import org.kde.kdeconnect.Helpers.SecurityHelpers.RsaHelper;
|
||||||
import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper;
|
import org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper;
|
||||||
@ -47,6 +46,7 @@ import java.security.PublicKey;
|
|||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import javax.net.ssl.SSLServerSocketFactory;
|
import javax.net.ssl.SSLServerSocketFactory;
|
||||||
|
import javax.net.ssl.SSLSocket;
|
||||||
|
|
||||||
public class LanLink extends BaseLink {
|
public class LanLink extends BaseLink {
|
||||||
|
|
||||||
@ -61,9 +61,6 @@ public class LanLink extends BaseLink {
|
|||||||
|
|
||||||
private Socket channel = null;
|
private Socket channel = null;
|
||||||
|
|
||||||
private boolean onSsl = false;
|
|
||||||
|
|
||||||
OutputStream writter;
|
|
||||||
Cancellable readThread;
|
Cancellable readThread;
|
||||||
|
|
||||||
public abstract class Cancellable implements Runnable {
|
public abstract class Cancellable implements Runnable {
|
||||||
@ -91,75 +88,65 @@ public class LanLink extends BaseLink {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Returns the old channel
|
//Returns the old channel
|
||||||
public Socket reset(final Socket channel, ConnectionStarted connectionSource, boolean onSsl, final LanLinkProvider linkProvider) throws IOException {
|
public Socket reset(final Socket channel, ConnectionStarted connectionSource, final LanLinkProvider linkProvider) throws IOException {
|
||||||
|
|
||||||
Socket oldChannel = this.channel;
|
Socket oldChannel = this.channel;
|
||||||
try {
|
this.channel = channel;
|
||||||
Log.e("reset", "1");
|
this.connectionSource = connectionSource;
|
||||||
//writter = channel.getOutputStream();
|
|
||||||
Log.e("reset", "2");
|
|
||||||
this.channel = channel;
|
|
||||||
this.connectionSource = connectionSource;
|
|
||||||
this.onSsl = onSsl;
|
|
||||||
Log.e("reset", "BBBBB");
|
|
||||||
|
|
||||||
if (oldChannel != null) {
|
if (oldChannel != null) {
|
||||||
readThread.cancel();
|
readThread.cancel();
|
||||||
oldChannel.close();
|
oldChannel.close();
|
||||||
}
|
|
||||||
|
|
||||||
Log.e("LanLink", "Start listening");
|
|
||||||
//Start listening
|
|
||||||
readThread = new Cancellable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream(), LanLinkProvider.UTF8));
|
|
||||||
while (!cancelled) {
|
|
||||||
if (channel.isClosed()) {
|
|
||||||
Log.e("BufferReader", "Channel closed");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
String packet;
|
|
||||||
try {
|
|
||||||
packet = reader.readLine();
|
|
||||||
Log.e("packet", "A" + packet);
|
|
||||||
} catch (SocketTimeoutException e) {
|
|
||||||
Log.w("BufferReader", "timeout");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (packet == null) {
|
|
||||||
Log.w("BufferReader", "null package");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (packet.isEmpty()) {
|
|
||||||
Log.w("BufferReader", "empty package: " + packet);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
NetworkPackage np = NetworkPackage.unserialize(packet);
|
|
||||||
injectNetworkPackage(np);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.e("LanLink", "Socket closed");
|
|
||||||
linkProvider.socketClosed(channel);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
new Thread(readThread).start();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Log.e("reset","except");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.e("LanLink", "Start listening");
|
||||||
|
//Start listening
|
||||||
|
readThread = new Cancellable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream(), LanLinkProvider.UTF8));
|
||||||
|
while (!cancelled) {
|
||||||
|
if (channel.isClosed()) {
|
||||||
|
Log.e("BufferReader", "Channel closed");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
String packet;
|
||||||
|
try {
|
||||||
|
packet = reader.readLine();
|
||||||
|
Log.e("packet", "A" + packet);
|
||||||
|
} catch (SocketTimeoutException e) {
|
||||||
|
Log.w("BufferReader", "timeout");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (packet == null) {
|
||||||
|
Log.w("BufferReader", "null package");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (packet.isEmpty()) {
|
||||||
|
Log.w("BufferReader", "empty package: " + packet);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
NetworkPackage np = NetworkPackage.unserialize(packet);
|
||||||
|
injectNetworkPackage(np);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
Log.e("LanLink", "Socket closed");
|
||||||
|
linkProvider.socketClosed(channel);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new Thread(readThread).start();
|
||||||
|
|
||||||
|
|
||||||
return oldChannel;
|
return oldChannel;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LanLink(Context context, String deviceId, LanLinkProvider linkProvider, Socket channel, ConnectionStarted connectionSource, boolean onSsl) throws IOException {
|
public LanLink(Context context, String deviceId, LanLinkProvider linkProvider, Socket channel, ConnectionStarted connectionSource) throws IOException {
|
||||||
super(context, deviceId, linkProvider);
|
super(context, deviceId, linkProvider);
|
||||||
reset(channel, connectionSource, onSsl, linkProvider);
|
reset(channel, connectionSource, linkProvider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -173,20 +160,6 @@ public class LanLink extends BaseLink {
|
|||||||
return new LanPairingHandler(device, callback);
|
return new LanPairingHandler(device, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void addPackageReceiver(PackageReceiver pr) {
|
|
||||||
super.addPackageReceiver(pr);
|
|
||||||
BackgroundService.RunCommand(context, new BackgroundService.InstanceCallback() {
|
|
||||||
@Override
|
|
||||||
public void onServiceStart(BackgroundService service) {
|
|
||||||
Device device = service.getDevice(getDeviceId());
|
|
||||||
if (device == null) return;
|
|
||||||
if (!device.isPaired()) return;
|
|
||||||
// If the device is already paired due to other link, just send a pairing request to get required attributes for this link
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//Blocking, do not call from main thread
|
//Blocking, do not call from main thread
|
||||||
private void sendPackageInternal(NetworkPackage np, final Device.SendPackageStatusCallback callback, PublicKey key) {
|
private void sendPackageInternal(NetworkPackage np, final Device.SendPackageStatusCallback callback, PublicKey key) {
|
||||||
if (channel == null) {
|
if (channel == null) {
|
||||||
@ -200,7 +173,7 @@ public class LanLink extends BaseLink {
|
|||||||
//Prepare socket for the payload
|
//Prepare socket for the payload
|
||||||
final ServerSocket server;
|
final ServerSocket server;
|
||||||
if (np.hasPayload()) {
|
if (np.hasPayload()) {
|
||||||
server = openTcpSocketOnFreePort(context, getDeviceId(), onSsl);
|
server = openTcpSocketOnFreePort(context, getDeviceId());
|
||||||
JSONObject payloadTransferInfo = new JSONObject();
|
JSONObject payloadTransferInfo = new JSONObject();
|
||||||
payloadTransferInfo.put("port", server.getLocalPort());
|
payloadTransferInfo.put("port", server.getLocalPort());
|
||||||
np.setPayloadTransferInfo(payloadTransferInfo);
|
np.setPayloadTransferInfo(payloadTransferInfo);
|
||||||
@ -213,12 +186,13 @@ public class LanLink extends BaseLink {
|
|||||||
np = RsaHelper.encrypt(np, key);
|
np = RsaHelper.encrypt(np, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.e("LanLink/sendPackage", np.getType());
|
//Log.e("LanLink/sendPackage", np.getType());
|
||||||
|
|
||||||
//Send body of the network package
|
//Send body of the network package
|
||||||
try {
|
try {
|
||||||
//writter.write(np.serialize().getBytes(LanLinkProvider.UTF8));
|
OutputStream writter = channel.getOutputStream();
|
||||||
//writter.flush();
|
writter.write(np.serialize().getBytes(LanLinkProvider.UTF8));
|
||||||
|
writter.flush();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
callback.sendFailure(e);
|
callback.sendFailure(e);
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -308,7 +282,7 @@ public class LanLink extends BaseLink {
|
|||||||
Socket payloadSocket = null;
|
Socket payloadSocket = null;
|
||||||
try {
|
try {
|
||||||
// Use ssl if existing link is on ssl
|
// Use ssl if existing link is on ssl
|
||||||
if (onSsl) {
|
if (channel instanceof SSLSocket) {
|
||||||
SSLContext sslContext = SslHelper.getSslContext(context, getDeviceId(), true);
|
SSLContext sslContext = SslHelper.getSslContext(context, getDeviceId(), true);
|
||||||
payloadSocket = sslContext.getSocketFactory().createSocket();
|
payloadSocket = sslContext.getSocketFactory().createSocket();
|
||||||
} else {
|
} else {
|
||||||
@ -330,8 +304,8 @@ public class LanLink extends BaseLink {
|
|||||||
packageReceived(np);
|
packageReceived(np);
|
||||||
}
|
}
|
||||||
|
|
||||||
static ServerSocket openTcpSocketOnFreePort(Context context, String deviceId, boolean useSsl) throws IOException {
|
ServerSocket openTcpSocketOnFreePort(Context context, String deviceId) throws IOException {
|
||||||
if (useSsl) {
|
if (channel instanceof SSLSocket) {
|
||||||
return openSecureServerSocket(context, deviceId);
|
return openSecureServerSocket(context, deviceId);
|
||||||
} else {
|
} else {
|
||||||
return openUnsecureSocketOnFreePort(1739);
|
return openUnsecureSocketOnFreePort(1739);
|
||||||
|
@ -87,7 +87,6 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
private ArrayList<InetAddress> reverseConnectionBlackList = new ArrayList<>();
|
private ArrayList<InetAddress> reverseConnectionBlackList = new ArrayList<>();
|
||||||
|
|
||||||
public void socketClosed(Socket socket) {
|
public void socketClosed(Socket socket) {
|
||||||
Log.e("socketClosed","socketClosed");
|
|
||||||
try {
|
try {
|
||||||
final LanLink brokenLink = nioLinks.get(socket.hashCode());
|
final LanLink brokenLink = nioLinks.get(socket.hashCode());
|
||||||
if (brokenLink != null) {
|
if (brokenLink != null) {
|
||||||
@ -267,7 +266,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
Certificate certificate = event.getPeerCertificates()[0];
|
Certificate certificate = event.getPeerCertificates()[0];
|
||||||
identityPackage.set("certificate", Base64.encodeToString(certificate.getEncoded(), 0));
|
identityPackage.set("certificate", Base64.encodeToString(certificate.getEncoded(), 0));
|
||||||
Log.i("KDE/LanLinkProvider","Handshake as " + mode + " successful with " + identityPackage.getString("deviceName") + " secured with " + event.getCipherSuite());
|
Log.i("KDE/LanLinkProvider","Handshake as " + mode + " successful with " + identityPackage.getString("deviceName") + " secured with " + event.getCipherSuite());
|
||||||
addLink(identityPackage, sslsocket, connectionStarted, true);
|
addLink(identityPackage, sslsocket, connectionStarted);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e("KDE/LanLinkProvider","Handshake as " + mode + " failed with " + identityPackage.getString("deviceName"));
|
Log.e("KDE/LanLinkProvider","Handshake as " + mode + " failed with " + identityPackage.getString("deviceName"));
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -294,7 +293,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
} else {
|
} else {
|
||||||
addLink(identityPackage, channel, connectionStarted, false);
|
addLink(identityPackage, channel, connectionStarted);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -302,7 +301,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addLink(final NetworkPackage identityPackage, Socket channel, LanLink.ConnectionStarted connectionOrigin, boolean useSsl) throws IOException {
|
private void addLink(final NetworkPackage identityPackage, Socket channel, LanLink.ConnectionStarted connectionOrigin) throws IOException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Log.e("addLink", identityPackage.serialize());
|
Log.e("addLink", identityPackage.serialize());
|
||||||
@ -315,7 +314,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
if (currentLink != null) {
|
if (currentLink != null) {
|
||||||
//Update old link
|
//Update old link
|
||||||
Log.i("KDE/LanLinkProvider", "Reusing same link for device " + deviceId);
|
Log.i("KDE/LanLinkProvider", "Reusing same link for device " + deviceId);
|
||||||
final Socket oldChannel = currentLink.reset(channel, connectionOrigin, useSsl, this);
|
final Socket oldChannel = currentLink.reset(channel, connectionOrigin, this);
|
||||||
new Timer().schedule(new TimerTask() {
|
new Timer().schedule(new TimerTask() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
@ -329,7 +328,7 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
|
|
||||||
Log.e("addLink", "create link");
|
Log.e("addLink", "create link");
|
||||||
//Let's create the link
|
//Let's create the link
|
||||||
LanLink link = new LanLink(context, deviceId, this, channel, connectionOrigin, useSsl);
|
LanLink link = new LanLink(context, deviceId, this, channel, connectionOrigin);
|
||||||
nioLinks.put(channel.hashCode(), link);
|
nioLinks.put(channel.hashCode(), link);
|
||||||
visibleComputers.put(deviceId, link);
|
visibleComputers.put(deviceId, link);
|
||||||
connectionAccepted(identityPackage, link);
|
connectionAccepted(identityPackage, link);
|
||||||
@ -389,14 +388,14 @@ public class LanLinkProvider extends BaseLinkProvider {
|
|||||||
private void setupTcpListener() {
|
private void setupTcpListener() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final ServerSocket serverSocket = LanLink.openUnsecureSocketOnFreePort(port);
|
tcpServer = LanLink.openUnsecureSocketOnFreePort(port);
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
while (running) {
|
while (running) {
|
||||||
try {
|
try {
|
||||||
Log.e("ServerSocket","Waiting...");
|
Log.e("ServerSocket","Waiting...");
|
||||||
Socket socket = serverSocket.accept();
|
Socket socket = tcpServer.accept();
|
||||||
Log.e("ServerSocket","Got a socket!");
|
Log.e("ServerSocket","Got a socket!");
|
||||||
configureSocket(socket);
|
configureSocket(socket);
|
||||||
tcpPackageReceived(socket);
|
tcpPackageReceived(socket);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user