Skip to content

Instantly share code, notes, and snippets.

@KevinAlavik
Created August 1, 2024 08:28
Show Gist options
  • Save KevinAlavik/c97bb50214beea6ef6653fa1ccfb1a09 to your computer and use it in GitHub Desktop.
Save KevinAlavik/c97bb50214beea6ef6653fa1ccfb1a09 to your computer and use it in GitHub Desktop.
Blocks any traffics to chatgpt (only for linux) using the iptables command. This blocks traffic when running and unblocks when stoped
import os
import sys
import time
BLOCKED_DOMAIN = "chatgpt.com"
def block_traffic():
try:
os.system(f"sudo iptables -A OUTPUT -p tcp -d {BLOCKED_DOMAIN} -j REJECT")
os.system(f"sudo iptables -A OUTPUT -p udp -d {BLOCKED_DOMAIN} -j REJECT")
print(f"Blocked all traffic to {BLOCKED_DOMAIN}.")
except Exception as e:
print(f"Failed to block traffic: {e}")
def unblock_traffic():
try:
os.system(f"sudo iptables -D OUTPUT -p tcp -d {BLOCKED_DOMAIN} -j REJECT")
os.system(f"sudo iptables -D OUTPUT -p udp -d {BLOCKED_DOMAIN} -j REJECT")
print(f"Unblocked all traffic to {BLOCKED_DOMAIN}.")
except Exception as e:
print(f"Failed to unblock traffic: {e}")
def main():
try:
block_traffic()
print("Press Ctrl+C to unblock and exit.")
while True:
time.sleep(1)
except KeyboardInterrupt:
unblock_traffic()
print("Exiting script.")
sys.exit(0)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment