<# .SYNOPSIS Builds openhop.exe (standalone Windows app: control center, tray, background workers). Supports Cloudflare WARP /64, Tor Network (SOCKS5), Custom Proxies, and Clean Endpoints. Usage: powershell -ExecutionPolicy Bypass -File .\build.ps1 (output lands in the repo root, not tracked by git) #> [CmdletBinding()] param( # Where openhop.exe lands. Default: this folder. Point it at a temp folder when the old exe is in use. [string]$Dist = "" ) $ErrorActionPreference = "Stop" $Root = $PSScriptRoot if (-not $Root) { if ($MyInvocation.MyCommand.Path) { $Root = Split-Path -Parent $MyInvocation.MyCommand.Path } else { $Root = (Get-Location).Path } } $SrcDir = Join-Path $Root "src" if (-not (Test-Path (Join-Path $SrcDir "openhop_app.py"))) { $SrcDir = Join-Path $Root "windows" } $SpecWork = Join-Path $env:TEMP "ouc-multi-build" $DistDir = if ($Dist) { [System.IO.Path]::GetFullPath($Dist) } else { $Root } if (-not (Test-Path $DistDir)) { New-Item -ItemType Directory -Force -Path $DistDir | Out-Null } if (Test-Path $SpecWork) { Remove-Item $SpecWork -Recurse -Force -ErrorAction SilentlyContinue } New-Item -ItemType Directory -Force -Path $SpecWork | Out-Null $PyInstaller = Get-Command pyinstaller.exe -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty Source $PyPrefix = @() if (-not $PyInstaller) { # Fall back to the module (pip often installs PyInstaller without putting pyinstaller.exe on PATH) & python -m PyInstaller --version *> $null if ($LASTEXITCODE -eq 0) { $PyInstaller = "python" $PyPrefix = @("-m", "PyInstaller") } else { throw "PyInstaller is not installed. Install it with: python -m pip install pyinstaller" } } # Dynamically locate webview, pythonnet, and clr_loader packages $WebviewDir = ((& python -c "import webview, os; print(os.path.dirname(webview.__file__))") | Select-Object -Last 1).Trim() $PythonnetDir = ((& python -c "import pythonnet, os; print(os.path.dirname(pythonnet.__file__))") | Select-Object -Last 1).Trim() $ClrLoaderDir = ((& python -c "import clr_loader, os; print(os.path.dirname(clr_loader.__file__))") | Select-Object -Last 1).Trim() $WvHooks = Join-Path $WebviewDir "__pyinstaller" $PnHooks = Join-Path $PythonnetDir "_pyinstaller" $Excludes = @( "torch", "torchvision", "ultralytics", "cv2", "numpy", "pandas", "scipy", "matplotlib", "scikit-learn", "sklearn", "sympy", "pyarrow", "polars", "streamlit", "selenium", "frida", "yfinance", "sqlalchemy", "uvicorn", "starlette", "Flask", "altair", "plotly", "networkx", "webview.platforms.android", "webview.platforms.gtk", "webview.platforms.qt", "webview.platforms.cocoa", # Pulled in by --collect-all / optional extras but never used by openhop (cffi stays: clr_loader needs it to start .NET; pywebview only needs # cryptography for its HTTPS server, which we don't use). Unused native modules are unsigned code that # makes antivirus heuristics and Smart App Control more suspicious, and they add megabytes. "cryptography", "bcrypt", "tornado", "ujson", "markupsafe", "jinja2", "win32ui", "pythonwin", "win32trace", "win32evtlog", "dde", "PIL._avif", "PIL.AvifImagePlugin", "PIL._webp", "PIL.WebPImagePlugin", "PIL._imagingtk", "PIL.ImageTk", "PIL._imagingcms", "PIL.ImageCms", "PIL._imagingmath", "PIL.ImageMath" ) $IconPath = Join-Path $SrcDir "assets\openhop.ico" if (-not (Test-Path $IconPath)) { $IconPath = Join-Path $Root "windows\assets\openhop.ico" } # Antivirus false positives: the stock PyInstaller bootloader is shared with lots of malware, so engines flag # anything built with it. tools\build_bootloader.ps1 compiles our own once per PyInstaller version. $BootDir = ((& python -c "import PyInstaller,os;print(os.path.join(os.path.dirname(PyInstaller.__file__),'bootloader','Windows-64bit-intel'))") | Select-Object -Last 1).Trim() if (-not (Test-Path (Join-Path $BootDir "openhop-custom.txt"))) { Write-Warning "PyInstaller's stock bootloader is installed: antivirus programs will flag this exe more often. Run .\tools\build_bootloader.ps1 once first." } # Version information shown in the file's Properties (and read by antivirus/SmartScreen): an exe with # no publisher, description or version looks like throw-away malware. $AppVersion = ((Select-String -Path (Join-Path $SrcDir "ouc_common.py") -Pattern '^VERSION\s*=\s*"([^"]+)"').Matches[0].Groups[1].Value) $VParts = @(($AppVersion -split '\.') + @("0", "0", "0", "0"))[0..3] | ForEach-Object { [int]$_ } $VTuple = $VParts -join ", " $VersionFile = Join-Path $SpecWork "version_info.txt" @" VSVersionInfo( ffi=FixedFileInfo(filevers=($VTuple), prodvers=($VTuple), mask=0x3f, flags=0x0, OS=0x40004, fileType=0x1, subtype=0x0, date=(0, 0)), kids=[ StringFileInfo([StringTable(u'040904B0', [ StringStruct(u'CompanyName', u'openhop.cfd'), StringStruct(u'FileDescription', u'openhop - keeps OpenCode working on the free tier'), StringStruct(u'FileVersion', u'$AppVersion'), StringStruct(u'InternalName', u'openhop'), StringStruct(u'LegalCopyright', u'MIT License. openhop.cfd, based on the original by Avatar (ShadowySphinX)'), StringStruct(u'OriginalFilename', u'openhop.exe'), StringStruct(u'ProductName', u'openhop'), StringStruct(u'ProductVersion', u'$AppVersion'), StringStruct(u'Comments', u'Open source: https://openhop.cfd/source')])]), VarFileInfo([VarStruct(u'Translation', [1033, 1200])]) ] ) "@ | Set-Content -Path $VersionFile -Encoding UTF8 $ArgsList = @( "--noconfirm", "--version-file", $VersionFile, "--clean", "--onefile", "--windowed", "--name", "openhop", "--icon", $IconPath, "--distpath", $DistDir, "--workpath", (Join-Path $SpecWork "work"), "--specpath", $SpecWork, "--paths", $SrcDir, "--collect-all", "webview", "--collect-all", "pythonnet", "--collect-all", "clr_loader", "--add-data", ((Join-Path $SrcDir "ui\index.html") + ";src\ui"), "--add-data", ((Join-Path $SrcDir "ui\index.html") + ";windows\ui"), "--add-data", ((Join-Path $SrcDir "ui\index.html") + ";ui"), "--add-data", ((Join-Path $SrcDir "ui\*.html") + ";src\ui"), "--add-data", ((Join-Path $SrcDir "assets\openhop.ico") + ";src\assets"), "--add-data", ((Join-Path $SrcDir "assets\openhop.ico") + ";windows\assets"), "--add-data", ((Join-Path $SrcDir "assets\openhop.ico") + ";assets"), "--add-data", ((Join-Path $SrcDir "*.py") + ";src"), "--add-data", ((Join-Path $SrcDir "*.py") + ";windows"), "--add-data", ((Join-Path $WebviewDir "lib") + ";webview\lib"), "--add-data", ((Join-Path $WebviewDir "js") + ";webview\js"), "--add-data", ((Join-Path $WebviewDir "lib\runtimes") + ";runtimes"), "--add-data", ((Join-Path $WebviewDir "lib\Microsoft.Web.WebView2.Core.dll") + ";."), "--add-data", ((Join-Path $WebviewDir "lib\Microsoft.Web.WebView2.WinForms.dll") + ";."), "--add-data", ((Join-Path $WebviewDir "lib\WebBrowserInterop.x64.dll") + ";."), "--add-data", ((Join-Path $WebviewDir "lib\runtimes\win-x64\native\WebView2Loader.dll") + ";."), "--hidden-import", "webview", "--hidden-import", "webview.platforms.winforms", "--hidden-import", "webview.platforms.edgechromium", "--hidden-import", "clr", "--hidden-import", "pythonnet", "--hidden-import", "clr_loader", "--hidden-import", "bottle", "--hidden-import", "psutil", "--hidden-import", "ouc_common", "--hidden-import", "ouc_firewall", "--hidden-import", "ouc_migrate", "--hidden-import", "zen_proxy", "--hidden-import", "zen_lane", "--hidden-import", "warp_keeper", "--hidden-import", "zen_guard", "--hidden-import", "zen_hop", "--hidden-import", "doctor", "--hidden-import", "tor_manager", "--hidden-import", "proxy_chain", "--hidden-import", "clean_endpoint", "--hidden-import", "auto_resume", "--hidden-import", "desktop_brand", "--hidden-import", "updater", "--hidden-import", "openhop_tray", "--hidden-import", "pystray", "--hidden-import", "pystray._win32", "--hidden-import", "PIL.Image", "--hidden-import", "PIL.ImageDraw", "--hidden-import", "winreg" ) if (Test-Path $WvHooks) { $ArgsList += @("--additional-hooks-dir", $WvHooks) } if (Test-Path $PnHooks) { $ArgsList += @("--additional-hooks-dir", $PnHooks) } foreach ($ex in $Excludes) { $ArgsList += @("--exclude-module", $ex) } $ArgsList += (Join-Path $SrcDir "openhop_app.py") Write-Host "Building openhop.exe..." -ForegroundColor Cyan & $PyInstaller @PyPrefix @ArgsList if ($LASTEXITCODE -ne 0) { throw "PyInstaller exited with code $LASTEXITCODE" } $OutExe = Join-Path $DistDir "openhop.exe" if (-not (Test-Path $OutExe)) { throw "Expected output executable missing: $OutExe" } Remove-Item $SpecWork -Recurse -Force -ErrorAction SilentlyContinue $SizeMB = [math]::Round((Get-Item $OutExe).Length / 1MB, 2) Write-Host "Successfully built: $OutExe ($SizeMB MB)" -ForegroundColor Green