You are on page 1of 2

import java.util.

ArrayList;
import java.util.List;
class Packet {
private int sourcePort;
private int destinationPort;

public Packet(int sourcePort, int destinationPort) {


this.sourcePort = sourcePort;
this.destinationPort = destinationPort;
}

public int getSourcePort() {


return sourcePort;
}

public int getDestinationPort() {


return destinationPort;
}
}

class Rule {
private int sourcePort;
private int destinationPort;

public Rule(int sourcePort, int destinationPort) {


this.sourcePort = sourcePort;
this.destinationPort = destinationPort;
}

public boolean matches(Packet packet) {


return sourcePort == packet.getSourcePort() && destinationPort ==
packet.getDestinationPort();
}
}

class Firewall {
private List<Rule> rules;

public Firewall() {
rules = new ArrayList<>();
}

public void addRule(Rule rule) {


rules.add(rule);
}

public boolean allowPacket(Packet packet) {


for (Rule rule : rules) {
if (rule.matches(packet)) {
return true; // Allow packet if a matching rule is found
}
}
return false; // Deny packet if no matching rule is found
}
}

public class Main {


public static void main(String[] args) {
Firewall firewall = new Firewall();
// Add rules to the firewall
firewall.addRule(new Rule(80, 8080)); // Allow HTTP traffic
firewall.addRule(new Rule(443, 8443)); // Allow HTTPS traffic
firewall.addRule(new Rule(440, 8440));
// Simulate incoming packets
Packet packet1 = new Packet(80, 8080);
Packet packet2 = new Packet(443, 8443);
Packet packet3 = new Packet(440, 8440); // Blocked port

// Check if packets are allowed by the firewall


System.out.println("Packet 1 Allowed: " + firewall.allowPacket(packet1));
System.out.println("Packet 2 Allowed: " + firewall.allowPacket(packet2));
System.out.println("Packet 3 Allowed: " + firewall.allowPacket(packet3));
}
}

You might also like