steamlogin.py
· 3.1 KiB · Python
Raw
#!/usr/bin/env python3
import sys
try:
import requests
from bs4 import BeautifulSoup as Soup
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
except ImportError:
print("Please run \"pip3 install requests beautifulsoup4 pycrypto\"", file=sys.stderr)
sys.exit(1)
import time
import json
import base64
import os
H = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"}
session = requests.Session()
session.headers.update(H)
yaml = """{3}:
id: "{0}"
token: "{1}"
date: "{2}" """.strip()
def get_key(username):
return session.post("https://steamcommunity.com/login/getrsakey/",
data=dict(username=username, donotcache=time.time()*1000) ).json()
def encode_passwd(key_data, passwd):
mod = int(key_data["publickey_mod"], 16)
exp = int(key_data["publickey_exp"], 16)
rsa = RSA.construct((mod, exp))
cipher = PKCS1_v1_5.new(rsa)
if hasattr(passwd, "encode"):
passwd = passwd.encode("utf-8")
return base64.b64encode(cipher.encrypt(passwd))
def login(username, password):
key = get_key(username)
encrypted_password = encode_passwd(key, password)
return session.post("https://steamcommunity.com/login/dologin/",
data=dict(
username=username,
password=encrypted_password,
emailauth="",
loginfriendlyname="",
captchagid="-1",
captcha_text="",
emailsteamid="",
rsatimestamp=key["timestamp"],
remember_login=True,
donotcache=time.time()*1000
) ).json()
def transfer(url, login_data):
session.post(url,
data=login_data["transfer_parameters"]
)
def get_gameservers():
"""Yields lists containing four items: steam game id, token, date last used and memo"""
page = Soup(session.get("https://steamcommunity.com/dev/managegameservers").text, "html.parser")
response = []
for row in page.findAll("tr")[1:]:
response.append([ i.text.replace("\\", "\\\\").replace('"', '\\"') for i in row.findAll("td")[:4] ])
return response
if __name__ == "__main__":
username = os.environ.get("STEAM_USERNAME")
password = os.environ.get("STEAM_PASSWORD")
if username and password:
if os.path.isfile("/tmp/steam-{}.json".format(username)):
with open("/tmp/steam-{}.json".format(username)) as f:
session.cookies = requests.utils.cookiejar_from_dict(json.loads(f.read()))
else:
login_data = login(username, password)
for url in login_data["transfer_urls"]:
transfer(url, login_data)
for gs in get_gameservers():
if "(expired)" in gs[1]:
gs[1] = gs[1][:32]
print(yaml.format(*gs) + "\n expired: true")
else:
print(yaml.format(*gs))
with open("/tmp/steam-{}.json".format(username), "w") as f:
f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies)))
else:
print("Please specify environment variables STEAM_USERNAME and STEAM_PASSWORD", file=sys.stderr)
| 1 | #!/usr/bin/env python3 |
| 2 | import sys |
| 3 | |
| 4 | try: |
| 5 | import requests |
| 6 | from bs4 import BeautifulSoup as Soup |
| 7 | from Crypto.PublicKey import RSA |
| 8 | from Crypto.Cipher import PKCS1_v1_5 |
| 9 | except ImportError: |
| 10 | print("Please run \"pip3 install requests beautifulsoup4 pycrypto\"", file=sys.stderr) |
| 11 | sys.exit(1) |
| 12 | |
| 13 | import time |
| 14 | import json |
| 15 | import base64 |
| 16 | import os |
| 17 | |
| 18 | H = {"User-Agent": "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)"} |
| 19 | session = requests.Session() |
| 20 | session.headers.update(H) |
| 21 | yaml = """{3}: |
| 22 | id: "{0}" |
| 23 | token: "{1}" |
| 24 | date: "{2}" """.strip() |
| 25 | |
| 26 | def get_key(username): |
| 27 | return session.post("https://steamcommunity.com/login/getrsakey/", |
| 28 | data=dict(username=username, donotcache=time.time()*1000) ).json() |
| 29 | |
| 30 | def encode_passwd(key_data, passwd): |
| 31 | mod = int(key_data["publickey_mod"], 16) |
| 32 | exp = int(key_data["publickey_exp"], 16) |
| 33 | rsa = RSA.construct((mod, exp)) |
| 34 | cipher = PKCS1_v1_5.new(rsa) |
| 35 | if hasattr(passwd, "encode"): |
| 36 | passwd = passwd.encode("utf-8") |
| 37 | return base64.b64encode(cipher.encrypt(passwd)) |
| 38 | |
| 39 | def login(username, password): |
| 40 | key = get_key(username) |
| 41 | encrypted_password = encode_passwd(key, password) |
| 42 | return session.post("https://steamcommunity.com/login/dologin/", |
| 43 | data=dict( |
| 44 | username=username, |
| 45 | password=encrypted_password, |
| 46 | emailauth="", |
| 47 | loginfriendlyname="", |
| 48 | captchagid="-1", |
| 49 | captcha_text="", |
| 50 | emailsteamid="", |
| 51 | rsatimestamp=key["timestamp"], |
| 52 | remember_login=True, |
| 53 | donotcache=time.time()*1000 |
| 54 | ) ).json() |
| 55 | |
| 56 | def transfer(url, login_data): |
| 57 | session.post(url, |
| 58 | data=login_data["transfer_parameters"] |
| 59 | ) |
| 60 | |
| 61 | def get_gameservers(): |
| 62 | """Yields lists containing four items: steam game id, token, date last used and memo""" |
| 63 | page = Soup(session.get("https://steamcommunity.com/dev/managegameservers").text, "html.parser") |
| 64 | response = [] |
| 65 | for row in page.findAll("tr")[1:]: |
| 66 | response.append([ i.text.replace("\\", "\\\\").replace('"', '\\"') for i in row.findAll("td")[:4] ]) |
| 67 | return response |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | username = os.environ.get("STEAM_USERNAME") |
| 71 | password = os.environ.get("STEAM_PASSWORD") |
| 72 | if username and password: |
| 73 | if os.path.isfile("/tmp/steam-{}.json".format(username)): |
| 74 | with open("/tmp/steam-{}.json".format(username)) as f: |
| 75 | session.cookies = requests.utils.cookiejar_from_dict(json.loads(f.read())) |
| 76 | else: |
| 77 | login_data = login(username, password) |
| 78 | for url in login_data["transfer_urls"]: |
| 79 | transfer(url, login_data) |
| 80 | for gs in get_gameservers(): |
| 81 | if "(expired)" in gs[1]: |
| 82 | gs[1] = gs[1][:32] |
| 83 | print(yaml.format(*gs) + "\n expired: true") |
| 84 | else: |
| 85 | print(yaml.format(*gs)) |
| 86 | with open("/tmp/steam-{}.json".format(username), "w") as f: |
| 87 | f.write(json.dumps(requests.utils.dict_from_cookiejar(session.cookies))) |
| 88 | else: |
| 89 | print("Please specify environment variables STEAM_USERNAME and STEAM_PASSWORD", file=sys.stderr) |