Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #40 from NickvisionApps/no-net-check
Browse files Browse the repository at this point in the history
AURA_DISABLE_NETCHECK
  • Loading branch information
nlogozzo authored Nov 8, 2023
2 parents 335bbbb + 044dd38 commit 6db68be
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Nickvision.Aura.Tests/NetworkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class NetworkTest
[SkippableFact]
public async Task NetworkAccessTest()
{
Environment.SetEnvironmentVariable("AURA_DISABLE_NETCHECK", "true");
var netmon = await NetworkMonitor.NewAsync();
Skip.If(netmon == null);
Assert.True(await netmon.GetStateAsync());
}

Expand Down
37 changes: 24 additions & 13 deletions Nickvision.Aura/Network/NetworkMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Nickvision.Aura.Network;
public class NetworkMonitor : IDisposable
{
private bool _disposed;
private bool _noNetCheck;
private Connection? _dbusConnection;
private INetworkMonitor? _networkMonitorDBus;
private string[]? _networkAddresses;
Expand All @@ -28,6 +29,7 @@ public class NetworkMonitor : IDisposable
private NetworkMonitor()
{
_disposed = false;
_noNetCheck = false;
}

/// <summary>
Expand All @@ -37,25 +39,30 @@ private NetworkMonitor()
public static async Task<NetworkMonitor> NewAsync()
{
var netmon = new NetworkMonitor();
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
var noNetCheckVar = (Environment.GetEnvironmentVariable("AURA_DISABLE_NETCHECK") ?? "").ToLower();
netmon._noNetCheck = !string.IsNullOrWhiteSpace(noNetCheckVar) && (noNetCheckVar == "true" || noNetCheckVar == "1" || noNetCheckVar == "t" || noNetCheckVar == "yes" || noNetCheckVar == "y");
if(!netmon._noNetCheck)
{
try
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
netmon._dbusConnection = new Connection(Address.Session);
await netmon._dbusConnection.ConnectAsync();
netmon._networkMonitorDBus = netmon._dbusConnection.CreateProxy<INetworkMonitor>("org.freedesktop.portal.Desktop", new ObjectPath("/org/freedesktop/portal/desktop"));
await netmon._networkMonitorDBus.WatchchangedAsync(async () => netmon._stateChanged?.Invoke(netmon, await netmon._networkMonitorDBus.GetAvailableAsync()));
try
{
netmon._dbusConnection = new Connection(Address.Session);
await netmon._dbusConnection.ConnectAsync();
netmon._networkMonitorDBus = netmon._dbusConnection.CreateProxy<INetworkMonitor>("org.freedesktop.portal.Desktop", new ObjectPath("/org/freedesktop/portal/desktop"));
await netmon._networkMonitorDBus.WatchchangedAsync(async () => netmon._stateChanged?.Invoke(netmon, await netmon._networkMonitorDBus.GetAvailableAsync()));
}
catch
{
netmon._networkMonitorDBus = null;
netmon.SetupPing();
}
}
catch
else
{
netmon._networkMonitorDBus = null;
netmon.SetupPing();
}
}
else
{
netmon.SetupPing();
}
return netmon;
}

Expand Down Expand Up @@ -106,9 +113,13 @@ protected virtual void Dispose(bool disposing)
/// <summary>
/// Get current network state
/// </summary>
/// <returns>True if internet connection, else false</returns>
/// <returns>True if internet connection available, else false</returns>
public async Task<bool> GetStateAsync()
{
if(_noNetCheck)
{
return true;
}
if (_networkMonitorDBus != null)
{
return await _networkMonitorDBus.GetAvailableAsync();
Expand Down
4 changes: 2 additions & 2 deletions Nickvision.Aura/Nickvision.Aura.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>Nickvision.Aura</PackageId>
<Version>2023.11.1</Version>
<Version>2023.11.2</Version>
<Company>Nickvision</Company>
<Authors>Nickvision</Authors>
<Description>A cross-platform base for Nickvision applications</Description>
Expand All @@ -14,7 +14,7 @@
<Copyright>(c) Nickvision 2021-2023</Copyright>
<PackageProjectUrl>https://nickvision.org</PackageProjectUrl>
<RepositoryUrl>https://github.com/NickvisionApps/Aura</RepositoryUrl>
<PackageReleaseNotes>- Fixed an issue where Updater was rate limited</PackageReleaseNotes>
<PackageReleaseNotes>- If the AURA_DISABLE_NETCHECK env var is set, NetworkMonitor will always return true when checking connectivity state</PackageReleaseNotes>
<PackageIcon>logo-r.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down

0 comments on commit 6db68be

Please sign in to comment.