Search in sources :

Example 1 with Cryptomator

use of org.cryptomator.ui.Cryptomator in project cryptomator by cryptomator.

the class SingleInstanceManager method getRemoteInstance.

/**
	 * Checks if there is a valid port at
	 * {@link Preferences#userNodeForPackage(Class)} for {@link Cryptomator} under the
	 * given applicationKey, tries to connect to the port at the loopback
	 * address and checks if the port identifies with the applicationKey.
	 * 
	 * @param applicationKey
	 *            key used to load the port and check the identity of the
	 *            connection.
	 * @return
	 */
public static Optional<RemoteInstance> getRemoteInstance(String applicationKey) {
    Optional<Integer> port = getSavedPort(applicationKey);
    if (!port.isPresent()) {
        return Optional.empty();
    }
    SocketChannel channel = null;
    boolean close = true;
    try {
        channel = SocketChannel.open();
        channel.configureBlocking(false);
        LOG.debug("connecting to instance {}", port.get());
        channel.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), port.get()));
        SocketChannel fChannel = channel;
        if (!TimeoutTask.attempt(t -> fChannel.finishConnect(), 1000, 10)) {
            return Optional.empty();
        }
        LOG.debug("connected to instance {}", port.get());
        final byte[] bytes = applicationKey.getBytes(StandardCharsets.UTF_8);
        ByteBuffer buf = ByteBuffer.allocate(bytes.length);
        tryFill(channel, buf, 1000);
        if (buf.hasRemaining()) {
            return Optional.empty();
        }
        buf.flip();
        for (int i = 0; i < bytes.length; i++) {
            if (buf.get() != bytes[i]) {
                return Optional.empty();
            }
        }
        close = false;
        return Optional.of(new RemoteInstance(channel));
    } catch (Exception e) {
        return Optional.empty();
    } finally {
        if (close) {
            IOUtils.closeQuietly(channel);
        }
    }
}
Also used : Cryptomator(org.cryptomator.ui.Cryptomator) Selector(java.nio.channels.Selector) LoggerFactory(org.slf4j.LoggerFactory) ByteBuffer(java.nio.ByteBuffer) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) ListenerRegistration(org.cryptomator.ui.util.ListenerRegistry.ListenerRegistration) SocketChannel(java.nio.channels.SocketChannel) ExecutorService(java.util.concurrent.ExecutorService) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Logger(org.slf4j.Logger) ClosedChannelException(java.nio.channels.ClosedChannelException) SelectionKey(java.nio.channels.SelectionKey) Set(java.util.Set) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Preferences(java.util.prefs.Preferences) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) ClosedSelectorException(java.nio.channels.ClosedSelectorException) SelectableChannel(java.nio.channels.SelectableChannel) Closeable(java.io.Closeable) WritableByteChannel(java.nio.channels.WritableByteChannel) Optional(java.util.Optional) SocketChannel(java.nio.channels.SocketChannel) ServerSocketChannel(java.nio.channels.ServerSocketChannel) InetSocketAddress(java.net.InetSocketAddress) ByteBuffer(java.nio.ByteBuffer) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ClosedSelectorException(java.nio.channels.ClosedSelectorException)

Example 2 with Cryptomator

use of org.cryptomator.ui.Cryptomator in project cryptomator by cryptomator.

the class SingleInstanceManager method startLocalInstance.

/**
	 * Creates a server socket on a free port and saves the port in
	 * {@link Preferences#userNodeForPackage(Class)} for {@link Cryptomator} under the
	 * given applicationKey.
	 * 
	 * @param applicationKey
	 *            key used to save the port and identify upon connection.
	 * @param exec
	 *            the task which is submitted is interruptable.
	 * @return
	 * @throws IOException
	 */
public static LocalInstance startLocalInstance(String applicationKey, ExecutorService exec) throws IOException {
    final ServerSocketChannel channel = ServerSocketChannel.open();
    boolean success = false;
    try {
        channel.configureBlocking(false);
        channel.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
        final int port = ((InetSocketAddress) channel.getLocalAddress()).getPort();
        Preferences.userNodeForPackage(Cryptomator.class).putInt(applicationKey, port);
        LOG.debug("InstanceManager bound to port {}", port);
        Selector selector = Selector.open();
        channel.register(selector, SelectionKey.OP_ACCEPT);
        LocalInstance instance = new LocalInstance(applicationKey, channel, selector, port);
        exec.submit(instance::selectionLoop);
        success = true;
        return instance;
    } finally {
        if (!success) {
            channel.close();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Cryptomator(org.cryptomator.ui.Cryptomator) ServerSocketChannel(java.nio.channels.ServerSocketChannel) Selector(java.nio.channels.Selector)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2 Selector (java.nio.channels.Selector)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)2 Cryptomator (org.cryptomator.ui.Cryptomator)2 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 ByteBuffer (java.nio.ByteBuffer)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 ClosedSelectorException (java.nio.channels.ClosedSelectorException)1 ReadableByteChannel (java.nio.channels.ReadableByteChannel)1 SelectableChannel (java.nio.channels.SelectableChannel)1 SelectionKey (java.nio.channels.SelectionKey)1 SocketChannel (java.nio.channels.SocketChannel)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 StandardCharsets (java.nio.charset.StandardCharsets)1 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Set (java.util.Set)1