From 585c936cf3be7becf9af62b8d93d01de63ab5a95 Mon Sep 17 00:00:00 2001 From: Lewis Hazell Date: Sun, 23 May 2021 18:14:57 +0200 Subject: [PATCH 1/7] Network: Create SocketListenerFactory. Services can be injected into the factory to pass into SocketListener, instead of referencing IRCd properties. task: #12 --- cmpctircd/IRCd.cs | 6 ++++-- cmpctircd/ISocketListenerFactory.cs | 7 +++++++ cmpctircd/Program.cs | 1 + cmpctircd/SocketListener.cs | 1 + cmpctircd/SocketListenerFactory.cs | 9 +++++++++ 5 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 cmpctircd/ISocketListenerFactory.cs create mode 100644 cmpctircd/SocketListenerFactory.cs diff --git a/cmpctircd/IRCd.cs b/cmpctircd/IRCd.cs index 41f2bd6..3328a6e 100644 --- a/cmpctircd/IRCd.cs +++ b/cmpctircd/IRCd.cs @@ -9,6 +9,7 @@ using cmpctircd.Modes; public class IRCd { + private readonly ISocketListenerFactory socketListenerFactory; private readonly IList Listeners = new List(); public readonly IList Connectors = new List(); public PacketManager PacketManager { get; } @@ -50,9 +51,10 @@ public class IRCd { public List Clients => ClientLists.SelectMany(clientList => clientList).ToList(); public List Servers => ServerLists.SelectMany(serverList => serverList).ToList(); - public IRCd(Log log, CmpctConfigurationSection config, IServiceProvider services) { + public IRCd(Log log, CmpctConfigurationSection config, IServiceProvider services, ISocketListenerFactory socketListenerFactory) { this.Log = log; this.Config = config; + this.socketListenerFactory = socketListenerFactory; // Interpret the ConfigData SID = config.SID; @@ -99,7 +101,7 @@ public void Run() { PacketManager.Load(); foreach(var listener in Config.Sockets.OfType()) { - SocketListener sl = new SocketListener(this, listener); + SocketListener sl = socketListenerFactory.CreateSocketListener(this, listener); Log.Info($"==> Listening on: {listener.Host}:{listener.Port} ({listener.Type}) ({(listener.IsTls ? "TLS" : "Plain" )})"); Listeners.Add(sl); diff --git a/cmpctircd/ISocketListenerFactory.cs b/cmpctircd/ISocketListenerFactory.cs new file mode 100644 index 0000000..824150b --- /dev/null +++ b/cmpctircd/ISocketListenerFactory.cs @@ -0,0 +1,7 @@ +using cmpctircd.Configuration; + +namespace cmpctircd { + public interface ISocketListenerFactory { + SocketListener CreateSocketListener(IRCd ircd, SocketElement config); + } +} \ No newline at end of file diff --git a/cmpctircd/Program.cs b/cmpctircd/Program.cs index 5d9c0af..301312e 100644 --- a/cmpctircd/Program.cs +++ b/cmpctircd/Program.cs @@ -28,6 +28,7 @@ static IHostBuilder CreateHostBuilder(string[] args) { services.AddScoped(); services.AddScoped(sp => sp.GetRequiredService().Sender as Client); services.AddScoped(sp => sp.GetRequiredService().Sender as Server); + services.AddTransient(); services.AddHostedService(); }); } diff --git a/cmpctircd/SocketListener.cs b/cmpctircd/SocketListener.cs index 19bdfa6..787ecc0 100644 --- a/cmpctircd/SocketListener.cs +++ b/cmpctircd/SocketListener.cs @@ -12,6 +12,7 @@ namespace cmpctircd { public class SocketListener { + private readonly Log log; protected IRCd _ircd; private Boolean _started = false; private TcpListener _listener = null; diff --git a/cmpctircd/SocketListenerFactory.cs b/cmpctircd/SocketListenerFactory.cs new file mode 100644 index 0000000..a1200a9 --- /dev/null +++ b/cmpctircd/SocketListenerFactory.cs @@ -0,0 +1,9 @@ +using cmpctircd.Configuration; + +namespace cmpctircd { + public class SocketListenerFactory : ISocketListenerFactory { + public SocketListener CreateSocketListener(IRCd ircd, SocketElement config) { + return new SocketListener(ircd, config); + } + } +} From 9747851172922593fc88a0a65879133ff74f5667 Mon Sep 17 00:00:00 2001 From: Lewis Hazell Date: Sun, 23 May 2021 18:16:32 +0200 Subject: [PATCH 2/7] Network: Pass log directly into SocketListener constructor. task: #12 --- cmpctircd/SocketListener.cs | 3 ++- cmpctircd/SocketListenerFactory.cs | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmpctircd/SocketListener.cs b/cmpctircd/SocketListener.cs index 787ecc0..22351b4 100644 --- a/cmpctircd/SocketListener.cs +++ b/cmpctircd/SocketListener.cs @@ -26,7 +26,8 @@ public class SocketListener { public int ServerCount = 0; public int AuthServerCount = 0; - public SocketListener(IRCd ircd, SocketElement info) { + public SocketListener(Log log, IRCd ircd, SocketElement info) { + this.log = log ?? throw new ArgumentNullException(nameof(log)); this._ircd = ircd; this.Info = info; _listener = new TcpListener(info.Host, info.Port); diff --git a/cmpctircd/SocketListenerFactory.cs b/cmpctircd/SocketListenerFactory.cs index a1200a9..bf9575b 100644 --- a/cmpctircd/SocketListenerFactory.cs +++ b/cmpctircd/SocketListenerFactory.cs @@ -1,9 +1,16 @@ using cmpctircd.Configuration; +using System; namespace cmpctircd { public class SocketListenerFactory : ISocketListenerFactory { + private readonly Log log; + + public SocketListenerFactory(Log log) { + this.log = log ?? throw new ArgumentNullException(nameof(log)); + } + public SocketListener CreateSocketListener(IRCd ircd, SocketElement config) { - return new SocketListener(ircd, config); + return new SocketListener(log, ircd, config); } } } From ae1f9c57d00ef8d0448d679e91db156d99f0f386 Mon Sep 17 00:00:00 2001 From: Lewis Hazell Date: Sun, 23 May 2021 18:39:17 +0200 Subject: [PATCH 3/7] Network: Use private log property in SocketListener instead of IRCd property. task: #12 --- cmpctircd/SocketListener.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cmpctircd/SocketListener.cs b/cmpctircd/SocketListener.cs index 22351b4..7ec3d5f 100644 --- a/cmpctircd/SocketListener.cs +++ b/cmpctircd/SocketListener.cs @@ -45,7 +45,7 @@ public virtual void Bind() { } public virtual void Stop() { if (_started) { - _ircd.Log.Debug($"Shutting down listener [IP: {Info.Host}, Port: {Info.Port}, TLS: {Info.IsTls}]"); + log.Debug($"Shutting down listener [IP: {Info.Host}, Port: {Info.Port}, TLS: {Info.IsTls}]"); _listener.Stop(); _started = false; } @@ -63,7 +63,7 @@ public async Task ListenToClients() { TcpClient tc = await _listener.AcceptTcpClientAsync(); HandleClientAsync(tc); // this should split off execution } catch(Exception e) { - _ircd.Log.Error($"Exception in ListenToClients(): {e.ToString()}"); + log.Error($"Exception in ListenToClients(): {e.ToString()}"); } } } @@ -74,7 +74,7 @@ protected async Task HandshakeIfNeededAsync(TcpClient tc, Stream stream) try { stream = await HandshakeTlsAsServerAsync(tc); } catch (Exception e) { - _ircd.Log.Debug($"Exception in {nameof(HandshakeTlsAsServerAsync)}: {e}"); + log.Debug($"Exception in {nameof(HandshakeTlsAsServerAsync)}: {e}"); tc.Close(); } } @@ -191,7 +191,7 @@ public async Task HandshakeTlsAsClient(TcpClient tc, string host, boo if (verifyCert) { stream = new SslStream(tc.GetStream(), true); } else { - _ircd.Log.Warn($"[SERVER] Connecting out to server {host} with TLS verification disabled: this is dangerous!"); + log.Warn($"[SERVER] Connecting out to server {host} with TLS verification disabled: this is dangerous!"); stream = new SslStream(tc.GetStream(), true, (sender, certificate, chain, sslPolicyErrors) => true); } From d56f0b1e5d98106c066b3b0e43e734cb786a7ec3 Mon Sep 17 00:00:00 2001 From: Lewis Hazell Date: Sun, 23 May 2021 18:40:17 +0200 Subject: [PATCH 4/7] Network: Add log parameter to SocketConnector constructor. Required for base constructor. task: #12 --- cmpctircd/SocketConnector.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmpctircd/SocketConnector.cs b/cmpctircd/SocketConnector.cs index 7c252e9..5f26caa 100644 --- a/cmpctircd/SocketConnector.cs +++ b/cmpctircd/SocketConnector.cs @@ -9,14 +9,15 @@ namespace cmpctircd { public class SocketConnector : SocketListener { - + private readonly Log log; public ServerElement ServerInfo; public bool Connected; private TcpClient tc; private NetworkStream stream; - public SocketConnector(IRCd ircd, ServerElement info) : base(ircd, info) { + public SocketConnector(Log log, IRCd ircd, ServerElement info) : base(log, ircd, info) { + this.log = log ?? throw new ArgumentNullException(nameof(log)); ServerInfo = info; } From 038165321b70837903f9cb11811cd392e59c5623 Mon Sep 17 00:00:00 2001 From: Lewis Hazell Date: Sun, 23 May 2021 18:46:04 +0200 Subject: [PATCH 5/7] Network: Add SocketConnectorFactory. Used to inject services into SocketConnector. task: #12 --- cmpctircd/ISocketConnectorFactory.cs | 7 +++++++ cmpctircd/SocketConnectorFactory.cs | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 cmpctircd/ISocketConnectorFactory.cs create mode 100644 cmpctircd/SocketConnectorFactory.cs diff --git a/cmpctircd/ISocketConnectorFactory.cs b/cmpctircd/ISocketConnectorFactory.cs new file mode 100644 index 0000000..d62ccf7 --- /dev/null +++ b/cmpctircd/ISocketConnectorFactory.cs @@ -0,0 +1,7 @@ +using cmpctircd.Configuration; + +namespace cmpctircd { + public interface ISocketConnectorFactory { + SocketConnector CreateSocketConnector(IRCd ircd, ServerElement config); + } +} \ No newline at end of file diff --git a/cmpctircd/SocketConnectorFactory.cs b/cmpctircd/SocketConnectorFactory.cs new file mode 100644 index 0000000..df3c25e --- /dev/null +++ b/cmpctircd/SocketConnectorFactory.cs @@ -0,0 +1,16 @@ +using cmpctircd.Configuration; +using System; + +namespace cmpctircd { + public class SocketConnectorFactory : ISocketConnectorFactory { + private readonly Log log; + + public SocketConnectorFactory(Log log) { + this.log = log ?? throw new ArgumentNullException(nameof(log)); + } + + public SocketConnector CreateSocketConnector(IRCd ircd, ServerElement config) { + return new SocketConnector(log, ircd, config); + } + } +} From 35dca0728644a340caf0528d753a42c7cf69c151 Mon Sep 17 00:00:00 2001 From: Lewis Hazell Date: Sun, 23 May 2021 18:47:07 +0200 Subject: [PATCH 6/7] Daemon: Construct SocketConnector instances with SocketConnectorFactory. task: #12 --- cmpctircd/IRCd.cs | 6 ++++-- cmpctircd/Program.cs | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cmpctircd/IRCd.cs b/cmpctircd/IRCd.cs index 3328a6e..bc132f3 100644 --- a/cmpctircd/IRCd.cs +++ b/cmpctircd/IRCd.cs @@ -10,6 +10,7 @@ public class IRCd { private readonly ISocketListenerFactory socketListenerFactory; + private readonly ISocketConnectorFactory socketConnectorFactory; private readonly IList Listeners = new List(); public readonly IList Connectors = new List(); public PacketManager PacketManager { get; } @@ -51,10 +52,11 @@ public class IRCd { public List Clients => ClientLists.SelectMany(clientList => clientList).ToList(); public List Servers => ServerLists.SelectMany(serverList => serverList).ToList(); - public IRCd(Log log, CmpctConfigurationSection config, IServiceProvider services, ISocketListenerFactory socketListenerFactory) { + public IRCd(Log log, CmpctConfigurationSection config, IServiceProvider services, ISocketListenerFactory socketListenerFactory, ISocketConnectorFactory socketConnectorFactory) { this.Log = log; this.Config = config; this.socketListenerFactory = socketListenerFactory; + this.socketConnectorFactory = socketConnectorFactory; // Interpret the ConfigData SID = config.SID; @@ -112,7 +114,7 @@ public void Run() { if (server.IsOutbound) { // tag with outbound="true" // We want to connect out to this server, not have them connect to us - var sc = new SocketConnector(this, server); + var sc = socketConnectorFactory.CreateSocketConnector(this, server); Log.Info($"==> Connecting to: {server.Destination}:{server.Port} ({server.Host}) ({(server.IsTls ? "TLS" : "Plain" )})"); Connectors.Add(sc); diff --git a/cmpctircd/Program.cs b/cmpctircd/Program.cs index 301312e..7176627 100644 --- a/cmpctircd/Program.cs +++ b/cmpctircd/Program.cs @@ -29,6 +29,7 @@ static IHostBuilder CreateHostBuilder(string[] args) { services.AddScoped(sp => sp.GetRequiredService().Sender as Client); services.AddScoped(sp => sp.GetRequiredService().Sender as Server); services.AddTransient(); + services.AddTransient(); services.AddHostedService(); }); } From 9279ed6b405117cc4c123a8d433bfc39ff9fd361 Mon Sep 17 00:00:00 2001 From: Lewis Hazell Date: Sun, 23 May 2021 18:48:05 +0200 Subject: [PATCH 7/7] Network: Use log property in SocketConnector instead of Log in IRCd. task: #12 --- cmpctircd/SocketConnector.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmpctircd/SocketConnector.cs b/cmpctircd/SocketConnector.cs index 5f26caa..ed20e8a 100644 --- a/cmpctircd/SocketConnector.cs +++ b/cmpctircd/SocketConnector.cs @@ -39,7 +39,7 @@ public async Task Connect() { await tc.ConnectAsync(Info.Host.ToString(), Info.Port); stream = tc.GetStream(); } catch (SocketException) { - _ircd.Log.Warn($"Unable to connect to server {Info.Host.ToString()}:{Info.Port}"); + log.Warn($"Unable to connect to server {Info.Host.ToString()}:{Info.Port}"); return; }