Skip to content
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
82 changes: 26 additions & 56 deletions Framework/Intersect.Framework.Core/Config/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ namespace Intersect;

public partial record Options
{
private static readonly JsonSerializerSettings PrivateIndentedSerializerSettings = new()
{
ContractResolver = new OptionsContractResolver(true, false),
Formatting = Formatting.Indented,
};

private static readonly JsonSerializerSettings PrivateSerializerSettings = new()
{
ContractResolver = new OptionsContractResolver(true, false),
};

private static readonly JsonSerializerSettings PublicSerializerSettings = new()
{
ContractResolver = new OptionsContractResolver(false, true),
};

#region Constants

public const string DefaultGameName = "Intersect";
Expand Down Expand Up @@ -48,10 +64,6 @@ public partial record Options
[JsonIgnore]
public string OptionsData { get; private set; } = string.Empty;

[Ignore]
[JsonIgnore]
public bool SendingToClient { get; set; } = true;

[Ignore]
public bool SmtpValid { get; private set; }

Expand Down Expand Up @@ -235,7 +247,8 @@ public static bool LoadFromDisk()
}
else if (File.Exists(pathToServerConfig))
{
instance = JsonConvert.DeserializeObject<Options>(File.ReadAllText(pathToServerConfig)) ?? instance;
var rawJson = File.ReadAllText(pathToServerConfig);
instance = JsonConvert.DeserializeObject<Options>(rawJson, PrivateSerializerSettings) ?? instance;
Instance = instance;
}

Expand Down Expand Up @@ -269,13 +282,10 @@ public static void SaveToDisk()

var pathToServerConfig = Path.Combine(ResourcesDirectory, "config.json");

instance.SendingToClient = false;
try
{
File.WriteAllText(
pathToServerConfig,
JsonConvert.SerializeObject(instance, Formatting.Indented)
);
var serializedPrivateConfiguration = JsonConvert.SerializeObject(instance, PrivateIndentedSerializerSettings);
File.WriteAllText(pathToServerConfig, serializedPrivateConfiguration);
}
catch (Exception exception)
{
Expand All @@ -285,15 +295,15 @@ public static void SaveToDisk()
pathToServerConfig
);
}
instance.SendingToClient = true;
instance.OptionsData = JsonConvert.SerializeObject(instance);

instance.OptionsData = JsonConvert.SerializeObject(instance, PublicSerializerSettings);
}

