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

Anidb nullref fix #1147

Merged
merged 2 commits into from
Jul 19, 2024
Merged
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
38 changes: 22 additions & 16 deletions Shoko.Server/Providers/AniDB/UDP/AniDBUDPConnectionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Shoko.Server.Providers.AniDB.UDP;

#nullable enable
public class AniDBUDPConnectionHandler : ConnectionHandler, IUDPConnectionHandler
{
// 10 minutes
Expand All @@ -28,16 +29,16 @@ public class AniDBUDPConnectionHandler : ConnectionHandler, IUDPConnectionHandle
private const int PingFrequency = 45 * 1000;
private readonly IRequestFactory _requestFactory;
private readonly IConnectivityService _connectivityService;
private IAniDBSocketHandler _socketHandler;
private IAniDBSocketHandler? _socketHandler;
private static readonly Regex s_logMask = new("(?<=(\\bpass=|&pass=\\bs=|&s=))[^&]+", RegexOptions.Compiled | RegexOptions.IgnoreCase);

public event EventHandler LoginFailed;
public event EventHandler? LoginFailed;

public override double BanTimerResetLength => 1.5D;
public override string Type => "UDP";
protected override UpdateType BanEnum => UpdateType.UDPBan;

public string SessionID { get; private set; }
public string? SessionID { get; private set; }
public bool IsAlive { get; private set; }

private string _cdnDomain = Constants.URLS.AniDB_Images_Domain;
Expand All @@ -46,8 +47,8 @@ public class AniDBUDPConnectionHandler : ConnectionHandler, IUDPConnectionHandle

private ISettingsProvider SettingsProvider { get; set; }

private Timer _pingTimer;
private Timer _logoutTimer;
private Timer? _pingTimer;
private Timer? _logoutTimer;

private bool _isLoggedOn;
private bool _isInvalidSession;
Expand All @@ -61,7 +62,9 @@ public bool IsInvalidSession
_isInvalidSession = value;
UpdateState(new AniDBStateUpdate
{
UpdateType = UpdateType.InvalidSession, UpdateTime = DateTime.Now, Value = value
UpdateType = UpdateType.InvalidSession,
UpdateTime = DateTime.Now,
Value = value
});
}
}
Expand Down Expand Up @@ -110,7 +113,7 @@ public async Task<bool> Init()
await InitInternal();
return true;
}

public async Task<bool> Init(string username, string password, string serverName, ushort serverPort, ushort clientPort)
{
var settings = SettingsProvider.GetSettings();
Expand Down Expand Up @@ -150,37 +153,37 @@ private async Task InitInternal()
IsAlive = true;
}

private void PingTimerElapsed(object sender, ElapsedEventArgs e)
private void PingTimerElapsed(object? sender, ElapsedEventArgs e)
{
try
{
if (!_isLoggedOn) return;
if (_socketHandler.IsLocked || !_socketHandler.IsConnected) return;
if (_socketHandler == null || _socketHandler.IsLocked || !_socketHandler.IsConnected) return;
if (IsBanned || BackoffSecs.HasValue) return;

var ping = _requestFactory.Create<RequestPing>();
ping.Send();
}
catch (UnexpectedUDPResponseException)
{
_pingTimer.Stop();
_pingTimer?.Stop();
}
catch (AniDBBannedException)
{
_pingTimer.Stop();
_pingTimer?.Stop();
}
catch (Exception exception)
{
Logger.LogError(exception, "{Message}", exception);
}
}

private void LogoutTimerElapsed(object sender, ElapsedEventArgs e)
private void LogoutTimerElapsed(object? sender, ElapsedEventArgs e)
{
try
{
if (!_isLoggedOn) return;
if (_socketHandler.IsLocked || !_socketHandler.IsConnected) return;
if (_socketHandler == null || _socketHandler.IsLocked || !_socketHandler.IsConnected) return;
if (IsBanned || BackoffSecs.HasValue) return;

ForceLogout();
Expand Down Expand Up @@ -210,7 +213,8 @@ public async Task<string> Send(string command, bool needsUnicode = true)
{
throw new AniDBBannedException
{
BanType = UpdateType.UDPBan, BanExpires = BanTime?.AddHours(BanTimerResetLength)
BanType = UpdateType.UDPBan,
BanExpires = BanTime?.AddHours(BanTimerResetLength)
};
}
// TODO Low Priority: We need to handle Login Attempt Decay, so that we can try again if it's not just a bad user/pass
Expand Down Expand Up @@ -262,7 +266,7 @@ private async Task<string> SendInternal(string command, bool needsUnicode = true
var sendByteAdd = encoding.GetBytes(command);

var timeoutPolicy = Policy
.Handle<SocketException>(e => e is { SocketErrorCode: SocketError.TimedOut})
.Handle<SocketException>(e => e is { SocketErrorCode: SocketError.TimedOut })
.Or<OperationCanceledException>()
.RetryAsync(async (_, _) =>
{
Expand All @@ -288,7 +292,8 @@ private async Task<string> SendInternal(string command, bool needsUnicode = true
IsBanned = true;
throw new AniDBBannedException
{
BanType = UpdateType.UDPBan, BanExpires = BanTime?.AddHours(BanTimerResetLength)
BanType = UpdateType.UDPBan,
BanExpires = BanTime?.AddHours(BanTimerResetLength)
};
}

Expand All @@ -305,6 +310,7 @@ private async Task<string> SendInternal(string command, bool needsUnicode = true
if (result.FinalException != null)
{
Logger.LogError(result.FinalException, "Failed to send AniDB message");
throw result.FinalException;
}

return result.Result;
Expand Down