Search in sources :

Example 41 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class LanPairingHandler method createPairPacket.

private NetworkPacket createPairPacket() {
    NetworkPacket np = new NetworkPacket(NetworkPacket.PACKET_TYPE_PAIR);
    np.set("pair", true);
    return np;
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 42 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class LanLinkProvider method identityPacketReceived.

/**
 * Called when a new 'identity' packet is received. Those are passed here by
 * {@link #tcpPacketReceived(Socket)} and {@link #udpPacketReceived(DatagramPacket)}.
 * <p>
 * If the remote device should be connected, this calls {@link #addLink}.
 * Otherwise, if there was an Exception, we unpair from that device.
 * </p>
 *
 * @param identityPacket    identity of a remote device
 * @param socket            a new Socket, which should be used to receive packets from the remote device
 * @param connectionStarted which side started this connection
 */
private void identityPacketReceived(final NetworkPacket identityPacket, final Socket socket, final LanLink.ConnectionStarted connectionStarted) {
    String myId = DeviceHelper.getDeviceId(context);
    final String deviceId = identityPacket.getString("deviceId");
    if (deviceId.equals(myId)) {
        Log.e("KDE/LanLinkProvider", "Somehow I'm connected to myself, ignoring. This should not happen.");
        return;
    }
    // If I'm the TCP server I will be the SSL client and viceversa.
    final boolean clientMode = (connectionStarted == LanLink.ConnectionStarted.Locally);
    // Do the SSL handshake
    try {
        SharedPreferences preferences = context.getSharedPreferences("trusted_devices", Context.MODE_PRIVATE);
        boolean isDeviceTrusted = preferences.getBoolean(deviceId, false);
        if (isDeviceTrusted && !SslHelper.isCertificateStored(context, deviceId)) {
            // Device paired with and old version, we can't use it as we lack the certificate
            BackgroundService.RunCommand(context, service -> {
                Device device = service.getDevice(deviceId);
                if (device == null)
                    return;
                device.unpair();
                // Retry as unpaired
                identityPacketReceived(identityPacket, socket, connectionStarted);
            });
        }
        Log.i("KDE/LanLinkProvider", "Starting SSL handshake with " + identityPacket.getString("deviceName") + " trusted:" + isDeviceTrusted);
        final SSLSocket sslsocket = SslHelper.convertToSslSocket(context, socket, deviceId, isDeviceTrusted, clientMode);
        sslsocket.addHandshakeCompletedListener(event -> {
            String mode = clientMode ? "client" : "server";
            try {
                Certificate certificate = event.getPeerCertificates()[0];
                identityPacket.set("certificate", Base64.encodeToString(certificate.getEncoded(), 0));
                Log.i("KDE/LanLinkProvider", "Handshake as " + mode + " successful with " + identityPacket.getString("deviceName") + " secured with " + event.getCipherSuite());
                addLink(identityPacket, sslsocket, connectionStarted);
            } catch (Exception e) {
                Log.e("KDE/LanLinkProvider", "Handshake as " + mode + " failed with " + identityPacket.getString("deviceName"), e);
                BackgroundService.RunCommand(context, service -> {
                    Device device = service.getDevice(deviceId);
                    if (device == null)
                        return;
                    device.unpair();
                });
            }
        });
        // Handshake is blocking, so do it on another thread and free this thread to keep receiving new connection
        new Thread(() -> {
            try {
                synchronized (this) {
                    sslsocket.startHandshake();
                }
            } catch (Exception e) {
                Log.e("KDE/LanLinkProvider", "Handshake failed with " + identityPacket.getString("deviceName"), e);
            // String[] ciphers = sslsocket.getSupportedCipherSuites();
            // for (String cipher : ciphers) {
            // Log.i("SupportedCiphers","cipher: " + cipher);
            // }
            }
        }).start();
    } catch (Exception e) {
        Log.e("LanLink", "Exception", e);
    }
}
Also used : Context(android.content.Context) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) HashMap(java.util.HashMap) Timer(java.util.Timer) Device(org.kde.kdeconnect.Device) ArrayList(java.util.ArrayList) Charsets(kotlin.text.Charsets) DatagramSocket(java.net.DatagramSocket) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) SocketException(java.net.SocketException) DeviceHelper(org.kde.kdeconnect.Helpers.DeviceHelper) CustomDevicesActivity(org.kde.kdeconnect.UserInterface.CustomDevicesActivity) PreferenceManager(android.preference.PreferenceManager) TimerTask(java.util.TimerTask) NetworkPacket(org.kde.kdeconnect.NetworkPacket) Log(android.util.Log) OutputStream(java.io.OutputStream) IOException(java.io.IOException) SslHelper(org.kde.kdeconnect.Helpers.SecurityHelpers.SslHelper) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) BackgroundService(org.kde.kdeconnect.BackgroundService) SocketFactory(javax.net.SocketFactory) Certificate(java.security.cert.Certificate) SharedPreferences(android.content.SharedPreferences) Base64(android.util.Base64) BaseLinkProvider(org.kde.kdeconnect.Backends.BaseLinkProvider) BaseLink(org.kde.kdeconnect.Backends.BaseLink) BufferedReader(java.io.BufferedReader) DatagramPacket(java.net.DatagramPacket) TrustedNetworkHelper(org.kde.kdeconnect.Helpers.TrustedNetworkHelper) SharedPreferences(android.content.SharedPreferences) Device(org.kde.kdeconnect.Device) SSLSocket(javax.net.ssl.SSLSocket) SocketException(java.net.SocketException) IOException(java.io.IOException) Certificate(java.security.cert.Certificate)

Example 43 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class MprisPlugin method sendCommand.

private void sendCommand(String player, String method, boolean value) {
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_MPRIS_REQUEST);
    np.set("player", player);
    np.set(method, value);
    device.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 44 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class ClipboardPlugin method sendConnectPacket.

private void sendConnectPacket() {
    String content = ClipboardListener.instance(context).getCurrentContent();
    NetworkPacket np = new NetworkPacket(ClipboardPlugin.PACKET_TYPE_CLIPBOARD_CONNECT);
    long timestamp = ClipboardListener.instance(context).getUpdateTimestamp();
    np.set("timestamp", timestamp);
    np.set("content", content);
    device.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Example 45 with NetworkPacket

use of org.kde.kdeconnect.NetworkPacket in project kdeconnect-android by KDE.

the class MousePadPlugin method sendRightClick.

public void sendRightClick() {
    NetworkPacket np = new NetworkPacket(PACKET_TYPE_MOUSEPAD_REQUEST);
    np.set("rightclick", true);
    device.sendPacket(np);
}
Also used : NetworkPacket(org.kde.kdeconnect.NetworkPacket)

Aggregations

NetworkPacket (org.kde.kdeconnect.NetworkPacket)77 IOException (java.io.IOException)6 SSLSocket (javax.net.ssl.SSLSocket)4 SMSHelper (org.kde.kdeconnect.Helpers.SMSHelper)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 ServerSocket (java.net.ServerSocket)3 Socket (java.net.Socket)3 SocketException (java.net.SocketException)3 ArrayList (java.util.ArrayList)3 Timer (java.util.Timer)3 SuppressLint (android.annotation.SuppressLint)2 Uri (android.net.Uri)2 SmsMessage (android.telephony.SmsMessage)2 SpannableString (android.text.SpannableString)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 DatagramSocket (java.net.DatagramSocket)2 InetAddress (java.net.InetAddress)2 InetSocketAddress (java.net.InetSocketAddress)2