@@ -21,6 +21,10 @@ $LogfileName = "$RegionName-AzureSimpleS2S-$(Get-Date -Format 'yyyy-MM-dd_Thh-mm
21
21
Try {Start-transcript " $PSScriptRoot \Logs\$LogfileName " - ErrorAction Stop}catch {Start-Transcript " $PSScriptRoot \$LogfileName " }
22
22
# endregion
23
23
24
+ # Make it a global variable so it used for the entire session
25
+ # TEST $Global:BasicPssKey='bB8u6Tj60uJL2RKYR0OCyiGMdds9gaEUs9Q2d3bRTTVRKJ516CCc1LeSMChAI0rc'
26
+ If (! $Global :BasicPssKey ){$Global :BasicPssKey = New-SharedPSKey }
27
+
24
28
# grab external interface for VyOS router
25
29
If ($null -ne $VyOSConfig.ExternalInterfaceIP ){
26
30
$VyOSExternalIP = $VyOSConfig.ExternalInterfaceIP
@@ -255,7 +259,7 @@ Elseif( $null -eq $currentGwConnection)
255
259
# Create the connection
256
260
New-AzVirtualNetworkGatewayConnection - Name $AzureSimpleConfig.ConnectionName - ResourceGroupName $AzureSimpleConfig.ResourceGroupName `
257
261
- Location $AzureSimpleConfig.LocationName - VirtualNetworkGateway1 $gateway1 - LocalNetworkGateway2 $Local `
258
- - ConnectionType IPsec - RoutingWeight 10 - SharedKey $sharedPSKKey - Force | Out-Null
262
+ - ConnectionType IPsec - RoutingWeight 10 - SharedKey $Global :BasicPssKey - Force | Out-Null
259
263
Write-Host " Done" - ForegroundColor Green
260
264
}
261
265
Catch {
@@ -274,7 +278,7 @@ Else{
274
278
If ( ($response1 -eq ' Y' ) -or ($VyOSConfig [' ResetVPNConfigs' ] -eq $true ) )
275
279
{
276
280
Write-Host (" Attempting to update vyos router vpn configurations to use Azure's public IP [{0}]..." -f $azpip.IpAddress ) - ForegroundColor Yellow
277
- $Global :sharedPSKKey = Get-AzVirtualNetworkGatewayConnectionSharedKey - Name $AzureSimpleConfig.ConnectionName - ResourceGroupName $AzureSimpleConfig.ResourceGroupName
281
+ $Global :BasicPssKey = Get-AzVirtualNetworkGatewayConnectionSharedKey - Name $AzureSimpleConfig.ConnectionName - ResourceGroupName $AzureSimpleConfig.ResourceGroupName
278
282
$VyOSConfig [' ResetVPNConfigs' ] = $true
279
283
}
280
284
Else {
@@ -324,7 +328,7 @@ set vpn ipsec ike-group azure-ike proposal 1 hash 'sha1'
324
328
set vpn ipsec ipsec-interfaces interface 'eth0'
325
329
set vpn ipsec nat-traversal 'enable'
326
330
set vpn ipsec site-to-site peer $ ( $azpip.IpAddress ) authentication mode 'pre-shared-secret'
327
- set vpn ipsec site-to-site peer $ ( $azpip.IpAddress ) authentication pre-shared-secret '$Global :sharedPSKKey '
331
+ set vpn ipsec site-to-site peer $ ( $azpip.IpAddress ) authentication pre-shared-secret '$ ( $ Global :BasicPssKey ) '
328
332
set vpn ipsec site-to-site peer $ ( $azpip.IpAddress ) connection-type 'initiate'
329
333
set vpn ipsec site-to-site peer $ ( $azpip.IpAddress ) default-esp-group 'azure'
330
334
set vpn ipsec site-to-site peer $ ( $azpip.IpAddress ) description '$ ( $AzureSimpleConfig.TunnelDescription ) '
@@ -340,20 +344,32 @@ set vpn ipsec site-to-site peer $($azpip.IpAddress) tunnel 1 remote prefix '$($A
340
344
set protocols static route 0.0.0.0/0 next-hop '$ ( $VyOSConfig.NextHopSubnet ) '
341
345
"@
342
346
343
- If ( $ VyOSConfig.ResetVPNConfigs ){
347
+ foreach ( $SubnetCIDR in $ VyOSConfig.LocalSubnetPrefix.GetEnumerator () | Sort Name ){
344
348
$VyOSFinal += @"
345
349
`n
346
- reset vpn ipsec-peer $ ( $azpip.IpAddress ) tunnel 1
350
+ set protocols static route ' $ ( $SubnetCIDR .Name ) ' next-hop ' $ ( $azpip.IpAddress ) '
347
351
"@
348
352
}
349
353
350
- foreach ($SubnetCIDR in $VyOSConfig.LocalSubnetPrefix.GetEnumerator () | Sort Name){
354
+ foreach ($vNetPrefix in $vNet.AddressSpace.AddressPrefixes ){
355
+ # use the last octet of network id as the rule id (keeps it unique)
356
+ $RuleID = ((Get-NetworkDetails - CidrAddress $vNetPrefix ).NetworkID -replace ' \.0' , ' ' ).split(' .' )[-1 ]
357
+ If ( ($RuleID -eq 10 ) -or ($RuleID -eq 100 ) ){$RuleID ++ }
351
358
$VyOSFinal += @"
352
359
`n
353
- set protocols static route '$ ( $SubnetCIDR.Name ) ' next-hop '$ ( $azpip.IpAddress ) '
360
+ set nat source rule $ ( $RuleID ) destination address '$ ( $vNetPrefix ) '
361
+ set nat source rule $ ( $RuleID ) exclude
362
+ set nat source rule $ ( $RuleID ) outbound-interface 'eth0'
363
+ set nat source rule $ ( $RuleID ) source address '$ ( $VyOSConfig.LocalCIDRPrefix ) '
354
364
"@
355
365
}
356
366
367
+ If ($VyOSConfig.ResetVPNConfigs ){
368
+ $VyOSFinal += @"
369
+ `n
370
+ reset vpn ipsec-peer $ ( $azpip.IpAddress ) tunnel 1
371
+ "@
372
+ }
357
373
358
374
$VyOSFinal += @"
359
375
@@ -453,7 +469,7 @@ public class TrustAllCertsPolicy : ICertificatePolicy {
453
469
$response2 = Read-host " Would you like to attempt to reset the VPN connection? [Y or N]"
454
470
If ($response2 -eq ' Y' ){
455
471
Set-AzVirtualNetworkGatewayConnectionSharedKey - Name $AzureSimpleConfig.ConnectionName `
456
- - ResourceGroupName $AzureSimpleConfig.ResourceGroupName - Value $Global :sharedPSKKey - Force | Out-Null
472
+ - ResourceGroupName $AzureSimpleConfig.ResourceGroupName - Value $Global :BasicPssKey - Force | Out-Null
457
473
458
474
Reset-AzVirtualNetworkGatewayConnection - Name $AzureSimpleConfig.ConnectionName `
459
475
- ResourceGroupName $AzureSimpleConfig.ResourceGroupName - Force | Out-Null
@@ -485,22 +501,23 @@ If($RunManualSteps)
485
501
Write-Host (" Azure Location: {0}" -f $AzureSimpleConfig.LocationName )
486
502
Write-Host (" Azure Public IP: {0}" -f $azpip.IpAddress )
487
503
Write-Host (" Azure Subnet Prefix: {0}" -f $AzureSimpleConfig.VnetSubnetPrefix )
488
- Write-host (" Shared Key (PSK): {0}" -f $Global :sharedPSKKey )
504
+ Write-host (" Shared Key (PSK): {0}" -f $Global :BasicPssKey )
489
505
Write-host (" Home Public IP: {0}" -f $HomePublicIP )
490
506
Write-Host (" Router CIDR Prefix: {0}" -f $VyOSConfig.LocalCIDRPrefix )
491
- Write-Host " Be sure to follow a the configuration file: '$PSScriptRoot \Logs\$ScriptName '`n " - ForegroundColor Yellow
492
507
493
508
# region Copy Paste Mode
494
- Write-Host " `n Open ssh session for $ ( $VyOSConfig.VMName ) :`n " - ForegroundColor Yellow
495
- Write-Host " Copy script below line or from $PSScriptRoot \Logs\$ScriptName " - ForegroundColor Yellow
496
509
Write-Host " --------------------------------------------------------" - ForegroundColor Yellow
497
510
Write-Host $VyOSFinal - ForegroundColor Gray
498
511
Write-Host " --------------------------------------------------------" - ForegroundColor Yellow
499
- Write-Host " Stop copying above line this and paste in ssh session" - ForegroundColor Yellow
512
+ Write-Host " `n Open ssh session for $ ( $VyOSConfig.VMName ) by running command [" - ForegroundColor White - NoNewline
513
+ Write-Host (" ssh vyos@{0}" -f $VyOSExternalIP ) - ForegroundColor Yellow - NoNewline
514
+ Write-Host " ]" - ForegroundColor White
515
+ Write-Host " Then copy the script between the lines or `n from $PSScriptRoot \Logs\$ScriptName " - ForegroundColor White
500
516
Write-Host " `n A reboot may be required on $ ( $VyOSConfig.VMName ) for updates to take effect" - ForegroundColor Red
501
- Write-Host " Log into router and run [" - ForegroundColor Gray - NoNewline
517
+ Write-Host " In router's ssh session, run command [" - ForegroundColor Gray - NoNewline
502
518
Write-Host " reboot now" - ForegroundColor Yellow - NoNewline
503
- Write-Host " ]" - ForegroundColor Gray
519
+ Write-Host " ] to reboot" - ForegroundColor Gray
520
+ # endregion
504
521
}
505
522
506
523
Stop-Transcript
0 commit comments