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

[Hosts] Duplicate check improvement #32805

Merged
merged 2 commits into from
Jun 3, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public App()
services.AddSingleton<IHostsService, HostsService>();
services.AddSingleton<IUserSettings, Hosts.Settings.UserSettings>();
services.AddSingleton<IElevationHelper, ElevationHelper>();
services.AddSingleton<IDuplicateService, DuplicateService>();

// Views and ViewModels
services.AddSingleton<ILogger, LoggerWrapper>();
Expand Down
5 changes: 2 additions & 3 deletions src/modules/Hosts/Hosts/Settings/UserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.IO.Abstractions;
using System.Threading;
using HostsUILib.Helpers;
using HostsUILib.Settings;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
Expand Down Expand Up @@ -45,6 +44,8 @@ public bool LoopbackDuplicates
// Moved from Settings.UI.Library
public HostsEncoding Encoding { get; set; }

public event EventHandler LoopbackDuplicatesChanged;

public UserSettings()
{
_settingsUtils = new SettingsUtils();
Expand All @@ -58,8 +59,6 @@ public UserSettings()
_watcher = Helper.GetFileWatcher(HostsModuleName, "settings.json", () => LoadSettingsFromJson());
}

public event EventHandler LoopbackDuplicatesChanged;

private void LoadSettingsFromJson()
{
lock (_loadingSettingsLock)
Expand Down
165 changes: 165 additions & 0 deletions src/modules/Hosts/HostsUILib/Helpers/DuplicateService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using HostsUILib.Models;
using HostsUILib.Settings;
using Microsoft.UI.Dispatching;

namespace HostsUILib.Helpers
{
public class DuplicateService : IDuplicateService, IDisposable
{
private record struct Check(string Address, string[] Hosts);

private readonly IUserSettings _userSettings;
private readonly DispatcherQueue _dispatcherQueue;
private readonly Queue<Check> _checkQueue;
private readonly ManualResetEvent _checkEvent;
private readonly Thread _queueThread;

private readonly string[] _loopbackAddresses =
{
"0.0.0.0",
"::",
"::0",
"0:0:0:0:0:0:0:0",

This comment was marked as outdated.

Copy link
Collaborator

Choose a reason for hiding this comment

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

"127.0.0.1",
"::1",
"0:0:0:0:0:0:0:1",
};

private ReadOnlyCollection<Entry> _entries;
private bool _disposed;

public DuplicateService(IUserSettings userSettings)
{
_userSettings = userSettings;

_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
_checkQueue = new Queue<Check>();
_checkEvent = new ManualResetEvent(false);

_queueThread = new Thread(ProcessQueue);
_queueThread.IsBackground = true;
_queueThread.Start();
}

public void Initialize(IList<Entry> entries)
{
_entries = entries.AsReadOnly();

if (_checkQueue.Count > 0)
{
_checkQueue.Clear();
}

foreach (var entry in _entries)
{
if (!_userSettings.LoopbackDuplicates && _loopbackAddresses.Contains(entry.Address))
{
continue;
}

_checkQueue.Enqueue(new Check(entry.Address, entry.SplittedHosts));
}

_checkEvent.Set();
}

public void CheckDuplicates(string address, string[] hosts)
{
_checkQueue.Enqueue(new Check(address, hosts));
_checkEvent.Set();
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

private void ProcessQueue()
{
while (true)
{
_checkEvent.WaitOne();

while (_checkQueue.Count > 0)
{
var check = _checkQueue.Dequeue();
FindDuplicates(check.Address, check.Hosts);
}

_checkEvent.Reset();
}
}

private void FindDuplicates(string address, string[] hosts)
{
var entries = _entries.Where(e =>
string.Equals(e.Address, address, StringComparison.OrdinalIgnoreCase)
|| hosts.Intersect(e.SplittedHosts, StringComparer.OrdinalIgnoreCase).Any());

foreach (var entry in entries)
{
SetDuplicate(entry);
}
}

private void SetDuplicate(Entry entry)
{
if (!_userSettings.LoopbackDuplicates && _loopbackAddresses.Contains(entry.Address))
{
_dispatcherQueue.TryEnqueue(() =>
{
entry.Duplicate = false;
});

return;
}

var duplicate = false;

/*
* Duplicate are based on the following criteria:
* Entries with the same type and at least one host in common
* Entries with the same type and address, except when there is only one entry with less than 9 hosts for that type and address
*/
if (_entries.Any(e => e != entry
&& e.Type == entry.Type
&& entry.SplittedHosts.Intersect(e.SplittedHosts, StringComparer.OrdinalIgnoreCase).Any()))
{
duplicate = true;
}
else if (_entries.Any(e => e != entry
&& e.Type == entry.Type
&& string.Equals(e.Address, entry.Address, StringComparison.OrdinalIgnoreCase)))
{
duplicate = entry.SplittedHosts.Length < Consts.MaxHostsCount
&& _entries.Count(e => e.Type == entry.Type
&& string.Equals(e.Address, entry.Address, StringComparison.OrdinalIgnoreCase)
&& e.SplittedHosts.Length < Consts.MaxHostsCount) > 1;
}

_dispatcherQueue.TryEnqueue(() => entry.Duplicate = duplicate);
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_checkEvent?.Dispose();
_disposed = true;
}
}
}
}
}
16 changes: 16 additions & 0 deletions src/modules/Hosts/HostsUILib/Helpers/IDuplicateService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using HostsUILib.Models;

namespace HostsUILib.Helpers
{
public interface IDuplicateService
{
void Initialize(IList<Entry> entries);

void CheckDuplicates(string address, string[] hosts);
}
}
2 changes: 1 addition & 1 deletion src/modules/Hosts/HostsUILib/Helpers/IHostsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace HostsUILib.Helpers
{
public interface IHostsService : IDisposable
public interface IHostsService
{
string HostsFilePath { get; }

Expand Down
1 change: 0 additions & 1 deletion src/modules/Hosts/HostsUILib/Settings/IUserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Net;

namespace HostsUILib.Settings
{
Expand Down