Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Sep 18, 2024
1 parent 90a364b commit 348f913
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Robust.Client/ClientIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static void RegisterIoC(GameController.DisplayMode mode, IDependencyColle

deps.Register<IXamlProxyHelper, XamlProxyHelper>();
deps.Register<MarkupTagManager>();
deps.Register<IHWId, BasicHWid>();
deps.Register<IHWId, BasicHWId>();
}
}
}
86 changes: 86 additions & 0 deletions Robust.Client/HWId/BasicHWId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.IO;
using System.Security.Cryptography;
using Microsoft.Win32;
using Robust.Client.Utility;
using Robust.Shared.IoC;
using Robust.Shared.Network;

namespace Robust.Client.HWId;

internal sealed class BasicHWId : IHWId
{
[Dependency] private readonly IGameControllerInternal _gameController = default!;

public const int LengthHwid = 32;

public byte[] GetLegacy()
{
if (OperatingSystem.IsWindows())
return GetWindowsHWid("Hwid");

return [];
}

public byte[] GetModern()
{
byte[] raw;

if (OperatingSystem.IsWindows())
raw = GetWindowsHWid("Hwid2");
else
raw = GetFileHWid();

return [0, ..raw];
}

private static byte[] GetWindowsHWid(string keyName)
{
const string keyPath = @"HKEY_CURRENT_USER\SOFTWARE\Space Wizards\Robust";

var regKey = Registry.GetValue(keyPath, keyName, null);
if (regKey is byte[] { Length: LengthHwid } bytes)
return bytes;

var newId = new byte[LengthHwid];
RandomNumberGenerator.Fill(newId);
Registry.SetValue(
keyPath,
keyName,
newId,
RegistryValueKind.Binary);

return newId;
}

private byte[] GetFileHWid()
{
var path = UserDataDir.GetRootUserDataDir(_gameController);
var hwidPath = Path.Combine(path, ".hwid");

var value = ReadHWidFile(hwidPath);
if (value != null)
return value;

value = RandomNumberGenerator.GetBytes(LengthHwid);
File.WriteAllBytes(hwidPath, value);

return value;
}

private static byte[]? ReadHWidFile(string path)
{
try
{
var value = File.ReadAllBytes(path);
if (value.Length == LengthHwid)
return value;
}
catch (FileNotFoundException)
{
// First time the file won't exist.
}

return null;
}
}
85 changes: 0 additions & 85 deletions Robust.Client/HWId/BasicHWid.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Robust.Server/ServerIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ internal static void RegisterIoC(IDependencyCollection deps)
deps.Register<NetworkResourceManager>();
deps.Register<IHttpClientHolder, HttpClientHolder>();
deps.Register<UploadedContentManager>();
deps.Register<IHWId, DummyHWid>();
deps.Register<IHWId, DummyHWId>();
}
}
}
2 changes: 1 addition & 1 deletion Robust.Shared/CVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ protected CVars()
/// Note that modern HWIDs are only available if the connection is authenticated.
/// </para>
/// </remarks>
public static readonly CVarDef<bool> NetHwid =
public static readonly CVarDef<bool> NetHWId =
CVarDef.Create("net.hwid", true, CVar.SERVERONLY);


Expand Down
2 changes: 1 addition & 1 deletion Robust.Shared/Network/IHWId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal interface IHWId
/// <summary>
/// Implementation of <see cref="IHWId"/> that does nothing, always returning an empty result.
/// </summary>
internal sealed class DummyHWid : IHWId
internal sealed class DummyHWId : IHWId
{
public byte[] GetLegacy()
{
Expand Down
2 changes: 1 addition & 1 deletion Robust.Shared/Network/NetManager.ServerAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private async void HandleHandshake(NetPeerData peer, NetConnection connection)

var verifyToken = new byte[4];
RandomNumberGenerator.Fill(verifyToken);
var wantHwid = _config.GetCVar(CVars.NetHwid);
var wantHwid = _config.GetCVar(CVars.NetHWId);
var msgEncReq = new MsgEncryptionRequest
{
PublicKey = needPk ? CryptoPublicKey : Array.Empty<byte>(),
Expand Down
2 changes: 1 addition & 1 deletion Robust.UnitTesting/Server/RobustServerSimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public ISimulation InitializeInstance()
container.RegisterInstance<ITaskManager>(new Mock<ITaskManager>().Object);
container.Register<HttpClientHolder>();
container.Register<IHttpClientHolder, HttpClientHolder>();
container.Register<IHWId, DummyHWid>();
container.Register<IHWId, DummyHWId>();

var realReflection = new ServerReflectionManager();
realReflection.LoadAssemblies(new List<Assembly>(2)
Expand Down
2 changes: 1 addition & 1 deletion Robust.UnitTesting/Shared/GameObjects/EntityState_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void ComponentChangedSerialized()
container.Register<IConfigurationManager, ServerNetConfigurationManager>();
container.Register<IConfigurationManagerInternal, ServerNetConfigurationManager>();
container.Register<INetManager, NetManager>();
container.Register<IHWId, DummyHWid>();
container.Register<IHWId, DummyHWId>();
container.Register<IReflectionManager, ServerReflectionManager>();
container.Register<IRobustSerializer, ServerRobustSerializer>();
container.Register<IRobustMappedStringSerializer, RobustMappedStringSerializer>();
Expand Down

0 comments on commit 348f913

Please sign in to comment.