10
10
11
11
namespace Example // Note: actual namespace depends on the project name.
12
12
{
13
-
13
+
14
14
internal class Program
15
15
{
16
16
private static Adapter ? _adapter = null ;
17
17
private static Guid _adapterGuid ;
18
18
private static NET_LUID _adapterLuid ;
19
19
20
- private static Vanara . PInvoke . IpHlpApi . MIB_IPINTERFACE_ROW _IpInterfaceRow ;
21
- private static Vanara . PInvoke . IpHlpApi . MIB_UNICASTIPADDRESS_ROW _unicastipaddressRow ;
22
- private static Vanara . PInvoke . IpHlpApi . MIB_IPFORWARD_ROW2 _ipforwardRow2 ;
20
+
23
21
24
22
private static WgConfig WgConfig = new WgConfig ( ) ;
25
-
23
+
26
24
27
25
public static void AddArch ( )
28
26
{
@@ -60,7 +58,7 @@ static void Main(string[] args)
60
58
var configPath = "client.conf" ;
61
59
var adapterName = "client" ;
62
60
var tunnelType = "client" ;
63
-
61
+
64
62
Parser . Default . ParseArguments < Options > ( args )
65
63
. WithParsed < Options > ( o =>
66
64
{
@@ -70,7 +68,7 @@ static void Main(string[] args)
70
68
Console . WriteLine ( "1.0.0" ) ;
71
69
return ;
72
70
}
73
- if ( o . ConfigPath != null && ! o . ConfigPath . Equals ( "" ) )
71
+ if ( o . ConfigPath != null && ! o . ConfigPath . Equals ( "" ) )
74
72
{
75
73
configPath = o . ConfigPath ;
76
74
}
@@ -91,15 +89,15 @@ static void Main(string[] args)
91
89
var baseName = System . IO . Path . GetDirectoryName ( Environment . ProcessPath ) ;
92
90
configPath = System . IO . Path . Combine ( baseName , configPath ) ;
93
91
}
94
-
92
+
95
93
if ( ! File . Exists ( configPath ) )
96
94
{
97
95
Console . WriteLine ( "Not Found : Config File" ) ;
98
96
return ;
99
97
}
100
-
98
+
101
99
//Get Conf File
102
-
100
+
103
101
104
102
_adapterGuid = Guid . Parse ( "{0xdeadc001,0xbeef,0xbabe,{0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef}}" ) ;
105
103
@@ -118,7 +116,7 @@ static void Main(string[] args)
118
116
var configAllLinesLine = File . ReadAllLines ( configPath ) ;
119
117
_adapter . ParseConfFile ( configAllLinesLine , out WgConfig ) ;
120
118
121
-
119
+
122
120
MIB_IPFORWARD_TABLE2 table ;
123
121
lastError = Vanara . PInvoke . IpHlpApi . GetIpForwardTable2 ( Ws2_32 . ADDRESS_FAMILY . AF_INET , out table ) ;
124
122
if ( lastError . Failed )
@@ -136,39 +134,53 @@ static void Main(string[] args)
136
134
Console . WriteLine ( "Start Delete Row [" + i + "] - Metric " + row . Metric ) ;
137
135
Vanara . PInvoke . IpHlpApi . DeleteIpForwardEntry2 ( ref table . Table [ i ] ) ;
138
136
}
139
-
140
- }
141
137
142
- //TODO: Endpoint Start
143
- Vanara . PInvoke . IpHlpApi . InitializeIpForwardEntry ( out _ipforwardRow2 ) ;
144
- _ipforwardRow2 . InterfaceLuid = _adapterLuid ;
145
- _ipforwardRow2 . NextHop . Ipv4 . sin_addr = Ws2_32 . IN_ADDR . INADDR_ANY ;
146
- _ipforwardRow2 . NextHop . si_family = Ws2_32 . ADDRESS_FAMILY . AF_INET ;
147
- _ipforwardRow2 . Metric = 0 ;
148
- _ipforwardRow2 . Protocol = MIB_IPFORWARD_PROTO . MIB_IPPROTO_LOCAL ;
149
- _ipforwardRow2 . DestinationPrefix . Prefix . Ipv4 . sin_addr = Ws2_32 . IN_ADDR . INADDR_ANY ;
150
- _ipforwardRow2 . DestinationPrefix . Prefix . si_family = Ws2_32 . ADDRESS_FAMILY . AF_INET ;
151
-
152
- lastError = Vanara . PInvoke . IpHlpApi . CreateIpForwardEntry2 ( ref _ipforwardRow2 ) ;
153
-
154
- if ( lastError . Failed )
155
- {
156
- //Failed to set default route
157
- Console . WriteLine ( "CreateIpForwardEntry2 " + lastError . ToString ( ) ) ;
158
138
}
159
- else
139
+
140
+ for ( var i = 0 ; i < WgConfig . LoctlWireGuardConfig . WgPeerConfigs . Length ; i ++ )
160
141
{
161
- Console . WriteLine ( "Set default route" + lastError . ToString ( ) ) ;
142
+ var peerConfig = WgConfig . LoctlWireGuardConfig . WgPeerConfigs [ i ] ;
143
+ Vanara . PInvoke . IpHlpApi . MIB_IPFORWARD_ROW2 row ;
144
+ Vanara . PInvoke . IpHlpApi . InitializeIpForwardEntry ( out row ) ;
145
+ row . InterfaceLuid = _adapterLuid ;
146
+
147
+ row . Metric = 1 ;
148
+
149
+ var maskedIp = IPNetwork . Parse ( "" + peerConfig . allowdIp . V4 . Addr , peerConfig . allowdIp . Cidr ) ;
150
+
151
+ row . DestinationPrefix . Prefix . Ipv4 . sin_addr = new Ws2_32 . IN_ADDR ( maskedIp . Network . GetAddressBytes ( ) ) ;
152
+ //row.DestinationPrefix.Prefix.Ipv4.sin_addr = Ws2_32.IN_ADDR.INADDR_ANY;
153
+ row . DestinationPrefix . Prefix . si_family = Ws2_32 . ADDRESS_FAMILY . AF_INET ;
154
+ row . DestinationPrefix . PrefixLength = maskedIp . Cidr ;
155
+
156
+ row . Protocol = MIB_IPFORWARD_PROTO . MIB_IPPROTO_LOCAL ;
157
+ row . NextHop . Ipv4 . sin_addr = Ws2_32 . IN_ADDR . INADDR_ANY ;
158
+ row . NextHop . si_family = Ws2_32 . ADDRESS_FAMILY . AF_INET ;
159
+
160
+ lastError = Vanara . PInvoke . IpHlpApi . CreateIpForwardEntry2 ( ref row ) ;
161
+ if ( lastError . Failed )
162
+ {
163
+ //Failed to set default route
164
+ Console . WriteLine ( "CreateIpForwardEntry2 [" + i + "] " + lastError . ToString ( ) ) ;
165
+ }
166
+ else
167
+ {
168
+ Console . WriteLine ( "Set default route [" + i + "] " + lastError . ToString ( ) ) ;
169
+ }
170
+
162
171
}
163
172
164
- Vanara . PInvoke . IpHlpApi . InitializeUnicastIpAddressEntry ( out _unicastipaddressRow ) ;
165
- _unicastipaddressRow . InterfaceLuid = _adapterLuid ;
166
- _unicastipaddressRow . Address . Ipv4 . sin_addr = new Ws2_32 . IN_ADDR ( WgConfig . InterfaceAddress . GetAddressBytes ( ) ) ;
167
- _unicastipaddressRow . Address . Ipv4 . sin_family = Ws2_32 . ADDRESS_FAMILY . AF_INET ;
168
- _unicastipaddressRow . OnLinkPrefixLength = WgConfig . InterfaceNetwork . Cidr ;
169
- _unicastipaddressRow . DadState = NL_DAD_STATE . IpDadStatePreferred ;
170
173
171
- lastError = Vanara . PInvoke . IpHlpApi . CreateUnicastIpAddressEntry ( ref _unicastipaddressRow ) ;
174
+
175
+ Vanara . PInvoke . IpHlpApi . MIB_UNICASTIPADDRESS_ROW unicastipaddressRow ;
176
+ Vanara . PInvoke . IpHlpApi . InitializeUnicastIpAddressEntry ( out unicastipaddressRow ) ;
177
+ unicastipaddressRow . InterfaceLuid = _adapterLuid ;
178
+ unicastipaddressRow . Address . Ipv4 . sin_addr = new Ws2_32 . IN_ADDR ( WgConfig . InterfaceAddress . GetAddressBytes ( ) ) ;
179
+ unicastipaddressRow . Address . Ipv4 . sin_family = Ws2_32 . ADDRESS_FAMILY . AF_INET ;
180
+ unicastipaddressRow . OnLinkPrefixLength = WgConfig . InterfaceNetwork . Cidr ;
181
+ unicastipaddressRow . DadState = NL_DAD_STATE . IpDadStatePreferred ;
182
+
183
+ lastError = Vanara . PInvoke . IpHlpApi . CreateUnicastIpAddressEntry ( ref unicastipaddressRow ) ;
172
184
if ( lastError . Failed )
173
185
{
174
186
//Failed to set IP address
@@ -178,12 +190,12 @@ static void Main(string[] args)
178
190
{
179
191
Console . WriteLine ( "Set Ip address " + lastError . ToString ( ) ) ;
180
192
}
193
+ Vanara . PInvoke . IpHlpApi . MIB_IPINTERFACE_ROW IpInterfaceRow ;
194
+ Vanara . PInvoke . IpHlpApi . InitializeIpInterfaceEntry ( out IpInterfaceRow ) ;
195
+ IpInterfaceRow . InterfaceLuid = _adapterLuid ;
196
+ IpInterfaceRow . Family = Ws2_32 . ADDRESS_FAMILY . AF_INET ;
181
197
182
- Vanara . PInvoke . IpHlpApi . InitializeIpInterfaceEntry ( out _IpInterfaceRow ) ;
183
- _IpInterfaceRow . InterfaceLuid = _adapterLuid ;
184
- _IpInterfaceRow . Family = Ws2_32 . ADDRESS_FAMILY . AF_INET ;
185
-
186
- lastError = Vanara . PInvoke . IpHlpApi . GetIpInterfaceEntry ( ref _IpInterfaceRow ) ;
198
+ lastError = Vanara . PInvoke . IpHlpApi . GetIpInterfaceEntry ( ref IpInterfaceRow ) ;
187
199
188
200
if ( lastError . Failed )
189
201
{
@@ -195,14 +207,14 @@ static void Main(string[] args)
195
207
Console . WriteLine ( "Set Ip address " + lastError . ToString ( ) ) ;
196
208
}
197
209
198
- _IpInterfaceRow . ForwardingEnabled = true ;
210
+ IpInterfaceRow . ForwardingEnabled = true ;
211
+
212
+ IpInterfaceRow . UseAutomaticMetric = false ;
213
+ IpInterfaceRow . Metric = 0 ;
214
+ IpInterfaceRow . NlMtu = WgConfig . InterfaceMtu ;
215
+ IpInterfaceRow . SitePrefixLength = 0 ;
199
216
200
- _IpInterfaceRow . UseAutomaticMetric = false ;
201
- _IpInterfaceRow . Metric = 0 ;
202
- _IpInterfaceRow . NlMtu = WgConfig . InterfaceMtu ;
203
- _IpInterfaceRow . SitePrefixLength = 0 ;
204
-
205
- lastError = Vanara . PInvoke . IpHlpApi . SetIpInterfaceEntry ( _IpInterfaceRow ) ;
217
+ lastError = Vanara . PInvoke . IpHlpApi . SetIpInterfaceEntry ( IpInterfaceRow ) ;
206
218
207
219
if ( lastError . Failed )
208
220
{
@@ -218,7 +230,7 @@ static void Main(string[] args)
218
230
{
219
231
Process . Start ( "netsh.exe" , String . Format ( "interface ipv4 add dnsservers name={0} address={1} validate=no" , adapterName , dnsAddress ) ) ;
220
232
}
221
-
233
+
222
234
_adapter . SetConfiguration ( WgConfig ) ;
223
235
_adapter . SetStateUp ( ) ;
224
236
@@ -232,11 +244,11 @@ static void Main(string[] args)
232
244
{
233
245
rx += peer . RxBytes ;
234
246
tx += peer . TxBytes ;
235
-
247
+
236
248
}
237
249
Console . WriteLine ( "rx :" + rx ) ;
238
250
Console . WriteLine ( "tx :" + tx ) ;
239
-
251
+
240
252
var state = _adapter . GetAdapterState ( ) ;
241
253
Console . WriteLine ( "state :" + state ) ;
242
254
Thread . Sleep ( 1000 ) ;
0 commit comments