Added nftables processing.
This commit is contained in:
@@ -4,12 +4,19 @@
|
||||
*/
|
||||
package controller.src.DataProcessing;
|
||||
|
||||
import controller.src.DataProcessing.ConnectionsProcessing.ConnectionInformation;
|
||||
import controller.src.DataProcessing.ConnectionsProcessing.ConnectionsProcessor;
|
||||
import controller.src.DataProcessing.nftablesProcessing.NFTableInformation;
|
||||
import controller.src.DataProcessing.nftablesProcessing.NFTablesProcessor;
|
||||
import model.src.Model;
|
||||
import model.src.ModelConstants.AccessModifier;
|
||||
import queues.src.ReceivingQueue;
|
||||
|
||||
public final class DataProcessorThread extends Thread {
|
||||
private static DataProcessorThread instance = null;
|
||||
|
||||
private Model model = null;
|
||||
|
||||
private DataProcessorThread() {}
|
||||
|
||||
private enum DataKind {
|
||||
@@ -50,28 +57,38 @@ public final class DataProcessorThread extends Thread {
|
||||
return this.dataProcessorRunning;
|
||||
}
|
||||
|
||||
public void setModel(Model model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
private DataKind getKindOfData(String headOfData) {
|
||||
System.out.println("Head of data:" + headOfData);
|
||||
if (headOfData.startsWith("sconn")) {
|
||||
return DataKind.Connection;
|
||||
}
|
||||
if (headOfData.startsWith("{\"nft")) {
|
||||
if (headOfData.startsWith("{\"nftables")) {
|
||||
return DataKind.NFTablesConfiguration;
|
||||
}
|
||||
return DataKind.Undefined;
|
||||
}
|
||||
|
||||
private void processDependingOnDataKind(DataKind dataKind, String data) {
|
||||
switch (dataKind) {
|
||||
case Connection: {
|
||||
System.out.println(ConnectionsProcessor.processDataString(data));
|
||||
}break;
|
||||
case NFTablesConfiguration: {
|
||||
|
||||
}break;
|
||||
case Undefined: // intended fall through
|
||||
default: {
|
||||
System.out.println("Unknown data kind.");
|
||||
}break;
|
||||
if (model != null) {
|
||||
switch (dataKind) {
|
||||
case Connection: {
|
||||
ConnectionInformation conInf = ConnectionsProcessor.processDataString(data);
|
||||
model.getNetworkModel().accessConnections(AccessModifier.WriteEntry, conInf);
|
||||
}break;
|
||||
case NFTablesConfiguration: {
|
||||
NFTablesProcessor.processDataString(data);
|
||||
//NFTableInformation nftConfig =
|
||||
//System.out.println(data);
|
||||
}break;
|
||||
case Undefined: // intended fall through
|
||||
default: {
|
||||
System.out.println("Unknown data kind.");
|
||||
}break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* @author Aaron Moser
|
||||
* @date 29.12.2023
|
||||
*/
|
||||
package controller.src.DataProcessing.nftablesProcessing;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class NFTableInformation {
|
||||
private List<String> tables = new LinkedList<String>();
|
||||
private List<String> chains = new LinkedList<String>();
|
||||
private List<String> rules = new LinkedList<String>();
|
||||
|
||||
public List<String> getTables() {
|
||||
return tables;
|
||||
}
|
||||
|
||||
public List<String> getChains() {
|
||||
return chains;
|
||||
}
|
||||
|
||||
public List<String> getRules() {
|
||||
return rules;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* @author Aaron Moser
|
||||
* @date 29.12.2023
|
||||
*/
|
||||
package controller.src.DataProcessing.nftablesProcessing;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeType;
|
||||
import com.fasterxml.jackson.databind.node.NullNode;
|
||||
|
||||
public final class NFTablesProcessor {
|
||||
private static final String NFT_MAIN_FIELD = "nftables";
|
||||
private static final String TABLE_FIELD = "table";
|
||||
private static final String CHAIN_FIELD = "chain";
|
||||
private static final String RULE_FIELD = "rule";
|
||||
private static final String NAME_FIELD = "name";
|
||||
|
||||
public static NFTableInformation processDataString(String data) {
|
||||
NFTableInformation nfTableInformation = new NFTableInformation();
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
try {
|
||||
JsonNode nfttree = objectMapper.readTree(data);
|
||||
JsonNode mainRoot = getField(nfttree, NFT_MAIN_FIELD);
|
||||
|
||||
for (JsonNode nftObject : mainRoot) {
|
||||
if (isTableEntry(nftObject)) {
|
||||
nfTableInformation.getTables().add(nftObject.toString());
|
||||
|
||||
} else if (isChainEntry(nftObject)) {
|
||||
nfTableInformation.getChains().add(nftObject.toString());
|
||||
|
||||
} else if (isRuleEntry(nftObject)) {
|
||||
nfTableInformation.getChains().add(nftObject.toString());
|
||||
|
||||
} else {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
} catch (JsonMappingException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return nfTableInformation;
|
||||
}
|
||||
|
||||
private static boolean isTableEntry(JsonNode node) {
|
||||
return isSpecifiedEntry(node, TABLE_FIELD);
|
||||
}
|
||||
|
||||
private static boolean isChainEntry(JsonNode node) {
|
||||
return isSpecifiedEntry(node, CHAIN_FIELD);
|
||||
}
|
||||
|
||||
private static boolean isRuleEntry(JsonNode node) {
|
||||
return isSpecifiedEntry(node, RULE_FIELD);
|
||||
}
|
||||
|
||||
private static boolean isSpecifiedEntry(JsonNode node, String type) {
|
||||
if (node.getNodeType() != JsonNodeType.NULL) {
|
||||
JsonNode maybeNode = node.get(type);
|
||||
if (maybeNode != null && maybeNode.toString() != "null") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static JsonNode getField(JsonNode node, String fieldName) {
|
||||
if (node.getNodeType() != JsonNodeType.NULL) {
|
||||
if (node.get(fieldName) != null) {
|
||||
return node.get(fieldName);
|
||||
}
|
||||
}
|
||||
return NullNode.getInstance();
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,7 @@
|
||||
package model.src;
|
||||
|
||||
public interface Model {
|
||||
|
||||
public NetworkModel getNetworkModel();
|
||||
public NFTablesModel getNFTablesModel();
|
||||
public ConnectionModel getConnectionModel();
|
||||
}
|
||||
|
||||
@@ -4,6 +4,18 @@
|
||||
*/
|
||||
package model.src;
|
||||
|
||||
import controller.src.DataProcessing.nftablesProcessing.NFTableInformation;
|
||||
|
||||
public class NFTablesModel {
|
||||
|
||||
private NFTableInformation nfTableInformation = null;
|
||||
|
||||
public NFTablesModel() {}
|
||||
|
||||
public void setNFTableInformation(NFTableInformation newInformation) {
|
||||
this.nfTableInformation = newInformation;
|
||||
}
|
||||
|
||||
public NFTableInformation getNFTableInformation() {
|
||||
return this.nfTableInformation;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user