Last active
September 29, 2024 15:42
-
-
Save Grub4K/a9898e0641288b57209c8d6d8fdc3c9d to your computer and use it in GitHub Desktop.
Satisfactory dedicated server shutdown through api
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
# /// script | |
# requires-python = ">=3.12" | |
# dependencies = [ | |
# "requests", | |
# ] | |
# /// | |
import getpass | |
import sys | |
import warnings | |
import requests | |
def get_parser(): | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
"host", | |
help="the host to connect to", | |
) | |
parser.add_argument( | |
"port", | |
type=int, | |
nargs="?", | |
default=7777, | |
help="the port to connect to", | |
) | |
parser.add_argument( | |
"--insecure", | |
"-k", | |
action="store_true", | |
help="use insecure connection to the server", | |
) | |
return parser | |
args = get_parser().parse_args() | |
if args.insecure: | |
warnings.filterwarnings("ignore", message="Unverified HTTPS") | |
auth_token = None | |
session = requests.Session() | |
def call_api(function: str, data: dict | None = None): | |
payload = { | |
"function": function, | |
"data": data or {}, | |
} | |
headers = {} | |
if auth_token: | |
headers["Authorization"] = f"Bearer {auth_token}" | |
with session.post( | |
f"https://{args.host}:{args.port}/api/v1", | |
json=payload, | |
headers=headers, | |
verify=not args.insecure, | |
) as response: | |
response.raise_for_status() | |
result = response.json() | |
if "data" not in result: | |
message = result.get("errorMessage") or "Unknown error while querying API" | |
if "errorCode" in result: | |
message = f"{message} ({result['errorCode']})" | |
raise ValueError(message) | |
return result["data"] | |
try: | |
password = getpass.getpass() | |
result = call_api( | |
"passwordLogin", | |
{ | |
"MinimumPrivilegeLevel": "Administrator", | |
"Password": password, | |
}, | |
) | |
if "authenticationToken" not in result: | |
raise ValueError("Authentication token not available") | |
auth_token = result["authenticationToken"] | |
try: | |
call_api("shutdown") | |
except ValueError: | |
pass | |
except Exception as error: | |
print(f"ERROR: {error}", file=sys.stderr) | |
sys.exit(1) | |
except KeyboardInterrupt: | |
print() | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment