forked from decolua/9router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraycastLocal.js
More file actions
160 lines (143 loc) · 4.43 KB
/
Copy pathraycastLocal.js
File metadata and controls
160 lines (143 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { copyFileSync, existsSync, mkdtempSync, readFileSync, rmdirSync, unlinkSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join } from "node:path";
const RAYCAST_SALT = "yvkwWXzxPPBAqY2tmaKrB*DvYjjMaeEf";
const RAYCAST_SUPPORT = join(homedir(), "Library", "Application Support", "com.raycast.macos");
const RAYCAST_DB = join(RAYCAST_SUPPORT, "raycast-enc.sqlite");
const POSTHOG_DISTINCT = join(RAYCAST_SUPPORT, "posthog.distinctId");
function readKeychainJson(account) {
try {
const raw = execFileSync(
"security",
["find-generic-password", "-s", "Raycast", "-a", account, "-w"],
{ encoding: "utf-8" }
).trim();
return JSON.parse(raw);
} catch {
return null;
}
}
function getDatabasePassphrase() {
const keyHex = execFileSync(
"security",
["find-generic-password", "-s", "Raycast", "-a", "database_key", "-w"],
{ encoding: "utf-8" }
).trim();
return createHash("sha256").update(keyHex + RAYCAST_SALT).digest("hex");
}
function queryEncryptedDb(passphrase, sql) {
if (!existsSync(RAYCAST_DB)) return [];
const tmpDir = mkdtempSync(join(tmpdir(), "omniroute-raycast-"));
const tmpDb = join(tmpDir, "raycast-enc.sqlite");
const cleanup = () => {
for (const ext of ["", "-wal", "-shm"]) {
try {
unlinkSync(tmpDb + ext);
} catch {
// ignore
}
}
try {
rmdirSync(tmpDir);
} catch {
// ignore
}
};
try {
copyFileSync(RAYCAST_DB, tmpDb);
for (const ext of ["-wal", "-shm"]) {
const src = RAYCAST_DB + ext;
if (existsSync(src)) copyFileSync(src, tmpDb + ext);
}
const input = `PRAGMA key = '${passphrase}';\n.mode json\n${sql}`;
const result = execFileSync("sqlcipher", [tmpDb], { input, encoding: "utf-8" });
const jsonStr = result.startsWith("ok\n") ? result.slice(3) : result;
return JSON.parse(jsonStr.trim() || "[]");
} catch {
return [];
} finally {
cleanup();
}
}
function readAnalyticsIdFromDb() {
try {
const passphrase = getDatabasePassphrase();
const rows = queryEncryptedDb(
passphrase,
"SELECT analyticsId FROM user WHERE analyticsId IS NOT NULL LIMIT 1;"
);
return rows[0]?.analyticsId?.trim() || null;
} catch {
return null;
}
}
function readAnalyticsIdFromPosthog() {
try {
if (!existsSync(POSTHOG_DISTINCT)) return null;
const parsed = JSON.parse(readFileSync(POSTHOG_DISTINCT, "utf-8"));
return parsed["posthog.distinctId"]?.trim() || null;
} catch {
return null;
}
}
function readUserProfile() {
try {
const passphrase = getDatabasePassphrase();
const rows = queryEncryptedDb(
passphrase,
"SELECT email, username, hasProFeatures, hasBetterAI FROM user LIMIT 1;"
);
const row = rows[0];
if (!row) return {};
return {
email: row.email,
username: row.username,
hasProFeatures: !!row.hasProFeatures,
hasBetterAI: !!row.hasBetterAI,
};
} catch {
return {};
}
}
export function isRaycastLocalExtractAvailable() {
if (process.platform !== "darwin") return false;
try {
execFileSync("which", ["sqlcipher"], { encoding: "utf-8" });
} catch {
return false;
}
return existsSync(RAYCAST_DB) || existsSync(POSTHOG_DISTINCT);
}
export function extractLocalRaycastCredentials() {
if (process.platform !== "darwin") {
throw new Error("Raycast auto-import is macOS-only");
}
const store = readKeychainJson("raycast-store_credentials");
const oauth = store?.oauth || {};
const accessToken = oauth.access_token?.trim();
if (!accessToken) {
throw new Error("Raycast bearer token not found in Keychain — open Raycast and sign in first");
}
const analyticsFromDb = readAnalyticsIdFromDb();
const analyticsFromPosthog = readAnalyticsIdFromPosthog();
const deviceId = analyticsFromDb || analyticsFromPosthog;
if (!deviceId) {
throw new Error(
"Raycast device/analytics ID not found — launch Raycast once so it writes local state"
);
}
const profile = readUserProfile();
const user = store?.user || {};
return {
accessToken,
deviceId,
aid: deviceId,
email: profile.email || user.email,
username: profile.username || user.username,
hasProFeatures: profile.hasProFeatures,
hasBetterAI: profile.hasBetterAI,
source: analyticsFromDb ? "keychain+analyticsId" : "keychain+posthog",
};
}