|
1 | | -param( |
2 | | - [switch] $FirewallOnly, |
3 | | - [switch] $SelfTest, |
4 | | - [switch] $ListenerSelfTest, |
5 | | - [switch] $UpdateCheckSelfTest, |
6 | | - [switch] $StartListener, |
7 | | - [int] $FirewallUdpPort = 514, |
8 | | - [int] $FirewallTcpPort = 0 |
9 | | -) |
10 | | - |
11 | | -# GW Router Logger tray application. |
12 | | -# This file keeps the listener usable without a console window and leaves the |
13 | | -# original CLI script intact for users who prefer the menu-driven terminal flow. |
| 1 | +# GW Router Logger tray support module. |
| 2 | +# This keeps tray behavior behind the main GW-Router-Logger.ps1 entrypoint so |
| 3 | +# installs only expose one app script to the user. |
14 | 4 |
|
15 | 5 | $script:AppName = 'GW Router Logger' |
16 | | -$script:Version = '1.2.1' |
| 6 | +$script:Version = '1.3.0' |
17 | 7 | $script:Publisher = 'Ghostwheel' |
18 | 8 | $script:GitHubOwner = 'GhostwheeI' |
19 | 9 | $script:GitHubRepo = 'GW-Router-Logger' |
@@ -67,6 +57,10 @@ function Get-ScriptRootPath { |
67 | 57 | return Split-Path -Parent $MyInvocation.MyCommand.Path |
68 | 58 | } |
69 | 59 |
|
| 60 | +function Get-LauncherScriptPath { |
| 61 | + return Join-Path -Path (Get-ScriptRootPath) -ChildPath 'GW-Router-Logger.ps1' |
| 62 | +} |
| 63 | + |
70 | 64 | function Get-AppDataRoot { |
71 | 65 | if (-not [string]::IsNullOrWhiteSpace($env:ProgramData)) { |
72 | 66 | $programDataPath = Join-Path -Path $env:ProgramData -ChildPath 'GW-Router-Logger' |
@@ -285,7 +279,9 @@ try { |
285 | 279 | '-ExecutionPolicy', 'Bypass', |
286 | 280 | '-File', `$installerPath, |
287 | 281 | '-InstallPath', `$InstallRoot, |
288 | | - '-ForceReinstall' |
| 282 | + '-ForceReinstall', |
| 283 | + '-InstallTrayMode', |
| 284 | + '-SkipTrayPrompt' |
289 | 285 | ) |
290 | 286 |
|
291 | 287 | if ((Test-IsProtectedInstallPath -Path `$InstallRoot) -and -not (Test-IsAdministrator)) { |
@@ -507,7 +503,7 @@ function Sync-StartupShortcut { |
507 | 503 | $wsh = New-Object -ComObject WScript.Shell |
508 | 504 | $shortcut = $wsh.CreateShortcut($shortcutPath) |
509 | 505 | $shortcut.TargetPath = "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" |
510 | | - $shortcut.Arguments = '-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -Sta -File "{0}"' -f $PSCommandPath |
| 506 | + $shortcut.Arguments = '-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -Sta -File "{0}" -TrayApp' -f (Get-LauncherScriptPath) |
511 | 507 | $shortcut.WorkingDirectory = Get-ScriptRootPath |
512 | 508 | $shortcut.Description = 'Start GW Router Logger in the notification area' |
513 | 509 | $iconPath = Get-AppIconPath |
@@ -900,7 +896,7 @@ function Add-FirewallRulesForConfig { |
900 | 896 | $args = @( |
901 | 897 | '-NoProfile', |
902 | 898 | '-ExecutionPolicy', 'Bypass', |
903 | | - '-File', ('"{0}"' -f $PSCommandPath), |
| 899 | + '-File', ('"{0}"' -f (Get-LauncherScriptPath)), |
904 | 900 | '-FirewallOnly', |
905 | 901 | '-FirewallUdpPort', ([string] $(if ([bool] $Config.UdpEnabled) { [int] $Config.UdpPort } else { 0 })), |
906 | 902 | '-FirewallTcpPort', ([string] $(if ([bool] $Config.TcpEnabled) { [int] $Config.TcpPort } else { 0 })) |
@@ -2038,75 +2034,89 @@ function Invoke-UpdateCheckSelfTest { |
2038 | 2034 | 'UPDATE_CHECK_OK {0} {1}' -f $releaseInfo.Version, $releaseInfo.AssetName |
2039 | 2035 | } |
2040 | 2036 |
|
2041 | | -if ($FirewallOnly) { |
2042 | | - Invoke-FirewallOnlyMode |
2043 | | - exit 0 |
2044 | | -} |
2045 | | - |
2046 | | -if ($SelfTest) { |
2047 | | - Invoke-SelfTest |
2048 | | - exit 0 |
2049 | | -} |
2050 | | - |
2051 | | -if ($ListenerSelfTest) { |
2052 | | - Invoke-ListenerSelfTest |
2053 | | - exit 0 |
2054 | | -} |
| 2037 | +function Start-GWRouterLoggerTrayApp { |
| 2038 | + param( |
| 2039 | + [switch] $FirewallOnly, |
| 2040 | + [switch] $SelfTest, |
| 2041 | + [switch] $ListenerSelfTest, |
| 2042 | + [switch] $UpdateCheckSelfTest, |
| 2043 | + [switch] $StartListener, |
| 2044 | + [int] $FirewallUdpPort = 514, |
| 2045 | + [int] $FirewallTcpPort = 0 |
| 2046 | + ) |
2055 | 2047 |
|
2056 | | -if ($UpdateCheckSelfTest) { |
2057 | | - Invoke-UpdateCheckSelfTest |
2058 | | - exit 0 |
2059 | | -} |
| 2048 | + if ($FirewallOnly) { |
| 2049 | + Invoke-FirewallOnlyMode |
| 2050 | + return |
| 2051 | + } |
2060 | 2052 |
|
2061 | | -$createdNew = $false |
2062 | | -$mutex = New-Object System.Threading.Mutex($true, 'Global\GW-Router-Logger-Tray', [ref] $createdNew) |
2063 | | -if (-not $createdNew) { |
2064 | | - [void] [System.Windows.Forms.MessageBox]::Show( |
2065 | | - 'GW Router Logger is already running.', |
2066 | | - $script:AppName, |
2067 | | - [System.Windows.Forms.MessageBoxButtons]::OK, |
2068 | | - [System.Windows.Forms.MessageBoxIcon]::Information |
2069 | | - ) |
2070 | | - exit 0 |
2071 | | -} |
| 2053 | + if ($SelfTest) { |
| 2054 | + Invoke-SelfTest |
| 2055 | + return |
| 2056 | + } |
2072 | 2057 |
|
2073 | | -try { |
2074 | | - [System.Windows.Forms.Application]::EnableVisualStyles() |
2075 | | - [System.Windows.Forms.Application]::SetCompatibleTextRenderingDefault($false) |
2076 | | - Get-AppConfig | Out-Null |
2077 | | - |
2078 | | - Build-ContextMenu |
2079 | | - $script:NotifyIcon = New-Object System.Windows.Forms.NotifyIcon |
2080 | | - $script:NotifyIcon.Icon = Get-AppIcon |
2081 | | - $script:NotifyIcon.Text = $script:AppName |
2082 | | - $script:NotifyIcon.ContextMenuStrip = $script:ContextMenu |
2083 | | - $script:NotifyIcon.Visible = $true |
2084 | | - |
2085 | | - $script:UiTimer = New-Object System.Windows.Forms.Timer |
2086 | | - $script:UiTimer.Interval = 1000 |
2087 | | - $script:UiTimer.Add_Tick({ Update-MenuState }) |
2088 | | - $script:UiTimer.Start() |
| 2058 | + if ($ListenerSelfTest) { |
| 2059 | + Invoke-ListenerSelfTest |
| 2060 | + return |
| 2061 | + } |
2089 | 2062 |
|
2090 | | - Update-MenuState |
2091 | | - Write-AppLog -Message ('Tray startup reached. StartListener={0}' -f [bool] $StartListener) |
2092 | | - if ($StartListener) { |
2093 | | - Start-Listener |
| 2063 | + if ($UpdateCheckSelfTest) { |
| 2064 | + Invoke-UpdateCheckSelfTest |
| 2065 | + return |
2094 | 2066 | } |
2095 | | - Write-AppLog -Message 'Tray application started.' -Diagnostic |
2096 | | - [System.Windows.Forms.Application]::Run() |
2097 | | -} |
2098 | | -finally { |
2099 | | - Stop-Listener |
2100 | | - if ($script:UiTimer) { |
2101 | | - $script:UiTimer.Stop() |
2102 | | - $script:UiTimer.Dispose() |
| 2067 | + |
| 2068 | + $createdNew = $false |
| 2069 | + $mutex = New-Object System.Threading.Mutex($true, 'Global\GW-Router-Logger-Tray', [ref] $createdNew) |
| 2070 | + if (-not $createdNew) { |
| 2071 | + [void] [System.Windows.Forms.MessageBox]::Show( |
| 2072 | + 'GW Router Logger is already running.', |
| 2073 | + $script:AppName, |
| 2074 | + [System.Windows.Forms.MessageBoxButtons]::OK, |
| 2075 | + [System.Windows.Forms.MessageBoxIcon]::Information |
| 2076 | + ) |
| 2077 | + return |
2103 | 2078 | } |
2104 | | - if ($script:NotifyIcon) { |
2105 | | - $script:NotifyIcon.Visible = $false |
2106 | | - $script:NotifyIcon.Dispose() |
| 2079 | + |
| 2080 | + try { |
| 2081 | + [System.Windows.Forms.Application]::EnableVisualStyles() |
| 2082 | + [System.Windows.Forms.Application]::SetCompatibleTextRenderingDefault($false) |
| 2083 | + Get-AppConfig | Out-Null |
| 2084 | + |
| 2085 | + Build-ContextMenu |
| 2086 | + $script:NotifyIcon = New-Object System.Windows.Forms.NotifyIcon |
| 2087 | + $script:NotifyIcon.Icon = Get-AppIcon |
| 2088 | + $script:NotifyIcon.Text = $script:AppName |
| 2089 | + $script:NotifyIcon.ContextMenuStrip = $script:ContextMenu |
| 2090 | + $script:NotifyIcon.Visible = $true |
| 2091 | + |
| 2092 | + $script:UiTimer = New-Object System.Windows.Forms.Timer |
| 2093 | + $script:UiTimer.Interval = 1000 |
| 2094 | + $script:UiTimer.Add_Tick({ Update-MenuState }) |
| 2095 | + $script:UiTimer.Start() |
| 2096 | + |
| 2097 | + Update-MenuState |
| 2098 | + Write-AppLog -Message ('Tray startup reached. StartListener={0}' -f [bool] $StartListener) |
| 2099 | + if ($StartListener) { |
| 2100 | + Start-Listener |
| 2101 | + } |
| 2102 | + Write-AppLog -Message 'Tray application started.' -Diagnostic |
| 2103 | + [System.Windows.Forms.Application]::Run() |
2107 | 2104 | } |
2108 | | - if ($mutex) { |
2109 | | - $mutex.ReleaseMutex() |
2110 | | - $mutex.Dispose() |
| 2105 | + finally { |
| 2106 | + Stop-Listener |
| 2107 | + if ($script:UiTimer) { |
| 2108 | + $script:UiTimer.Stop() |
| 2109 | + $script:UiTimer.Dispose() |
| 2110 | + } |
| 2111 | + if ($script:NotifyIcon) { |
| 2112 | + $script:NotifyIcon.Visible = $false |
| 2113 | + $script:NotifyIcon.Dispose() |
| 2114 | + } |
| 2115 | + if ($mutex) { |
| 2116 | + $mutex.ReleaseMutex() |
| 2117 | + $mutex.Dispose() |
| 2118 | + } |
2111 | 2119 | } |
2112 | 2120 | } |
| 2121 | + |
| 2122 | +Export-ModuleMember -Function Start-GWRouterLoggerTrayApp |
0 commit comments