Last active
May 14, 2016 18:51
-
-
Save Higgs1/009c7e2bc282c071c82d07f7970d7767 to your computer and use it in GitHub Desktop.
Python Slither.IO Server List Reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import ipaddress | |
import itertools | |
import requests | |
import re | |
# Source: https://github.com/ClitherProject/Slither.io-Protocol/blob/master/ServerList.md | |
def Layer1Decoder(stream): # (Steps 2, 3, and 4) | |
shift = 0 | |
for c in stream: | |
yield (c - 7 * shift + 7) % 26 | |
shift += 1 | |
def Layer2Decoder(stream): # (Step 5) | |
for c in stream: | |
yield c * 16 + next(stream) | |
def parse_int24(bytes): | |
return (bytes[0] << 16) + (bytes[1] << 8) + bytes[2] | |
@functools.total_ordering | |
class ServerEntry: | |
def __init__(self, ip_addr, port, ca, clu): | |
self.ip_addr = ipaddress.ip_address(ip_addr) | |
self.port = port | |
self.ca = ca | |
self.clu = clu | |
@classmethod | |
def from_bytes(cls, bytes): # (Step 6) | |
if len(bytes) >= 11: | |
return cls('{}.{}.{}.{}'.format(*bytes[:4]), | |
parse_int24(bytes[4:7]), | |
parse_int24(bytes[7:10]), | |
bytes[10]) | |
def __repr__(self): | |
return '<{} {}:{}>'.format(type(self).__name__, self.ip_addr, self.port) | |
def __eq__(self, other): | |
if not isinstance(other, type(self)): | |
return NotImplemented | |
return self.ip_addr == other.ip_addr and self.port == other.port \ | |
and self.ca == other.ca and self.clu == other.clu | |
def __lt__(self, other): | |
return (self.ip_addr, self.port) < (other.ip_addr, other.port) | |
def ServerListReader(stream): | |
for c in stream: | |
yield ServerEntry.from_bytes([c] + list(next(stream) for _ in range(10))) | |
def ServerList(url = 'http://slither.io/i49526.txt', bytes = None): | |
if not bytes: | |
bytes = requests.get(url).content[1:] | |
yield from ServerListReader(Layer2Decoder(Layer1Decoder(bytes))) | |
#Example: servers = list(ServerList()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment