Skip to content

Commit

Permalink
Merge pull request #182 from allombar/master
Browse files Browse the repository at this point in the history
Implement Nitro Feature for macOS
  • Loading branch information
sirjonasxx authored Jun 20, 2024
2 parents 8a717ad + ed94340 commit 625d31d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gearth.protocol.connection.proxy.nitro.os;

import gearth.misc.OSValidator;
import gearth.protocol.connection.proxy.nitro.os.macos.NitroMacOS;
import gearth.protocol.connection.proxy.nitro.os.windows.NitroWindows;
import org.apache.commons.lang3.NotImplementedException;

Expand All @@ -15,7 +16,10 @@ public static NitroOsFunctions create() {
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");
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit 625d31d

Please sign in to comment.