Refactor bridge modules: imports & cleanup
Standardize and reorganize imports, type usages and minor formatting across the bridge modules. This commit moves protobuf helpers, makeOidbEnvelope/runOidb, OidbBase and logger imports into consistent places, reorders and groups action/contacts exports in Bridge, removes or trims file-level comments, and fixes whitespace/logic ordering in several highway, element-builder and msg-push files. These are refactors and import/type adjustments across many files under packages/core/src (bridge, highway, msg-push, webui, utils, etc.) with no intended behavioral changes.
This commit is contained in:
1 parent
c49ae1c8d9
commit
b56868878c
83 files changed
+356
-919
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { randomBytes, scryptSync, timingSafeEqual } from 'crypto';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { randomBytes, scryptSync, timingSafeEqual } from 'crypto';
|
||||
import { createLogger } from '../utils/logger';
|
||||
|
||||
const log = createLogger('WebUI.Auth');
|
||||
@@ -13,11 +13,6 @@ const SCRYPT_N = 16384; // cost
|
||||
const SCRYPT_R = 8;
|
||||
const SCRYPT_P = 1;
|
||||
|
||||
/**
|
||||
* Fixed credentials used when SNOWLUMA_DEV_MODE=1 is set. The state is kept
|
||||
* entirely in memory and never written to `config/webui.json`, so toggling
|
||||
* dev mode on/off never disturbs the real persisted password.
|
||||
*/
|
||||
const DEV_PASSWORD = 'snowluma-dev';
|
||||
|
||||
export function isDevAuthMode(): boolean {
|
||||
@@ -27,9 +22,6 @@ export function isDevAuthMode(): boolean {
|
||||
function envBootstrapPassword(): string | null {
|
||||
const raw = process.env.SNOWLUMA_WEBUI_BOOTSTRAP_PASSWORD;
|
||||
if (!raw || typeof raw !== 'string') return null;
|
||||
// Minimum sanity: avoid accidental empty / 1-char seeds. Strength
|
||||
// rules still don't apply because we want trusted launchers to be
|
||||
// able to pick a high-entropy random secret (e.g. base64(16)).
|
||||
if (raw.length < 8) return null;
|
||||
return raw;
|
||||
}
|
||||
@@ -111,7 +103,6 @@ function atomicWrite(state: WebuiAuthState): void {
|
||||
ensureConfigDir();
|
||||
const tmp = WEBUI_CONFIG_PATH + '.tmp';
|
||||
fs.writeFileSync(tmp, JSON.stringify(state, null, 2), { encoding: 'utf8', mode: 0o600 });
|
||||
// chmod again in case the file already existed with looser perms
|
||||
try {
|
||||
fs.chmodSync(tmp, 0o600);
|
||||
} catch {
|
||||
@@ -174,11 +165,6 @@ export class WebuiAuth {
|
||||
backupCorruptConfig();
|
||||
}
|
||||
}
|
||||
// SNOWLUMA_WEBUI_BOOTSTRAP_PASSWORD lets a trusted launcher seed the
|
||||
// password on first run instead of having to scrape it from logs.
|
||||
// Effect: write webui.json with the env-provided password, hashed,
|
||||
// and mustChangePassword=false. Only honoured when there is NO existing
|
||||
// webui.json — once persisted, password changes happen via the UI.
|
||||
const envPassword = envBootstrapPassword();
|
||||
if (envPassword !== null) {
|
||||
const salt = randomBytes(16);
|
||||
@@ -235,10 +221,6 @@ export class WebuiAuth {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update password atomically. Throws if the new password fails strength rules.
|
||||
* After success, mustChangePassword=false and the file is rewritten on disk.
|
||||
*/
|
||||
setPassword(newPassword: string): void {
|
||||
if (this.devMode) {
|
||||
throw new Error('开发模式 (SNOWLUMA_DEV_MODE=1) 已禁用密码修改');
|
||||
|
||||
@@ -1,33 +1,5 @@
|
||||
// Resolve the request's "real" client IP for per-IP rate limiting.
|
||||
//
|
||||
// Threat model:
|
||||
// - By default we ONLY trust the TCP socket peer. `X-Forwarded-For` and
|
||||
// `X-Real-IP` are client-controllable, so honoring them lets any
|
||||
// client rotate the header to escape the 5-attempts/15-min login
|
||||
// lockout.
|
||||
// - Operators who actually run behind a reverse proxy (nginx, Caddy,
|
||||
// a SaaS CDN) need a way to opt back into the real client IP. The
|
||||
// `SNOWLUMA_WEBUI_TRUST_PROXY` env var lets them say "when the
|
||||
// immediate TCP peer is THIS, treat the headers as authoritative".
|
||||
//
|
||||
// Supported values for SNOWLUMA_WEBUI_TRUST_PROXY:
|
||||
// - unset / empty — default; ignore headers, use socket peer
|
||||
// - "loopback" — trust headers when the socket peer is 127.0.0.1
|
||||
// or ::1 (typical for nginx/Caddy on the same host)
|
||||
// - "1" / "true" / — trust headers from any peer (only safe behind a
|
||||
// "all" trusted SaaS CDN that strips client-set headers)
|
||||
// - comma-separated — trust headers when the socket peer matches one
|
||||
// IP list of these IPs (reverse proxy on another machine)
|
||||
//
|
||||
// When trusted, we prefer X-Real-IP (a single value the reverse proxy
|
||||
// overwrites unconditionally) and fall back to the first hop of
|
||||
// X-Forwarded-For. We never parse the FULL XFF chain — if the proxy
|
||||
// chain isn't fully trusted, the leftmost value can still be spoofed
|
||||
// by the original client, and a single-tier proxy is the only setup
|
||||
// this app realistically sees.
|
||||
|
||||
import type { Context } from 'hono';
|
||||
import { getConnInfo } from '@hono/node-server/conninfo';
|
||||
import type { Context } from 'hono';
|
||||
|
||||
export type TrustProxyMode =
|
||||
| { kind: 'none' }
|
||||
@@ -69,12 +41,6 @@ function shouldTrustHeaders(mode: TrustProxyMode, socketIp: string): boolean {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the right "client IP" for rate-limit keying.
|
||||
*
|
||||
* Exposed as a pure function over a small dependency surface so the
|
||||
* unit tests can fabricate request shapes without spinning up a server.
|
||||
*/
|
||||
export function pickClientIp(
|
||||
c: Pick<Context, 'req'>,
|
||||
mode: TrustProxyMode,
|
||||
|
||||
Reference in new issue
Block a user