ソースを参照

set loopback alias in Cryptomator app during build time

Armin Schrenk 2 年 前
コミット
13debaafbe

+ 1 - 0
.github/workflows/win-exe.yml

@@ -98,6 +98,7 @@ jobs:
           --java-options "-Dcryptomator.p12Path=\"~/AppData/Roaming/Cryptomator/key.p12\""
           --java-options "-Dcryptomator.ipcSocketPath=\"~/AppData/Roaming/Cryptomator/ipc.socket\""
           --java-options "-Dcryptomator.mountPointsDir=\"~/Cryptomator\""
+          --java-options "-Dcryptomator.loopbackAlias=\"${{ env.LOOPBACK_ALIAS }}\""
           --java-options "-Dcryptomator.showTrayIcon=true"
           --java-options "-Dcryptomator.buildNumber=\"msi-${{ steps.versions.outputs.revNum }}\""
           --java-options "-Dcryptomator.integrationsWin.autoStartShellLinkName=\"Cryptomator\""

+ 3 - 2
dist/win/build.ps1

@@ -35,6 +35,7 @@ Write-Output "`$buildDir=$buildDir"
 Write-Output "`$Env:JAVA_HOME=$Env:JAVA_HOME"
 
 $copyright = "(C) $CopyrightStartYear - $((Get-Date).Year) $Vendor"
+$loopbackAlias = 'cryptomator-vault'
 
 # compile
 &mvn -B -f $buildDir/../../pom.xml clean package -DskipTests -Pwin
