Ready auth_client!!

This commit is contained in:
Maxim Khomutov 2023-07-08 02:20:49 +03:00
parent 3b6496146a
commit 987e445a8f
3 changed files with 52 additions and 21 deletions

View File

@ -10,11 +10,11 @@ BeamingDrive Multiplayer (BeamMP) server compatible with BeamMP clients.
- [ ] TCP Server part: - [ ] TCP Server part:
- [x] Handle code - [x] Handle code
- [x] Understanding beamp header - [x] Understanding beamp header
- [ ] Authorization - [X] Authorization
- [ ] Upload mods - [ ] Upload mods
- [ ] UDP Server part:
- [ ] Connecting to the world - [ ] Connecting to the world
- [ ] Any.... - [ ] UDP Server part:
- [ ] Players synchronizations
- [x] Additional: - [x] Additional:
- [x] Events System - [x] Events System
- [x] Plugins support - [x] Plugins support

View File

@ -5,9 +5,10 @@
# Licence: FPA # Licence: FPA
# (c) kuitoi.su 2023 # (c) kuitoi.su 2023
import asyncio import asyncio
import socket
import traceback import traceback
import aiohttp
from core import utils from core import utils
@ -52,30 +53,57 @@ class TCPServer:
# return DeComp(Data); # return DeComp(Data);
return data return data
async def auth_client(self, sock): async def auth_client(self, reader, writer):
# TODO: Authentication # TODO: Authentication
client = self.Core.create_client(sock) client = self.Core.create_client(reader, writer)
self.log.debug(f"Client: \"IP: {client.addr!r}; ID: {client.cid}\" - Authentication!") self.log.info(f"Identifying new ClientConnection...")
data = await self.recv(client) data = await self.recv(client)
self.log.debug(f"recv1 data: {data}") self.log.debug(f"recv1 data: {data}")
if len(data) > 50: if len(data) > 50:
client.kick("Too long data") await client.kick("Too long data")
return return
if "VC2.0" not in data.decode("utf-8"): if "VC2.0" not in data.decode("utf-8"):
client.kick("Outdated Version.") await client.kick("Outdated Version.")
return return
else: else:
pass await client.tcp_send(b"A") # Accepted client version
# self.log.debug('tcp_send(b"A")') # await client.tcp_send(b"S") # Ask client key
# client.tcp_send(b"A")
# data = await self.recv(client) data = await self.recv(client)
# self.log.debug(f"recv2 data: {data}") self.log.debug(f"recv2 data: {data}")
if len(data) > 50:
await client.kick("Invalid Key (too long)!")
return
client.key = data.decode("utf-8")
async with aiohttp.ClientSession() as session:
url = 'https://auth.beammp.com/pkToUser'
async with session.post(url, data={'key': client.key}) as response:
res = await response.json()
self.log.debug(f"res: {res}")
try:
if res.get("error"):
await client.kick('Invalid key! Please restart your game.')
return
client.nick = res["username"]
client.roles = res["roles"]
client.guest = res["guest"]
except Exception as e:
self.log.error(f"Auth error: {e}")
await client.kick('Invalid authentication data! Try to econnect in 5 minutes.')
client.kick("TODO Authentication") # TODO: Password party
return False
async def handle_download(self, sock): ev.call_event("on_auth", client)
if len(self.Core.clients) > config.Game["players"]:
await client.kick("Server full!")
else:
self.log.info("Identification success")
self.Core.insert_client(client)
return True, client
async def handle_download(self, writer):
# TODO: HandleDownload # TODO: HandleDownload
self.log.debug(f"Client: \"IP: {0!r}; ID: {0}\" - HandleDownload!") self.log.debug(f"Client: \"IP: {0!r}; ID: {0}\" - HandleDownload!")
return False return False

View File

@ -4,8 +4,10 @@
# Version 0.1.2 # Version 0.1.2
# Licence: FPA # Licence: FPA
# (c) kuitoi.su 2023 # (c) kuitoi.su 2023
import asyncio
from asyncio import StreamWriter, StreamReader from asyncio import StreamWriter, StreamReader
import socket import socket
from typing import Tuple
from core import utils, Core from core import utils, Core
from core.core import Client from core.core import Client
@ -17,11 +19,12 @@ class TCPServer:
self.Core = core self.Core = core
self.host = host self.host = host
self.port = port self.port = port
self.loop = asyncio.get_event_loop()
async def recv(self, client: Client) -> bytes: ... async def recv(self, client: Client) -> bytes: ...
async def auth_client(self, sock: socket.socket) -> None: ... async def auth_client(self, reader: StreamReader, writer: StreamWriter) -> Tuple[bool, Client]: ...
async def handle_download(self, sock: socket.socket) -> None: ... async def handle_download(self, writer: StreamWriter) -> bool: ...
async def handle_code(self, code: str, sock: socket.socket) -> None: ... async def handle_code(self, code: str, reader: StreamReader, writer: StreamWriter) -> bool: ...
async def handle_client(self, sock: socket.socket) -> None: ... async def handle_client(self, reader: StreamReader, writer: StreamWriter) -> None: ...
async def start(self) -> None: ... async def start(self) -> None: ...
async def stop(self) -> None: ... async def stop(self) -> None: ...