打开浏览器改用 explorer.exe(shell 关联),找不到时才用 rundll32 (#29)
Windows 上先同步判断 explorer.exe 是否存在:存在就用 explorer.exe <url>,由已登录、未提权的 shell 按默认浏览器关联打开,从管理员控制台启动时浏览器不会跟着提权(Edge 不再弹「现有实例正在以提升的权限运行」);找不到 explorer.exe 才退回 rundll32 url.dll,FileProtocolHandler。不经过 cmd,URL 里的 & 不会被截断。macOS / Linux 不变。 打开浏览器的实现集中到 scripts/open-browser.mjs;scripts/launch.mjs 加 isMain() 守卫(与 tools/setup.mjs、tools/doctor.mjs 一致),被 import 时不会启动服务器。测试:test/open-browser.test.js。
This commit is contained in:
1 parent
0b73a39b12
commit
8b10625d3c
3 files changed
+207
-16
No files matched your search
+12
-16
@@ -16,6 +16,8 @@ import { spawn, spawnSync } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import fs from 'node:fs';
|
||||
// 打开浏览器只有一份实现(走 shell 关联 = 默认浏览器,且不会把浏览器拉成提权)
|
||||
import { openBrowser as openInBrowser } from './open-browser.mjs';
|
||||
|
||||
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const IS_WIN = process.platform === 'win32';
|
||||
@@ -46,21 +48,7 @@ function parseArgs(argv) {
|
||||
}
|
||||
|
||||
function openBrowser(url) {
|
||||
try {
|
||||
let cmd; let args;
|
||||
if (IS_WIN) { cmd = 'rundll32'; args = ['url.dll,FileProtocolHandler', url]; }
|
||||
else if (process.platform === 'darwin') { cmd = 'open'; args = [url]; }
|
||||
else {
|
||||
if (!process.env.DISPLAY && !process.env.WAYLAND_DISPLAY) return false;
|
||||
cmd = 'xdg-open'; args = [url];
|
||||
}
|
||||
const child = spawn(cmd, args, { stdio: 'ignore', detached: true, windowsHide: true });
|
||||
child.on('error', () => {});
|
||||
child.unref();
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
return openInBrowser(url) !== null;
|
||||
}
|
||||
|
||||
function printShare(port) {
|
||||
@@ -132,4 +120,12 @@ async function main() {
|
||||
return exited;
|
||||
}
|
||||
|
||||
main().then((code) => { process.exitCode = code; }, (e) => { console.error(e?.stack || e); process.exitCode = 1; });
|
||||
// Same convention as tools/setup.mjs: the helpers above are importable, but only running this file starts anything.
|
||||
// Without the guard, importing launch.mjs would fall through to main() in the background and bind a port.
|
||||
function isMain() {
|
||||
try { return !!process.argv[1] && fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url)); } catch { return false; }
|
||||
}
|
||||
|
||||
if (isMain()) {
|
||||
main().then((code) => { process.exitCode = code; }, (e) => { console.error(e?.stack || e); process.exitCode = 1; });
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Opening the game page in the player's browser.
|
||||
//
|
||||
// Why not `rundll32 url.dll,FileProtocolHandler` (the usual one-liner): it runs *inside* the caller's token, so a
|
||||
// launcher started from an elevated console starts Edge/Chrome **elevated** too — and from then on Edge greets the
|
||||
// player with 「Microsoft Edge 未响应,因为现有实例正在以提升的权限运行。是否要用普通权限重启现有实例? 是/否」
|
||||
// instead of just opening the page.
|
||||
//
|
||||
// `explorer.exe <url>` avoids that because Explorer is not the process that opens the browser: a second Explorer
|
||||
// invocation hands the request to the shell instance that is already running, and *that* one (started at logon, not
|
||||
// elevated) resolves the URL through the default-browser association. Caveat: this hand-off is how the shell behaves
|
||||
// in practice, not an interface Microsoft documents or promises — treat it as the best available trick rather than a
|
||||
// supported API, and keep the rundll32 rung around for hosts where explorer.exe cannot be found.
|
||||
//
|
||||
// Windows therefore has a single rung, chosen synchronously from what is actually on disk: explorer.exe when it is
|
||||
// there, rundll32 otherwise. There is deliberately no "try one, fall back on spawn failure" ladder: spawn() reports
|
||||
// ENOENT / EACCES through the child's asynchronous `error` event, not by throwing, so a fallback wired to a thrown
|
||||
// exception would never fire in practice.
|
||||
//
|
||||
// There is deliberately no `cmd /c start "" <url>` rung either: cmd re-parses everything after `/c`, so a URL holding
|
||||
// an `&` (e.g. the room link `?room=ABCD&name=…`) is split at the `&` — cmd runs `start "" <url-up-to-&>` and then
|
||||
// tries to run the remainder as a second command. The browser then opens a truncated address, or a stray console
|
||||
// window flashes. Passing an argument array to CreateProcess avoids the shell entirely, which is why the remaining
|
||||
// rungs are safe.
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { spawn } from 'node:child_process';
|
||||
|
||||
/** Does this path exist? Injectable as `exists` so the Windows rung can be tested without the host's filesystem. */
|
||||
function realExists(p) {
|
||||
try { return fs.existsSync(p); } catch { return false; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Absolute path of a system executable, or null when it is not where we expect it.
|
||||
*
|
||||
* Built with **win32** semantics whatever the host is: the value is handed to a Windows process, and the rung is
|
||||
* also inspected from tests (CI runs them on Linux, where `path.join` is posix and would emit `C:\Windows/explorer.exe`
|
||||
* — a mixed-separator string that never matches a real Windows path).
|
||||
* @param {NodeJS.ProcessEnv} env
|
||||
* @param {string} sub subdirectory under the Windows root ('' / '.' for the root itself)
|
||||
* @param {string} name
|
||||
* @param {(p: string) => boolean} exists
|
||||
* @returns {string | null}
|
||||
*/
|
||||
function systemExe(env, sub, name, exists) {
|
||||
const root = env.SystemRoot || env.windir || 'C:\\Windows';
|
||||
const abs = path.win32.join(root, sub || '.', name);
|
||||
return exists(abs) ? abs : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Candidate launch commands for `url`, best first.
|
||||
*
|
||||
* At most one entry per platform: which rung Windows gets is decided here, synchronously, from the filesystem.
|
||||
* @param {string} url
|
||||
* @param {{ platform?: string, env?: NodeJS.ProcessEnv, exists?: (p: string) => boolean }} [o]
|
||||
* `exists` defaults to the real filesystem; pass one in to test the Windows rung from any host.
|
||||
* @returns {{ cmd: string, args: string[], label: string }[]}
|
||||
*/
|
||||
export function browserCommands(url, { platform = process.platform, env = process.env, exists = realExists } = {}) {
|
||||
if (platform === 'win32') {
|
||||
// shell association: the default browser, opened by the non-elevated shell instance (see the header)
|
||||
const explorer = systemExe(env, '.', 'explorer.exe', exists);
|
||||
if (explorer) return [{ cmd: explorer, args: [url], label: 'explorer' }];
|
||||
// legacy fallback for the theoretical case of a Windows host without explorer.exe; it *does* inherit our token,
|
||||
// so it is only ever reached when the de-elevating rung is unavailable
|
||||
const rundll32 = systemExe(env, 'System32', 'rundll32.exe', exists);
|
||||
if (rundll32) return [{ cmd: rundll32, args: ['url.dll,FileProtocolHandler', url], label: 'rundll32' }];
|
||||
return [];
|
||||
}
|
||||
if (platform === 'darwin') return [{ cmd: 'open', args: [url], label: 'open' }];
|
||||
if (env.DISPLAY || env.WAYLAND_DISPLAY) return [{ cmd: 'xdg-open', args: [url], label: 'xdg-open' }];
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Open `url` with the player's default browser. Returns the label of the launcher that was spawned, or null when
|
||||
* there is nothing to start (headless Linux, no known system binary) — callers then just print the URL.
|
||||
*
|
||||
* The label is returned as soon as spawn() accepted the command. A missing binary surfaces later as the child's
|
||||
* `error` event (ENOENT / EACCES), which is why one is attached: an unhandled `error` event would crash the
|
||||
* launcher, and retrying a different rung at that point would mean opening a second browser after a race.
|
||||
* @param {string} url
|
||||
* @param {{ spawnImpl?: typeof spawn, platform?: string, env?: NodeJS.ProcessEnv,
|
||||
* exists?: (p: string) => boolean, log?: (m: string) => void }} [o]
|
||||
* @returns {string | null}
|
||||
*/
|
||||
export function openBrowser(url, { spawnImpl = spawn, platform = process.platform, env = process.env, exists = realExists, log } = {}) {
|
||||
for (const { cmd, args, label } of browserCommands(url, { platform, env, exists })) {
|
||||
try {
|
||||
const child = spawnImpl(cmd, args, { stdio: 'ignore', detached: true, windowsHide: true });
|
||||
if (!child || typeof child.unref !== 'function') continue;
|
||||
child.on?.('error', () => {});
|
||||
child.unref();
|
||||
return label;
|
||||
} catch (err) {
|
||||
log?.(`${label}: ${err?.code || err?.message || err}`);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Reference in new issue
Block a user