File size: 1,357 Bytes
18bba18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr

# -----------------------------
# Firewall Rules
# -----------------------------
BLOCKED_IPS = ["192.168.1.10", "10.0.0.5"]
ALLOWED_PORTS = [80, 443, 8080]

# Store logs
logs = []

# -----------------------------
# Firewall Function
# -----------------------------
def firewall(ip, port):
    try:
        port = int(port)
    except:
        return "⚠️ Invalid port number"

    if not ip:
        return "⚠️ Please enter an IP address"

    # Rule checking
    if ip in BLOCKED_IPS:
        result = "❌ Blocked (IP is blacklisted)"
    elif port not in ALLOWED_PORTS:
        result = "❌ Blocked (Port not allowed)"
    else:
        result = "✅ Allowed"

    # Save logs
    logs.append(f"{ip}:{port}{result}")

    # Show last 5 logs
    recent_logs = "\n".join(logs[-5:])

    return f"{result}\n\n📜 Recent Logs:\n{recent_logs}"


# -----------------------------
# Gradio Interface
# -----------------------------
iface = gr.Interface(
    fn=firewall,
    inputs=[
        gr.Textbox(label="Enter IP Address", placeholder="e.g. 192.168.1.1"),
        gr.Textbox(label="Enter Port", placeholder="e.g. 80")
    ],
    outputs="text",
    title="🔥 Simple Firewall Simulator",
    description="Simulates firewall filtering using IP and Port rules.\nEnter values to check access.",
)

# Run app
iface.launch()