@seaveyon/harness-switch 0.20.0 → 0.21.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/dist/harness-switch.js
CHANGED
|
@@ -7031,8 +7031,8 @@ import { once } from "node:events";
|
|
|
7031
7031
|
import { createServer } from "node:http";
|
|
7032
7032
|
|
|
7033
7033
|
// src/app.ts
|
|
7034
|
-
import { existsSync as
|
|
7035
|
-
import { extname, join as
|
|
7034
|
+
import { existsSync as existsSync5, readFileSync as readFileSync4 } from "node:fs";
|
|
7035
|
+
import { extname, join as join14, relative, resolve as resolve3 } from "node:path";
|
|
7036
7036
|
|
|
7037
7037
|
// ../../node_modules/hono/dist/compose.js
|
|
7038
7038
|
var compose = (middleware, onError, onNotFound) => {
|
|
@@ -8645,6 +8645,7 @@ var ERROR_CODES = {
|
|
|
8645
8645
|
syncSourceCacheMissing: "sync.sourceCodexLoginCacheMissing",
|
|
8646
8646
|
syncSourceEqualsTarget: "sync.sourceEqualsTarget",
|
|
8647
8647
|
userNotManageable: "user.notManageable",
|
|
8648
|
+
userNotSwitchable: "user.notSwitchable",
|
|
8648
8649
|
storageUnreadable: "storage.unreadable",
|
|
8649
8650
|
storageQuarantineFailed: "storage.quarantineFailed",
|
|
8650
8651
|
storageCorruptQuarantined: "storage.corruptQuarantined"
|
|
@@ -8696,6 +8697,13 @@ var PROBE_CODES = {
|
|
|
8696
8697
|
httpError: "probe.httpError",
|
|
8697
8698
|
invalidResponse: "probe.invalidResponse"
|
|
8698
8699
|
};
|
|
8700
|
+
var USER_BLOCK_CODES = {
|
|
8701
|
+
homeUnsearchable: "user.block.homeUnsearchable",
|
|
8702
|
+
homeUnwritable: "user.block.homeUnwritable",
|
|
8703
|
+
storeInaccessible: "user.block.storeInaccessible",
|
|
8704
|
+
ownershipRequiresRoot: "user.block.ownershipRequiresRoot",
|
|
8705
|
+
homeMissing: "user.block.homeMissing"
|
|
8706
|
+
};
|
|
8699
8707
|
// ../../packages/shared/src/harnesses.ts
|
|
8700
8708
|
var HARNESS_IDS = ["claude", "codex", "kimi", "pi", "dsh"];
|
|
8701
8709
|
var HARNESS_LABELS = {
|
|
@@ -23498,7 +23506,7 @@ class EnvironmentService {
|
|
|
23498
23506
|
};
|
|
23499
23507
|
}
|
|
23500
23508
|
get writeRoots() {
|
|
23501
|
-
const roots = [this.
|
|
23509
|
+
const roots = [this.managerDataDir, this.homeDir, this.dataDir];
|
|
23502
23510
|
for (const home of Object.values(this.harnessHomes)) {
|
|
23503
23511
|
if (!isInside(this.homeDir, home)) {
|
|
23504
23512
|
roots.push(home);
|
|
@@ -23823,13 +23831,24 @@ class LogService {
|
|
|
23823
23831
|
console.warn(`[harness-switch] ${message}`);
|
|
23824
23832
|
}
|
|
23825
23833
|
error(message, error51) {
|
|
23826
|
-
|
|
23827
|
-
console.error(`[harness-switch] ${message}`, error51);
|
|
23828
|
-
return;
|
|
23829
|
-
}
|
|
23830
|
-
console.error(`[harness-switch] ${message}`);
|
|
23834
|
+
console.error(`[harness-switch] ${message}${formatError2(error51)}`);
|
|
23831
23835
|
}
|
|
23832
23836
|
}
|
|
23837
|
+
function formatError2(error51) {
|
|
23838
|
+
if (error51 === undefined)
|
|
23839
|
+
return "";
|
|
23840
|
+
if (!(error51 instanceof Error))
|
|
23841
|
+
return ` | thrown=${String(error51)}`;
|
|
23842
|
+
const errno = error51;
|
|
23843
|
+
const details = [`${error51.name}: ${error51.message}`];
|
|
23844
|
+
if (errno.code)
|
|
23845
|
+
details.push(`code=${errno.code}`);
|
|
23846
|
+
if (errno.syscall)
|
|
23847
|
+
details.push(`syscall=${errno.syscall}`);
|
|
23848
|
+
if (errno.path)
|
|
23849
|
+
details.push(`path=${errno.path}`);
|
|
23850
|
+
return ` | ${details.join(" | ")}`;
|
|
23851
|
+
}
|
|
23833
23852
|
|
|
23834
23853
|
// src/services/auth.ts
|
|
23835
23854
|
var STORE_VERSION = 1;
|
|
@@ -29779,6 +29798,98 @@ function createUpdateRoutes(_services) {
|
|
|
29779
29798
|
return api2;
|
|
29780
29799
|
}
|
|
29781
29800
|
|
|
29801
|
+
// src/services/user-access.ts
|
|
29802
|
+
import { accessSync as accessSync2, constants as constants2, existsSync as existsSync4, readFileSync as readFileSync3 } from "node:fs";
|
|
29803
|
+
import { join as join13 } from "node:path";
|
|
29804
|
+
var IUserAccessService = createDecorator("userAccessService");
|
|
29805
|
+
var _dec = [
|
|
29806
|
+
inject(IEnvironmentService)
|
|
29807
|
+
];
|
|
29808
|
+
var _init = __decoratorStart(undefined);
|
|
29809
|
+
|
|
29810
|
+
class UserAccessService {
|
|
29811
|
+
environment;
|
|
29812
|
+
constructor(environment) {
|
|
29813
|
+
this.environment = environment;
|
|
29814
|
+
}
|
|
29815
|
+
inspect(user) {
|
|
29816
|
+
if (user.username === this.environment.defaultUser.username) {
|
|
29817
|
+
return { ok: true };
|
|
29818
|
+
}
|
|
29819
|
+
if (process.platform === "win32") {
|
|
29820
|
+
return { ok: true };
|
|
29821
|
+
}
|
|
29822
|
+
if (!existsSync4(user.homeDir)) {
|
|
29823
|
+
return this.blocked(USER_BLOCK_CODES.homeMissing, `主目录 ${user.homeDir} 不存在`, {
|
|
29824
|
+
home: user.homeDir
|
|
29825
|
+
});
|
|
29826
|
+
}
|
|
29827
|
+
if (!can(user.homeDir, constants2.X_OK)) {
|
|
29828
|
+
return this.blocked(USER_BLOCK_CODES.homeUnsearchable, `无法进入 ${user.username} 的主目录 ${user.homeDir}`, { username: user.username, home: user.homeDir });
|
|
29829
|
+
}
|
|
29830
|
+
const store = this.storeDir(user);
|
|
29831
|
+
const anchor = existsSync4(store) ? store : user.homeDir;
|
|
29832
|
+
if (!can(anchor, constants2.W_OK) || !can(anchor, constants2.X_OK)) {
|
|
29833
|
+
return anchor === store ? this.blocked(USER_BLOCK_CODES.storeInaccessible, `${user.username} 的配置目录 ${store} 不可写`, { username: user.username, path: store }) : this.blocked(USER_BLOCK_CODES.homeUnwritable, `${user.username} 的主目录 ${user.homeDir} 不可写`, { username: user.username, home: user.homeDir });
|
|
29834
|
+
}
|
|
29835
|
+
if (!this.canOwnAs(user)) {
|
|
29836
|
+
return this.blocked(USER_BLOCK_CODES.ownershipRequiresRoot, `把文件所有权设置为 ${user.username} 需要以 root 运行或授予 CAP_CHOWN`, { username: user.username });
|
|
29837
|
+
}
|
|
29838
|
+
return { ok: true };
|
|
29839
|
+
}
|
|
29840
|
+
storeDir(user) {
|
|
29841
|
+
return join13(user.homeDir, ".harness-switch");
|
|
29842
|
+
}
|
|
29843
|
+
canOwnAs(user) {
|
|
29844
|
+
const uid = process.getuid?.();
|
|
29845
|
+
const gid = process.getgid?.();
|
|
29846
|
+
if (uid === undefined || gid === undefined) {
|
|
29847
|
+
return true;
|
|
29848
|
+
}
|
|
29849
|
+
if (uid === user.uid && (gid === user.gid || groupsOf().includes(user.gid))) {
|
|
29850
|
+
return true;
|
|
29851
|
+
}
|
|
29852
|
+
return hasChownCapability(uid);
|
|
29853
|
+
}
|
|
29854
|
+
blocked(code, reason2, params) {
|
|
29855
|
+
return { ok: false, code, reason: reason2, params };
|
|
29856
|
+
}
|
|
29857
|
+
}
|
|
29858
|
+
UserAccessService = __decorateElement(_init, 0, "UserAccessService", _dec, UserAccessService);
|
|
29859
|
+
__runInitializers(_init, 1, UserAccessService);
|
|
29860
|
+
__decoratorMetadata(_init, UserAccessService);
|
|
29861
|
+
let _UserAccessService = UserAccessService;
|
|
29862
|
+
function can(path, mode) {
|
|
29863
|
+
try {
|
|
29864
|
+
accessSync2(path, mode);
|
|
29865
|
+
return true;
|
|
29866
|
+
} catch {
|
|
29867
|
+
return false;
|
|
29868
|
+
}
|
|
29869
|
+
}
|
|
29870
|
+
var cachedGroups;
|
|
29871
|
+
function groupsOf() {
|
|
29872
|
+
cachedGroups ??= process.getgroups?.() ?? [];
|
|
29873
|
+
return cachedGroups;
|
|
29874
|
+
}
|
|
29875
|
+
var CAP_CHOWN_BIT = 1n;
|
|
29876
|
+
var cachedChownCapability;
|
|
29877
|
+
function hasChownCapability(uid) {
|
|
29878
|
+
if (cachedChownCapability === undefined) {
|
|
29879
|
+
const effective = readEffectiveCaps();
|
|
29880
|
+
cachedChownCapability = effective === undefined ? uid === 0 : (effective & CAP_CHOWN_BIT) !== 0n;
|
|
29881
|
+
}
|
|
29882
|
+
return cachedChownCapability;
|
|
29883
|
+
}
|
|
29884
|
+
function readEffectiveCaps() {
|
|
29885
|
+
try {
|
|
29886
|
+
const match2 = /^CapEff:\s*([0-9a-fA-F]+)$/m.exec(readFileSync3("/proc/self/status", "utf8"));
|
|
29887
|
+
return match2 ? BigInt(`0x${match2[1]}`) : undefined;
|
|
29888
|
+
} catch {
|
|
29889
|
+
return;
|
|
29890
|
+
}
|
|
29891
|
+
}
|
|
29892
|
+
|
|
29782
29893
|
// src/services/user-sync.ts
|
|
29783
29894
|
import { isDeepStrictEqual as isDeepStrictEqual2 } from "node:util";
|
|
29784
29895
|
var IUserSyncService = createDecorator("userSyncService");
|
|
@@ -30064,12 +30175,26 @@ function createUserRoutes(services) {
|
|
|
30064
30175
|
const auth = services.get(IAuthService);
|
|
30065
30176
|
const environment = services.get(IEnvironmentService);
|
|
30066
30177
|
const users = services.get(IUserService);
|
|
30178
|
+
const access = services.get(IUserAccessService);
|
|
30067
30179
|
const sync = services.get(IUserSyncService);
|
|
30068
30180
|
app.get("/", (c) => {
|
|
30069
30181
|
const current = environment.currentUser.username;
|
|
30070
30182
|
return c.json({
|
|
30071
30183
|
currentUser: current,
|
|
30072
|
-
items: users.list().map((user) =>
|
|
30184
|
+
items: users.list().map((user) => {
|
|
30185
|
+
const verdict = access.inspect(user);
|
|
30186
|
+
const base = {
|
|
30187
|
+
...user,
|
|
30188
|
+
current: user.username === current,
|
|
30189
|
+
manageable: verdict.ok
|
|
30190
|
+
};
|
|
30191
|
+
return verdict.ok ? base : {
|
|
30192
|
+
...base,
|
|
30193
|
+
blockCode: verdict.code,
|
|
30194
|
+
blockParams: verdict.params,
|
|
30195
|
+
blockReason: verdict.reason
|
|
30196
|
+
};
|
|
30197
|
+
})
|
|
30073
30198
|
});
|
|
30074
30199
|
});
|
|
30075
30200
|
app.post("/sync/preview", async (c) => {
|
|
@@ -30085,6 +30210,13 @@ function createUserRoutes(services) {
|
|
|
30085
30210
|
const token = getCookie(c, environment.cookieName);
|
|
30086
30211
|
if (!token)
|
|
30087
30212
|
throw new HttpError(401, "authentication required");
|
|
30213
|
+
const verdict = access.inspect(user);
|
|
30214
|
+
if (!verdict.ok) {
|
|
30215
|
+
throw new HttpError(403, verdict.reason, {
|
|
30216
|
+
code: ERROR_CODES.userNotSwitchable,
|
|
30217
|
+
params: { username: user.username, ...verdict.params }
|
|
30218
|
+
});
|
|
30219
|
+
}
|
|
30088
30220
|
auth.selectUser(token, user.username);
|
|
30089
30221
|
return c.json({ currentUser: user.username });
|
|
30090
30222
|
});
|
|
@@ -30139,20 +30271,20 @@ function createApp(services) {
|
|
|
30139
30271
|
api2.route("/drift", createDriftRoutes(services));
|
|
30140
30272
|
app.route("/api", api2);
|
|
30141
30273
|
const publicDir = environment.publicDir;
|
|
30142
|
-
if (
|
|
30274
|
+
if (existsSync5(publicDir)) {
|
|
30143
30275
|
app.get("*", async (c, next) => {
|
|
30144
30276
|
if (c.req.path.startsWith("/api") || c.req.path === "/healthz") {
|
|
30145
30277
|
return next();
|
|
30146
30278
|
}
|
|
30147
30279
|
const assetPath = resolve3(publicDir, c.req.path.replace(/^\//, ""));
|
|
30148
|
-
if (c.req.path !== "/" && isPublicAsset(publicDir, assetPath) &&
|
|
30280
|
+
if (c.req.path !== "/" && isPublicAsset(publicDir, assetPath) && existsSync5(assetPath)) {
|
|
30149
30281
|
c.header("Content-Type", contentType(assetPath));
|
|
30150
|
-
return c.body(
|
|
30282
|
+
return c.body(readFileSync4(assetPath));
|
|
30151
30283
|
}
|
|
30152
|
-
const index =
|
|
30153
|
-
if (
|
|
30284
|
+
const index = join14(publicDir, "index.html");
|
|
30285
|
+
if (existsSync5(index)) {
|
|
30154
30286
|
c.header("Cache-Control", "no-store");
|
|
30155
|
-
return c.html(
|
|
30287
|
+
return c.html(readFileSync4(index, "utf8"));
|
|
30156
30288
|
}
|
|
30157
30289
|
return c.text("frontend not built", 503);
|
|
30158
30290
|
});
|
|
@@ -30170,7 +30302,7 @@ function createApp(services) {
|
|
|
30170
30302
|
...error52.params ? { params: error52.params } : {}
|
|
30171
30303
|
}, error52.status);
|
|
30172
30304
|
}
|
|
30173
|
-
log.error(`unhandled error on ${request}
|
|
30305
|
+
log.error(`unhandled error on ${request}`, error52);
|
|
30174
30306
|
return c.json({ error: "internal server error" }, 500);
|
|
30175
30307
|
});
|
|
30176
30308
|
return app;
|
|
@@ -30197,20 +30329,6 @@ function contentType(path) {
|
|
|
30197
30329
|
return "application/octet-stream";
|
|
30198
30330
|
}
|
|
30199
30331
|
}
|
|
30200
|
-
function describe3(error52) {
|
|
30201
|
-
if (!(error52 instanceof Error)) {
|
|
30202
|
-
return `non-error thrown: ${String(error52)}`;
|
|
30203
|
-
}
|
|
30204
|
-
const errno = error52;
|
|
30205
|
-
const parts = [error52.name, error52.message];
|
|
30206
|
-
if (errno.code)
|
|
30207
|
-
parts.push(`code=${errno.code}`);
|
|
30208
|
-
if (errno.syscall)
|
|
30209
|
-
parts.push(`syscall=${errno.syscall}`);
|
|
30210
|
-
if (errno.path)
|
|
30211
|
-
parts.push(`path=${errno.path}`);
|
|
30212
|
-
return parts.join(" ");
|
|
30213
|
-
}
|
|
30214
30332
|
|
|
30215
30333
|
// src/bootstrap.ts
|
|
30216
30334
|
function createServices() {
|
|
@@ -30218,6 +30336,7 @@ function createServices() {
|
|
|
30218
30336
|
collection.set(ILogService, new SyncDescriptor(LogService));
|
|
30219
30337
|
collection.set(IEnvironmentService, new SyncDescriptor(EnvironmentService));
|
|
30220
30338
|
collection.set(IUserService, new SyncDescriptor(UserService));
|
|
30339
|
+
collection.set(IUserAccessService, new SyncDescriptor(UserAccessService));
|
|
30221
30340
|
collection.set(IUserSyncService, new SyncDescriptor(UserSyncService));
|
|
30222
30341
|
collection.set(IFileService, new SyncDescriptor(FileService));
|
|
30223
30342
|
collection.set(ICodexLoginCacheService, new SyncDescriptor(CodexLoginCacheService));
|
|
@@ -30349,33 +30468,33 @@ import { stdin, stdout } from "node:process";
|
|
|
30349
30468
|
import { createInterface } from "node:readline/promises";
|
|
30350
30469
|
|
|
30351
30470
|
// src/cli/client.ts
|
|
30352
|
-
import { readFileSync as
|
|
30353
|
-
import { join as
|
|
30471
|
+
import { readFileSync as readFileSync6 } from "node:fs";
|
|
30472
|
+
import { join as join16 } from "node:path";
|
|
30354
30473
|
|
|
30355
30474
|
// src/daemon.ts
|
|
30356
30475
|
import { spawn as spawn2 } from "node:child_process";
|
|
30357
30476
|
import { randomBytes as randomBytes5 } from "node:crypto";
|
|
30358
|
-
import { closeSync, mkdirSync as mkdirSync4, openSync as openSync2, readFileSync as
|
|
30477
|
+
import { closeSync, mkdirSync as mkdirSync4, openSync as openSync2, readFileSync as readFileSync5, unlinkSync, writeFileSync as writeFileSync2 } from "node:fs";
|
|
30359
30478
|
import { readFile as readFile2 } from "node:fs/promises";
|
|
30360
30479
|
import { homedir as homedir3 } from "node:os";
|
|
30361
|
-
import { join as
|
|
30480
|
+
import { join as join15, resolve as resolve4 } from "node:path";
|
|
30362
30481
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
30363
30482
|
var DEFAULT_HOST = "127.0.0.1";
|
|
30364
30483
|
var DEFAULT_PORT = 8787;
|
|
30365
30484
|
var STOP_TIMEOUT_MS = 5000;
|
|
30366
30485
|
var START_TIMEOUT_MS = 5000;
|
|
30367
30486
|
function daemonDataDir() {
|
|
30368
|
-
return process.env.HSW_DATA_DIR ||
|
|
30487
|
+
return process.env.HSW_DATA_DIR || join15(homedir3(), ".harness-switch");
|
|
30369
30488
|
}
|
|
30370
30489
|
function daemonPidFile() {
|
|
30371
|
-
return
|
|
30490
|
+
return join15(daemonDataDir(), "daemon.pid");
|
|
30372
30491
|
}
|
|
30373
30492
|
function daemonLogFile() {
|
|
30374
|
-
return
|
|
30493
|
+
return join15(daemonDataDir(), "daemon.log");
|
|
30375
30494
|
}
|
|
30376
30495
|
function readRecord() {
|
|
30377
30496
|
try {
|
|
30378
|
-
const raw2 =
|
|
30497
|
+
const raw2 = readFileSync5(daemonPidFile(), "utf8").trim();
|
|
30379
30498
|
if (/^\d+$/.test(raw2)) {
|
|
30380
30499
|
const pid = Number(raw2);
|
|
30381
30500
|
return Number.isInteger(pid) && pid > 0 ? { version: 0, pid } : null;
|
|
@@ -30450,7 +30569,7 @@ async function printStatus() {
|
|
|
30450
30569
|
console.log(`harness-switch daemon: ${running ? `running (pid ${record2.pid})` : "not running"}`);
|
|
30451
30570
|
console.log(` url: ${httpOrigin(host, port)}`);
|
|
30452
30571
|
console.log(` log: ${daemonLogFile()}`);
|
|
30453
|
-
console.log(` password: ${
|
|
30572
|
+
console.log(` password: ${join15(daemonDataDir(), "web_password")}`);
|
|
30454
30573
|
console.log(` data: ${daemonDataDir()}`);
|
|
30455
30574
|
return running ? 0 : 1;
|
|
30456
30575
|
}
|
|
@@ -30507,7 +30626,7 @@ async function daemonize() {
|
|
|
30507
30626
|
console.log(` url: http://${host}:${port}`);
|
|
30508
30627
|
console.log(` log: ${logPath}`);
|
|
30509
30628
|
console.log(" manage: harness-switch status | harness-switch stop");
|
|
30510
|
-
console.log(` password (first run): ${
|
|
30629
|
+
console.log(` password (first run): ${join15(daemonDataDir(), "web_password")}`);
|
|
30511
30630
|
return 0;
|
|
30512
30631
|
}
|
|
30513
30632
|
async function isOwnedDaemon(record2) {
|
|
@@ -30521,14 +30640,14 @@ async function isOwnedDaemon(record2) {
|
|
|
30521
30640
|
}
|
|
30522
30641
|
function processHasToken(pid, token) {
|
|
30523
30642
|
try {
|
|
30524
|
-
return
|
|
30643
|
+
return readFileSync5(`/proc/${pid}/environ`, "utf8").split("\x00").includes(`HSW_DAEMON_TOKEN=${token}`);
|
|
30525
30644
|
} catch {
|
|
30526
30645
|
return false;
|
|
30527
30646
|
}
|
|
30528
30647
|
}
|
|
30529
30648
|
function processLooksLikeHarnessSwitch(pid) {
|
|
30530
30649
|
try {
|
|
30531
|
-
return
|
|
30650
|
+
return readFileSync5(`/proc/${pid}/cmdline`, "utf8").includes("harness-switch");
|
|
30532
30651
|
} catch {
|
|
30533
30652
|
return false;
|
|
30534
30653
|
}
|
|
@@ -30642,9 +30761,9 @@ function resolveBaseUrl() {
|
|
|
30642
30761
|
return process.env.HSW_URL || `http://127.0.0.1:${process.env.PORT || 8787}`;
|
|
30643
30762
|
}
|
|
30644
30763
|
function readWebPassword() {
|
|
30645
|
-
const file2 =
|
|
30764
|
+
const file2 = join16(daemonDataDir(), "web_password");
|
|
30646
30765
|
try {
|
|
30647
|
-
return
|
|
30766
|
+
return readFileSync6(file2, "utf8").trim();
|
|
30648
30767
|
} catch {
|
|
30649
30768
|
throw new CliError(`未找到 ${file2}。请先运行一次服务(harness-switch daemon 或 server)生成密码,再使用 CLI。`);
|
|
30650
30769
|
}
|
|
@@ -30947,7 +31066,8 @@ async function cmdUsers(client, json2) {
|
|
|
30947
31066
|
printJson(payload);
|
|
30948
31067
|
} else {
|
|
30949
31068
|
for (const user of payload.items) {
|
|
30950
|
-
|
|
31069
|
+
const note = user.manageable === false ? ` (${user.blockReason ?? "不可切换"})` : "";
|
|
31070
|
+
console.log(`${user.current ? "*" : " "} ${user.username.padEnd(20)} ${user.homeDir}${note}`);
|
|
30951
31071
|
}
|
|
30952
31072
|
}
|
|
30953
31073
|
return 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seaveyon/harness-switch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.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",
|