Skip to content

Commit

Permalink
Merge pull request #127 from szymonos/dev
Browse files Browse the repository at this point in the history
Merge dev to main
  • Loading branch information
szymonos authored Oct 3, 2023
2 parents e3beffa + e909500 commit e42fa61
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 42 deletions.
19 changes: 15 additions & 4 deletions wsl/wsl_certs_add.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,21 @@ begin {
process {
# intercept certificates from all uris
foreach ($uri in $Uris) {
Get-Certificate -Uri $Uri -BuildChain `
| Select-Object -Skip 1 `
| ForEach-Object {
$certSet.Add($_) | Out-Null
try {
Get-Certificate -Uri $Uri -BuildChain `
| Select-Object -Skip 1 `
| ForEach-Object {
$certSet.Add($_) | Out-Null
}
} catch [System.Management.Automation.MethodInvocationException] {
if ($_.Exception.Message -match 'No such host is known') {
Write-Warning "No such host is known ($uri)."
} else {
$_.Exception.Message
}
} catch {
$_.Exception.GetType().FullName
$_
}
}
# check if root certificate from chain is in the cert store
Expand Down
37 changes: 28 additions & 9 deletions wsl/wsl_install.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
<#
.SYNOPSIS
Script synopsis.
Install and set up the specified WSL distro.
.DESCRIPTION
The script will perform the following:
- install PowerShell Core if not present to intercept TLS certificates in chain,
- enable WSL feature on Windows if not yet enabled,
- install specified WSL distro from available online distros,
- set up the specified WSL distro with sane defaults,
- can fix networkin issues on VPN by rewriting DNS settings from selected Windows network interface,
- can fix self-signed certificate in chain error, if the host is behind MITM proxy.
.PARAMETER Distro
Name of the WSL distro to install and set up.
.PARAMETER Scope
List of installation scopes. Valid values:
- az: azure-cli, do-az from ps-modules if pwsh scope specified; autoselects python scope
- docker: docker, containerd buildx docker-compose (WSL2 only)
- python: pip, venv, miniconda
.PARAMETER Repos
List of GitHub repositories in format "Owner/RepoName" to clone into the WSL.
.PARAMETER FixNetwork
Set network settings from the selected network interface in Windows.
.EXAMPLE
# :perform basic Ubuntu WSL setup
wsl/wsl_install.ps1 -Distro 'Ubuntu'
Expand All @@ -12,24 +34,21 @@ $Scope = @('az', 'docker')
wsl/wsl_install.ps1 -Distro 'Ubuntu' -s $Scope
# :set up WSL distro and clone specified GitHub repositories
$Repos = @('szymonos/linux-setup-scripts')
wsl/wsl_install.ps1 -Distro 'Ubuntu' -s $Scope -r $Repos
wsl/wsl_install.ps1 -Distro 'Ubuntu' -r $Repos -s $Scope
#>
[CmdletBinding(DefaultParameterSetName = 'Setup')]
[CmdletBinding()]
param (
[Parameter(Mandatory, Position = 0)]
[string]$Distro,

[Parameter(ParameterSetName = 'Setup')]
[Parameter(ParameterSetName = 'GitHub')]
[ValidateScript({ $_.ForEach({ $_ -in @('az', 'docker', 'python') }) -notcontains $false })]
[string[]]$Scope,

[Parameter(Mandatory, ParameterSetName = 'GitHub')]
[ValidateScript({ $_.ForEach({ $_ -match '^[\w-]+/[\w-]+$' }) -notcontains $false })]
[string[]]$Repos,

[Parameter(ParameterSetName = 'Setup')]
[Parameter(ParameterSetName = 'GitHub')]
[switch]$AddCertificate,

[switch]$FixNetwork
)

Expand Down Expand Up @@ -72,8 +91,8 @@ process {
$reposStr = $Repos | Join-Str -Separator ',' -SingleQuote
$sb.Append(" -Repos @($reposStr)") | Out-Null
}
if ($AddCertificate) { $sb.Append(" -AddCertificate") | Out-Null }
$sb.Append(" -OmpTheme 'base'") | Out-Null
$sb.Append(' -AddCertificate') | Out-Null
# run the wsl_setup script
pwsh.exe -NoProfile -Command $sb.ToString()
}
Expand Down
77 changes: 51 additions & 26 deletions wsl/wsl_network_fix.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ Flag whether to disable swap in WSL.
Flag whether to shutdown specified distro.
.PARAMETER Revert
Revert changes and set generateResolvConf to 'true'.
.PARAMETER ShowConf
Print current configuration after changes.
.EXAMPLE
$Distro = 'Ubuntu'
wsl/wsl_network_fix.ps1 $Distro
wsl/wsl_network_fix.ps1 $Distro -ShowConf
wsl/wsl_network_fix.ps1 $Distro -Shutdown
wsl/wsl_network_fix.ps1 $Distro -DisableSwap
wsl/wsl_network_fix.ps1 $Distro -Shutdown -DisableSwap
# :revert changes
wsl/wsl_network_fix.ps1 $Distro -Revert
wsl/wsl_network_fix.ps1 $Distro -Revert -ShowConf
#>

[CmdletBinding()]
Expand All @@ -33,7 +37,9 @@ param (

[switch]$Shutdown,

[switch]$Revert
[switch]$Revert,

[switch]$ShowConf
)

begin {
Expand Down Expand Up @@ -69,7 +75,7 @@ begin {
}

# instantiate string builder
$builder = [System.Text.StringBuilder]::new()
$builder = [System.Text.StringBuilder]::new("# Generated by wsl_network_fix.ps1 on $((Get-Date).ToString('s'))")
}

process {
Expand Down Expand Up @@ -99,37 +105,52 @@ process {
if (-not $Revert) {
Write-Host 'replacing resolv.conf' -ForegroundColor DarkGreen
# get DNS servers for specified interface
$netAdapters = Get-NetAdapter | Where-Object Status -EQ 'Up'
$list = for ($i = 0; $i -lt $netAdapters.Count; $i++) {
[PSCustomObject]@{
No = "[$i]"
Name = $netAdapters[$i].Name
InterfaceDescription = $netAdapters[$i].InterfaceDescription
$props = @(
@{ Name = 'Name'; Expression = { $_.InterfaceAlias } }
@{ Name = 'InterfaceDescription'; Expression = { $_.InterfaceDescription } }
@{ Name = 'IPv4Address'; Expression = { $_.IPv4Address.IPAddress } }
@{ Name = 'DNSServer'; Expression = { $_.DNSServer.Where({ $_.AddressFamily -eq 2 }).Address } }
)
$ipConfig = Get-NetIPConfiguration `
| Where-Object { $_.NetAdapter.Status -eq 'Up' } `
| Select-Object $props
if ($ipConfig) {
$list = for ($i = 0; $i -lt $ipConfig.Count; $i++) {
[PSCustomObject]@{
No = "[$i]"
Name = $ipConfig[$i].Name
InterfaceDescription = $ipConfig[$i].InterfaceDescription
IPv4Address = $ipConfig[$i].IPv4Address
DNSServer = $ipConfig[$i].DNSServer
}
}
do {
$idx = -1
$selection = Read-Host -Prompt "Please select the interface for propagating DNS Servers:`n$($list | Format-Table | Out-String)"
[bool]$returnedInt = [int]::TryParse($selection, [ref]$idx)
} until ($returnedInt -and $idx -ge 0 -and $idx -lt $netAdapters.Count)
$dnsServers = $ipConfig[$idx].DNSServer
$dnsServers.ForEach({ $builder.AppendLine("nameserver $_") | Out-Null })
}
do {
$idx = -1
$selection = Read-Host -Prompt "Please select the interface for propagating DNS Servers:`n$($list | Out-String)"
[bool]$returnedInt = [int]::TryParse($selection, [ref]$idx)
} until ($returnedInt -and $idx -ge 0 -and $idx -lt $netAdapters.Count)
$dnsServers = ($netAdapters[$idx] | Get-DnsClientServerAddress).ServerAddresses
# get DNS suffix search list
$searchSuffix = (Get-DnsClientGlobalSetting).SuffixSearchList -join ','
if ($searchSuffix) {
$builder.AppendLine("search $searchSuffix") | Out-Null
}
# get distro default gateway
$def_gtw = (wsl.exe -d $Distro -u root --exec sh -c 'ip route show default' | Select-String '(?<=via )[\d\.]+(?= dev)').Matches.Value
# build resolv.conf
$builder.AppendLine("# Generated by wsl_network_fix.ps1 on $((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))") | Out-Null
$dnsServers.ForEach({ $builder.AppendLine("nameserver $_") | Out-Null })
if ($def_gtw) {
$builder.AppendLine("nameserver $def_gtw") | Out-Null
}
if ($searchSuffix) {
$builder.AppendLine("search $searchSuffix") | Out-Null
}
$builder.AppendLine('options timeout:1 retries:1') | Out-Null
$resolvConf = $builder.ToString().Replace("`r`n", "`n")
# save resolv.conf file
$cmd = "rm -f /etc/resolv.conf || true; echo '$resolvConf' >/etc/resolv.conf && chattr -f +i /etc/resolv.conf 2>/dev/null"
$cmd = [string]::Join("`n",
'chattr -fi /etc/resolv.conf 2>/dev/null || true',
'rm -f /etc/resolv.conf 2>/dev/null || true',
"echo '$resolvConf' >/etc/resolv.conf",
'chattr -f +i /etc/resolv.conf 2>/dev/null || true'
)
wsl.exe -d $Distro --user root --exec bash -c $cmd
}

Expand All @@ -152,15 +173,19 @@ process {

# *shutdown specified distro
if ($Shutdown -or $Revert) {
wsl.exe -d $Distro --user root --exec bash -c 'chattr -fi /etc/resolv.conf 2>/dev/null'
wsl.exe -d $Distro --user root --exec bash -c 'chattr -fi /etc/resolv.conf 2>/dev/null || true'
Write-Host "shutting down '$Distro' distro" -ForegroundColor DarkGreen
wsl.exe --shutdown $Distro
}
}

end {
Write-Host "`nwsl.conf" -ForegroundColor Magenta
wsl.exe -d $Distro --exec cat /etc/wsl.conf | Write-Host
Write-Host "`nresolv.conf" -ForegroundColor Magenta
wsl.exe -d $Distro --exec cat /etc/resolv.conf | Write-Host
if ($ShowConf) {
Write-Host "`nwsl.conf" -ForegroundColor Magenta
wsl.exe -d $Distro --exec cat /etc/wsl.conf | Write-Host
Write-Host "`nresolv.conf" -ForegroundColor Magenta
wsl.exe -d $Distro --exec cat /etc/resolv.conf | Write-Host
} else {
$Revert ? 'resolv.conf configuration reverted' : 'resolv.conf configuration updated'
}
}
16 changes: 13 additions & 3 deletions wsl/wsl_systemd.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-w
Name of the WSL distro to install the certificate to.
.PARAMETER Systemd
Specify the value to true or false to enable/disable systemd accordingly in the distro.
.PARAMETER ShowConf
Print current configuration after changes.
.EXAMPLE
$Distro = 'Ubuntu'
wsl/wsl_systemd.ps1 $Distro -Systemd 'true'
wsl/wsl_systemd.ps1 $Distro -Systemd 'true' -ShowConf
wsl/wsl_systemd.ps1 $Distro -Systemd 'false'
wsl/wsl_systemd.ps1 $Distro -Systemd 'false' -ShowConf
# :check systemd services
systemctl list-units --type=service --no-pager
Expand All @@ -24,7 +28,9 @@ param (
[string]$Distro,

[ValidateSet('true', 'false')]
[string]$Systemd
[string]$Systemd,

[switch]$ShowConf
)

begin {
Expand Down Expand Up @@ -71,6 +77,10 @@ process {
}

end {
Write-Host "wsl.conf" -ForegroundColor Magenta
wsl.exe -d $Distro --exec cat /etc/wsl.conf | Write-Host
if ($ShowConf) {
Write-Host 'wsl.conf' -ForegroundColor Magenta
wsl.exe -d $Distro --exec cat /etc/wsl.conf | Write-Host
} else {
Write-Host "systemd $($Systemd -eq 'true' ? 'enabled' : 'disabled')"
}
}

0 comments on commit e42fa61

Please sign in to comment.