From 520853a8a659f0dc296f0d5fb301913cbee37688 Mon Sep 17 00:00:00 2001 From: Ritabrata Das Date: Sun, 2 Mar 2025 11:14:16 +0530 Subject: handle client exits gracefully and improve plugin manager --- plugins/chat_weather_setter.py | 8 ++++---- plugins/refuel.py | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) (limited to 'plugins') diff --git a/plugins/chat_weather_setter.py b/plugins/chat_weather_setter.py index 5b31a38..675bac5 100644 --- a/plugins/chat_weather_setter.py +++ b/plugins/chat_weather_setter.py @@ -13,10 +13,10 @@ class Plugin: self.plugin_manager = plugin_manager #self.plugin_manager.register_hook('on_chat', self.on_chat) self.plugin_manager.register_hook('on_environment_server', self.on_environment) - self.plugin_manager.register_command('fog', self.fog) - self.plugin_manager.register_command('sky', self.sky) - self.plugin_manager.register_command('time', self.time) - self.plugin_manager.register_command('vis', self.visibility) + self.plugin_manager.register_command('fog', self.fog, "Sets fog color. Usage : /fog r,g,b") + self.plugin_manager.register_command('sky', self.sky, "Sets sky color. Usage : /sky r,g,b") + self.plugin_manager.register_command('time', self.time, "Sets time. Usage : /time ") + self.plugin_manager.register_command('visibility', self.visibility, "Sets Visibilty : /vis ", "vis") def fog(self, full_message, player, message_to_client, message_to_server): try: diff --git a/plugins/refuel.py b/plugins/refuel.py index ff96699..cde2e03 100644 --- a/plugins/refuel.py +++ b/plugins/refuel.py @@ -1,6 +1,7 @@ """ This plugin enables you to perform air to air refueling. """ +from ast import alias import math from lib.PacketManager.packets import FSNETCMD_AIRCMD, FSNETCMD_AIRPLANESTATE from lib import YSchat @@ -21,8 +22,8 @@ class Plugin: def register(self, plugin_manager): self.plugin_manager = plugin_manager - self.plugin_manager.register_command('refuel', self.refuel) - self.plugin_manager.register_command('refueler', self.refueler) + self.plugin_manager.register_command('refuel', self.refuel, "Allows you to get refuled", alias = "r") + self.plugin_manager.register_command('refueler', self.refueler, "Allows other players to refuel from you", alias = "rf") self.plugin_manager.register_hook('on_flight_data', self.on_flight_data) self.plugin_manager.register_hook('on_unjoin', self.on_unjoin) -- cgit v1.2.3 From aa558bb5e8542ef8ba5e723d78cc7db0899d35c6 Mon Sep 17 00:00:00 2001 From: Ritabrata Das Date: Mon, 3 Mar 2025 10:44:02 +0530 Subject: add plugin to ensure planes die on ground --- plugins/crash_on_ground.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 plugins/crash_on_ground.py (limited to 'plugins') diff --git a/plugins/crash_on_ground.py b/plugins/crash_on_ground.py new file mode 100644 index 0000000..935ace3 --- /dev/null +++ b/plugins/crash_on_ground.py @@ -0,0 +1,15 @@ +# This plugin renders the dead plane until crashes in ground +# Thus disabling mid air dispawning when dead + +ENABLED = True + +class Plugin: + def __init__(self): + self.plugin_manager = None + + def register(self, plugin_manager): + self.plugin_manager = plugin_manager + self.plugin_manager.register_hook('on_unjoin', self.on_death) + + def on_death(self, data, player, message_to_client, message_to_server): + return False -- cgit v1.2.3 From 4c59fc24b86fda2e317f4fce77eea7a11ff87e30 Mon Sep 17 00:00:00 2001 From: Ritabrata Das Date: Mon, 3 Mar 2025 14:41:24 +0530 Subject: Fix crash on ground plugin, add additional logging, fix readback packet --- lib/PacketManager/packets/FSNETCMD_READBACK.py | 8 ++++---- plugins/crash_on_ground.py | 14 ++++++++++++-- proxy.py | 4 ++-- 3 files changed, 18 insertions(+), 8 deletions(-) (limited to 'plugins') diff --git a/lib/PacketManager/packets/FSNETCMD_READBACK.py b/lib/PacketManager/packets/FSNETCMD_READBACK.py index 0fd4ead..b86199f 100644 --- a/lib/PacketManager/packets/FSNETCMD_READBACK.py +++ b/lib/PacketManager/packets/FSNETCMD_READBACK.py @@ -6,7 +6,7 @@ class FSNETCMD_READBACK: #6 * Client sends FSNETREADBACK_ADDAIRPLAN or FSNETREADBACK_ADDGROUND to acknowledge FSNETCMD_ADDOBJECT - * Client sends FSNETREADBACK_REMOVEAIRPLANE or FSNETREADBACK_REMOVEGROUND + * Client sends FSNETREADBACK_REMOVEAIRPLANE or FSNETREADBACK_REMOVEGROUND to acknowledge FSNETCMD_REMOVEAIRPLANE or FSNETCMD_REMOVEGROUND * Client sends FSNETREADBACK_ENVIRONMENT to acknowledge FSNETCMD_ENVIRONMENT * Client sends FSNETREADBACK_JOINREQUEST to acknowledge FSNETCMD_JOINREQUEST @@ -14,7 +14,7 @@ class FSNETCMD_READBACK: #6 * Client sends FSNETREADBACK_USEMISSILE to acknowledge FSNETCMD_USEMISSILE * Client sends FSNETREADBACK_USEUNGUIDEDWEAPON to acknowledge FSNETCMD_USEUNGUIDEDWEAPON * Client sends FSNETREADBACK_CTRLSHOWUSERNAME to acknowledge FSNETCMD_CTRLSHOWUSERNAME - + * Server sends FSNETREADBACK_JOINREQUEST to acknowledge FSNETCMD_JOINREQUEST - Will punt the user if the server receives this from them * There are probably more, but I've not gone into much detail here yet. @@ -33,7 +33,7 @@ class FSNETCMD_READBACK: #6 @staticmethod def encode(read_back_type, read_back_param, with_size:bool=False): - buffer = pack("IhhI", 6, read_back_type, 0, read_back_param) + buffer = pack("IhhI", 6, 0, read_back_type, read_back_param) if with_size: return pack("I",len(buffer))+buffer - return buffer \ No newline at end of file + return buffer diff --git a/plugins/crash_on_ground.py b/plugins/crash_on_ground.py index 935ace3..fb9d080 100644 --- a/plugins/crash_on_ground.py +++ b/plugins/crash_on_ground.py @@ -1,5 +1,6 @@ # This plugin renders the dead plane until crashes in ground # Thus disabling mid air dispawning when dead +from lib.PacketManager.packets import FSNETCMD_REMOVEAIRPLANE, FSNETCMD_READBACK ENABLED = True @@ -9,7 +10,16 @@ class Plugin: def register(self, plugin_manager): self.plugin_manager = plugin_manager - self.plugin_manager.register_hook('on_unjoin', self.on_death) + #self.plugin_manager.register_hook('on_unjoin', self.on_death) + self.plugin_manager.register_hook('on_remove_airplane_server', self.on_death) def on_death(self, data, player, message_to_client, message_to_server): - return False + if player.aircraft.id != -1: + if player.aircraft.id == FSNETCMD_REMOVEAIRPLANE(data).object_id: + return True + else: + a = FSNETCMD_READBACK.encode(2, player.aircraft.id, True) + message_to_server.append(a) + return False + else: + return True diff --git a/proxy.py b/proxy.py index 433e316..d55bba2 100644 --- a/proxy.py +++ b/proxy.py @@ -125,7 +125,7 @@ async def handle_client(client_reader, client_writer): data = header + packet packet_type = PacketManager().get_packet_type(packet) if direction == "client_to_server": - debug("C2S" + str(packet_type)) + debug("C2S" + str(packet_type) + str(player.username)) debug(data) try: @@ -203,7 +203,7 @@ async def handle_client(client_reader, client_writer): traceback.print_exc() # This will display the full traceback else : - debug("S2C" + str(packet_type)) + debug("S2C" + str(packet_type) + str(player.username)) debug(data) #if packet_type == "FSNETCMD_AIRCMD": -- cgit v1.2.3 From 19ac5fec99c54086f52a17b254e98b37192a9627 Mon Sep 17 00:00:00 2001 From: Ritabrata Das Date: Tue, 4 Mar 2025 15:55:34 +0530 Subject: Complete crash on ground plugin and some api changes --- lib/Aircraft.py | 3 +-- lib/plugin_manager.py | 3 ++- plugins/crash_on_ground.py | 9 ++++++++- plugins/radar.py | 2 +- proxy.py | 12 ++++++++---- 5 files changed, 20 insertions(+), 9 deletions(-) (limited to 'plugins') diff --git a/lib/Aircraft.py b/lib/Aircraft.py index 5e6e24d..9f162ef 100644 --- a/lib/Aircraft.py +++ b/lib/Aircraft.py @@ -78,9 +78,8 @@ class Aircraft: return None if self.life == -1: self.life=packet.life - + self.prev_life = self.life - self.life = packet.life self.set_position(packet.position) self.set_attitude(packet.atti) diff --git a/lib/plugin_manager.py b/lib/plugin_manager.py index 0a9174c..b96045b 100644 --- a/lib/plugin_manager.py +++ b/lib/plugin_manager.py @@ -7,12 +7,13 @@ PLUGIN_DIR = os.path.join(os.path.dirname(__file__), '../plugins') sys.path.append(PLUGIN_DIR) class PluginManager: - def __init__(self): + def __init__(self, connected_players): self.plugins = {} self.hooks= {} self.commands = {'help': None} self.help_message = "List of Available Commands:\n" self.load_plugins() + self.connected_players = connected_players def load_plugins(self): for plugin in os.listdir(PLUGIN_DIR): diff --git a/plugins/crash_on_ground.py b/plugins/crash_on_ground.py index fb9d080..4cc334b 100644 --- a/plugins/crash_on_ground.py +++ b/plugins/crash_on_ground.py @@ -1,6 +1,6 @@ # This plugin renders the dead plane until crashes in ground # Thus disabling mid air dispawning when dead -from lib.PacketManager.packets import FSNETCMD_REMOVEAIRPLANE, FSNETCMD_READBACK +from lib.PacketManager.packets import FSNETCMD_REMOVEAIRPLANE, FSNETCMD_READBACK, FSNETCMD_GETDAMAGE ENABLED = True @@ -11,6 +11,7 @@ class Plugin: def register(self, plugin_manager): self.plugin_manager = plugin_manager #self.plugin_manager.register_hook('on_unjoin', self.on_death) + #self.plugin_manager.register_hook('on_add_object_server', self.on_join) self.plugin_manager.register_hook('on_remove_airplane_server', self.on_death) def on_death(self, data, player, message_to_client, message_to_server): @@ -18,6 +19,12 @@ class Plugin: if player.aircraft.id == FSNETCMD_REMOVEAIRPLANE(data).object_id: return True else: + decode = FSNETCMD_REMOVEAIRPLANE(data) + b = FSNETCMD_GETDAMAGE.encode(decode.object_id, 1, 1, + player.aircraft.id, 10000, + 11, 0, True) + message_to_client.append(b) + message_to_server.append(b) a = FSNETCMD_READBACK.encode(2, player.aircraft.id, True) message_to_server.append(a) return False diff --git a/plugins/radar.py b/plugins/radar.py index 7636dc4..e09447d 100644 --- a/plugins/radar.py +++ b/plugins/radar.py @@ -9,7 +9,7 @@ import struct import traceback ENABLED = True -RADIUS = 5000 # in meters, within this range planes with different IFF can see each other. +RADIUS = 9000 # in meters, within this range planes with different IFF can see each other. flying_players = {} diff --git a/proxy.py b/proxy.py index d55bba2..ad81590 100644 --- a/proxy.py +++ b/proxy.py @@ -56,7 +56,7 @@ info("Lisenced under GPLv3") info("Press CTRL+C to stop the proxy") #Load the plugins -plugin_manager = PluginManager() +plugin_manager = PluginManager(CONNECTED_PLAYERS) # Close Connection async def close_connection(client_writer, server_writer): @@ -135,6 +135,7 @@ async def handle_client(client_reader, client_writer): # if not keep_message: # data = None player.login(FSNETCMD_LOGON(packet)) + info(f"Player {player.username} connected from {player.ip}") if player.version != YSF_VERSION and VIA_VERSION: info(f"ViaVersion enabled : Porting {player.username} from {player.version} to {YSF_VERSION}") message_to_client.append(YSchat.message(f"Porting you to YSFlight {YSF_VERSION}, This is currently Experimental")) @@ -150,7 +151,8 @@ async def handle_client(client_reader, client_writer): asyncio.create_task(discord_send_message(CHANNEL_ID, f"{player.username} has took off in a {decode.aircraft}! 🛫")) elif packet_type == "FSNETCMD_AIRPLANESTATE": - player.aircraft.add_state(FSNETCMD_AIRPLANESTATE(packet)) #TODO: Do we want to convert all this to plugins? Probably not, but there is duplicated functionality + decode = FSNETCMD_AIRPLANESTATE(packet) + player.aircraft.add_state(decode) #TODO: Do we want to convert all this to plugins? Probably not, but there is duplicated functionality # keep_message = plugin_manager.triggar_hook('on_flight_data', packet, player, message_to_client, message_to_server) # if not keep_message: # data = None @@ -265,8 +267,10 @@ async def handle_client(client_reader, client_writer): """ if not player.connection_closed: player.connection_closed = True - if DISCORD_ENABLED and not player.is_a_bot: - await discord_send_message(CHANNEL_ID, f"{player.username} has left the server!") + if not player.is_a_bot: + message_to_server.append(YSchat.message(f"{player.username} has left the server!")) + if DISCORD_ENABLED: + await discord_send_message(CHANNEL_ID, f"{player.username} has left the server!") await close_connection(client_writer, server_writer) -- cgit v1.2.3 From 42d2d3acfc44455b70bb46c7bcd9cb1487a79ca4 Mon Sep 17 00:00:00 2001 From: Ritabrata Das Date: Sun, 9 Mar 2025 12:01:43 +0530 Subject: Add support for longer than 16 char username and fix bugs --- lib/PacketManager/packets/FSNETCMD_LOGON.py | 2 +- lib/Player.py | 3 ++- plugins/crash_on_ground.py | 2 -- plugins/radar.py | 2 +- proxy.py | 23 +++++++++++++++++------ 5 files changed, 21 insertions(+), 11 deletions(-) (limited to 'plugins') diff --git a/lib/PacketManager/packets/FSNETCMD_LOGON.py b/lib/PacketManager/packets/FSNETCMD_LOGON.py index 9848590..620b2cd 100644 --- a/lib/PacketManager/packets/FSNETCMD_LOGON.py +++ b/lib/PacketManager/packets/FSNETCMD_LOGON.py @@ -55,4 +55,4 @@ class FSNETCMD_LOGON: #1 @staticmethod def alter_version(buffer:bytes, new_version:int): - return buffer[:20]+pack("I",new_version)+buffer[24:] \ No newline at end of file + return buffer[:20]+pack("I",new_version)+buffer[24:] diff --git a/lib/Player.py b/lib/Player.py index cfd50a8..f8da70e 100644 --- a/lib/Player.py +++ b/lib/Player.py @@ -27,7 +27,8 @@ class Player: self.ip = ip def check_add_object(self, packet:FSNETCMD_ADDOBJECT): - if packet.pilot == self.username: + # we check the full name here + if packet.pilot == self.alias: self.aircraft = Aircraft() self.aircraft.name = packet.identifier self.aircraft.id = packet.object_id diff --git a/plugins/crash_on_ground.py b/plugins/crash_on_ground.py index 4cc334b..15b1fbf 100644 --- a/plugins/crash_on_ground.py +++ b/plugins/crash_on_ground.py @@ -10,8 +10,6 @@ class Plugin: def register(self, plugin_manager): self.plugin_manager = plugin_manager - #self.plugin_manager.register_hook('on_unjoin', self.on_death) - #self.plugin_manager.register_hook('on_add_object_server', self.on_join) self.plugin_manager.register_hook('on_remove_airplane_server', self.on_death) def on_death(self, data, player, message_to_client, message_to_server): diff --git a/plugins/radar.py b/plugins/radar.py index e09447d..ac47b5b 100644 --- a/plugins/radar.py +++ b/plugins/radar.py @@ -60,7 +60,7 @@ class Plugin: try: # This is a really arbitary position thats hard to reach, better would # be to randomise it for every time the server starts. - position_data = struct.pack("3f", 10e7, 10e7, 10e7) + position_data = struct.pack("3f", 10e8, 200, 10e8) if decode.packet_version == 4 or 5: updated_data = data[:14] + position_data + data[26:] else: diff --git a/proxy.py b/proxy.py index ad81590..f68e6d7 100644 --- a/proxy.py +++ b/proxy.py @@ -131,11 +131,17 @@ async def handle_client(client_reader, client_writer): try: if packet_type == "FSNETCMD_LOGON": - # keep_message = plugin_manager.triggar_hook('on_login', packet, player, message_to_client, message_to_server) - # if not keep_message: - # data = None - player.login(FSNETCMD_LOGON(packet)) + decode = FSNETCMD_LOGON(packet) + for p in CONNECTED_PLAYERS: + if p.username == decode.username: + client_writer.write(YSchat.message(f"Same username {decode.username} is aldready connected to server! Kicked {ipAddr}")) + data = None + info(f"Same username {decode.username} is aldready connected to server! Kicked {ipAddr}") + await close_connection(client_writer, server_writer) + + player.login(decode) info(f"Player {player.username} connected from {player.ip}") + if player.version != YSF_VERSION and VIA_VERSION: info(f"ViaVersion enabled : Porting {player.username} from {player.version} to {YSF_VERSION}") message_to_client.append(YSchat.message(f"Porting you to YSFlight {YSF_VERSION}, This is currently Experimental")) @@ -152,7 +158,9 @@ async def handle_client(client_reader, client_writer): elif packet_type == "FSNETCMD_AIRPLANESTATE": decode = FSNETCMD_AIRPLANESTATE(packet) - player.aircraft.add_state(decode) #TODO: Do we want to convert all this to plugins? Probably not, but there is duplicated functionality + player.aircraft.add_state(decode) + + #TODO: Do we want to convert all this to plugins? Probably not, but there is duplicated functionality # keep_message = plugin_manager.triggar_hook('on_flight_data', packet, player, message_to_client, message_to_server) # if not keep_message: # data = None @@ -186,6 +194,7 @@ async def handle_client(client_reader, client_writer): data = None command = msg.message.split(" ")[0][1:] asyncio.create_task(triggerCommand.triggerCommand(command, msg.message, player, message_to_client, message_to_server, plugin_manager)) + info(f"Command {command} triggered by {player.username}") else: if DISCORD_ENABLED: # Make it non blocking! @@ -267,8 +276,10 @@ async def handle_client(client_reader, client_writer): """ if not player.connection_closed: player.connection_closed = True + CONNECTED_PLAYERS.remove(player) if not player.is_a_bot: - message_to_server.append(YSchat.message(f"{player.username} has left the server!")) + for player in CONNECTED_PLAYERS: + player.streamWriterObject.write(YSchat.message(f"{player.username} has left the server!")) if DISCORD_ENABLED: await discord_send_message(CHANNEL_ID, f"{player.username} has left the server!") await close_connection(client_writer, server_writer) -- cgit v1.2.3