Skip to content

Commit

Permalink
Refactor GetLocalAddresses()
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacyway committed May 17, 2024
1 parent 744453d commit 42e1e43
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions Fika.Core/FikaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
using Fika.Core.UI;
using Fika.Core.UI.Models;
using Fika.Core.UI.Patches;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -383,19 +384,39 @@ private void SetupConfig()

private string[] GetLocalAddresses()
{
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
List<string> ips = [];
ips.Add("Disabled");
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
ips.Add("0.0.0.0");

try
{
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
ips.Add(ip.ToString());
foreach (UnicastIPAddressInformation ip in networkInterface.GetIPProperties().UnicastAddresses)
{
if (!ip.IsDnsEligible)
{
continue;
}

if (ip.Address.AddressFamily == AddressFamily.InterNetwork)
{
string stringIp = ip.Address.ToString();
if (stringIp != "127.0.0.1")
{
ips.Add(stringIp);
}
}
}
}
}

LocalIPs = ips.Skip(1).ToArray();
return [.. ips];
LocalIPs = ips.Skip(1).ToArray();
return [.. ips];
}
catch (Exception)
{
return [.. ips];
}
}

private void DisableSPTPatches()
Expand Down

0 comments on commit 42e1e43

Please sign in to comment.