Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Database SSL support #7

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ jobs:
mkdir -p plugin/plugins/TestPlugin
mkdir -p plugin/shared/Sessions.API

- name: Move Files
run: |
mv ./src/bin/Release/net8.0/* ./plugin/plugins/Sessions
mv ./TestPlugin/bin/Release/net8.0/* ./plugin/plugins/TestPlugin
mv ./Sessions.API/bin/Release/net8.0/* ./plugin/shared/Sessions.API

- name: Remove .API
run: |
rm ./plugin/plugins/Sessions/Sessions.API.*
rm ./plugin/plugins/TestPlugin/Sessions.API.*

Expand Down
1 change: 1 addition & 0 deletions Sessions.API/Sessions.API.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Server
public Map? Map { get; set; }
public required string Ip { get; set; }
public required ushort Port { get; set; }
public string? MapName { get; set; }
}

public class Map
Expand Down
9 changes: 4 additions & 5 deletions TestPlugin/TestPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override void Load(bool isReload)
var server = CapabilityServer.Get()!.Server;

if (server != null)
Logger.LogInformation($"Server: {server.Id} (${server.Ip}:${server.Port}) - Map: ${server.Map!.Id}");
Logger.LogInformation($"Server: {server.Id} ({server.Ip}:{server.Port}) - Map: {server.MapName} [{server.Map!.Id}]");

AddCommand("css_test", "test", CommandTest);
RegisterEventHandler<EventPlayerConnect>(EventConnect, HookMode.Pre);
Expand All @@ -37,7 +37,7 @@ private void CommandTest(CCSPlayerController? controller, CommandInfo info)
if (server == null || player == null || session == null)
return;

Logger.LogInformation($"Player: {player.Id} - Session: {session.Id} - Server: {server.Id}/{CounterStrikeSharp.API.Server.MapName}[{server.Map!.Id}] ({server.Ip}:{server.Port}");
Logger.LogInformation($"Player: {player.Id} - Session: {session.Id} - Server: {server.Id}/{server.MapName}[{server.Map!.Id}] ({server.Ip}:{server.Port}");
}

private HookResult EventConnect(EventPlayerConnect @event, GameEventInfo info)
Expand All @@ -48,10 +48,9 @@ private HookResult EventConnect(EventPlayerConnect @event, GameEventInfo info)
var controller = @event.Userid;
var player = CapabilityPlayer.Get(controller)!.Player;

if (player == null)
return HookResult.Continue;
if (player != null)
Logger.LogInformation($"Player {player.Id} connected");

Logger.LogInformation($"Player {player.Id} connected");
return HookResult.Continue;
}
}
10 changes: 5 additions & 5 deletions src/DatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;
using System.Text.RegularExpressions;

namespace Sessions;

Expand All @@ -11,9 +11,9 @@ public DatabaseFactory(SessionsConfig config, Sessions plugin)
throw new InvalidOperationException("Database is not set in the configuration file");

//#if DEBUG
plugin.Logger.LogInformation($"Checked: ${config.DatabaseType} SSL: ${config.DatabaseSsl.ToString()} " +
$"${config.DatabaseUser}@${config.DatabaseHost}:${config.DatabasePort} " +
$"${config.DatabaseName}:${Regex.Replace(config.DatabasePassword, ".", "*")}");
plugin.Logger.LogInformation($"Checked: {config.DatabaseType} SSL: {config.DatabaseSsl.ToString()} " +
$"{config.DatabaseUser}@{config.DatabaseHost}:{config.DatabasePort} " +
$"{config.DatabaseName}:{Regex.Replace(config.DatabasePassword, ".", "*")}");
//#endif

var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
Expand Down
4 changes: 2 additions & 2 deletions src/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public partial class Sessions
{
private HookResult OnPlayerChat(EventPlayerChat @event, GameEventInfo info)
{
var player = Utilities.GetPlayerFromUserid(@event.Userid);
var controller = Utilities.GetPlayerFromUserid(@event.Userid);

if (!IsValidPlayer(player) || !Players.TryGetValue(player!.Slot, out var value) || value.Session == null)
if (!IsValidPlayer(controller) || !Players.TryGetValue(controller!.Slot, out var value) || value.Session == null)
return HookResult.Continue;

var messageType = @event.Teamonly ? MessageType.TeamChat : MessageType.Chat;
Expand Down
2 changes: 1 addition & 1 deletion src/Ip.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//https://discord.com/channels/1160907911501991946/1233009182857494588
//nuko8964
using System.Runtime.InteropServices;
using CounterStrikeSharp.API.Core;
using System.Runtime.InteropServices;

namespace Sessions;

Expand Down
13 changes: 7 additions & 6 deletions src/Listeners.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Entities;

namespace Sessions;

Expand All @@ -10,15 +11,15 @@ private void OnMapStart(string mapName)
Server!.Map = Database.GetMapAsync(mapName).GetAwaiter().GetResult();
}

private void OnClientAuthorized(int playerSlot, CounterStrikeSharp.API.Modules.Entities.SteamID steamId)
private void OnClientAuthorized(int playerSlot, SteamID steamId)
{
var player = Utilities.GetPlayerFromSlot(playerSlot);
var controller = Utilities.GetPlayerFromSlot(playerSlot);

if (!IsValidPlayer(player))
if (!IsValidPlayer(controller))
return;

OnPlayerConnect(playerSlot, steamId.SteamId64, NativeAPI.GetPlayerIpAddress(playerSlot).Split(":")[0]).GetAwaiter().GetResult();
CheckAlias(playerSlot, player!.PlayerName).GetAwaiter().GetResult();
CheckAlias(playerSlot, controller!.PlayerName).GetAwaiter().GetResult();
}

private void OnClientDisconnect(int playerSlot)
Expand Down
14 changes: 14 additions & 0 deletions src/PostgresService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ private static string BuildConnectionString(SessionsConfig config)
Pooling = true
};

if (!config.DatabaseSsl)
return builder.ConnectionString;

builder.SslMode = SslMode.Require;

if (config.DatabaseCa.Length > 0)
{
builder.SslMode = SslMode.VerifyCA;
builder.RootCertificate = config.DatabaseCa;
}

builder.SslKey = config.DatabaseKey;
builder.SslCertificate = config.DatabaseCert;

return builder.ConnectionString;
}

Expand Down
20 changes: 12 additions & 8 deletions src/Sessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ public void OnConfigParsed(SessionsConfig config)
}

public override void Load(bool isReload)
{
RegisterCapabilities();
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);
RegisterEventHandler<EventPlayerChat>(OnPlayerChat);
AddTimer(1.0f, Timer_Repeat, TimerFlags.REPEAT);
}

public override void OnAllPluginsLoaded(bool isReload)
{
var ip = _ip.GetPublicIp()!;
var port = (ushort)ConVar.Find("hostport")!.GetPrimitiveValue<int>();
Expand All @@ -21,18 +31,12 @@ public override void Load(bool isReload)
Server = Database.GetServerAsync(ip, port).GetAwaiter().GetResult();
Server.Ip = ip;
Server.Port = port;

RegisterCapabilities();
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);
RegisterEventHandler<EventPlayerChat>(OnPlayerChat);
AddTimer(1.0f, Timer_Repeat, TimerFlags.REPEAT);
Server.MapName = CounterStrikeSharp.API.Server.MapName;

if (!isReload)
return;

Server.Map = Database.GetMapAsync(CounterStrikeSharp.API.Server.MapName).GetAwaiter().GetResult();
Server.Map = Database.GetMapAsync(Server.MapName).GetAwaiter().GetResult();

foreach (var player in Utilities.GetPlayers().Where(IsValidPlayer))
{
Expand Down
17 changes: 16 additions & 1 deletion src/SqlService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Dapper;
using MySqlConnector;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using Npgsql;
using Sessions.API;

namespace Sessions;
Expand Down Expand Up @@ -42,6 +43,20 @@ private static string BuildConnectionString(SessionsConfig config)
Pooling = true,
};

if (!config.DatabaseSsl)
return builder.ConnectionString;

builder.SslMode = MySqlSslMode.Required;

if (config.DatabaseCa.Length > 0)
{
builder.SslMode = MySqlSslMode.VerifyCA;
builder.SslCa = config.DatabaseCa;
}

builder.SslKey = config.DatabaseKey;
builder.SslCert = config.DatabaseCert;

return builder.ConnectionString;
}

Expand Down
Loading