11using System ;
22using System . Buffers . Binary ;
3- using System . Collections . Generic ;
43using System . ComponentModel ;
5- using System . Drawing ;
64using System . Linq ;
75using System . Net ;
86using System . Net . Sockets ;
97using System . Runtime . InteropServices ;
10- using System . Text ;
11- using System . Xml . Linq ;
128using Vanara . PInvoke ;
139using WireGuardNT_PInvoke . WireGuard ;
14- using static Vanara . PInvoke . IpHlpApi ;
1510
1611namespace WireGuardNT_PInvoke
1712{
@@ -31,7 +26,7 @@ public Adapter(string name, string tunnelType)
3126 Name = name ;
3227 TunnelType = tunnelType ;
3328 _lastGetGuess = 1024 ;
34-
29+
3530 }
3631
3732 public void Init ( ref Guid guid , out IpHlpApi . NET_LUID luid )
@@ -61,10 +56,10 @@ public void Init(ref Guid guid, out IpHlpApi.NET_LUID luid)
6156 throw new Win32Exception ( errorCode ) ;
6257 }
6358 }
64-
59+
6560 NativeFunctions . getAdapterLUID ( _handle , out luid . Value ) ;
6661
67- if ( ! NativeFunctions . setAdapterLogging ( _handle , WireGuardAdapterLoggerLevel . WIREGUARD_LOG_ON ) )
62+ if ( ! NativeFunctions . setAdapterLogging ( _handle , WireGuardAdapterLoggerLevel . WIREGUARD_LOG_ON ) )
6863 {
6964 OnEvent ( EventErrorMessage , new WireGuardErrorEventArg ( "Fail to set adapter logging : " , Marshal . GetLastWin32Error ( ) ) ) ;
7065
@@ -90,7 +85,7 @@ public WireGuardAdapterState GetAdapterState()
9085 return wireGuardAdapterState ;
9186 }
9287
93- public bool ParseConfFile ( string [ ] lines , out WgConfig wgConfig )
88+ public bool ParseConfFile ( string [ ] lines , out WgConfig wgConfig )
9489 {
9590 //Dns.GetHostEntry(host).AddressList.First(addr => addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
9691 //loctlWireGuardConfig _loctlWireGuardConfig = wgConfig.LoctlWireGuardConfig;
@@ -100,7 +95,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
10095 var peerCount = 0 ;
10196 var peerSize = 0 ;
10297 var lineNum = 0 ;
103-
98+
10499 foreach ( var line in lines )
105100 {
106101 var lineLower = line . Trim ( ) . ToLower ( ) ;
@@ -116,7 +111,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
116111 foreach ( var line in lines )
117112 {
118113 lineNum ++ ;
119-
114+
120115 if ( line . StartsWith ( "#" ) )
121116 {
122117 continue ;
@@ -126,7 +121,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
126121 {
127122 continue ;
128123 }
129-
124+
130125 if ( lineLower == "[interface]" )
131126 {
132127 IsInterFaceSection = true ;
@@ -144,14 +139,14 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
144139 if ( ! IsPeerSection && ! IsInterFaceSection )
145140 {
146141 OnEvent ( EventErrorMessage , new WireGuardErrorEventArg ( "ParseConfFile Error : No Section In conf File \n line :" , lineNum ) ) ;
147-
142+
148143 }
149144
150145 var confArray = line . Split ( '=' ) . Select ( s => s . Trim ( ) ) . ToArray ( ) ;
151146
152147 if ( confArray . Length < 1 )
153148 {
154-
149+
155150 OnEvent ( EventErrorMessage , new WireGuardErrorEventArg ( "ParseConfFile Error : No = Separator \n line :" , lineNum ) ) ;
156151 continue ;
157152 //return false;
@@ -171,7 +166,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
171166 var privateKey = Convert . FromBase64String ( value ) ;
172167 fixed ( byte * p = wgConfig . LoctlWireGuardConfig . Interfaze . PrivateKey )
173168 {
174- Marshal . Copy ( privateKey , 0 , ( IntPtr ) p , 32 ) ;
169+ Marshal . Copy ( privateKey , 0 , ( IntPtr ) p , 32 ) ;
175170 }
176171 continue ;
177172 case "listenport" :
@@ -183,7 +178,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
183178 wgConfig . InterfaceMtu = Convert . ToUInt16 ( value ) ;
184179 continue ;
185180 case "address" :
186- wgConfig . InterfaceNetwork = IPNetwork . Parse ( value ) ;
181+ wgConfig . InterfaceNetwork = IPNetwork . Parse ( value . Split ( ',' ) . First ( ) ) ;
187182 var ipStr = value . Split ( '/' ) . First ( ) . Trim ( ) ;
188183 wgConfig . InterfaceAddress = IPAddress . Parse ( ipStr ) ;
189184 continue ;
@@ -204,7 +199,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
204199 case "publickey" :
205200 wgConfig . LoctlWireGuardConfig . WgPeerConfigs [ peerCount - 1 ] . client . Flags |= IoctlPeerFlags . HasPublicKey ;
206201 var publicKey = Convert . FromBase64String ( value ) ;
207-
202+
208203 fixed ( byte * p = wgConfig . LoctlWireGuardConfig . WgPeerConfigs [ peerCount - 1 ] . client . PublicKey )
209204 {
210205 Marshal . Copy ( publicKey , 0 , ( IntPtr ) p , 32 ) ;
@@ -222,7 +217,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
222217
223218 var allowedIpTopStr = value . Split ( ',' ) . First ( ) . Trim ( ) ;
224219 var allowTopIp = IPNetwork . Parse ( allowedIpTopStr ) ;
225-
220+
226221 if ( allowTopIp . AddressFamily == AddressFamily . InterNetworkV6 )
227222 {
228223 wgConfig . LoctlWireGuardConfig . WgPeerConfigs [ peerCount - 1 ] . allowdIp . AddressFamily = Win32 . ADDRESS_FAMILY . AF_INET6 ;
@@ -251,13 +246,13 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
251246 IPEndPoint endPoint ;
252247 if ( ValidateIPv4 ( addrs [ 0 ] ) )
253248 {
254- endPoint = new IPEndPoint ( IPAddress . Parse ( addrs [ 0 ] ) , Convert . ToInt32 ( addrs [ 1 ] ) ) ;
249+ endPoint = new IPEndPoint ( IPAddress . Parse ( addrs [ 0 ] ) , Convert . ToInt32 ( addrs [ 1 ] ) ) ;
250+ }
251+ else
252+ {
253+ var ipEntry = Dns . GetHostEntry ( addrs [ 0 ] ) ;
254+ endPoint = new IPEndPoint ( ipEntry . AddressList [ 0 ] , Convert . ToInt32 ( addrs [ 1 ] ) ) ;
255255 }
256- else
257- {
258- var ipEntry = Dns . GetHostEntry ( addrs [ 0 ] ) ;
259- endPoint = new IPEndPoint ( ipEntry . AddressList [ 0 ] , Convert . ToInt32 ( addrs [ 1 ] ) ) ;
260- }
261256 if ( endPoint . AddressFamily == AddressFamily . InterNetworkV6 )
262257 {
263258 wgConfig . LoctlWireGuardConfig . WgPeerConfigs [ peerCount - 1 ] . client . Endpoint . Ipv6 . sin6_family = Win32 . ADDRESS_FAMILY . AF_INET6 ;
@@ -270,7 +265,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
270265 wgConfig . LoctlWireGuardConfig . WgPeerConfigs [ peerCount - 1 ] . client . Endpoint . Ipv4 . sin_family = Win32 . ADDRESS_FAMILY . AF_INET ;
271266 wgConfig . LoctlWireGuardConfig . WgPeerConfigs [ peerCount - 1 ] . client . Endpoint . Ipv4 . sin_port = BitConverter . IsLittleEndian ? ( ushort ) BinaryPrimitives . ReverseEndianness ( ( short ) endPoint . Port ) : ( ushort ) endPoint . Port ;
272267 wgConfig . LoctlWireGuardConfig . WgPeerConfigs [ peerCount - 1 ] . client . Endpoint . Ipv4 . sin_addr . Addr = endPoint . Address ;
273-
268+
274269 }
275270 wgConfig . LoctlWireGuardConfig . WgPeerConfigs [ peerCount - 1 ] . client . Flags |= IoctlPeerFlags . HasEndpoint ;
276271 continue ;
@@ -280,7 +275,7 @@ public bool ParseConfFile(string[] lines, out WgConfig wgConfig )
280275 continue ;
281276 }
282277 }
283-
278+
284279
285280
286281 }
@@ -335,7 +330,7 @@ public void SetConfiguration(WgConfig wgConfig)
335330 var byteCursor = 0 ;
336331 var interfaze = _loctlWireGuardConfig . Interfaze ;
337332 var interfazeSize = Marshal . SizeOf ( interfaze ) ;
338-
333+
339334
340335 totalSize += interfazeSize ;
341336 for ( var i = 0 ; i < _loctlWireGuardConfig . WgPeerConfigs . Length ; i ++ )
@@ -344,7 +339,7 @@ public void SetConfiguration(WgConfig wgConfig)
344339 var configSize = Marshal . SizeOf ( wgPeerConfig ) ;
345340 totalSize += configSize ;
346341 }
347-
342+
348343
349344 wgConfig . ConfigBuffer = new ConfigBuffer ( totalSize ) ;
350345
@@ -375,7 +370,7 @@ public void SetConfiguration(WgConfig wgConfig)
375370 }
376371 private bool SetConfiguration ( ConfigBuffer wireGuardConfig , int totalSize )
377372 {
378-
373+
379374
380375 var setConfigResult = NativeFunctions . setConfiguration ( _handle , wireGuardConfig . BufferPointer , ( uint ) totalSize ) ;
381376 if ( ! setConfigResult )
@@ -471,7 +466,7 @@ public unsafe WGInterface GetConfiguration()
471466 }
472467 else if ( ioctlAllowedIP ->AddressFamily == Win32 . ADDRESS_FAMILY . AF_INET6 )
473468 {
474-
469+
475470 //Marshal.Copy((IntPtr)ioctlAllowedIP->V6.bytes, ip, 0, 16);
476471 allowedIP . Address = ioctlAllowedIP ->V6 . Addr ;
477472 }
0 commit comments