$ErrorActionPreference = 'Stop' $ProxyHostName = 'hproxy.local' $ProxyPort = 12334 $HttpProxy = "http://${ProxyHostName}:${ProxyPort}" $SocksProxy = "socks5://${ProxyHostName}:${ProxyPort}" $NoProxy = "${ProxyHostName},localhost,127.0.0.1,::1,[::1],.concept-software.ru" $ProxyBypassList = "${ProxyHostName};localhost;127.0.0.1;[::1];*.concept-software.ru" function Set-ProxyEnvironment { $env:HTTP_PROXY = $HttpProxy $env:HTTPS_PROXY = $HttpProxy $env:ALL_PROXY = $SocksProxy $env:NO_PROXY = $NoProxy $env:http_proxy = $HttpProxy $env:https_proxy = $HttpProxy $env:all_proxy = $SocksProxy $env:no_proxy = $NoProxy } function Add-WslEnvEntries { $RequiredEntries = @( 'HTTP_PROXY/u', 'HTTPS_PROXY/u', 'ALL_PROXY/u', 'NO_PROXY/u', 'http_proxy/u', 'https_proxy/u', 'all_proxy/u', 'no_proxy/u' ) $Entries = New-Object System.Collections.Generic.List[string] if (-not [string]::IsNullOrWhiteSpace($env:WSLENV)) { foreach ($Entry in ($env:WSLENV -split ':')) { if (-not [string]::IsNullOrWhiteSpace($Entry) -and -not $Entries.Contains($Entry)) { [void]$Entries.Add($Entry) } } } foreach ($Entry in $RequiredEntries) { if (-not $Entries.Contains($Entry)) { [void]$Entries.Add($Entry) } } $env:WSLENV = [string]::Join(':', $Entries) } function Get-CodexApplication { $Package = Get-AppxPackage -Name 'OpenAI.Codex' -ErrorAction SilentlyContinue | Select-Object -First 1 if ($null -eq $Package) { throw 'Codex Windows App is not installed. Expected AppX package: OpenAI.Codex.' } $Manifest = Get-AppxPackageManifest $Package $Application = @($Manifest.Package.Applications.Application) | Select-Object -First 1 if ($null -eq $Application -or [string]::IsNullOrWhiteSpace($Application.Executable)) { throw 'Codex Windows App manifest does not contain an executable entry.' } $Executable = Join-Path $Package.InstallLocation ([string]$Application.Executable) if (-not (Test-Path -LiteralPath $Executable)) { throw "Codex Windows App executable was not found: $Executable" } [PSCustomObject]@{ AppUserModelId = "$($Package.PackageFamilyName)!$([string]$Application.Id)" Executable = $Executable } } function Start-CodexApplication { param( [Parameter(Mandatory)] [string]$AppUserModelId, [Parameter(Mandatory)] [string]$Arguments ) if (-not ('CodexProxyLauncher.ApplicationActivationManager' -as [type])) { Add-Type -TypeDefinition @' using System; using System.Runtime.InteropServices; namespace CodexProxyLauncher { [Flags] public enum ActivateOptions : uint { None = 0 } [ComImport] [Guid("2e941141-7f97-4756-ba1d-9decde894a3d")] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] public interface IApplicationActivationManager { [PreserveSig] int ActivateApplication( [MarshalAs(UnmanagedType.LPWStr)] string appUserModelId, [MarshalAs(UnmanagedType.LPWStr)] string arguments, ActivateOptions options, out uint processId); } [ComImport] [Guid("45BA127D-10A8-46EA-8AB7-56EA9078943C")] public class ApplicationActivationManager { } public static class ApplicationActivator { public static uint Activate(string appUserModelId, string arguments) { IApplicationActivationManager manager = (IApplicationActivationManager)new ApplicationActivationManager(); uint processId; int hResult = manager.ActivateApplication( appUserModelId, arguments, ActivateOptions.None, out processId); Marshal.ThrowExceptionForHR(hResult); return processId; } } } '@ } [void][CodexProxyLauncher.ApplicationActivator]::Activate( $AppUserModelId, $Arguments ) } Set-ProxyEnvironment Add-WslEnvEntries $CodexApplication = Get-CodexApplication $CodexArguments = "--proxy-server=${HttpProxy} --proxy-bypass-list=${ProxyBypassList}" Start-CodexApplication -AppUserModelId $CodexApplication.AppUserModelId -Arguments $CodexArguments