Merge pull request #182 from allombar/master

Implement Nitro Feature for macOS
This commit is contained in:
sirjonasxx 2024-06-21 00:11:32 +02:00 committed by GitHub
commit 625d31d327
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 102 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package gearth.protocol.connection.proxy.nitro.os; package gearth.protocol.connection.proxy.nitro.os;
import gearth.misc.OSValidator; import gearth.misc.OSValidator;
import gearth.protocol.connection.proxy.nitro.os.macos.NitroMacOS;
import gearth.protocol.connection.proxy.nitro.os.windows.NitroWindows; import gearth.protocol.connection.proxy.nitro.os.windows.NitroWindows;
import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.NotImplementedException;
@ -15,7 +16,10 @@ public final class NitroOsFunctionsFactory {
throw new NotImplementedException("unix nitro is not implemented yet"); throw new NotImplementedException("unix nitro is not implemented yet");
} }
throw new NotImplementedException("macOS nitro is not implemented yet"); if (OSValidator.isMac()) {
} return new NitroMacOS();
}
throw new NotImplementedException("unsupported operating system");
}
} }

View File

@ -0,0 +1,96 @@
package gearth.protocol.connection.proxy.nitro.os.macos;
import gearth.misc.RuntimeUtil;
import gearth.protocol.connection.proxy.nitro.os.NitroOsFunctions;
import java.io.File;
import java.io.IOException;
public class NitroMacOS implements NitroOsFunctions {
/**
* Semicolon separated hosts to ignore for proxying.
*/
private static final String PROXY_IGNORE = "discord.com;discordapp.com;github.com;";
/**
* Checks if the certificate is trusted by the local machine.
* @param certificate Absolute path to the certificate.
* @return true if trusted
*/
@Override
public boolean isRootCertificateTrusted(File certificate) {
try {
final String output = RuntimeUtil.getCommandOutput(new String[] {"sh", "-c", "security verify-cert -c \"" + certificate.getAbsolutePath() + "\""});
return !output.contains("CSSMERR_TP_NOT_TRUSTED");
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean installRootCertificate(File certificate) {
final String certificatePath = certificate.getAbsolutePath();
try {
// Install the certificate
Process process = new ProcessBuilder("sh", "-c", "sudo security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain \"" + certificatePath + "\"")
.inheritIO()
.start();
// Wait for the process to complete
int exitCode = process.waitFor();
if (exitCode != 0) {
System.out.printf("security add-trusted-cert command exited with exit code %d%n", exitCode);
return false;
}
return true;
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean registerSystemProxy(String host, int port) {
try {
final String[] ignoreHosts = PROXY_IGNORE.split(";");
// Enable proxy
Runtime.getRuntime().exec("networksetup -setwebproxy Wi-Fi " + host + " " + port);
Runtime.getRuntime().exec("networksetup -setsecurewebproxy Wi-Fi " + host + " " + port);
// Set proxy bypass domains
for (String ignoreHost : ignoreHosts) {
Runtime.getRuntime().exec("networksetup -setproxybypassdomains Wi-Fi " + ignoreHost);
}
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean unregisterSystemProxy() {
try {
// Disable proxy
Runtime.getRuntime().exec("networksetup -setwebproxystate Wi-Fi off");
Runtime.getRuntime().exec("networksetup -setsecurewebproxystate Wi-Fi off");
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}