|
| 1 | +import aiohttp |
| 2 | +import base64 |
| 3 | +import json |
| 4 | + |
| 5 | +from opengsq.exceptions import ServerNotFoundException |
| 6 | +from opengsq.protocol_base import ProtocolBase |
| 7 | + |
| 8 | + |
| 9 | +class EOS(ProtocolBase): |
| 10 | + """Epic Online Services (EOS) Protocol""" |
| 11 | + full_name = 'Epic Online Services (EOS) Protocol' |
| 12 | + |
| 13 | + _api_url = 'https://api.epicgames.dev' |
| 14 | + |
| 15 | + def __init__(self, host: str, port: int, timeout: float = 5, client_id: str = None, client_secret: str = None, deployment_id: str = None): |
| 16 | + super().__init__(host, port, timeout) |
| 17 | + |
| 18 | + if client_id is None or client_secret is None or deployment_id is None: |
| 19 | + raise ValueError( |
| 20 | + "client_id, client_secret, and deployment_id must not be None") |
| 21 | + |
| 22 | + self.client_id = client_id |
| 23 | + self.client_secret = client_secret |
| 24 | + self.deployment_id = deployment_id |
| 25 | + self.access_token = None |
| 26 | + |
| 27 | + async def _get_access_token(self) -> str: |
| 28 | + url = f'{self._api_url}/auth/v1/oauth/token' |
| 29 | + body = f"grant_type=client_credentials&deployment_id={self.deployment_id}" |
| 30 | + headers = { |
| 31 | + "Authorization": f"Basic {base64.b64encode(f'{self.client_id}:{self.client_secret}'.encode('utf-8')).decode('utf-8')}", |
| 32 | + "Content-Type": "application/x-www-form-urlencoded" |
| 33 | + } |
| 34 | + |
| 35 | + async with aiohttp.ClientSession() as session: |
| 36 | + async with session.post(url, data=body, headers=headers) as response: |
| 37 | + response.raise_for_status() |
| 38 | + data = await response.json() |
| 39 | + |
| 40 | + return data["access_token"] |
| 41 | + |
| 42 | + async def _get_matchmaking(self, data: dict): |
| 43 | + if self.access_token is None: |
| 44 | + self.access_token = await self._get_access_token() |
| 45 | + assert self.access_token is not None, "Failed to get access token" |
| 46 | + |
| 47 | + url = f"{self._api_url}/matchmaking/v1/{self.deployment_id}/filter" |
| 48 | + headers = { |
| 49 | + "Content-Type": "application/json", |
| 50 | + "Accept": "application/json", |
| 51 | + "Authorization": f"Bearer {self.access_token}" |
| 52 | + } |
| 53 | + |
| 54 | + async with aiohttp.ClientSession() as session: |
| 55 | + async with session.post(url, data=json.dumps(data), headers=headers) as response: |
| 56 | + response.raise_for_status() |
| 57 | + data = await response.json() |
| 58 | + |
| 59 | + return data |
| 60 | + |
| 61 | + async def get_info(self) -> dict: |
| 62 | + data = await self._get_matchmaking({ |
| 63 | + "criteria": [ |
| 64 | + { |
| 65 | + "key": "attributes.ADDRESS_s", |
| 66 | + "op": "EQUAL", |
| 67 | + "value": self._host |
| 68 | + }, |
| 69 | + { |
| 70 | + "key": "attributes.ADDRESSBOUND_s", |
| 71 | + "op": "CONTAINS", |
| 72 | + "value": self._port |
| 73 | + }, |
| 74 | + ] |
| 75 | + }) |
| 76 | + |
| 77 | + if data["count"] <= 0: |
| 78 | + raise ServerNotFoundException() |
| 79 | + |
| 80 | + return data['sessions'][0] |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + import asyncio |
| 85 | + |
| 86 | + async def main_async(): |
| 87 | + client_id = 'xyza7891muomRmynIIHaJB9COBKkwj6n' |
| 88 | + client_secret = 'PP5UGxysEieNfSrEicaD1N2Bb3TdXuD7xHYcsdUHZ7s' |
| 89 | + deployment_id = 'ad9a8feffb3b4b2ca315546f038c3ae2' |
| 90 | + |
| 91 | + eos = EOS(host='150.138.77.118', port=10019, timeout=5.0, client_id=client_id, |
| 92 | + client_secret=client_secret, deployment_id=deployment_id) |
| 93 | + data = await eos.get_info() |
| 94 | + print(json.dumps(data, indent=None) + '\n') |
| 95 | + |
| 96 | + asyncio.run(main_async()) |
0 commit comments