@@ -85,6 +86,7 @@ if ($clean -and (Test-Path -Path $appPath)) {
 	--java-options "-Dcryptomator.ipcSocketPath=`"~/AppData/Roaming/$AppName/ipc.socket`"" `
 	--java-options "-Dcryptomator.p12Path=`"~/AppData/Roaming/$AppName/key.p12`"" `
 	--java-options "-Dcryptomator.mountPointsDir=`"~/$AppName`"" `
+	--java-options "-Dcryptomator.loopbackAlias=`"$loopbackAlias`"" `
 	--java-options "-Dcryptomator.integrationsWin.autoStartShellLinkName=`"$AppName`"" `
 	--java-options "-Dcryptomator.integrationsWin.keychainPaths=`"~/AppData/Roaming/$AppName/keychain.json`"" `
 	--java-options "-Dcryptomator.showTrayIcon=true" `
@@ -107,9 +109,8 @@ Copy-Item "contrib\*" -Destination "$AppName"
 attrib -r "$AppName\$AppName.exe"
 # patch batch script to set hostfile
 $webDAVPatcher = "$AppName\patchWebDAV.bat"
-$alias = 'cryptomator-vault'
 try {
-	(Get-Content $webDAVPatcher ) -replace '::REPLACE ME', "SET LOOPBACK_ALIAS=`"$alias`"" | Set-Content $webDAVPatcher
+	(Get-Content $webDAVPatcher ) -replace '::REPLACE ME', "SET LOOPBACK_ALIAS=`"$loopbackAlias`"" | Set-Content $webDAVPatcher
 } catch {
    Write-Host "Failed to set LOOPBACK_ALIAS for patchWebDAV.bat"
    exit 1

+ 7 - 10
src/main/java/org/cryptomator/common/Environment.java

@@ -26,6 +26,7 @@ public class Environment {
 	private static final String KEYCHAIN_PATHS_PROP_NAME = "cryptomator.integrationsWin.keychainPaths";
 	private static final String P12_PATH_PROP_NAME = "cryptomator.p12Path";
 	private static final String LOG_DIR_PROP_NAME = "cryptomator.logDir";
+	private static final String LOOPBACK_ALIAS_PROP_NAME = "cryptomator.loopbackAlias";
 	private static final String MOUNTPOINT_DIR_PROP_NAME = "cryptomator.mountPointsDir";
 	private static final String MIN_PW_LENGTH_PROP_NAME = "cryptomator.minPwLength";
 	private static final String APP_VERSION_PROP_NAME = "cryptomator.appVersion";
@@ -45,6 +46,7 @@ public class Environment {
 		logCryptomatorSystemProperty(IPC_SOCKET_PATH_PROP_NAME);
 		logCryptomatorSystemProperty(KEYCHAIN_PATHS_PROP_NAME);
 		logCryptomatorSystemProperty(LOG_DIR_PROP_NAME);
+		logCryptomatorSystemProperty(LOOPBACK_ALIAS_PROP_NAME);
 		logCryptomatorSystemProperty(PLUGIN_DIR_PROP_NAME);
 		logCryptomatorSystemProperty(MOUNTPOINT_DIR_PROP_NAME);
 		logCryptomatorSystemProperty(MIN_PW_LENGTH_PROP_NAME);
@@ -90,6 +92,10 @@ public class Environment {
 		return getPath(LOG_DIR_PROP_NAME).map(this::replaceHomeDir);
 	}
 
+	public Optional<String> getLoopbackAlias() {
+		return Optional.ofNullable(System.getProperty(LOOPBACK_ALIAS_PROP_NAME));
+	}
+
 	public Optional<Path> getPluginDir() {
 		return getPath(PLUGIN_DIR_PROP_NAME).map(this::replaceHomeDir);
 	}
@@ -112,22 +118,13 @@ public class Environment {
 	}
 
 	public int getMinPwLength() {
-		return getInt(MIN_PW_LENGTH_PROP_NAME, DEFAULT_MIN_PW_LENGTH);
+		return Integer.getInteger(MIN_PW_LENGTH_PROP_NAME, DEFAULT_MIN_PW_LENGTH);
 	}
 
 	public boolean showTrayIcon() {
 		return Boolean.getBoolean(TRAY_ICON_PROP_NAME);
 	}
 
-	private int getInt(String propertyName, int defaultValue) {
-		String value = System.getProperty(propertyName);
-		try {
-			return Integer.parseInt(value);
-		} catch (NumberFormatException e) { // includes "null" values
-			return defaultValue;
-		}
-	}
-
 	private Optional<Path> getPath(String propertyName) {
 		String value = System.getProperty(propertyName);
 		return Optional.ofNullable(value).map(Paths::get);

+ 13 - 11
src/main/java/org/cryptomator/common/vaults/WebDavVolume.java

@@ -2,6 +2,7 @@ package org.cryptomator.common.vaults;
 
 
 import com.google.common.base.CharMatcher;
+import org.cryptomator.common.Environment;
 import org.cryptomator.common.settings.Settings;
 import org.cryptomator.common.settings.VaultSettings;
 import org.cryptomator.common.settings.VolumeImpl;
@@ -22,12 +23,11 @@ import java.util.function.Supplier;
 
 public class WebDavVolume implements Volume {
 
-	private static final String LOCALHOST_ALIAS = "cryptomator-vault";
-
 	private final Provider<WebDavServer> serverProvider;
 	private final VaultSettings vaultSettings;
 	private final Settings settings;
 	private final WindowsDriveLetters windowsDriveLetters;
+	private final Environment environment;
 
 	private WebDavServer server;
 	private WebDavServletController servlet;
@@ -35,11 +35,12 @@ public class WebDavVolume implements Volume {
 	private Consumer<Throwable> onExitAction;
 
 	@Inject
-	public WebDavVolume(Provider<WebDavServer> serverProvider, VaultSettings vaultSettings, Settings settings, WindowsDriveLetters windowsDriveLetters) {
+	public WebDavVolume(Provider<WebDavServer> serverProvider, VaultSettings vaultSettings, Settings settings, WindowsDriveLetters windowsDriveLetters, Environment environment) {
 		this.serverProvider = serverProvider;
 		this.vaultSettings = vaultSettings;
 		this.settings = settings;
 		this.windowsDriveLetters = windowsDriveLetters;
+		this.environment = environment;
 	}
 
 	@Override
@@ -129,16 +130,17 @@ public class WebDavVolume implements Volume {
 	}
 
 	private String getLocalhostAliasOrNull() {
-		try {
-			InetAddress alias = InetAddress.getByName(LOCALHOST_ALIAS);
-			if (alias.getHostAddress().equals("127.0.0.1")) {
-				return LOCALHOST_ALIAS;
-			} else {
-				return null;
+		return environment.getLoopbackAlias().map(alias -> {
+			try {
+				var address = InetAddress.getByName(alias);
+				if (address.getHostAddress().equals("127.0.0.1")) {
+					return alias;
+				}
+			} catch (UnknownHostException e) {
+				//no-op
 			}
-		} catch (UnknownHostException e) {
 			return null;
-		}
+		}).orElse(null);
 	}
 
 	private void cleanup() {