Skip to content

Instantly share code, notes, and snippets.

@KevinAlavik
Created August 1, 2024 09:17
Show Gist options
  • Save KevinAlavik/81601a6a6d7997a11b1dea614dbf42ed to your computer and use it in GitHub Desktop.
Save KevinAlavik/81601a6a6d7997a11b1dea614dbf42ed to your computer and use it in GitHub Desktop.
A python program to block chatgpt on linux (this uses the iptables command to block and unblock GPT) this uses tkinter for UI
import tkinter as tk
import subprocess
import logging
logging.basicConfig(filename='/home/kevin/Kod/BlockGPT/block_gpt.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
class BlockerApp:
def __init__(self, root):
self.root = root
self.root.title("ChatGPT Blocker")
self.root.geometry("300x150")
self.status_var = tk.StringVar(value="Checking...")
self.status_label = tk.Label(root, textvariable=self.status_var, font=("Arial", 16))
self.status_label.pack(pady=20)
self.toggle_button = tk.Button(root, text="Toggle Blocking", command=self.toggle_blocking)
self.toggle_button.pack(pady=10)
self.update_status()
def toggle_blocking(self):
if self.status_var.get() == "Blocked":
self.stop_blocking()
else:
self.start_blocking()
def start_blocking(self):
try:
self._block_ip("104.18.30.2")
self._block_ip("104.18.31.2")
self._block_ip("2606:4700::6812:1e02", ipv6=True)
self._block_ip("2606:4700::6812:1f02", ipv6=True)
self.status_var.set("Blocked")
self.toggle_button.config(text="Disable Blocking")
logging.info("Blocking started successfully.")
except Exception as e:
logging.error(f"Failed to start blocking: {e}")
self.status_var.set("Error")
def stop_blocking(self):
try:
self._unblock_ip("104.18.30.2")
self._unblock_ip("104.18.31.2")
self._unblock_ip("2606:4700::6812:1e02", ipv6=True)
self._unblock_ip("2606:4700::6812:1f02", ipv6=True)
self.status_var.set("Unblocked")
self.toggle_button.config(text="Enable Blocking")
logging.info("Blocking stopped successfully.")
except Exception as e:
logging.error(f"Failed to stop blocking: {e}")
self.status_var.set("Error")
def _block_ip(self, ip, ipv6=False):
try:
cmd_tcp = ["sudo", "iptables", "-A", "OUTPUT", "-p", "tcp", "-d", ip, "-j", "REJECT"] if not ipv6 else ["sudo", "ip6tables", "-A", "OUTPUT", "-p", "tcp", "-d", ip, "-j", "REJECT"]
cmd_udp = ["sudo", "iptables", "-A", "OUTPUT", "-p", "udp", "-d", ip, "-j", "REJECT"] if not ipv6 else ["sudo", "ip6tables", "-A", "OUTPUT", "-p", "udp", "-d", ip, "-j", "REJECT"]
subprocess.run(cmd_tcp, check=True)
subprocess.run(cmd_udp, check=True)
logging.info(f"Blocked IP: {ip}")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to block IP {ip}: {e}")
raise
def _unblock_ip(self, ip, ipv6=False):
try:
cmd_tcp = ["sudo", "iptables", "-D", "OUTPUT", "-p", "tcp", "-d", ip, "-j", "REJECT"] if not ipv6 else ["sudo", "ip6tables", "-D", "OUTPUT", "-p", "tcp", "-d", ip, "-j", "REJECT"]
cmd_udp = ["sudo", "iptables", "-D", "OUTPUT", "-p", "udp", "-d", ip, "-j", "REJECT"] if not ipv6 else ["sudo", "ip6tables", "-D", "OUTPUT", "-p", "udp", "-d", ip, "-j", "REJECT"]
if self._check_ip_blocked(ip, ipv6):
subprocess.run(cmd_tcp, check=True)
subprocess.run(cmd_udp, check=True)
logging.info(f"Unblocked IP: {ip}")
else:
logging.info(f"IP {ip} is not blocked, skipping removal.")
except subprocess.CalledProcessError as e:
logging.error(f"Failed to unblock IP {ip}: {e}")
raise
def _check_ip_blocked(self, ip, ipv6=False):
try:
cmd = ["sudo", "iptables", "-L", "OUTPUT", "-v", "-n"] if not ipv6 else ["sudo", "ip6tables", "-L", "OUTPUT", "-v", "-n"]
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
blocked = ip in result.stdout
logging.info(f"Checked IP {ip}: {'Blocked' if blocked else 'Not Blocked'}")
return blocked
except subprocess.CalledProcessError as e:
logging.error(f"Failed to check if IP {ip} is blocked: {e}")
raise
def update_status(self):
try:
blocked_ipv4 = self._check_ip_blocked("104.18.30.2") or self._check_ip_blocked("104.18.31.2")
blocked_ipv6 = self._check_ip_blocked("2606:4700::6812:1e02", ipv6=True) or self._check_ip_blocked("2606:4700::6812:1f02", ipv6=True)
if blocked_ipv4 or blocked_ipv6:
self.status_var.set("Blocked")
self.toggle_button.config(text="Disable Blocking")
else:
self.status_var.set("Unblocked")
self.toggle_button.config(text="Enable Blocking")
except Exception as e:
logging.error(f"Failed to update status: {e}")
self.status_var.set("Error")
if __name__ == "__main__":
root = tk.Tk()
app = BlockerApp(root)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment