@seaveyon/harness-switch 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -1
- package/dist/harness-switch.js +333 -25
- package/package.json +1 -1
- package/public/06ac09dcb8ec7d56.woff2 +0 -0
- package/public/110e2a7693b4a5f3.woff2 +0 -0
- package/public/192d14b24f70b621.woff2 +0 -0
- package/public/2258ff2f7eae77c2.woff +0 -0
- package/public/2a0db5363856276c.woff2 +0 -0
- package/public/5a49cbe35ab2dd3c.woff2 +0 -0
- package/public/60bbe6c85a6afad1.woff2 +0 -0
- package/public/651bc97e3669e21f.woff2 +0 -0
- package/public/66a2e8650a2dcd92.woff2 +0 -0
- package/public/7d495472d32ca07d.woff +0 -0
- package/public/88f8fd5b09b976bb.woff +0 -0
- package/public/9533b5204f120066.woff +0 -0
- package/public/994c96595d20046b.woff +0 -0
- package/public/9faee8e8b774991c.woff2 +0 -0
- package/public/a7358d2e3dd919a9.woff2 +0 -0
- package/public/a8a9073d6f10c294.woff2 +0 -0
- package/public/assets/{5646.a72b2c1e.js → 5646.1b33e69c.js} +1 -1
- package/public/assets/{6150.910aaed9.js → 6150.5cb126a5.js} +1 -1
- package/public/assets/main.5b6d51f1.css +2 -0
- package/public/assets/main.7aee2734.js +1 -0
- package/public/b0b919fcf36c2073.woff2 +0 -0
- package/public/bdc4ddcb892cb87b.woff2 +0 -0
- package/public/cd8df815c9344be0.woff2 +0 -0
- package/public/d2ac083e197becc1.woff +0 -0
- package/public/dece0821e8ed059f.woff2 +0 -0
- package/public/e32cf7a40c90e778.woff +0 -0
- package/public/e53d79b9a7d8ae7b.woff +0 -0
- package/public/ef1c8a30e0ed6275.woff2 +0 -0
- package/public/f14ffb91743624fd.woff +0 -0
- package/public/f9f2cfdc8f44d943.woff +0 -0
- package/public/index.html +1 -1
- package/public/assets/main.9128def8.css +0 -2
- package/public/assets/main.95434834.js +0 -1
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@seaveyon/harness-switch)
|
|
4
4
|
|
|
5
|
+
English | [简体中文](https://github.com/B1anker/harness-switch/blob/main/README.zh-CN.md)
|
|
6
|
+
|
|
5
7
|
**harness-switch** is a Bun-powered web control plane for managing API profiles on an SSH or headless server. It switches API Base URL, API key, and model profiles for **Claude Code**, **Codex**, **Kimi Code**, **Pi**, and **DeepSeek Harness (DSH)**.
|
|
6
8
|
|
|
7
9
|
It is a configuration manager, not an API proxy: it does not route or inspect model traffic.
|
|
@@ -78,12 +80,22 @@ A write either lands completely or not at all: content is validated first, and a
|
|
|
78
80
|
|
|
79
81
|
Switching away from a profile first reads the live file back into that profile's record, so edits you made directly in the CLI tool are not lost on the next switch.
|
|
80
82
|
|
|
83
|
+
### Moving every profile to another machine
|
|
84
|
+
|
|
85
|
+
Use **导入 / 导出** in the top bar to create one `.hsw-backup` file containing every Harness profile, API key, raw-file override, and current activation choice. The bundle is encrypted with a migration password you choose, so it does not depend on the source machine's `aes-256-gcm.key`.
|
|
86
|
+
|
|
87
|
+
On the destination machine, select the bundle and enter the migration password. The UI shows profile counts and same-name conflicts before it writes anything. Import keeps destination profiles by default; overwriting is an explicit choice. Restoring the exported activation state is optional.
|
|
88
|
+
|
|
89
|
+
Keep the migration password separately from the bundle. It cannot be recovered from the export file.
|
|
90
|
+
|
|
81
91
|
## Security
|
|
82
92
|
|
|
83
93
|
The service defaults to loopback only. Do **not** expose the management port directly to the public Internet. Use SSH port forwarding or a TLS-enabled reverse proxy with additional access control.
|
|
84
94
|
|
|
85
95
|
API keys are encrypted with AES-256-GCM in `~/.harness-switch/profiles.json`; the local encryption key lives in `~/.harness-switch/aes-256-gcm.key`. Those files, the generated password, and `env.sh` are stored with permissions set to `0600` on POSIX systems. When writing a harness's own configuration file, existing permissions are preserved and newly created files start at `0600`.
|
|
86
96
|
|
|
97
|
+
Web sessions live in `~/.harness-switch/sessions.json` (also `0600`) so restarting the service does not log you out. Only a SHA-256 digest of each session token is stored, so the file cannot be replayed as a cookie, and the table is tied to a fingerprint of the password that issued it: replacing `web_password` invalidates every existing session.
|
|
98
|
+
|
|
87
99
|
Two places deliberately expose a key to an authenticated session: the raw config preview and the generated files themselves must contain the credential to be useful. Profile listings never echo it.
|
|
88
100
|
|
|
89
101
|
This protects against accidental plaintext disclosure in profile storage, but does not replace host-level controls such as disk encryption and a secure Unix account.
|
|
@@ -94,8 +106,9 @@ This protects against accidental plaintext disclosure in profile storage, but do
|
|
|
94
106
|
|---|---:|---|
|
|
95
107
|
| `HOST` | `127.0.0.1` | Bind address. Keep the default when using SSH tunnelling. |
|
|
96
108
|
| `PORT` | `8787` | Listening port. |
|
|
97
|
-
| `HSW_DATA_DIR` | `~/.harness-switch` | Directory for encrypted profiles, active state, key, password, backups, and env file. |
|
|
109
|
+
| `HSW_DATA_DIR` | `~/.harness-switch` | Directory for encrypted profiles, active state, key, password, sessions, backups, and env file. |
|
|
98
110
|
| `HSW_HOME_DIR` | `$HOME` | Home directory used to locate the harness config directories. |
|
|
111
|
+
| `HSW_SESSION_TTL_HOURS` | `24` | How long a Web login stays valid. Sessions survive a service restart. |
|
|
99
112
|
| `HSW_BACKUP_RETAIN` | `10` | Number of snapshots to keep. |
|
|
100
113
|
| `HSW_PUBLIC_DIR` | auto | Optional override for the built frontend directory. |
|
|
101
114
|
|
|
@@ -120,6 +133,9 @@ The UI is a React SPA. Authentication uses an HttpOnly `hsw_session` cookie.
|
|
|
120
133
|
| `POST` | `/api/harnesses/:id/profiles/:name/activate` | Write the native config, then commit the switch |
|
|
121
134
|
| `GET` | `/api/backups` | Snapshots, newest first |
|
|
122
135
|
| `POST` | `/api/backups/:id/restore` | Restore a snapshot verbatim |
|
|
136
|
+
| `POST` | `/api/transfer/export` | Create a passphrase-encrypted portable bundle |
|
|
137
|
+
| `POST` | `/api/transfer/preview` | Decrypt and report profile counts and conflicts without writing |
|
|
138
|
+
| `POST` | `/api/transfer/import` | Import with `skip` or `overwrite` conflict handling |
|
|
123
139
|
|
|
124
140
|
## systemd
|
|
125
141
|
|
package/dist/harness-switch.js
CHANGED
|
@@ -9034,7 +9034,7 @@ var deleteCookie = (c, name, opt) => {
|
|
|
9034
9034
|
};
|
|
9035
9035
|
|
|
9036
9036
|
// src/services/auth.ts
|
|
9037
|
-
import { randomBytes as randomBytes3 } from "crypto";
|
|
9037
|
+
import { createHash, randomBytes as randomBytes3 } from "crypto";
|
|
9038
9038
|
|
|
9039
9039
|
// src/di/collection.ts
|
|
9040
9040
|
class ServiceCollection {
|
|
@@ -9195,14 +9195,15 @@ class EnvironmentService {
|
|
|
9195
9195
|
homeDir = process.env.HSW_HOME_DIR || homedir();
|
|
9196
9196
|
dataDir = process.env.HSW_DATA_DIR || join2(this.homeDir, ".harness-switch");
|
|
9197
9197
|
publicDir = process.env.HSW_PUBLIC_DIR || resolvePublicDir();
|
|
9198
|
-
sessionTtlMs =
|
|
9198
|
+
sessionTtlMs = resolveSessionTtlMs();
|
|
9199
9199
|
cookieName = "hsw_session";
|
|
9200
9200
|
files = {
|
|
9201
9201
|
profiles: join2(this.dataDir, "profiles.json"),
|
|
9202
9202
|
active: join2(this.dataDir, "active.json"),
|
|
9203
9203
|
password: join2(this.dataDir, "web_password"),
|
|
9204
9204
|
key: join2(this.dataDir, "aes-256-gcm.key"),
|
|
9205
|
-
env: join2(this.dataDir, "env.sh")
|
|
9205
|
+
env: join2(this.dataDir, "env.sh"),
|
|
9206
|
+
sessions: join2(this.dataDir, "sessions.json")
|
|
9206
9207
|
};
|
|
9207
9208
|
backupsDir = join2(this.dataDir, "backups");
|
|
9208
9209
|
backupRetainCount = Math.max(1, Number(process.env.HSW_BACKUP_RETAIN || 10));
|
|
@@ -9217,6 +9218,10 @@ class EnvironmentService {
|
|
|
9217
9218
|
mkdirSync(this.dataDir, { recursive: true, mode: 448 });
|
|
9218
9219
|
}
|
|
9219
9220
|
}
|
|
9221
|
+
function resolveSessionTtlMs() {
|
|
9222
|
+
const hours = Number(process.env.HSW_SESSION_TTL_HOURS || 24);
|
|
9223
|
+
return (Number.isFinite(hours) && hours > 0 ? hours : 24) * 60 * 60 * 1000;
|
|
9224
|
+
}
|
|
9220
9225
|
function resolvePublicDir() {
|
|
9221
9226
|
const candidates = [
|
|
9222
9227
|
join2(import.meta.dir, "../public"),
|
|
@@ -9404,6 +9409,7 @@ class LogService {
|
|
|
9404
9409
|
}
|
|
9405
9410
|
|
|
9406
9411
|
// src/services/auth.ts
|
|
9412
|
+
var STORE_VERSION = 1;
|
|
9407
9413
|
var IAuthService = createDecorator("authService");
|
|
9408
9414
|
var _dec = [
|
|
9409
9415
|
inject(IEnvironmentService, IFileService, ICryptoService, ILogService)
|
|
@@ -9415,7 +9421,8 @@ class AuthService {
|
|
|
9415
9421
|
files;
|
|
9416
9422
|
crypto;
|
|
9417
9423
|
log;
|
|
9418
|
-
sessions
|
|
9424
|
+
sessions;
|
|
9425
|
+
fingerprint = "";
|
|
9419
9426
|
constructor(environment, files, crypto2, log) {
|
|
9420
9427
|
this.environment = environment;
|
|
9421
9428
|
this.files = files;
|
|
@@ -9438,30 +9445,77 @@ class AuthService {
|
|
|
9438
9445
|
return null;
|
|
9439
9446
|
}
|
|
9440
9447
|
const token = randomBytes3(32).toString("base64url");
|
|
9441
|
-
this.
|
|
9448
|
+
this.load().set(digest("session", token), {
|
|
9449
|
+
expires: Date.now() + this.environment.sessionTtlMs
|
|
9450
|
+
});
|
|
9451
|
+
this.persist();
|
|
9442
9452
|
return token;
|
|
9443
9453
|
}
|
|
9444
9454
|
logout(token) {
|
|
9445
|
-
if (token) {
|
|
9446
|
-
this.
|
|
9455
|
+
if (token && this.load().delete(digest("session", token))) {
|
|
9456
|
+
this.persist();
|
|
9447
9457
|
}
|
|
9448
9458
|
}
|
|
9449
9459
|
isAuthenticated(token) {
|
|
9450
9460
|
if (!token) {
|
|
9451
9461
|
return false;
|
|
9452
9462
|
}
|
|
9453
|
-
const
|
|
9463
|
+
const sessions = this.load();
|
|
9464
|
+
const key = digest("session", token);
|
|
9465
|
+
const session = sessions.get(key);
|
|
9454
9466
|
if (!session || session.expires < Date.now()) {
|
|
9455
|
-
|
|
9467
|
+
if (sessions.delete(key)) {
|
|
9468
|
+
this.persist();
|
|
9469
|
+
}
|
|
9456
9470
|
return false;
|
|
9457
9471
|
}
|
|
9458
9472
|
return true;
|
|
9459
9473
|
}
|
|
9474
|
+
load() {
|
|
9475
|
+
const loaded = this.sessions;
|
|
9476
|
+
if (loaded) {
|
|
9477
|
+
return loaded;
|
|
9478
|
+
}
|
|
9479
|
+
this.fingerprint = digest("password", this.ensurePassword());
|
|
9480
|
+
const store = this.files.readJson(this.environment.files.sessions, {
|
|
9481
|
+
version: STORE_VERSION,
|
|
9482
|
+
password: this.fingerprint,
|
|
9483
|
+
sessions: {}
|
|
9484
|
+
});
|
|
9485
|
+
const sessions = new Map;
|
|
9486
|
+
if (store.version === STORE_VERSION && store.password === this.fingerprint) {
|
|
9487
|
+
const now = Date.now();
|
|
9488
|
+
for (const [key, session] of Object.entries(store.sessions ?? {})) {
|
|
9489
|
+
if (typeof session?.expires === "number" && session.expires > now) {
|
|
9490
|
+
sessions.set(key, { expires: session.expires });
|
|
9491
|
+
}
|
|
9492
|
+
}
|
|
9493
|
+
}
|
|
9494
|
+
this.sessions = sessions;
|
|
9495
|
+
if (sessions.size > 0) {
|
|
9496
|
+
this.log.info(`restored ${sessions.size} web session(s)`);
|
|
9497
|
+
}
|
|
9498
|
+
return sessions;
|
|
9499
|
+
}
|
|
9500
|
+
persist() {
|
|
9501
|
+
const sessions = this.sessions;
|
|
9502
|
+
if (!sessions) {
|
|
9503
|
+
return;
|
|
9504
|
+
}
|
|
9505
|
+
this.files.writeJson(this.environment.files.sessions, {
|
|
9506
|
+
version: STORE_VERSION,
|
|
9507
|
+
password: this.fingerprint,
|
|
9508
|
+
sessions: Object.fromEntries(sessions)
|
|
9509
|
+
});
|
|
9510
|
+
}
|
|
9460
9511
|
}
|
|
9461
9512
|
AuthService = __decorateElement(_init, 0, "AuthService", _dec, AuthService);
|
|
9462
9513
|
__runInitializers(_init, 1, AuthService);
|
|
9463
9514
|
__decoratorMetadata(_init, AuthService);
|
|
9464
9515
|
let _AuthService = AuthService;
|
|
9516
|
+
function digest(kind, value) {
|
|
9517
|
+
return createHash("sha256").update(`${kind}:${value}`).digest("base64url");
|
|
9518
|
+
}
|
|
9465
9519
|
|
|
9466
9520
|
// src/http/middleware.ts
|
|
9467
9521
|
function createServiceMiddleware(services) {
|
|
@@ -11166,28 +11220,19 @@ class CodexAdapter {
|
|
|
11166
11220
|
}
|
|
11167
11221
|
return rendered;
|
|
11168
11222
|
}
|
|
11169
|
-
renderOfficial(
|
|
11223
|
+
renderOfficial(_profile, current) {
|
|
11170
11224
|
const config = parseTomlObject(current[CONFIG]);
|
|
11171
|
-
const activeProviderId = readString(config, "model_provider");
|
|
11172
11225
|
delete config.model_provider;
|
|
11173
11226
|
delete config.model;
|
|
11174
11227
|
delete config.model_reasoning_effort;
|
|
11175
|
-
|
|
11176
|
-
if (profile) {
|
|
11177
|
-
delete config.model_providers[this.providerId(profile)];
|
|
11178
|
-
}
|
|
11179
|
-
if (activeProviderId) {
|
|
11180
|
-
delete config.model_providers[activeProviderId];
|
|
11181
|
-
}
|
|
11182
|
-
if (Object.keys(config.model_providers).length === 0) {
|
|
11183
|
-
delete config.model_providers;
|
|
11184
|
-
}
|
|
11185
|
-
}
|
|
11228
|
+
delete config.model_providers;
|
|
11186
11229
|
const rendered = { [CONFIG]: stringifyToml(config) };
|
|
11187
|
-
if (
|
|
11230
|
+
if (current[AUTH] !== undefined) {
|
|
11188
11231
|
const auth = parseJsonObject(current[AUTH]);
|
|
11189
|
-
|
|
11190
|
-
|
|
11232
|
+
if (DEFAULT_ENV_KEY in auth) {
|
|
11233
|
+
delete auth[DEFAULT_ENV_KEY];
|
|
11234
|
+
rendered[AUTH] = stringifyJson(auth);
|
|
11235
|
+
}
|
|
11191
11236
|
}
|
|
11192
11237
|
return rendered;
|
|
11193
11238
|
}
|
|
@@ -12371,6 +12416,266 @@ async function readBody(read) {
|
|
|
12371
12416
|
});
|
|
12372
12417
|
}
|
|
12373
12418
|
|
|
12419
|
+
// src/services/transfer.ts
|
|
12420
|
+
import { createCipheriv as createCipheriv2, createDecipheriv as createDecipheriv2, randomBytes as randomBytes4, scryptSync } from "crypto";
|
|
12421
|
+
var FORMAT = "harness-switch-encrypted-export";
|
|
12422
|
+
var VERSION = 1;
|
|
12423
|
+
var MAX_ENCRYPTED_BYTES = 10 * 1024 * 1024;
|
|
12424
|
+
var ITransferService = createDecorator("transferService");
|
|
12425
|
+
var _dec = [
|
|
12426
|
+
inject(IEnvironmentService, IFileService, ICryptoService, IAdapterRegistry, IActivationService)
|
|
12427
|
+
];
|
|
12428
|
+
var _init = __decoratorStart(undefined);
|
|
12429
|
+
|
|
12430
|
+
class TransferService {
|
|
12431
|
+
environment;
|
|
12432
|
+
files;
|
|
12433
|
+
crypto;
|
|
12434
|
+
adapters;
|
|
12435
|
+
activation;
|
|
12436
|
+
constructor(environment, files, crypto2, adapters, activation) {
|
|
12437
|
+
this.environment = environment;
|
|
12438
|
+
this.files = files;
|
|
12439
|
+
this.crypto = crypto2;
|
|
12440
|
+
this.adapters = adapters;
|
|
12441
|
+
this.activation = activation;
|
|
12442
|
+
}
|
|
12443
|
+
exportAll(passphrase) {
|
|
12444
|
+
this.assertPassphrase(passphrase);
|
|
12445
|
+
const store = this.readStore();
|
|
12446
|
+
const profiles = [];
|
|
12447
|
+
for (const harness of HARNESS_IDS) {
|
|
12448
|
+
for (const [name, stored] of Object.entries(store[harness] ?? {})) {
|
|
12449
|
+
profiles.push({
|
|
12450
|
+
harness,
|
|
12451
|
+
name,
|
|
12452
|
+
baseUrl: stored.base_url || "",
|
|
12453
|
+
apiKey: this.crypto.decrypt(stored.api_key),
|
|
12454
|
+
model: stored.model || "",
|
|
12455
|
+
notes: stored.notes || "",
|
|
12456
|
+
extras: stored.extras ?? {},
|
|
12457
|
+
overrides: stored.overrides ?? {}
|
|
12458
|
+
});
|
|
12459
|
+
}
|
|
12460
|
+
}
|
|
12461
|
+
const active = HARNESS_IDS.flatMap((harness) => {
|
|
12462
|
+
const entry = this.activation.getActive(harness);
|
|
12463
|
+
return entry ? [
|
|
12464
|
+
{
|
|
12465
|
+
harness,
|
|
12466
|
+
name: entry.name,
|
|
12467
|
+
official: entry.official === true
|
|
12468
|
+
}
|
|
12469
|
+
] : [];
|
|
12470
|
+
});
|
|
12471
|
+
return encrypt({
|
|
12472
|
+
format: "harness-switch-portable-config",
|
|
12473
|
+
version: VERSION,
|
|
12474
|
+
exportedAt: new Date().toISOString(),
|
|
12475
|
+
profiles,
|
|
12476
|
+
active
|
|
12477
|
+
}, passphrase);
|
|
12478
|
+
}
|
|
12479
|
+
preview(envelope, passphrase) {
|
|
12480
|
+
const payload = this.decrypt(envelope, passphrase);
|
|
12481
|
+
const store = this.readStore();
|
|
12482
|
+
return {
|
|
12483
|
+
exportedAt: payload.exportedAt,
|
|
12484
|
+
profileCount: payload.profiles.length,
|
|
12485
|
+
harnesses: HARNESS_IDS.map((harness) => ({
|
|
12486
|
+
harness,
|
|
12487
|
+
profiles: payload.profiles.filter((profile) => profile.harness === harness).length
|
|
12488
|
+
})).filter((item) => item.profiles > 0),
|
|
12489
|
+
conflicts: payload.profiles.filter((profile) => store[profile.harness]?.[profile.name] !== undefined).map(({ harness, name }) => ({ harness, name })),
|
|
12490
|
+
activeCount: payload.active.length
|
|
12491
|
+
};
|
|
12492
|
+
}
|
|
12493
|
+
importAll(envelope, passphrase, conflictPolicy, restoreActive) {
|
|
12494
|
+
if (conflictPolicy !== "skip" && conflictPolicy !== "overwrite") {
|
|
12495
|
+
throw new HttpError(400, "invalid conflict policy");
|
|
12496
|
+
}
|
|
12497
|
+
const payload = this.decrypt(envelope, passphrase);
|
|
12498
|
+
const store = structuredClone(this.readStore());
|
|
12499
|
+
let imported = 0;
|
|
12500
|
+
let overwritten = 0;
|
|
12501
|
+
let skipped = 0;
|
|
12502
|
+
for (const profile of payload.profiles) {
|
|
12503
|
+
this.validateProfile(profile);
|
|
12504
|
+
store[profile.harness] ||= {};
|
|
12505
|
+
const exists = store[profile.harness][profile.name] !== undefined;
|
|
12506
|
+
if (exists && conflictPolicy === "skip") {
|
|
12507
|
+
skipped++;
|
|
12508
|
+
continue;
|
|
12509
|
+
}
|
|
12510
|
+
store[profile.harness][profile.name] = this.toStored(profile);
|
|
12511
|
+
if (exists) {
|
|
12512
|
+
overwritten++;
|
|
12513
|
+
} else {
|
|
12514
|
+
imported++;
|
|
12515
|
+
}
|
|
12516
|
+
}
|
|
12517
|
+
this.files.writeJson(this.environment.files.profiles, store);
|
|
12518
|
+
const warnings = [];
|
|
12519
|
+
let activeRestored = 0;
|
|
12520
|
+
if (restoreActive) {
|
|
12521
|
+
for (const active of payload.active) {
|
|
12522
|
+
try {
|
|
12523
|
+
if (active.official) {
|
|
12524
|
+
this.activation.activateOfficial(active.harness);
|
|
12525
|
+
} else if (store[active.harness]?.[active.name]) {
|
|
12526
|
+
this.activation.activate(active.harness, active.name);
|
|
12527
|
+
} else {
|
|
12528
|
+
throw new Error("\u5BF9\u5E94\u914D\u7F6E\u4E0D\u5B58\u5728");
|
|
12529
|
+
}
|
|
12530
|
+
activeRestored++;
|
|
12531
|
+
} catch (error) {
|
|
12532
|
+
warnings.push(`${active.harness}/${active.name} \u672A\u80FD\u6062\u590D\u6FC0\u6D3B\u72B6\u6001\uFF1A${error.message}`);
|
|
12533
|
+
}
|
|
12534
|
+
}
|
|
12535
|
+
}
|
|
12536
|
+
return { ok: true, imported, overwritten, skipped, activeRestored, warnings };
|
|
12537
|
+
}
|
|
12538
|
+
decrypt(envelope, passphrase) {
|
|
12539
|
+
this.assertPassphrase(passphrase);
|
|
12540
|
+
const payload = decrypt(envelope, passphrase);
|
|
12541
|
+
if (payload.format !== "harness-switch-portable-config" || payload.version !== VERSION || !Array.isArray(payload.profiles) || !Array.isArray(payload.active) || typeof payload.exportedAt !== "string") {
|
|
12542
|
+
throw new HttpError(400, "\u4E0D\u652F\u6301\u7684\u5BFC\u51FA\u6587\u4EF6\u683C\u5F0F");
|
|
12543
|
+
}
|
|
12544
|
+
if (payload.profiles.length > 1e4) {
|
|
12545
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u5305\u542B\u8FC7\u591A\u914D\u7F6E");
|
|
12546
|
+
}
|
|
12547
|
+
for (const profile of payload.profiles) {
|
|
12548
|
+
if (!isPortableProfile(profile)) {
|
|
12549
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u4E2D\u7684\u914D\u7F6E\u6570\u636E\u65E0\u6548");
|
|
12550
|
+
}
|
|
12551
|
+
}
|
|
12552
|
+
for (const active of payload.active) {
|
|
12553
|
+
if (!isPortableActive(active)) {
|
|
12554
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u4E2D\u7684\u6FC0\u6D3B\u72B6\u6001\u65E0\u6548");
|
|
12555
|
+
}
|
|
12556
|
+
}
|
|
12557
|
+
return payload;
|
|
12558
|
+
}
|
|
12559
|
+
validateProfile(profile) {
|
|
12560
|
+
if (!profile.name || profile.name.length > 120 || /[\\/]/.test(profile.name)) {
|
|
12561
|
+
throw new HttpError(400, "\u5BFC\u51FA\u6587\u4EF6\u5305\u542B\u65E0\u6548\u7684\u914D\u7F6E\u540D\u79F0");
|
|
12562
|
+
}
|
|
12563
|
+
this.adapters.get(profile.harness).validate?.({
|
|
12564
|
+
name: profile.name,
|
|
12565
|
+
baseUrl: profile.baseUrl,
|
|
12566
|
+
apiKey: profile.apiKey,
|
|
12567
|
+
model: profile.model,
|
|
12568
|
+
extras: profile.extras
|
|
12569
|
+
});
|
|
12570
|
+
}
|
|
12571
|
+
toStored(profile) {
|
|
12572
|
+
return {
|
|
12573
|
+
base_url: profile.baseUrl,
|
|
12574
|
+
api_key: this.crypto.encrypt(profile.apiKey),
|
|
12575
|
+
model: profile.model,
|
|
12576
|
+
notes: profile.notes,
|
|
12577
|
+
extras: profile.extras,
|
|
12578
|
+
overrides: profile.overrides,
|
|
12579
|
+
updated_at: new Date().toISOString()
|
|
12580
|
+
};
|
|
12581
|
+
}
|
|
12582
|
+
readStore() {
|
|
12583
|
+
return this.files.readJson(this.environment.files.profiles, {});
|
|
12584
|
+
}
|
|
12585
|
+
assertPassphrase(passphrase) {
|
|
12586
|
+
if (passphrase.length < 8) {
|
|
12587
|
+
throw new HttpError(400, "\u8FC1\u79FB\u5BC6\u7801\u81F3\u5C11\u9700\u8981 8 \u4E2A\u5B57\u7B26");
|
|
12588
|
+
}
|
|
12589
|
+
}
|
|
12590
|
+
}
|
|
12591
|
+
TransferService = __decorateElement(_init, 0, "TransferService", _dec, TransferService);
|
|
12592
|
+
__runInitializers(_init, 1, TransferService);
|
|
12593
|
+
__decoratorMetadata(_init, TransferService);
|
|
12594
|
+
let _TransferService = TransferService;
|
|
12595
|
+
function encrypt(payload, passphrase) {
|
|
12596
|
+
const salt = randomBytes4(16);
|
|
12597
|
+
const iv = randomBytes4(12);
|
|
12598
|
+
const key = scryptSync(passphrase, salt, 32);
|
|
12599
|
+
const cipher = createCipheriv2("aes-256-gcm", key, iv);
|
|
12600
|
+
const data = Buffer.concat([cipher.update(JSON.stringify(payload), "utf8"), cipher.final()]);
|
|
12601
|
+
return {
|
|
12602
|
+
format: FORMAT,
|
|
12603
|
+
version: VERSION,
|
|
12604
|
+
kdf: { name: "scrypt", salt: salt.toString("base64url") },
|
|
12605
|
+
cipher: {
|
|
12606
|
+
name: "aes-256-gcm",
|
|
12607
|
+
iv: iv.toString("base64url"),
|
|
12608
|
+
tag: cipher.getAuthTag().toString("base64url"),
|
|
12609
|
+
data: data.toString("base64url")
|
|
12610
|
+
}
|
|
12611
|
+
};
|
|
12612
|
+
}
|
|
12613
|
+
function decrypt(envelope, passphrase) {
|
|
12614
|
+
try {
|
|
12615
|
+
if (envelope?.format !== FORMAT || envelope.version !== VERSION || envelope.kdf?.name !== "scrypt" || envelope.cipher?.name !== "aes-256-gcm" || typeof envelope.kdf.salt !== "string" || typeof envelope.cipher.iv !== "string" || typeof envelope.cipher.tag !== "string" || typeof envelope.cipher.data !== "string" || envelope.cipher.data.length > MAX_ENCRYPTED_BYTES) {
|
|
12616
|
+
throw new Error("invalid envelope");
|
|
12617
|
+
}
|
|
12618
|
+
const salt = Buffer.from(envelope.kdf.salt, "base64url");
|
|
12619
|
+
const key = scryptSync(passphrase, salt, 32);
|
|
12620
|
+
const decipher = createDecipheriv2("aes-256-gcm", key, Buffer.from(envelope.cipher.iv, "base64url"));
|
|
12621
|
+
decipher.setAuthTag(Buffer.from(envelope.cipher.tag, "base64url"));
|
|
12622
|
+
const plain = Buffer.concat([
|
|
12623
|
+
decipher.update(Buffer.from(envelope.cipher.data, "base64url")),
|
|
12624
|
+
decipher.final()
|
|
12625
|
+
]).toString("utf8");
|
|
12626
|
+
return JSON.parse(plain);
|
|
12627
|
+
} catch {
|
|
12628
|
+
throw new HttpError(400, "\u8FC1\u79FB\u5BC6\u7801\u9519\u8BEF\u6216\u5BFC\u51FA\u6587\u4EF6\u5DF2\u635F\u574F");
|
|
12629
|
+
}
|
|
12630
|
+
}
|
|
12631
|
+
function isStringRecord(value) {
|
|
12632
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) && Object.values(value).every((item) => typeof item === "string");
|
|
12633
|
+
}
|
|
12634
|
+
function isPortableProfile(value) {
|
|
12635
|
+
if (typeof value !== "object" || value === null) {
|
|
12636
|
+
return false;
|
|
12637
|
+
}
|
|
12638
|
+
const profile = value;
|
|
12639
|
+
return typeof profile.harness === "string" && isHarnessId(profile.harness) && typeof profile.name === "string" && typeof profile.baseUrl === "string" && typeof profile.apiKey === "string" && typeof profile.model === "string" && typeof profile.notes === "string" && isStringRecord(profile.extras) && isStringRecord(profile.overrides);
|
|
12640
|
+
}
|
|
12641
|
+
function isPortableActive(value) {
|
|
12642
|
+
if (typeof value !== "object" || value === null) {
|
|
12643
|
+
return false;
|
|
12644
|
+
}
|
|
12645
|
+
const active = value;
|
|
12646
|
+
return typeof active.harness === "string" && isHarnessId(active.harness) && typeof active.name === "string" && typeof active.official === "boolean";
|
|
12647
|
+
}
|
|
12648
|
+
|
|
12649
|
+
// src/http/routes/transfer.ts
|
|
12650
|
+
function createTransferRoutes(services) {
|
|
12651
|
+
const app = new Hono2;
|
|
12652
|
+
const transfer = services.get(ITransferService);
|
|
12653
|
+
app.post("/export", async (c) => {
|
|
12654
|
+
const body = await readBody2(c.req.json.bind(c.req));
|
|
12655
|
+
return c.json(transfer.exportAll(String(body.passphrase ?? "")));
|
|
12656
|
+
});
|
|
12657
|
+
app.post("/preview", async (c) => {
|
|
12658
|
+
const body = await readBody2(c.req.json.bind(c.req));
|
|
12659
|
+
return c.json(transfer.preview(requireEnvelope(body.envelope), String(body.passphrase ?? "")));
|
|
12660
|
+
});
|
|
12661
|
+
app.post("/import", async (c) => {
|
|
12662
|
+
const body = await readBody2(c.req.json.bind(c.req));
|
|
12663
|
+
return c.json(transfer.importAll(requireEnvelope(body.envelope), String(body.passphrase ?? ""), body.conflictPolicy ?? "skip", body.restoreActive === true));
|
|
12664
|
+
});
|
|
12665
|
+
return app;
|
|
12666
|
+
}
|
|
12667
|
+
function requireEnvelope(envelope) {
|
|
12668
|
+
if (!envelope || typeof envelope !== "object") {
|
|
12669
|
+
throw new HttpError(400, "\u7F3A\u5C11\u6709\u6548\u7684\u5BFC\u51FA\u6587\u4EF6");
|
|
12670
|
+
}
|
|
12671
|
+
return envelope;
|
|
12672
|
+
}
|
|
12673
|
+
async function readBody2(read) {
|
|
12674
|
+
return read().catch(() => {
|
|
12675
|
+
throw new HttpError(400, "invalid json");
|
|
12676
|
+
});
|
|
12677
|
+
}
|
|
12678
|
+
|
|
12374
12679
|
// src/app.ts
|
|
12375
12680
|
function createApp(services) {
|
|
12376
12681
|
const app = new Hono2;
|
|
@@ -12387,6 +12692,8 @@ function createApp(services) {
|
|
|
12387
12692
|
api.use("/backups/*", createAuthGuard(services));
|
|
12388
12693
|
api.use("/backups", createAuthGuard(services));
|
|
12389
12694
|
api.route("/backups", createBackupRoutes(services));
|
|
12695
|
+
api.use("/transfer/*", createAuthGuard(services));
|
|
12696
|
+
api.route("/transfer", createTransferRoutes(services));
|
|
12390
12697
|
app.route("/api", api);
|
|
12391
12698
|
const publicDir = environment.publicDir;
|
|
12392
12699
|
if (existsSync3(publicDir)) {
|
|
@@ -12432,6 +12739,7 @@ function createServices() {
|
|
|
12432
12739
|
collection.set(ILiveWriteService, new SyncDescriptor(LiveWriteService));
|
|
12433
12740
|
collection.set(IProfileService, new SyncDescriptor(ProfileService));
|
|
12434
12741
|
collection.set(IActivationService, new SyncDescriptor(ActivationService));
|
|
12742
|
+
collection.set(ITransferService, new SyncDescriptor(TransferService));
|
|
12435
12743
|
return new InstantiationService(collection);
|
|
12436
12744
|
}
|
|
12437
12745
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seaveyon/harness-switch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "A secure web control plane that switches API providers for Claude Code, Codex, Kimi Code, Pi and DeepSeek Harness by writing their native config files on SSH servers.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"claude-code",
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|