initial work

This commit is contained in:
ori agranat
2020-02-22 21:38:17 +02:00
parent ad8c0f0105
commit 676db036f8
3 changed files with 97 additions and 54 deletions

View File

@@ -36,6 +36,7 @@
<orderEntry type="library" name="Maven: org.json:json:20190722" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-nop:1.7.26" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-math3:3.6.1" level="project" />
<orderEntry type="library" name="Maven: commons-io:commons-io:2.6" level="project" />
<orderEntry type="library" name="Maven: org.msgpack:msgpack-core:0.8.18" level="project" />
<orderEntry type="library" name="Maven: org.msgpack:jackson-dataformat-msgpack:0.8.18" level="project" />
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.9" level="project" />

View File

@@ -86,6 +86,11 @@
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>msgpack-core</artifactId>

View File

@@ -1,73 +1,110 @@
package com.chameleonvision.network;
import io.javalin.core.util.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class LinuxNetworking extends SysNetworking {
@Override
public boolean setDHCP() {
String[] clearArgs = { "addr", "flush", "dev", networkInterface.name };
try {
int clearRetCode = shell.execute("ip", clearArgs);
int dhcpRetCode = shell.execute("dhclient", networkInterface.name);
return clearRetCode == 0 && dhcpRetCode == 0;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Override
public boolean setDHCP() {
File interfaces = new File("/etc/network/interfaces");
try {
List<String> lines = FileUtils.readLines(interfaces, StandardCharsets.UTF_8);
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
if (line.contains("iface " + networkInterface.name)) {
line = "iface " + networkInterface.name + "inet dhcp";
lines.set(i, line);
List<Integer> rLines = new ArrayList<>();
for (var j = i; j < lines.size(); j++) {
String tmp = lines.get(j);
if (tmp.contains("address") || tmp.contains("netmask") || tmp.contains("gateway")) {
rLines.add(j);
}
if (tmp.contains("iface")) {
break;
}
}
for (Integer rLine : rLines) {
lines.remove(rLine.intValue());
}
FileUtils.writeLines(interfaces, lines);
Process p = Runtime.getRuntime().exec("systemctl restart network");
p.waitFor();
return true;
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
@Override
public boolean setHostname(String newHostname) {
String[] setHostnameArgs = { "set-hostname", newHostname };
try {
var setHostnameRetCode = shell.execute("hostnamectl", setHostnameArgs);
return setHostnameRetCode == 0;
} catch(Exception e) {
e.printStackTrace();
return false;
}
}
return false;
}
@Override
public boolean setStatic(String ipAddress, String netmask, String gateway, String broadcast) {
try {
String[] clearArgs = { "addr", "flush", "dev", networkInterface.name };
String[] setIPArgs = { "addr", "add", String.format("%s/%s", ipAddress, netmask), "broadcast", broadcast, "dev", networkInterface.name };
String[] setGatewayArgs = { "route", "replace", "default", "via", gateway, "dev", networkInterface.name };
@Override
public boolean setHostname(String newHostname) {
String[] setHostnameArgs = {"set-hostname", newHostname};
try {
var setHostnameRetCode = shell.execute("hostnamectl", setHostnameArgs);
return setHostnameRetCode == 0;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
int clearRetCode = shell.execute("ip", clearArgs);
int setIPRetCode = shell.execute("ip", setIPArgs);
int setGatewayRetCode = shell.execute("ip", setGatewayArgs);
@Override
public boolean setStatic(String ipAddress, String netmask, String gateway, String broadcast) {
File interfaces = new File("/etc/network/interfaces");
try {
List<String> lines = FileUtils.readLines(interfaces, StandardCharsets.UTF_8);
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
if (line.contains("iface " + networkInterface.name)) {
line = "iface " + networkInterface.name + "inet static";
lines.set(i, line);
lines.add(i + 1, "address " + ipAddress);
lines.add(i + 2, "netmask " + netmask);
lines.add(i + 2, "gateway " + gateway);
FileUtils.writeLines(interfaces, lines);
Process p = Runtime.getRuntime().exec("systemctl restart network");
p.waitFor();
return true;
}
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return false;
}
return clearRetCode == 0 && setIPRetCode == 0 && setGatewayRetCode == 0;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
@Override
public List<java.net.NetworkInterface> getNetworkInterfaces() throws SocketException {
List<java.net.NetworkInterface> netInterfaces;
try {
netInterfaces = Collections.list(java.net.NetworkInterface.getNetworkInterfaces());
} catch (SocketException e) {
return null;
}
@Override
public List<java.net.NetworkInterface> getNetworkInterfaces() throws SocketException {
List<java.net.NetworkInterface> netInterfaces;
try {
netInterfaces = Collections.list(java.net.NetworkInterface.getNetworkInterfaces());
} catch (SocketException e) {
return null;
}
List<java.net.NetworkInterface> goodInterfaces = new ArrayList<>();
List<java.net.NetworkInterface> goodInterfaces = new ArrayList<>();
for (var netInterface : netInterfaces) {
if (netInterface.getDisplayName().contains("lo")) continue;
if (!netInterface.isUp()) continue;
goodInterfaces.add(netInterface);
}
return goodInterfaces;
for (var netInterface : netInterfaces) {
if (netInterface.getDisplayName().contains("lo")) continue;
if (!netInterface.isUp()) continue;
goodInterfaces.add(netInterface);
}
return goodInterfaces;
}
}
}