public static void LoadFromServer(string data)
{
try
{
var loadedOptions = JsonConvert.DeserializeObject<Options>(data);
var loadedOptions = JsonConvert.DeserializeObject<Options>(data, PublicSerializerSettings);
Instance = loadedOptions;
OptionsLoaded?.Invoke(loadedOptions);
}
Expand All @@ -306,47 +316,7 @@ public static void LoadFromServer(string data)

public static event OptionsLoadedEventHandler? OptionsLoaded;

// ReSharper disable once UnusedMember.Global
public bool ShouldSerializeGameDatabase()
{
return !SendingToClient;
}

// ReSharper disable once UnusedMember.Global
public bool ShouldSerializeLoggingDatabase()
{
return !SendingToClient;
}

// ReSharper disable once UnusedMember.Global
public bool ShouldSerializeLogging()
{
return !SendingToClient;
}

// ReSharper disable once UnusedMember.Global
public bool ShouldSerializePlayerDatabase()
{
return !SendingToClient;
}

// ReSharper disable once UnusedMember.Global
public bool ShouldSerializeSmtpSettings()
{
return !SendingToClient;
}

// ReSharper disable once UnusedMember.Global
public bool ShouldSerializeSmtpValid()
{
return SendingToClient;
}

// ReSharper disable once UnusedMember.Global
public bool ShouldSerializeSecurity()
{
return !SendingToClient;
}

public Options DeepClone() => JsonConvert.DeserializeObject<Options>(JsonConvert.SerializeObject(this with { SendingToClient = false }));
public Options DeepClone() => JsonConvert.DeserializeObject<Options>(
JsonConvert.SerializeObject(this, PrivateSerializerSettings)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Intersect;

public sealed class OptionsContractResolver(bool serializePrivateProperties, bool serializePublicProperties) : DefaultContractResolver
{
private readonly bool _serializePrivateProperties = serializePrivateProperties;
private readonly bool _serializePublicProperties = serializePublicProperties;

private static readonly HashSet<PropertyInfo> PrivateProperties =
[
typeof(Options).GetProperty(nameof(Options.AdminOnly)),
typeof(Options).GetProperty(nameof(Options.EventWatchdogKillThreshold)),
typeof(Options).GetProperty(nameof(Options.GameDatabase)),
typeof(Options).GetProperty(nameof(Options.Logging)),
typeof(Options).GetProperty(nameof(Options.LoggingDatabase)),
typeof(Options).GetProperty(nameof(Options.MaxClientConnections)),
typeof(Options).GetProperty(nameof(Options.MaximumLoggedInUsers)),
typeof(Options).GetProperty(nameof(Options.Metrics)),
typeof(Options).GetProperty(nameof(Options.OpenPortChecker)),
typeof(Options).GetProperty(nameof(Options.PlayerDatabase)),
typeof(Options).GetProperty(nameof(Options.PortCheckerUrl)),
typeof(Options).GetProperty(nameof(Options.Security)),
typeof(Options).GetProperty(nameof(Options.ServerPort)),
typeof(Options).GetProperty(nameof(Options.SmtpSettings)),
typeof(Options).GetProperty(nameof(Options.UPnP)),
typeof(Options).GetProperty(nameof(Options.ValidPasswordResetTimeMinutes)),
];

private static readonly HashSet<PropertyInfo> PublicProperties =
[
typeof(Options).GetProperty(nameof(Options.SmtpValid)),
];

protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);

if (PrivateProperties.Contains(member))
{
property.ShouldDeserialize = AlwaysSerialize;
property.ShouldSerialize = ShouldSerializePrivateProperty;
property.Writable = true;
}

if (PublicProperties.Contains(member))
{
property.ShouldDeserialize = AlwaysSerialize;
property.ShouldSerialize = ShouldSerializePublicProperty;
property.Writable = true;
}

return property;
}

private static bool AlwaysSerialize(object _) => true;

private bool ShouldSerializePublicProperty(object _) => _serializePublicProperties;

private bool ShouldSerializePrivateProperty(object _) => _serializePrivateProperties;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using MessagePack;

namespace Intersect.Network.Packets.Client;

[MessagePackObject]
public partial class PasswordChangeRequestPacket : IntersectPacket
{
//Parameterless Constructor for MessagePack
public PasswordChangeRequestPacket()
{
}

public PasswordChangeRequestPacket(string identifier, string token, string password)
{
Identifier = identifier;
Token = token;
Password = password;
}

[Key(0)]
public string? Identifier { get; set; }

[Key(1)]
public string? Token { get; set; }

[Key(2)]
public string? Password { get; set; }

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Intersect.Network.Packets.Client;

[MessagePackObject]
public partial class CreateAccountPacket : IntersectPacket
public partial class UserRegistrationRequestPacket : IntersectPacket
{
//Parameterless Constructor for MessagePack
public CreateAccountPacket()
public UserRegistrationRequestPacket()
{
}

public CreateAccountPacket(string username, string password, string email)
public UserRegistrationRequestPacket(string username, string password, string email)
{
Username = username;
Password = password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ public CharactersPacket()
{
}

public CharactersPacket(CharacterPacket[] characters, bool freeSlot)
public CharactersPacket(string username, CharacterPacket[] characters, bool freeSlot)
{
Username = username;
Characters = characters;
FreeSlot = freeSlot;
}

[Key(0)]
public CharacterPacket[] Characters { get; set; }
public string Username { get; set; }

[Key(1)]
public CharacterPacket[] Characters { get; set; }

[Key(2)]
public bool FreeSlot { get; set; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Intersect.Framework.Core.Security;
using MessagePack;

namespace Intersect.Network.Packets.Server;

[MessagePackObject]
public partial class PasswordChangeResultPacket : IntersectPacket
{

//Parameterless Constructor for MessagePack
public PasswordChangeResultPacket()
{
}

public PasswordChangeResultPacket(PasswordResetResultType succeeded)
{
ResultType = succeeded;
}

[Key(0)]
public PasswordResetResultType ResultType { get; set; }

}

This file was deleted.

4 changes: 4 additions & 0 deletions Framework/Intersect.Framework.Core/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public static Point FromString(string val)

public static Point operator +(Point left, Point right) => new(left.X + right.X, left.Y + right.Y);

public static Vector2 operator +(Point left, Vector2 right) => new(left.X + right.X, left.Y + right.Y);

public static Vector2 operator +(Vector2 left, Point right) => new(left.X + right.X, left.Y + right.Y);

public static Point operator -(Point left, Point right) => new(left.X - right.X, left.Y - right.Y);

public static Point operator *(Point point, float scalar) => new((int)(point.X * scalar), (int)(point.Y * scalar));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Intersect.Framework.Core.Security;

public enum PasswordResetResultType
{
Unknown,
Success,
NoUserFound,
InvalidRequest,
InvalidToken,
}
6 changes: 4 additions & 2 deletions Intersect (Core)/Security/PasswordUtils.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.Text;

namespace Intersect.Security;

public partial class PasswordUtils
{
public static string ComputePasswordHash(string password)
[return: NotNull]
public static string ComputePasswordHash(string? password)
{
return BitConverter.ToString(SHA256.HashData(Encoding.UTF8.GetBytes(password ?? string.Empty))).Replace("-", string.Empty);
}

public static bool IsValidClientPasswordHash(string? hashToValidate) =>
public static bool IsValidClientPasswordHash([NotNullWhen(true)] string? hashToValidate) =>
hashToValidate is { Length: 64 } && hashToValidate.All(char.IsAsciiHexDigit);
}
Loading
Loading