Skip to content

Instantly share code, notes, and snippets.

@ostr00000
Last active May 27, 2024 22:17
Show Gist options
  • Save ostr00000/b6bc9b331f7f6b0a7a137e530b73e00f to your computer and use it in GitHub Desktop.
Save ostr00000/b6bc9b331f7f6b0a7a137e530b73e00f to your computer and use it in GitHub Desktop.
Python code to create bash script that block all Steam servers.
#!/usr/bin/env python
"""
Python code to create bash script that block all Steam servers.
You must have installed ufw:
$ sudo apt install ufw
Then you can run this script and generated bash script:
$ python3 generate_block_steam_script.py > steam_block.sh
$ chmod +x steam_block.sh && sudo bash steam_block.sh
This block only direct Valve addresses.
You also may want to block other ports used by steam application.
This can be easy done via GUFW program, by adding preconfigured rule for Steam.
"""
import ipaddress
import logging
import re
from urllib.request import urlopen
logger = logging.getLogger(__name__)
def genSteamIpAddress() -> ipaddress.IPv4Network | ipaddress.IPv6Network:
"""Get steam IP addresses.
Based on https://support.steampowered.com/kb_article.php?ref=8571-GLVN-8711
"""
body = urlopen('https://bgp.he.net/AS32590#_prefixes').read().decode('utf-8')
ipv4 = re.findall(r'<a href=.*?>([\d./]+)</a>', body)
yield from map(ipaddress.ip_network, ipv4)
ipv6 = re.findall(r'<a href=.*?>([\da-f:/]+)</a>', body)
yield from map(ipaddress.ip_network, ipv6)
def createUfwScript():
content = """
#!/bin/bash
ufw reset
ufw default allow incoming
ufw default allow outgoing
"""
for ip in genSteamIpAddress():
content += f'ufw deny from {ip} to any\n'
content += f'ufw deny out to {ip}\n'
print(content)
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
createUfwScript()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment