Skip to content

Commit af81943

Browse files
committed
Get default network interface dynamically
1 parent 685d65d commit af81943

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

GostGen/source/Program.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ namespace GostGen;
55
using System.Globalization;
66
using System.IO;
77
using System.Linq;
8+
using System.Net;
89
using System.Net.Http;
10+
using System.Net.NetworkInformation;
911
using System.Reflection;
1012
using System.Text.Json;
1113
using System.Text.RegularExpressions;
@@ -23,11 +25,7 @@ public class Program
2325
{
2426
private const string AutherMullvadGroup = "auther-mullvad";
2527
private const string BypassMullvadGroup = "bypass-mullvad";
26-
2728
private const string ServiceLocalName = "service-local";
28-
29-
private const string WireguardInterfaceName = "wg0-mullvad";
30-
private const string InputInterfaceName = "eth0";
3129
private const string SocksType = "socks5";
3230
private const string NetworkProtocol = "tcp";
3331

@@ -40,6 +38,7 @@ public class Program
4038
private const string PoolSelectorFailTimeout = "10s";
4139
private const SelectorStrategy PoolSelectorStrategy = SelectorStrategy.round;
4240

41+
private static readonly string InputInterfaceName = GetDefaultInterface();
4342
private static readonly Regex AddressProtRegex = new(@":(?<port>\d+)$", RegexOptions.Compiled);
4443

4544
private static LoggingLevelSwitch _logLevelSwitch = null!;
@@ -541,4 +540,15 @@ private static int FindNextFreeCityPortAreaStart(GostConfig gostConfig, string s
541540
Log.Error($"Unable to find free port area for city pool `{servicePoolName}` since last used port `{lastPort}` is to close to end of city proxy port area ({ProxyPortCitiesStart}-{ProxyPortCitiesEnd})");
542541
return -1;
543542
}
543+
544+
public static string GetDefaultInterface()
545+
{
546+
return NetworkInterface.GetAllNetworkInterfaces()
547+
.Where(i =>
548+
i.OperationalStatus == OperationalStatus.Up &&
549+
i.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
550+
i.GetIPProperties().GatewayAddresses.Any(g => !Equals(g.Address, IPAddress.Any)))
551+
.Select(i => i.Name)
552+
.FirstOrDefault() ?? "eth0";
553+
}
544554
}

0 commit comments

Comments
 (0)