Skip to content

Commit dce783e

Browse files
committed
FIX: Dns error
1 parent 0850195 commit dce783e

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

WireGuardNT-PInvoke/Adapter.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public WireGuardAdapterState GetAdapterState()
9292

9393
public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
9494
{
95-
//TODO: Resolve EndPoint, Set DNS
9695
//Dns.GetHostEntry(host).AddressList.First(addr => addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
9796
//loctlWireGuardConfig _loctlWireGuardConfig = wgConfig.LoctlWireGuardConfig;
9897
wgConfig = new WgConfig();
@@ -109,11 +108,6 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
109108
{
110109
peerSize++;
111110
}
112-
if (lineLower.StartsWith("endpoint"))
113-
{
114-
//TODO: support endpoints
115-
//List<IPNetwork> allowedIps = new List<IPNetwork>();
116-
}
117111
}
118112

119113

@@ -125,7 +119,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
125119

126120
if (line.StartsWith("#"))
127121
{
128-
continue; ;
122+
continue;
129123
}
130124
var lineLower = line.Trim().ToLower();
131125
if (lineLower.Length == 0)
@@ -255,15 +249,15 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
255249
var addrs = value.Split(':');
256250
//check dns
257251
IPEndPoint endPoint;
258-
try
259-
{
260-
var ipEntry = Dns.GetHostEntry(addrs[0]);
261-
endPoint = new IPEndPoint(ipEntry.AddressList[0], Convert.ToInt32(addrs[1]));
262-
}
263-
catch
252+
if (ValidateIPv4(addrs[0]))
264253
{
265-
endPoint = new IPEndPoint(IPAddress.Parse(addrs[0]), Convert.ToInt32(addrs[1]));
254+
endPoint = new IPEndPoint(IPAddress.Parse(addrs[0]), Convert.ToInt32(addrs[1]));
266255
}
256+
else
257+
{
258+
var ipEntry = Dns.GetHostEntry(addrs[0]);
259+
endPoint = new IPEndPoint(ipEntry.AddressList[0], Convert.ToInt32(addrs[1]));
260+
}
267261
if (endPoint.AddressFamily == AddressFamily.InterNetworkV6)
268262
{
269263
wgConfig.LoctlWireGuardConfig.WgPeerConfigs[peerCount - 1].client.Endpoint.Ipv6.sin6_family = Win32.ADDRESS_FAMILY.AF_INET6;
@@ -313,7 +307,26 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
313307
}
314308
return true;
315309
}
316-
310+
public bool ValidateIPv4(string ipString)
311+
{
312+
if (String.IsNullOrWhiteSpace(ipString))
313+
{
314+
return false;
315+
}
316+
317+
string[] splitValues = ipString.Split('.');
318+
if (splitValues.Length != 4)
319+
{
320+
return false;
321+
}
322+
323+
byte tempForParsing;
324+
325+
return splitValues.All(r => byte.TryParse(r, out tempForParsing));
326+
327+
}
328+
329+
317330
public void SetConfiguration(WgConfig wgConfig)
318331
{
319332
var _loctlWireGuardConfig = wgConfig.LoctlWireGuardConfig;

0 commit comments

Comments
 (0)