From e59cd7cf5686b573bae8e97285615d8673f398ad Mon Sep 17 00:00:00 2001 From: Ernesto Montada Romero Date: Mon, 1 Mar 2021 15:58:03 +0100 Subject: [PATCH 1/2] now with graphics --- get-interfaces.bat | 3 - install.bat | 0 run.bat | 2 +- switcher.ps1 | 431 +++++++++++++++++++++++++++------------------ 4 files changed, 257 insertions(+), 179 deletions(-) delete mode 100644 get-interfaces.bat delete mode 100644 install.bat diff --git a/get-interfaces.bat b/get-interfaces.bat deleted file mode 100644 index 30e0f6c..0000000 --- a/get-interfaces.bat +++ /dev/null @@ -1,3 +0,0 @@ -@echo off -Powershell.exe -executionpolicy remotesigned -File switcher.ps1 -interfaces -pause \ No newline at end of file diff --git a/install.bat b/install.bat deleted file mode 100644 index e69de29..0000000 diff --git a/run.bat b/run.bat index 98eecd8..3a18d33 100644 --- a/run.bat +++ b/run.bat @@ -1,2 +1,2 @@ @echo off -Powershell.exe -executionpolicy remotesigned -File switcher.ps1 -noheadless \ No newline at end of file +Powershell.exe -executionpolicy remotesigned -File switcher.ps1 \ No newline at end of file diff --git a/switcher.ps1 b/switcher.ps1 index 9662be6..21288da 100644 --- a/switcher.ps1 +++ b/switcher.ps1 @@ -1,69 +1,218 @@ -param ( - [switch] $client5, - [switch] $client2, - [switch] $interfaces, - [switch] $status, - [switch] $noheadless, - [switch] $help, - [string] $output = "" -) - -# Setup -$routerIP = "10.0.0.1" -$routerUser = "root" -$routerPassword = "iLozBkZb2CvCNz" - -# CHANGE ME -# Find interface names using switcher.ps1 -interfaces -$5ghzClientInterfaceName = "wifinet3" -$24ghzClientInterfaceName = "wifinet2" -$5ghzAPInterfaceName = "default_radio0" -$24ghzAPInterfaceName = "wifinet4" - -$coreVersion = "1.0.2" -$githubBaseUrl = "https://github.com/raw/n4y0n/wifi-swapper/master" - +#region Setup Windows Forms +Add-Type -AssemblyName System.Windows.Forms +[System.Windows.Forms.Application]::EnableVisualStyles() + +$form = New-Object system.Windows.Forms.Form +$form.ClientSize = New-Object System.Drawing.Point(966, 216) +$form.text = "WiFi Swapper." +$form.TopMost = $false + +$lip = New-Object system.Windows.Forms.Label +$lip.text = "IP:" +$lip.AutoSize = $true +$lip.width = 25 +$lip.height = 10 +$lip.location = New-Object System.Drawing.Point(30, 27) +$lip.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$lc5 = New-Object system.Windows.Forms.Label +$lc5.text = "Client 5Ghz:" +$lc5.AutoSize = $true +$lc5.width = 25 +$lc5.height = 10 +$lc5.location = New-Object System.Drawing.Point(324, 27) +$lc5.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$tip = New-Object system.Windows.Forms.TextBox +$tip.multiline = $false +$tip.width = 199 +$tip.height = 20 +$tip.location = New-Object System.Drawing.Point(100, 23) +$tip.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$tc5 = New-Object system.Windows.Forms.TextBox +$tc5.multiline = $false +$tc5.width = 186 +$tc5.height = 20 +$tc5.location = New-Object System.Drawing.Point(419, 23) +$tc5.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$lpsw = New-Object system.Windows.Forms.Label +$lpsw.text = "Password:" +$lpsw.AutoSize = $true +$lpsw.width = 25 +$lpsw.height = 10 +$lpsw.location = New-Object System.Drawing.Point(30, 62) +$lpsw.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$bint = New-Object system.Windows.Forms.Button +$bint.text = "Get Interfaces" +$bint.width = 94 +$bint.height = 35 +$bint.enabled = $true +$bint.location = New-Object System.Drawing.Point(30, 113) +$bint.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) +$bint.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("") + +$bh5 = New-Object system.Windows.Forms.Button +$bh5.text = "5GHz Hotspot" +$bh5.width = 95 +$bh5.height = 35 +$bh5.visible = $true +$bh5.enabled = $true +$bh5.location = New-Object System.Drawing.Point(30, 159) +$bh5.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$bh2 = New-Object system.Windows.Forms.Button +$bh2.text = "2.4GHz Hotspot" +$bh2.width = 96 +$bh2.height = 35 +$bh2.visible = $true +$bh2.enabled = $true +$bh2.location = New-Object System.Drawing.Point(145, 159) +$bh2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$tc2 = New-Object system.Windows.Forms.TextBox +$tc2.multiline = $false +$tc2.width = 186 +$tc2.height = 20 +$tc2.location = New-Object System.Drawing.Point(744, 23) +$tc2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$lc2 = New-Object system.Windows.Forms.Label +$lc2.text = "Client 2.4Ghz:" +$lc2.AutoSize = $true +$lc2.width = 25 +$lc2.height = 10 +$lc2.location = New-Object System.Drawing.Point(639, 27) +$lc2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$lap5 = New-Object system.Windows.Forms.Label +$lap5.text = "Hotspot 5Ghz:" +$lap5.AutoSize = $true +$lap5.width = 25 +$lap5.height = 10 +$lap5.location = New-Object System.Drawing.Point(324, 64) +$lap5.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$tap5 = New-Object system.Windows.Forms.TextBox +$tap5.multiline = $false +$tap5.width = 186 +$tap5.height = 20 +$tap5.location = New-Object System.Drawing.Point(419, 60) +$tap5.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$lap2 = New-Object system.Windows.Forms.Label +$lap2.text = "Hotspot 2.4Ghz:" +$lap2.AutoSize = $true +$lap2.width = 25 +$lap2.height = 10 +$lap2.location = New-Object System.Drawing.Point(639, 64) +$lap2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$tap2 = New-Object system.Windows.Forms.TextBox +$tap2.multiline = $false +$tap2.width = 186 +$tap2.height = 20 +$tap2.location = New-Object System.Drawing.Point(745, 60) +$tap2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$bstatus = New-Object system.Windows.Forms.Button +$bstatus.text = "Get Status" +$bstatus.width = 96 +$bstatus.height = 35 +$bstatus.visible = $true +$bstatus.enabled = $true +$bstatus.location = New-Object System.Drawing.Point(145, 112) +$bstatus.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) +$bstatus.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("") + +$save = New-Object system.Windows.Forms.Button +$save.text = "Save" +$save.width = 96 +$save.height = 35 +$save.visible = $true +$save.enabled = $true +$save.location = New-Object System.Drawing.Point(257, 113) +$save.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) +$save.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("") + +$load = New-Object system.Windows.Forms.Button +$load.text = "Load" +$load.width = 96 +$load.height = 35 +$load.visible = $true +$load.enabled = $true +$load.location = New-Object System.Drawing.Point(257, 159) +$load.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) +$load.ForeColor = [System.Drawing.ColorTranslator]::FromHtml("") + +$tpsw = New-Object system.Windows.Forms.MaskedTextBox +$tpsw.multiline = $false +$tpsw.width = 200 +$tpsw.height = 20 +$tpsw.location = New-Object System.Drawing.Point(99, 60) +$tpsw.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$log = New-Object System.Windows.Forms.TextBox +$log.multiline = $true +$log.width = 512 +$log.ScrollBars = "Vertical" +$log.height = 80 +$log.enabled = $true +$log.Anchor = 'top,right,bottom,left' +$log.location = New-Object System.Drawing.Point(418, 112) +$log.Font = New-Object System.Drawing.Font('Microsoft Sans Serif', 10) + +$form.controls.AddRange(@($lip, $lc5, $tip, $tc5, $lpsw, $bint, $bh5, $bh2, $tc2, $lc2, $lap5, $tap5, $lap2, $tap2, $bstatus, $save, $load, $tpsw, $log)) + +$bint.Add_Click( { Get-Interfaces }) +$bh5.Add_Click( { Set-Hotspot5 }) +$bh2.Add_Click( { Set-Hotspot2 }) +$bstatus.Add_Click( { Get-Status }) +$save.Add_Click( { Write-Settings }) +$load.Add_Click( { Read-Settings }) + +$tpsw.PasswordChar = '*' + +#endregion Setup Windows Forms + +#region Setup Router Interface $plink = ".\plink.exe" -$connection = "$plink -ssh $routerUser@$routerIP -pw $routerPassword -no-antispoof" - -# Functions +#endregion Setup Router Interface +#region Functions function Invoke-RemoteCommand($str) { - $command = "$connection $str" + $routerUser = "root" + $routerIP = $tip.Text + $routerPassword = $tpsw.Text + + $command = "$plink -ssh $routerUser@$routerIP -pw $routerPassword -no-antispoof $str" Invoke-Expression $command } function Read-WifiStatus($obj) { - $obj | ForEach-Object { "Section: " + $_.section + "`n`tInterface: " + $_.ifname + "`n`tMode: " + $_.config.mode + "`n`tSSID: " + $_.config.ssid } - Write-Line + $obj | ForEach-Object { + $section = $_.section + $ifname = $_.ifname + $mode = $_.config.mode + $ssid = $_.config.ssid + Write-Log ("Section: $section`r`n`tInterface: $ifname`r`n`tMode: $mode`r`n`tSSID: $ssid") + } } function Read-WifiDevices { - Invoke-RemoteCommand "uci show wireless" | Where-Object { $_.Contains("ssid") } | ForEach-Object { "Interface: " + $_.replace("wireless.", "").replace(".ssid=", " -> ") } + $result = [System.Collections.ArrayList]@() + Invoke-RemoteCommand "uci show wireless" | Where-Object { $_.Contains("ssid") } | ForEach-Object { $result.Add("Interface: " + $_.replace("wireless.", "").replace(".ssid=", " -> ")) } + return $result } -function Toggle-WifiInterface($ifname) { - $device = Invoke-RemoteCommand "`"uci get wireless.$ifname`"" 2>$null - - if ($device -ne "wifi-iface") { - Write-Line "No Interface $ifname found" - exit - } - - $disabled = Invoke-RemoteCommand "`"uci get wireless.$ifname.disabled`"" 2>$null - $name = Invoke-RemoteCommand "`"uci get wireless.$ifname.ssid`"" 2>$null +function Commit-Changes { + Invoke-RemoteCommand "`"uci commit`"" 2>$null +} - if ($disabled -eq "1") { - Enable-WifiInterface $ifname - Write-Line "Enabled $name." - } - else { - Disable-WifiInterface $ifname - Write-Line "Disabled $name." - } - - Commit-Changes - Reload-Settings +function Reload-Settings { + Invoke-RemoteCommand "`"wifi`"" 2>$null } function Disable-WifiInterface($ifname) { @@ -94,144 +243,76 @@ function Enable-WifiInterface($ifname) { } } -function Commit-Changes { - Invoke-RemoteCommand "`"uci commit`"" 2>$null -} - -function Reload-Settings { - Invoke-RemoteCommand "`"wifi`"" 2>$null -} - -function Download-Self { - Invoke-RestMethod -OutFile "./switcher.ps1" -Uri "${githubBaseUrl}/switcher.ps1" +function Write-Log { + param ($Str) + Write-Line $Str + $log.AppendText($Str + "`r`n") } -function Client2 { - Write-Line "Enabling 2.4Ghz client" - Enable-WifiInterface $24ghzClientInterfaceName - Write-Line "Disablig 2.4Ghz AP" - Disable-WifiInterface $24ghzAPInterfaceName - - Write-Line "Disablig 5Ghz client" - Disable-WifiInterface $5ghzClientInterfaceName - Write-Line "Enabling 5Ghz hotspot" - Enable-WifiInterface $5ghzAPInterfaceName - - Commit-Changes - Reload-Settings -} -function Client5 { - Write-Line "Enabling 2.4Ghz hotspot" - Enable-WifiInterface $24ghzAPInterfaceName - Write-Line "Disabling 2.4Ghz client" - Disable-WifiInterface $24ghzClientInterfaceName - - Write-Line "Enabling 5Ghz client" - Enable-WifiInterface $5ghzClientInterfaceName - Write-Line "Disablig 5Ghz hotspot" - Disable-WifiInterface $5ghzAPInterfaceName - - Commit-Changes - Reload-Settings -} - -function List-Commands { - Write-Line " -Available commands: - --client5 --client2 --interfaces --status --noheadless --help +function Write-Settings { + $ip = $tip.Text + $psw = $tpsw.Text + $c2 = $tc2.Text + $c5 = $tc5.Text + $h2 = $tap2.Text + $h5 = $tap5.Text + + $settings = [PSCustomObject]@{ + ip = $ip + password = $psw + h5g = $h5 + c5g = $c5 + h2g = $h2 + c2g = $c2 + } -" + $settings | ConvertTo-Json | Out-File -FilePath "$env:HOMEPATH\.wifi-swapper.conf" } - -function Write-Line($str) { - if ($env:SWAPPER_QUIET -ne "true") { - Write-Host $str +function Read-Settings { + if (Test-Path -Path "$env:HOMEPATH\.wifi-swapper.conf") { + $settings = Get-Content -Path "$env:HOMEPATH\.wifi-swapper.conf" | ConvertFrom-Json + + $tip.Text = $settings.ip + $tpsw.Text = $settings.password + $tc5.Text = $settings.c5g + $tc2.Text = $settings.c2g + $tap2.Text = $settings.h2g + $tap5.Text = $settings.h5g } } -# Intro - -Write-Line @' -__ ___ ______ _ _____ -\ \ / (_) ____(_) / ____| - \ \ /\ / / _| |__ _ | (_____ ____ _ _ __ _ __ ___ _ __ - \ \/ \/ / | | __| | | \___ \ \ /\ / / _` | '_ \| '_ \ / _ \ '__| - \ /\ / | | | | | ____) \ V V / (_| | |_) | |_) | __/ | - \/ \/ |_|_| |_| |_____/ \_/\_/ \__,_| .__/| .__/ \___|_| - | | | | - |_| |_| -'@ - -Write-Line " -author: @n4y0n on Github -license: MIT -page: https://github.com/n4y0n/wifi-swapper.git -=================================================== -" - -if ($env:SWAPPER_QUIET -ne "true") { - Write-Line "swapper.ps1 version ${coreVersion}" -} - -Write-Line "" - -# Commands - -if ($client5) { - Client5 -} -elseif ($client2) { - Client2 -} -elseif ($interfaces) { - Read-WifiDevices -} -elseif ($status) { +function Get-Status { $jsonStatus = (Invoke-RemoteCommand "wifi status" | ConvertFrom-Json) $Iradio0 = $jsonStatus.radio0.interfaces $Iradio1 = $jsonStatus.radio1.interfaces Read-WifiStatus $Iradio0 Read-WifiStatus $Iradio1 + Write-Log "==================" } -elseif ($help) { - List-Commands +function Set-Hotspot2 { + } -elseif ($noheadless) { - Write-Line "===================================================" - Write-Line - - Write-Line "Options: " - Write-Line "1 : 5Ghz client -> 2.4Ghz hotspot" - Write-Line "2 : 2.4Ghz client -> 5Ghz hotspot" - +function Set-Hotspot5 { - $counter = 0 - while (!$Host.UI.RawUI.KeyAvailable -and ($counter++ -lt 1000)) { - $key = $Host.UI.RawUI.ReadKey() - if ($key.Character -eq '1') { - Write-Line - Client5 - } elseif ($key.Character -eq '2') { - Write-Line - Client2 - } - - Write-Line "Done!" - Write-Line "Press any key to exit..." - [Threading.Thread]::Sleep( 1000 ) - } +} +function Get-Interfaces { + $interfaces = Read-WifiDevices + $interfaces | Where-Object { $_.GetType().Name -eq "String" } | ForEach-Object { Write-Log $_ } + Write-Log "==================" +} +function Write-Line($str) { + if ($env:SWAPPER_QUIET -ne "true") { + Write-Host $str + } } -else { - Write-Line "No command from command line found." - Write-Line "" - List-Commands -} \ No newline at end of file + +#endregion Functions + +Read-Settings + +#Write your logic code here + +[void]$form.ShowDialog() \ No newline at end of file From f721f2406eba7ed079c1784dfc2062a1b6e1f85b Mon Sep 17 00:00:00 2001 From: Ernesto Montada Romero Date: Mon, 1 Mar 2021 16:16:53 +0100 Subject: [PATCH 2/2] do --- switcher.ps1 | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/switcher.ps1 b/switcher.ps1 index 21288da..2555dd4 100644 --- a/switcher.ps1 +++ b/switcher.ps1 @@ -292,10 +292,32 @@ function Get-Status { Write-Log "==================" } function Set-Hotspot2 { - + Write-Log "Enabling 2.4Ghz hotspot" + Enable-WifiInterface $tap2.Text + Write-Log "Disabling 2.4Ghz client" + Disable-WifiInterface $tc2.Text + + Write-Log "Enabling 5Ghz client" + Enable-WifiInterface $tc5.Text + Write-Log "Disablig 5Ghz hotspot" + Disable-WifiInterface $tap5.Text + + Commit-Changes + Reload-Settings } function Set-Hotspot5 { - + Write-Log "Enabling 2.4Ghz client" + Enable-WifiInterface $tc2.Text + Write-Log "Disablig 2.4Ghz AP" + Disable-WifiInterface $tap2.Text + + Write-Log "Disablig 5Ghz client" + Disable-WifiInterface $tc5.Text + Write-Log "Enabling 5Ghz hotspot" + Enable-WifiInterface $tap5.Text + + Commit-Changes + Reload-Settings } function Get-Interfaces { $interfaces = Read-WifiDevices