add claim server
This commit is contained in:
65
SatiAPI.py
65
SatiAPI.py
@@ -16,7 +16,7 @@ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
|
|||||||
class SatiAPI:
|
class SatiAPI:
|
||||||
"""A API wrapper for Satisfactory dedicated server"""
|
"""A API wrapper for Satisfactory dedicated server"""
|
||||||
__AUTHOR__ = 'anima'
|
__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:
|
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
|
"""create a wrapper for satisfactory dedicated server
|
||||||
@@ -139,8 +139,9 @@ class SatiAPI:
|
|||||||
self.port = self._config['port']
|
self.port = self._config['port']
|
||||||
|
|
||||||
if 'auth' in self._config:
|
if 'auth' in self._config:
|
||||||
if 'token' in self._config['auth'] and self.token is None:
|
if 'Administrator' in self._config['auth']:
|
||||||
self.token = self._config['auth']['token']
|
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:
|
def __query(self, query: str, data: dict = None, auth: bool = False) -> dict | bool | None:
|
||||||
"""run a query to satisfacoty dedicated server
|
"""run a query to satisfacoty dedicated server
|
||||||
@@ -166,7 +167,7 @@ class SatiAPI:
|
|||||||
if self.token is None:
|
if self.token is None:
|
||||||
self.get_token()
|
self.get_token()
|
||||||
|
|
||||||
if self.token is None:
|
if self.token is not None:
|
||||||
headers['Authorization'] = f'Bearer {self.token}'
|
headers['Authorization'] = f'Bearer {self.token}'
|
||||||
else:
|
else:
|
||||||
self._log.error('no data to authencitate')
|
self._log.error('no data to authencitate')
|
||||||
@@ -208,6 +209,40 @@ class SatiAPI:
|
|||||||
else:
|
else:
|
||||||
return response
|
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:
|
def get_token(self, password: str = None, privilegeLevel: str = 'Administrator') -> bool:
|
||||||
"""get token from satisfacory dedicated server, password is needed!
|
"""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.
|
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,16 +260,13 @@ class SatiAPI:
|
|||||||
self._log.debug('password used from parameter')
|
self._log.debug('password used from parameter')
|
||||||
else:
|
else:
|
||||||
if self._config is not None and 'auth' in self._config:
|
if self._config is not None and 'auth' in self._config:
|
||||||
if 'password' in self._config['auth']:
|
if privilegeLevel in self._config['auth']:
|
||||||
password = self._config['auth']['password']
|
if 'password' in self._config['auth'][privilegeLevel]:
|
||||||
|
password = self._config['auth'][privilegeLevel]['password']
|
||||||
self._log.debug('password used from conf file')
|
self._log.debug('password used from conf file')
|
||||||
else:
|
else:
|
||||||
self._log('missing information in config file under auth')
|
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')
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self._log.error(f'no password for auth given!')
|
self._log.error(f'no password for auth given!')
|
||||||
return False
|
return False
|
||||||
@@ -256,7 +288,7 @@ class SatiAPI:
|
|||||||
:return: full return from server
|
:return: full return from server
|
||||||
:rtpye: dict
|
:rtpye: dict
|
||||||
"""
|
"""
|
||||||
return self.__query('QueryServerState')
|
return self.__query('QueryServerState', auth=True)
|
||||||
|
|
||||||
def get_health(self, ClientCustomData: str = '') -> dict:
|
def get_health(self, ClientCustomData: str = '') -> dict:
|
||||||
"""get health check
|
"""get health check
|
||||||
@@ -290,8 +322,6 @@ class SatiAPI:
|
|||||||
|
|
||||||
|
|
||||||
## todo
|
## todo
|
||||||
#PasswordlessLogin
|
|
||||||
#ClaimServer
|
|
||||||
#RenameServer
|
#RenameServer
|
||||||
#SetClientPassword
|
#SetClientPassword
|
||||||
#SetAdminPassword
|
#SetAdminPassword
|
||||||
@@ -312,10 +342,5 @@ class SatiAPI:
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sati = SatiAPI(loglevel=10)
|
sati = SatiAPI(loglevel=10)
|
||||||
# print('Verbundene Spieler: ', sati.get_status()['serverGameState']['numConnectedPlayers'])
|
# print('Verbundene Spieler: ', sati.get_status()['serverGameState']['numConnectedPlayers'])
|
||||||
# print(sati.get_token())
|
print(sati.claim_server('SDGame01'))
|
||||||
# print(sati.token)
|
print(sati.get_status())
|
||||||
# print(sati.get_status())
|
|
||||||
print(sati._passwordless_login())
|
|
||||||
# print(sati.claim_server('SDGame01', 'Admin123!'))
|
|
||||||
|
|
||||||
# print(sati.get_status())
|
|
||||||
Reference in New Issue
Block a user