Skip to content

Commit 3b1fdd8

Browse files
fix(desktop): make the Windows shell load its own origin and hide the console (#5399)
* fix(desktop): make the Windows shell load its own origin and hide the console Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(desktop): let Stop proxy settle before deciding whether the proxy is gone Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(desktop): only trust http for the Windows app origin and log a stuck Stop proxy Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: jun <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent e245308 commit 3b1fdd8

5 files changed

Lines changed: 72 additions & 11 deletions

File tree

‎desktop/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ misleading on a workstation.
3737
bun run build:local
3838
```
3939

40-
This asks for the app and dmg only, so no updater archive is produced and none is expected to be
41-
signed. It prints the bundle path and exits zero. The release path below is unchanged: a published
40+
This asks for the host platform's installable bundles only (app and dmg on macOS, msi and nsis
41+
setup exe on Windows, AppImage and deb on Linux), so no updater archive is produced and none is
42+
expected to be signed. It prints the bundle paths and exits zero. The release path below is unchanged: a published
4243
updater artifact still has to be signed.
4344

4445
## Release packaging and updates

‎desktop/scripts/build-local.ts‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,24 @@
2121
* `OpenCodex.app.tar.gz (updater)` and still fails. The override has to reach the config itself.
2222
*/
2323
import { spawnSync } from "node:child_process";
24-
import { existsSync } from "node:fs";
24+
import { existsSync, readdirSync } from "node:fs";
2525
import { dirname, join } from "node:path";
2626
import { fileURLToPath } from "node:url";
2727

2828
const desktopDir = dirname(dirname(fileURLToPath(import.meta.url)));
2929

30-
/** Bundle targets that carry no updater archive. */
31-
const LOCAL_BUNDLES = ["app", "dmg"] as const;
30+
/** Bundle targets per host platform that carry no updater archive. */
31+
const LOCAL_BUNDLES: Record<string, readonly string[]> = {
32+
darwin: ["app", "dmg"],
33+
win32: ["msi", "nsis"],
34+
linux: ["appimage", "deb"],
35+
};
36+
37+
const bundles = LOCAL_BUNDLES[process.platform];
38+
if (!bundles) {
39+
console.error(`[build:local] unsupported host platform: ${process.platform}`);
40+
process.exit(1);
41+
}
3242

3343
/**
3444
* Config merged over `tauri.conf.json` for this invocation only.
@@ -43,7 +53,7 @@ function run(): number {
4353
const extra = process.argv.slice(2);
4454
const args = [
4555
"tauri", "build", "--ci",
46-
"--bundles", LOCAL_BUNDLES.join(","),
56+
"--bundles", bundles.join(","),
4757
"--config", LOCAL_CONFIG,
4858
...extra,
4959
];
@@ -58,10 +68,15 @@ function run(): number {
5868
const status = run();
5969
if (status === 0) {
6070
const bundleRoot = join(desktopDir, "src-tauri", "target", "release", "bundle");
61-
const app = join(bundleRoot, "macos", "OpenCodex.app");
6271
// Naming what exists is the point of the script: the previous output ended on an error line, so
6372
// the artifacts it had already written were the least visible thing in it.
64-
if (existsSync(app)) console.log(`[build:local] ${app}`);
73+
for (const dir of ["macos", "dmg", "msi", "nsis", "appimage", "deb"]) {
74+
const directory = join(bundleRoot, dir);
75+
if (!existsSync(directory)) continue;
76+
for (const name of readdirSync(directory)) {
77+
if (/\.(app|dmg|msi|exe|AppImage|deb)$/i.test(name)) console.log(`[build:local] ${join(directory, name)}`);
78+
}
79+
}
6580
console.log("[build:local] updater artifacts skipped; release signing is unchanged.");
6681
}
6782
process.exit(status);

‎desktop/src-tauri/src/main.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
2+
13
fn main() {
24
opencodex_desktop_lib::run();
35
}

‎desktop/src-tauri/src/tray.rs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,19 @@ pub fn install(app: &AppHandle, proxy: ProxyClient) -> tauri::Result<()> {
126126
let app = app.clone();
127127
let stop_item = stop_item.clone();
128128
tauri::async_runtime::spawn(async move {
129-
let stopped = proxy.stop().await.is_ok() || proxy.is_alive().await.is_err();
129+
let mut stopped = proxy.stop().await.is_ok();
130+
for _ in 0..10 {
131+
if stopped || proxy.is_alive().await.is_err() {
132+
stopped = true;
133+
break;
134+
}
135+
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
136+
}
130137
if stopped {
131138
app.state::<crate::AppState>().shutdown_child();
132139
let _ = stop_item.set_enabled(false);
140+
} else {
141+
eprintln!("tray: proxy still answering /healthz after stop request");
133142
}
134143
});
135144
}

‎desktop/src-tauri/src/window.rs‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub fn configure(window: &WebviewWindow) {
2525

2626
pub fn navigation_allowed(endpoint: ProxyEndpoint) -> impl Fn(&Url) -> bool {
2727
move |url| {
28-
if url.scheme() == "tauri" {
28+
if is_app_origin(url) {
2929
return true;
3030
}
3131
if url.scheme() == "http" && url.host_str() == Some(endpoint.host) {
@@ -39,6 +39,17 @@ pub fn navigation_allowed(endpoint: ProxyEndpoint) -> impl Fn(&Url) -> bool {
3939
}
4040
}
4141

42+
/// The bundled `frontendDist` origin. Tauri serves it as `tauri://localhost` on macOS and
43+
/// Linux, and as `http://tauri.localhost` on Windows, where WebView2 has no custom-scheme
44+
/// support.
45+
fn is_app_origin(url: &Url) -> bool {
46+
match url.scheme() {
47+
"tauri" => true,
48+
"http" => url.host_str() == Some("tauri.localhost"),
49+
_ => false,
50+
}
51+
}
52+
4253
pub fn show(window: &WebviewWindow) {
4354
let _ = window.show();
4455
let _ = window.set_focus();
@@ -70,7 +81,30 @@ pub fn set_tray_policy(app: &AppHandle, visible: bool) {
7081

7182
#[cfg(test)]
7283
mod tests {
73-
use super::webview_user_agent;
84+
use super::{is_app_origin, navigation_allowed, webview_user_agent};
85+
use crate::discovery::ProxyEndpoint;
86+
use tauri::Url;
87+
88+
#[test]
89+
fn navigation_allows_the_app_origin_on_every_platform() {
90+
let allowed = navigation_allowed(ProxyEndpoint {
91+
host: "127.0.0.1",
92+
port: 10100,
93+
});
94+
assert!(allowed(
95+
&Url::parse("tauri://localhost/index.html?port=10100").unwrap()
96+
));
97+
assert!(allowed(
98+
&Url::parse("http://tauri.localhost/index.html?port=10100").unwrap()
99+
));
100+
assert!(allowed(
101+
&Url::parse("http://127.0.0.1:10100/#/usage").unwrap()
102+
));
103+
assert!(!is_app_origin(
104+
&Url::parse("https://tauri.localhost/index.html").unwrap()
105+
));
106+
assert!(!allowed(&Url::parse("file:///C:/index.html").unwrap()));
107+
}
74108

75109
#[test]
76110
fn webview_user_agent_marks_the_desktop_shell() {

0 commit comments

Comments
 (0)