From 985ae8f90dae572ed70f3d26ac242c17f6ea642b Mon Sep 17 00:00:00 2001
From: anima
Date: Sat, 21 Sep 2024 21:44:19 +0200
Subject: [PATCH] add claim server
---
SatiAPI.py | 71 ++++++++++++++++++++++++++++++++++++------------------
1 file changed, 48 insertions(+), 23 deletions(-)
diff --git a/SatiAPI.py b/SatiAPI.py
index 7881709..dc3c296 100644
--- a/SatiAPI.py
+++ b/SatiAPI.py
@@ -16,7 +16,7 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class SatiAPI:
"""A API wrapper for Satisfactory dedicated server"""
__AUTHOR__ = 'anima'
- __VERSION__ = '0.5.2'
+ __VERSION__ = '0.7.3'
def __init__(self, host: str = None, port: int = 7777, token: str = None, conffile: str = 'conf.yml', logfile: str = 'SatiAPI.log', loglevel: int = 20) -> None:
"""create a wrapper for satisfactory dedicated server
@@ -139,8 +139,9 @@ class SatiAPI:
self.port = self._config['port']
if 'auth' in self._config:
- if 'token' in self._config['auth'] and self.token is None:
- self.token = self._config['auth']['token']
+ if 'Administrator' in self._config['auth']:
+ if 'token' in self._config['auth']['Administrator'] and self.token is None:
+ self.token = self._config['auth']['Administrator']['token']
def __query(self, query: str, data: dict = None, auth: bool = False) -> dict | bool | None:
"""run a query to satisfacoty dedicated server
@@ -166,7 +167,7 @@ class SatiAPI:
if self.token is None:
self.get_token()
- if self.token is None:
+ if self.token is not None:
headers['Authorization'] = f'Bearer {self.token}'
else:
self._log.error('no data to authencitate')
@@ -208,6 +209,40 @@ class SatiAPI:
else:
return response
+ def claim_server(self, servername: str, password: str = None) -> bool:
+ """Claims this Dedicated Server if it is not claimed. Requires InitialAdmin privilege level, which can only be acquired by attempting passwordless login while the server does not have an Admin Password set, e.g. it is not claimed yet. Function does not return any data in case of success, and the server is claimed. The client should drop InitialAdmin privileges after that and use returned AuthenticationToken instead, and update it's cached server game state by calling QueryServerState.
+
+ :param servername: New name of the Dedicated Server
+ :type servername: string
+ :param password: Admin Password to set on the Dedicated Server, in plaintext. If empty the password will cread from conf file
+ :type password: string
+ :return: true if claim successfull
+ :rtype: bool
+
+ after claim it get a new token automaticly
+ """
+ if self._passwordless_login():
+ data = dict()
+ if password is None:
+ if 'auth' in self._config:
+ if 'Administrator' in self._config['auth']:
+ if 'token' in self._config['auth']['Administrator'] and self.token is None:
+ password = self._config['auth']['Administrator']['password']
+ else:
+ self._log.error('no password for server claim set')
+ return False
+ data['ServerName'] = servername
+ data['AdminPassword'] = password
+
+ response = self.__query('ClaimServer', data, True)
+ if response:
+ self.token = response['data']['authenticationToken']
+ return True
+ else:
+ return False
+ else:
+ self._log.error('server can not be clamed because no authentication possable')
+
def get_token(self, password: str = None, privilegeLevel: str = 'Administrator') -> bool:
"""get token from satisfacory dedicated server, password is needed!
Attempts to log in to the Dedicated Server as a player using either Admin Password or Client Protection Password. This function requires no Authentication.
@@ -225,15 +260,12 @@ class SatiAPI:
self._log.debug('password used from parameter')
else:
if self._config is not None and 'auth' in self._config:
- if 'password' in self._config['auth']:
- password = self._config['auth']['password']
- self._log.debug('password used from conf file')
- else:
- self._log('missing information in config file under auth')
-
- if 'privilegeLevel' in self._config['auth']:
- privilegeLevel = self._config['auth']['privilegeLevel']
- self._log.debug('privilege level used from conff ile')
+ if privilegeLevel in self._config['auth']:
+ if 'password' in self._config['auth'][privilegeLevel]:
+ password = self._config['auth'][privilegeLevel]['password']
+ self._log.debug('password used from conf file')
+ else:
+ self._log('missing information in config file under auth')
else:
self._log.error(f'no password for auth given!')
@@ -256,7 +288,7 @@ class SatiAPI:
:return: full return from server
:rtpye: dict
"""
- return self.__query('QueryServerState')
+ return self.__query('QueryServerState', auth=True)
def get_health(self, ClientCustomData: str = '') -> dict:
"""get health check
@@ -290,8 +322,6 @@ class SatiAPI:
## todo
- #PasswordlessLogin
- #ClaimServer
#RenameServer
#SetClientPassword
#SetAdminPassword
@@ -312,10 +342,5 @@ class SatiAPI:
if __name__ == "__main__":
sati = SatiAPI(loglevel=10)
# print('Verbundene Spieler: ', sati.get_status()['serverGameState']['numConnectedPlayers'])
- # print(sati.get_token())
- # print(sati.token)
- # print(sati.get_status())
- print(sati._passwordless_login())
- # print(sati.claim_server('SDGame01', 'Admin123!'))
-
- # print(sati.get_status())
\ No newline at end of file
+ print(sati.claim_server('SDGame01'))
+ print(sati.get_status())
\ No newline at end of file