|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2016 Sebastian Stenzel and others. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the accompanying LICENSE.txt. |
| 5 | + * |
| 6 | + * Contributors: |
| 7 | + * Sebastian Stenzel - initial API and implementation |
| 8 | + *******************************************************************************/ |
| 9 | +package org.cryptomator.frontend.webdav; |
| 10 | + |
| 11 | +import com.google.common.base.Preconditions; |
| 12 | +import org.cryptomator.cryptofs.CryptoFileSystemProperties; |
| 13 | +import org.cryptomator.cryptofs.CryptoFileSystemProvider; |
| 14 | +import org.cryptomator.cryptofs.DirStructure; |
| 15 | +import org.cryptomator.cryptolib.common.MasterkeyFileAccess; |
| 16 | +import org.cryptomator.integrations.mount.MountCapability; |
| 17 | +import org.cryptomator.integrations.mount.MountFailedException; |
| 18 | +import org.cryptomator.integrations.mount.MountService; |
| 19 | +import org.cryptomator.integrations.mount.UnmountFailedException; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import org.slf4j.simple.SimpleLogger; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.UncheckedIOException; |
| 26 | +import java.nio.file.FileSystem; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.nio.file.Paths; |
| 29 | +import java.security.NoSuchAlgorithmException; |
| 30 | +import java.security.SecureRandom; |
| 31 | +import java.util.Scanner; |
| 32 | + |
| 33 | +public class CryptoFsTest { |
| 34 | + |
| 35 | + static { |
| 36 | + System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "INFO"); |
| 37 | + System.setProperty(SimpleLogger.LOG_FILE_KEY, "System.out"); |
| 38 | + System.setProperty(SimpleLogger.SHOW_DATE_TIME_KEY, "true"); |
| 39 | + System.setProperty(SimpleLogger.DATE_TIME_FORMAT_KEY, "HH:mm:ss:SSS"); |
| 40 | + } |
| 41 | + |
| 42 | + private static final Logger LOG = LoggerFactory.getLogger(CryptoFsTest.class); |
| 43 | + |
| 44 | + public static void main(String[] args) throws IOException, MountFailedException, NoSuchAlgorithmException { |
| 45 | + var webDAVMountService = MountService.get() // |
| 46 | + .filter(svc -> svc.getClass().getPackageName().startsWith("org.cryptomator.frontend.webdav.")) // |
| 47 | + .findFirst() // |
| 48 | + .orElseThrow(() -> new MountFailedException("Did not find a mount provider")); |
| 49 | + LOG.info("Using mount provider: {}", webDAVMountService.displayName()); |
| 50 | + try (Scanner scanner = new Scanner(System.in)) { |
| 51 | + LOG.info("Enter path to the vault you want to mirror:"); |
| 52 | + Path vaultPath = Paths.get(scanner.nextLine()); |
| 53 | + Preconditions.checkArgument(CryptoFileSystemProvider.checkDirStructureForVault(vaultPath, "vault.cryptomator", "masterkey.cryptomator") == DirStructure.VAULT, "Not a vault: " + vaultPath); |
| 54 | + |
| 55 | + LOG.info("Enter vault password:"); |
| 56 | + String passphrase = scanner.nextLine(); |
| 57 | + |
| 58 | + SecureRandom csprng = SecureRandom.getInstanceStrong(); |
| 59 | + CryptoFileSystemProperties props = CryptoFileSystemProperties.cryptoFileSystemProperties() |
| 60 | + .withKeyLoader(url -> new MasterkeyFileAccess(new byte[0], csprng).load(vaultPath.resolve("masterkey.cryptomator"), passphrase)) |
| 61 | + .build(); |
| 62 | + try (FileSystem cryptoFs = CryptoFileSystemProvider.newFileSystem(vaultPath, props)) { |
| 63 | + Path p = cryptoFs.getPath("/"); |
| 64 | + mount(webDAVMountService, p, scanner); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void mount(MountService mountProvider, Path pathToMirror, Scanner scanner) throws MountFailedException { |
| 70 | + var mountBuilder = mountProvider.forFileSystem(pathToMirror); |
| 71 | + if (mountProvider.hasCapability(MountCapability.VOLUME_ID)) { |
| 72 | + mountBuilder.setVolumeId("mirror"); |
| 73 | + } |
| 74 | + if (mountProvider.hasCapability(MountCapability.VOLUME_NAME)) { |
| 75 | + mountBuilder.setVolumeName("Mirror"); |
| 76 | + } |
| 77 | + if (mountProvider.hasCapability(MountCapability.FILE_SYSTEM_NAME)) { |
| 78 | + mountBuilder.setFileSystemName("MirrorFS"); |
| 79 | + } |
| 80 | + if (! mountProvider.hasCapability(MountCapability.MOUNT_TO_SYSTEM_CHOSEN_PATH)) { |
| 81 | + // don't set a mount point |
| 82 | + } else { |
| 83 | + LOG.info("Enter mount point: "); |
| 84 | + Path m = Paths.get(scanner.nextLine()); |
| 85 | + mountBuilder.setMountpoint(m); |
| 86 | + } |
| 87 | + |
| 88 | + try (var mount = mountBuilder.mount()) { |
| 89 | + LOG.info("Mounted successfully to: {}", mount.getMountpoint().uri()); |
| 90 | + LOG.info("Enter anything to unmount..."); |
| 91 | + System.in.read(); |
| 92 | + |
| 93 | + try { |
| 94 | + mount.unmount(); |
| 95 | + } catch (UnmountFailedException e) { |
| 96 | + if (mountProvider.hasCapability(MountCapability.UNMOUNT_FORCED)) { |
| 97 | + LOG.warn("Graceful unmount failed. Attempting force-unmount..."); |
| 98 | + mount.unmountForced(); |
| 99 | + } |
| 100 | + } |
| 101 | + } catch (UnmountFailedException e) { |
| 102 | + LOG.warn("Unmount failed.", e); |
| 103 | + } catch (IOException e) { |
| 104 | + throw new UncheckedIOException(e); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments