|
| 1 | +import Foundation |
| 2 | + |
| 3 | +#if canImport(SQLite3) |
| 4 | +import SQLite3 |
| 5 | +#elseif canImport(CSQLite3) |
| 6 | +import CSQLite3 |
| 7 | +#endif |
| 8 | + |
| 9 | +#if canImport(SQLite3) || canImport(CSQLite3) |
| 10 | +/// Read-only access to the official Kimi Desktop Chromium cookie store. |
| 11 | +public enum KimiDesktopAuthToken: Sendable { |
| 12 | + private static let log = CodexBarLog.logger(LogCategories.kimiCookie) |
| 13 | + |
| 14 | + public static func cookiesDatabaseURL( |
| 15 | + homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser) -> URL |
| 16 | + { |
| 17 | + homeDirectory |
| 18 | + .appendingPathComponent("Library", isDirectory: true) |
| 19 | + .appendingPathComponent("Application Support", isDirectory: true) |
| 20 | + .appendingPathComponent("kimi-desktop", isDirectory: true) |
| 21 | + .appendingPathComponent("Cookies", isDirectory: false) |
| 22 | + } |
| 23 | + |
| 24 | + public static func load( |
| 25 | + homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser) -> String? |
| 26 | + { |
| 27 | + self.load(databaseURL: self.cookiesDatabaseURL(homeDirectory: homeDirectory)) |
| 28 | + } |
| 29 | + |
| 30 | + static func load(databaseURL: URL) -> String? { |
| 31 | + guard FileManager.default.isReadableFile(atPath: databaseURL.path) else { return nil } |
| 32 | + do { |
| 33 | + return try self.read(databaseURL: databaseURL, immutable: false) |
| 34 | + } catch let failure as SQLiteReadFailure { |
| 35 | + // Chromium can leave the main database in WAL mode after a clean shutdown removes both sidecars. |
| 36 | + // Immutable mode reads that idle file without recreating sidecars; active WAL databases stay on the |
| 37 | + // normal read-only path so committed WAL records remain visible. |
| 38 | + guard failure.code == SQLITE_CANTOPEN, self.walSidecarsAreMissing(databaseURL: databaseURL) else { |
| 39 | + Self.log.debug("Kimi Desktop Cookies read failed: \(failure.message)") |
| 40 | + return nil |
| 41 | + } |
| 42 | + do { |
| 43 | + return try self.read(databaseURL: databaseURL, immutable: true) |
| 44 | + } catch let fallbackFailure as SQLiteReadFailure { |
| 45 | + Self.log.debug("Kimi Desktop Cookies immutable read failed: \(fallbackFailure.message)") |
| 46 | + return nil |
| 47 | + } catch { |
| 48 | + return nil |
| 49 | + } |
| 50 | + } catch { |
| 51 | + return nil |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private static func read(databaseURL: URL, immutable: Bool) throws -> String? { |
| 56 | + var db: OpaquePointer? |
| 57 | + let filename = immutable ? "\(databaseURL.absoluteURL.absoluteString)?immutable=1" : databaseURL.path |
| 58 | + let flags = immutable ? SQLITE_OPEN_READONLY | SQLITE_OPEN_URI : SQLITE_OPEN_READONLY |
| 59 | + let openResult = sqlite3_open_v2(filename, &db, flags, nil) |
| 60 | + guard openResult == SQLITE_OK else { |
| 61 | + let failure = self.sqliteFailure(db: db, resultCode: openResult) |
| 62 | + sqlite3_close(db) |
| 63 | + throw failure |
| 64 | + } |
| 65 | + defer { sqlite3_close(db) } |
| 66 | + sqlite3_busy_timeout(db, 250) |
| 67 | + |
| 68 | + let sql = """ |
| 69 | + SELECT value |
| 70 | + FROM cookies |
| 71 | + WHERE name = 'kimi-auth' |
| 72 | + AND host_key IN ('www.kimi.com', '.www.kimi.com', '.kimi.com', 'kimi.com') |
| 73 | + ORDER BY last_access_utc DESC |
| 74 | + LIMIT 1 |
| 75 | + """ |
| 76 | + var statement: OpaquePointer? |
| 77 | + let prepareResult = sqlite3_prepare_v2(db, sql, -1, &statement, nil) |
| 78 | + guard prepareResult == SQLITE_OK else { |
| 79 | + throw self.sqliteFailure(db: db, resultCode: prepareResult) |
| 80 | + } |
| 81 | + defer { sqlite3_finalize(statement) } |
| 82 | + |
| 83 | + let step = sqlite3_step(statement) |
| 84 | + if step == SQLITE_DONE { |
| 85 | + return nil |
| 86 | + } |
| 87 | + guard step == SQLITE_ROW else { |
| 88 | + throw self.sqliteFailure(db: db, resultCode: step) |
| 89 | + } |
| 90 | + guard let text = sqlite3_column_text(statement, 0) else { return nil } |
| 91 | + let token = String(cString: text).trimmingCharacters(in: .whitespacesAndNewlines) |
| 92 | + return token.isEmpty ? nil : token |
| 93 | + } |
| 94 | + |
| 95 | + private static func walSidecarsAreMissing(databaseURL: URL) -> Bool { |
| 96 | + !FileManager.default.fileExists(atPath: databaseURL.path + "-wal") && |
| 97 | + !FileManager.default.fileExists(atPath: databaseURL.path + "-shm") |
| 98 | + } |
| 99 | + |
| 100 | + private static func sqliteFailure(db: OpaquePointer?, resultCode: Int32) -> SQLiteReadFailure { |
| 101 | + SQLiteReadFailure( |
| 102 | + code: db.map { sqlite3_errcode($0) } ?? resultCode, |
| 103 | + message: db.map { String(cString: sqlite3_errmsg($0)) } ?? "unknown error") |
| 104 | + } |
| 105 | + |
| 106 | + private struct SQLiteReadFailure: Error { |
| 107 | + let code: Int32 |
| 108 | + let message: String |
| 109 | + } |
| 110 | +} |
| 111 | +#else |
| 112 | +public enum KimiDesktopAuthToken: Sendable { |
| 113 | + public static func cookiesDatabaseURL( |
| 114 | + homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser) -> URL |
| 115 | + { |
| 116 | + homeDirectory |
| 117 | + .appendingPathComponent("Library", isDirectory: true) |
| 118 | + .appendingPathComponent("Application Support", isDirectory: true) |
| 119 | + .appendingPathComponent("kimi-desktop", isDirectory: true) |
| 120 | + .appendingPathComponent("Cookies", isDirectory: false) |
| 121 | + } |
| 122 | + |
| 123 | + public static func load(homeDirectory _: URL = FileManager.default.homeDirectoryForCurrentUser) -> String? { |
| 124 | + nil |
| 125 | + } |
| 126 | +} |
| 127 | +#endif |
0 commit comments