mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-21 01:01:41 +00:00
Continued rework of networking, added root check
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package com.chameleonvision.settings;
|
||||
|
||||
import com.chameleonvision.util.ShellExec;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public enum Platform {
|
||||
WINDOWS_64("Windows x64"),
|
||||
LINUX_64("Linux x64"),
|
||||
LINUX_RASPBIAN("Linux Raspbian"),
|
||||
LINUX_AARCH64("Linux ARM 64bit"),
|
||||
LINUX_AARCH64("Linux For Tegra"),
|
||||
MACOS_64("Mac OS x64"),
|
||||
UNSUPPORTED("Unsupported Platform");
|
||||
|
||||
@@ -26,6 +30,29 @@ public enum Platform {
|
||||
return this == MACOS_64;
|
||||
}
|
||||
|
||||
private static ShellExec shell = new ShellExec(true, false);
|
||||
|
||||
public boolean isRoot() {
|
||||
if (isLinux() || isMac()) {
|
||||
try {
|
||||
shell.execute("id", null, true, "-u");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
while (!shell.isOutputCompleted()) {}
|
||||
if (shell.getExitCode() == 0) {
|
||||
var out = shell.getOutput();
|
||||
out = out.split("\n")[0];
|
||||
return out.equals("0");
|
||||
}
|
||||
} else if (isWindows()) {
|
||||
return true;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Platform getCurrentPlatform() {
|
||||
var osName = System.getProperty("os.name");
|
||||
var osArch = System.getProperty("os.arch");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.chameleonvision.settings;
|
||||
|
||||
import com.chameleonvision.settings.network.NetworkManager;
|
||||
import com.chameleonvision.network.NetworkManager;
|
||||
import com.chameleonvision.util.FileHelper;
|
||||
import com.chameleonvision.vision.camera.CameraManager;
|
||||
import com.google.gson.Gson;
|
||||
@@ -19,10 +19,10 @@ public class SettingsManager {
|
||||
|
||||
private SettingsManager() {}
|
||||
|
||||
public static void initialize(boolean manageNetwork) {
|
||||
public static void initialize() {
|
||||
initGeneralSettings();
|
||||
if (manageNetwork) {
|
||||
NetworkManager.init();
|
||||
// if (manageNetwork) {
|
||||
|
||||
// NetworkSettings netSettings = new NetworkSettings();
|
||||
// netSettings.hostname = GeneralSettings.hostname;
|
||||
// netSettings.gateway = GeneralSettings.gateway;
|
||||
@@ -30,7 +30,7 @@ public class SettingsManager {
|
||||
// netSettings.connectionType = GeneralSettings.connection_type;
|
||||
// netSettings.ip = GeneralSettings.ip;
|
||||
// netSettings.run();
|
||||
}
|
||||
// }
|
||||
var allCameras = CameraManager.getAllCamerasByName();
|
||||
if (!allCameras.containsKey(GeneralSettings.curr_camera) && allCameras.size() > 0) {
|
||||
var cam = allCameras.entrySet().stream().findFirst().get().getValue();
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.chameleonvision.settings.network;
|
||||
|
||||
import java.net.SocketException;
|
||||
import java.util.List;
|
||||
|
||||
public interface INetworking {
|
||||
String getHostname();
|
||||
NetworkIPMode getIPMode();
|
||||
boolean setDHCP();
|
||||
boolean setHostname(String hostname);
|
||||
boolean setStatic(String ipAddress, String netmask, String gateway);
|
||||
List<NetworkInterface> getNetworkInterfaces() throws SocketException;
|
||||
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.chameleonvision.settings.network;
|
||||
|
||||
import java.net.SocketException;
|
||||
import java.util.List;
|
||||
|
||||
public class LinuxNetworking implements INetworking {
|
||||
|
||||
@Override
|
||||
public String getHostname() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkIPMode getIPMode() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setDHCP() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setHostname(String hostname) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatic(String ipAddress, String netmask, String gateway) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NetworkInterface> getNetworkInterfaces() throws SocketException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.chameleonvision.settings.network;
|
||||
|
||||
public enum NetworkIPMode {
|
||||
DHCP,
|
||||
STATIC,
|
||||
UNKNOWN
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package com.chameleonvision.settings.network;
|
||||
|
||||
import com.chameleonvision.settings.NetworkSettings;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.util.Collections;
|
||||
|
||||
public class NetworkInterface {
|
||||
public final String name;
|
||||
public final String displayName;
|
||||
// public NetworkIPMode IPMode;
|
||||
// public String IPAddress;
|
||||
// public String Netmask;
|
||||
// public String Gateway;
|
||||
|
||||
public NetworkInterface(java.net.NetworkInterface inetface) {
|
||||
|
||||
name = inetface.getName();
|
||||
displayName = inetface.getDisplayName();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.chameleonvision.settings.network;
|
||||
|
||||
|
||||
import com.chameleonvision.settings.Platform;
|
||||
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class NetworkManager {
|
||||
private NetworkManager() {}
|
||||
|
||||
private static INetworking networking;
|
||||
|
||||
public static void init() {
|
||||
Platform platform = Platform.getCurrentPlatform();
|
||||
|
||||
if (platform.isLinux()) {
|
||||
networking = new LinuxNetworking();
|
||||
} else if (platform.isWindows()) {
|
||||
networking = new WindowsNetworking();
|
||||
}
|
||||
|
||||
List<NetworkInterface> interfaces = new ArrayList<>();
|
||||
|
||||
try {
|
||||
interfaces = networking.getNetworkInterfaces();
|
||||
} catch (SocketException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (interfaces != null) {
|
||||
for (var inetface : interfaces) {
|
||||
if (inetface.displayName.toLowerCase().contains("asus")) {
|
||||
// networking.setHostname("BIGRIG");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
package com.chameleonvision.settings.network;
|
||||
|
||||
import com.chameleonvision.settings.NetworkSettings;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class WindowsNetworking implements INetworking {
|
||||
|
||||
@Override
|
||||
public String getHostname() {
|
||||
try {
|
||||
InetAddress localhost = InetAddress.getLocalHost();
|
||||
return localhost.getHostName().split("/")[0];
|
||||
} catch (UnknownHostException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkIPMode getIPMode() {
|
||||
return NetworkIPMode.UNKNOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setDHCP() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setHostname(String newHostname) {
|
||||
var currentHostname = getHostname();
|
||||
|
||||
if (getHostname() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String command = String.format("wmic computersystem where name=\"%s\" call rename name=\"%s\"", currentHostname, newHostname);
|
||||
|
||||
try {
|
||||
var process = Runtime.getRuntime().exec(command);
|
||||
var returnCode = process.waitFor();
|
||||
return returnCode == 0;
|
||||
} catch(Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean setStatic(String ipAddress, String netmask, String gateway) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<NetworkInterface> getNetworkInterfaces() throws SocketException {
|
||||
var netInterfaces = Collections.list(java.net.NetworkInterface.getNetworkInterfaces());
|
||||
|
||||
List<NetworkInterface> goodInterfaces = new ArrayList<>();
|
||||
|
||||
var teamBytes = NetworkSettings.GetTeamNumberIPBytes(SettingsManager.GeneralSettings.team_number);
|
||||
|
||||
for (var inetface : netInterfaces) {
|
||||
if (inetface.getDisplayName().toLowerCase().contains("bluetooth")) continue;
|
||||
if (inetface.getDisplayName().toLowerCase().contains("virtual")) continue;
|
||||
if (inetface.getDisplayName().toLowerCase().contains("loopback")) continue;
|
||||
if (!inetface.isUp()) continue;
|
||||
for (var inetAddr : Collections.list(inetface.getInetAddresses())) {
|
||||
var rawAddr = inetAddr.getAddress();
|
||||
if (rawAddr[1] == teamBytes[0] && rawAddr[2] == teamBytes[1]) {
|
||||
goodInterfaces.add(new NetworkInterface(inetface));
|
||||
}
|
||||
}
|
||||
}
|
||||
return goodInterfaces;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user