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

Increases usage of dependency injection #14

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
10 changes: 7 additions & 3 deletions cmpctircd/IRCd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using cmpctircd.Modes;

public class IRCd {
private readonly ISocketListenerFactory socketListenerFactory;
private readonly ISocketConnectorFactory socketConnectorFactory;
private readonly IList<SocketListener> Listeners = new List<SocketListener>();
public readonly IList<SocketConnector> Connectors = new List<SocketConnector>();
public PacketManager PacketManager { get; }
Expand Down Expand Up @@ -50,9 +52,11 @@ public class IRCd {

public List<Client> Clients => ClientLists.SelectMany(clientList => clientList).ToList();
public List<Server> Servers => ServerLists.SelectMany(serverList => serverList).ToList();
public IRCd(Log log, CmpctConfigurationSection config, IServiceProvider services) {
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;
Expand Down Expand Up @@ -99,7 +103,7 @@ public void Run() {
PacketManager.Load();

foreach(var listener in Config.Sockets.OfType<SocketElement>()) {
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);
Expand All @@ -110,7 +114,7 @@ public void Run() {
if (server.IsOutbound) {
// <server> 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);
Expand Down
7 changes: 7 additions & 0 deletions cmpctircd/ISocketConnectorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using cmpctircd.Configuration;

namespace cmpctircd {
public interface ISocketConnectorFactory {
SocketConnector CreateSocketConnector(IRCd ircd, ServerElement config);
}
}
7 changes: 7 additions & 0 deletions cmpctircd/ISocketListenerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using cmpctircd.Configuration;

namespace cmpctircd {
public interface ISocketListenerFactory {
SocketListener CreateSocketListener(IRCd ircd, SocketElement config);
}
}
2 changes: 2 additions & 0 deletions cmpctircd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ static IHostBuilder CreateHostBuilder(string[] args) {
services.AddScoped<IrcContext>();
services.AddScoped(sp => sp.GetRequiredService<IrcContext>().Sender as Client);
services.AddScoped(sp => sp.GetRequiredService<IrcContext>().Sender as Server);
services.AddTransient<ISocketListenerFactory, SocketListenerFactory>();
services.AddTransient<ISocketConnectorFactory, SocketConnectorFactory>();
services.AddHostedService<IrcApplicationLifecycle>();
});
}
Expand Down
7 changes: 4 additions & 3 deletions cmpctircd/SocketConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -38,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;
}

Expand Down
16 changes: 16 additions & 0 deletions cmpctircd/SocketConnectorFactory.cs
Original file line number Diff line number Diff line change
@@ -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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and SocketListenerFactory) are only really just returning an object conditionless at the moment, I am guessing this is being done with a bigger picture in mind? I can see it nicely abstracts the Log creation but I don't really have the bigger picture in my head at the moment

}
}
}
12 changes: 7 additions & 5 deletions cmpctircd/SocketListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace cmpctircd {
public class SocketListener {
private readonly Log log;
protected IRCd _ircd;
private Boolean _started = false;
private TcpListener _listener = null;
Expand All @@ -25,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);
Expand All @@ -43,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}]");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can already see that this is so much better than passing around everything inside IRCd

_listener.Stop();
_started = false;
}
Expand All @@ -61,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()}");
}
}
}
Expand All @@ -72,7 +74,7 @@ protected async Task<Stream> 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();
}
}
Expand Down Expand Up @@ -189,7 +191,7 @@ public async Task<SslStream> 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);
}

Expand Down
16 changes: 16 additions & 0 deletions cmpctircd/SocketListenerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +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(log, ircd, config);
}
}
}