checkIn() opened a fake over the one the test had already opened; closing
the second put the first's 127.0.0.1 origin back as trae-cn's hosts, so
global.test.mjs, run after it in one process, read a fake host. The
publish run for 2009f49 went red on it and 0.2.14 never reached npm.
checkIn now closes the fake before it opens another.
Ran: HOME=$(mktemp -d) bun test at the repo root: 1509 pass, 0 fail.
Broken by hand: without the close, `bun test ./ownclient.test.mjs
./global.test.mjs` fails "the realm's table is its own" (26 pass, 1 fail),
as the publish run did.
162 lines
7.6 KiB
JavaScript
162 lines
7.6 KiB
JavaScript
// The account this machine's own Trae CN client is signed in as checks in
|
|
// on the client's device id, read from the client's storage.json and
|
|
// never written; every other account keeps its own (yetone/magpie#1920).
|
|
import "./nonet.mjs"
|
|
import { afterEach, beforeEach, expect, test } from "bun:test"
|
|
import { createCipheriv, createHash, randomBytes } from "node:crypto"
|
|
import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import { TraeCNAuthPlugin, _internal } from "./index.mjs"
|
|
import { fakeTrae, json, signedIn } from "./fake.mjs"
|
|
|
|
const DC = "1234567890123456" // the client's: 16 digits, as #808's
|
|
let f, dir, file
|
|
|
|
beforeEach(() => {
|
|
dir = mkdtempSync(join(tmpdir(), "trae-own-"))
|
|
file = join(dir, "storage.json")
|
|
_internal.ownClient.paths = () => [join(dir, "missing.json"), file]
|
|
})
|
|
afterEach(() => {
|
|
f?.close()
|
|
f = undefined
|
|
_internal.ownClient.paths = () => []
|
|
rmSync(dir, { recursive: true, force: true })
|
|
})
|
|
|
|
// encode is the client's codec run forwards, the inverse of clientDecode
|
|
function encode(text, header = "746305100000") {
|
|
const salt = header === "746305100000"
|
|
? xor([82, 9, 106, 213, 48, 54, 165, 56, 191, 64, 163, 158, 129, 243, 215, 251, 124, 227, 57, 130, 155, 47, 255, 135, 52, 142, 67, 68, 196, 222, 233, 203, 84, 123, 148, 50, 166, 194, 35, 61, 238, 76, 149, 11, 66, 250, 195, 78, 8, 46, 161, 102, 40, 217, 36, 178, 118, 91, 162, 73, 109, 139, 209, 37],
|
|
[31, 221, 168, 51, 136, 7, 199, 49, 177, 18, 16, 89, 39, 128, 236, 95, 96, 81, 127, 169, 25, 181, 74, 13, 45, 229, 122, 159, 147, 201, 156, 239, 160, 224, 59, 77, 174, 42, 245, 176, 200, 235, 187, 60, 131, 83, 153, 97, 23, 43, 4, 126, 186, 119, 214, 38, 225, 105, 20, 99, 85, 33, 12, 125])
|
|
: undefined
|
|
const random = randomBytes(32)
|
|
const key = createHash("sha512").update(Buffer.concat([createHash("sha512").update(random).digest(), salt])).digest()
|
|
const body = Buffer.from(text, "utf8")
|
|
const c = createCipheriv("aes-128-cbc", key.subarray(0, 16), key.subarray(16, 32))
|
|
const enc = Buffer.concat([c.update(Buffer.concat([createHash("sha512").update(body).digest(), body])), c.final()])
|
|
return Buffer.concat([Buffer.from(header, "hex"), random, enc]).toString("base64")
|
|
}
|
|
const xor = (a, b) => Buffer.from(a.map((x, i) => x ^ b[i]))
|
|
|
|
function client(userId, { encoded = true, dcs = [DC] } = {}) {
|
|
const auth = JSON.stringify({ token: "client-jwt", refreshToken: "client-refresh", userId, host: "https://api.trae.cn" })
|
|
const doc = { "telemetry.machineId": "m".repeat(64), [`iCubeAuthInfo://icube.cloudide`]: encoded ? encode(auth) : auth }
|
|
for (const d of dcs) doc[`iCubeAuthInfo://icube-dc:${d}`] = "device-key-pair"
|
|
writeFileSync(file, JSON.stringify(doc))
|
|
}
|
|
|
|
async function checkIn(auth = signedIn()) {
|
|
// one fake at a time: a second opened over the first would put the
|
|
// first's host back as the realm's own when it closes
|
|
f?.close()
|
|
f = fakeTrae()
|
|
f.route("POST /trae/api/v2/ug/checkin_credits/claim", () => json({ code: 0 }))
|
|
f.route("POST /trae/api/v2/pay/ide_user_ent_usage", () => json({ code: 0 }))
|
|
const o = await (await TraeCNAuthPlugin({ client: {} })).auth.loader(async () => auth)
|
|
await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/claim", { method: "POST", body: "{}" })
|
|
return f.seen.at(-1).headers
|
|
}
|
|
|
|
const plugins = (h) => {
|
|
expect(h.get("x-device-id")).toBe("1234567890123456789")
|
|
expect(h.get("x-device-brand")).toBeTruthy()
|
|
expect(h.get("x-device-type")).toBe("Windows")
|
|
expect(h.get("x-os-version")).toBe("10.0.22631")
|
|
}
|
|
|
|
test("the account the client is signed in as checks in on the client's device and this machine's OS", async () => {
|
|
client("u-1")
|
|
const before = readFileSync(file, "utf8")
|
|
const mtime = statSync(file).mtimeMs
|
|
const h = await checkIn()
|
|
expect(h.get("x-device-id")).toBe(DC)
|
|
expect(h.has("x-device-brand")).toBe(false)
|
|
const m = _internal.thisMachine()
|
|
expect(h.get("x-device-type")).toBe(m.type)
|
|
expect(h.get("x-os-version")).toBe(m.os)
|
|
// the account's own token, never the client's
|
|
expect(h.get("authorization")).toBe("Cloud-IDE-JWT jwt-1")
|
|
expect(h.get("x-app-version")).toBe("0.1.69")
|
|
// read, never written
|
|
expect(readFileSync(file, "utf8")).toBe(before)
|
|
expect(statSync(file).mtimeMs).toBe(mtime)
|
|
})
|
|
|
|
test("a client's sign-in stored as plain JSON is read too", async () => {
|
|
client("u-1", { encoded: false })
|
|
expect((await checkIn()).get("x-device-id")).toBe(DC)
|
|
})
|
|
|
|
test("an account the client isn't signed in as keeps its own device", async () => {
|
|
client("someone-else")
|
|
plugins(await checkIn())
|
|
})
|
|
|
|
test("an account with no uid keeps its own device", async () => {
|
|
client("")
|
|
plugins(await checkIn(signedIn({ uid: "" })))
|
|
})
|
|
|
|
test("a client with no single device names none", async () => {
|
|
client("u-1", { dcs: [] })
|
|
plugins(await checkIn())
|
|
client("u-1", { dcs: [DC, "6543210987654321"] })
|
|
plugins(await checkIn())
|
|
client("u-1", { dcs: ["not-digits"] })
|
|
plugins(await checkIn())
|
|
})
|
|
|
|
test("a client file that can't be made out names no device", async () => {
|
|
writeFileSync(file, "{not json")
|
|
plugins(await checkIn())
|
|
writeFileSync(file, JSON.stringify({ [`iCubeAuthInfo://icube-dc:${DC}`]: "x", "iCubeAuthInfo://icube.cloudide": "dGFtcGVyZWQ=" + "A".repeat(200) }))
|
|
plugins(await checkIn())
|
|
// a value whose check fails (one byte changed) is not read as anyone
|
|
const bad = Buffer.from(encode(JSON.stringify({ userId: "u-1" })), "base64")
|
|
bad[bad.length - 20] ^= 1
|
|
writeFileSync(file, JSON.stringify({ [`iCubeAuthInfo://icube-dc:${DC}`]: "x", "iCubeAuthInfo://icube.cloudide": bad.toString("base64") }))
|
|
plugins(await checkIn())
|
|
})
|
|
|
|
test("no client on this machine: the account's own device, as before", async () => {
|
|
plugins(await checkIn())
|
|
})
|
|
|
|
test("only the check-in goes on the client's device; the chat's pages keep the account's", async () => {
|
|
client("u-1")
|
|
f = fakeTrae()
|
|
f.route("POST /trae/api/v2/pay/ide_user_ent_usage", () => json({ code: 0 }))
|
|
const o = await (await TraeCNAuthPlugin({ client: {} })).auth.loader(async () => signedIn())
|
|
await o.fetch(f.origin + "/trae/api/v2/pay/ide_user_ent_usage", { method: "POST", body: "{}" })
|
|
expect(f.seen[0].headers.get("x-device-id")).toBe("1234567890123456789")
|
|
})
|
|
|
|
test("the client's codec decodes what it encodes, and refuses another header", () => {
|
|
expect(_internal.clientDecode(encode('{"userId":"7"}'))).toBe('{"userId":"7"}')
|
|
expect(() => _internal.clientDecode(Buffer.concat([Buffer.from("000000000000", "hex"), randomBytes(120)]).toString("base64"))).toThrow()
|
|
})
|
|
|
|
test("where each OS keeps the client's storage.json", () => {
|
|
const p = _internal.clientStoragePaths
|
|
expect(p("darwin", "/Users/a", {})).toEqual(["/Users/a/Library/Application Support/Trae CN/User/globalStorage/storage.json"])
|
|
const w = p("win32", "/h", { APPDATA: "/h/Roam" })
|
|
expect(w).toEqual([
|
|
"/h/Roam/Trae CN/User/globalStorage/storage.json",
|
|
"/h/Roam/trae-cn/User/globalStorage/storage.json",
|
|
"/h/AppData/Roaming/Trae CN/User/globalStorage/storage.json",
|
|
"/h/AppData/Roaming/trae-cn/User/globalStorage/storage.json",
|
|
])
|
|
expect(p("linux", "/home/a", {})[0]).toBe("/home/a/.config/trae-cn/User/globalStorage/storage.json")
|
|
expect(p("linux", "/home/a", { XDG_CONFIG_HOME: "/x" })[0]).toBe("/x/trae-cn/User/globalStorage/storage.json")
|
|
})
|
|
|
|
test("this machine's OS as the client names it", () => {
|
|
const w = _internal.thisMachine("win32")
|
|
expect(w.type).toBe("windows")
|
|
expect(w.os).toMatch(/^Windows /)
|
|
expect(_internal.thisMachine("darwin")).toEqual({ type: "mac", os: expect.stringMatching(/^macOS \d/) })
|
|
expect(_internal.thisMachine("linux").type).toBe("linux")
|
|
})
|