From dd3516f6e41d8312d25a1b04c179eeae77f0e309 Mon Sep 17 00:00:00 2001 From: Neil Date: Wed, 31 May 2023 13:30:36 +0800 Subject: [PATCH] #273 User-Agent used is from teaxyz/lib (#637) * #273 User-Agent used is from teaxyz/lib * bump v0.2.22 --- modules/desktop/electron/libs/ipc.ts | 11 +++++ modules/desktop/electron/libs/v1-client.ts | 23 +++++++---- modules/desktop/package.json | 3 +- modules/desktop/src/libs/native-electron.ts | 5 +++ modules/desktop/src/libs/native-mock.ts | 2 + modules/desktop/src/libs/v1-client.ts | 24 +---------- pnpm-lock.yaml | 45 +++++++++++++++++++-- 7 files changed, 80 insertions(+), 33 deletions(-) diff --git a/modules/desktop/electron/libs/ipc.ts b/modules/desktop/electron/libs/ipc.ts index a2dcc2d..8e1b7aa 100644 --- a/modules/desktop/electron/libs/ipc.ts +++ b/modules/desktop/electron/libs/ipc.ts @@ -21,6 +21,7 @@ import { loadPackageCache, writePackageCache } from "./package"; import { nanoid } from "nanoid"; import { MainWindowNotifier } from "./types"; import { unsubscribeToPackageTopic } from "./push-notification"; +import { getHeaders } from "./v1-client"; export type HandlerOptions = { // A function to call back to the current main @@ -270,4 +271,14 @@ export default function initializeHandlers({ notifyMainWindow }: HandlerOptions) return err; } }); + + ipcMain.handle("get-api-headers", async (_event, path: string) => { + try { + const session = await readSessionData(); + return await getHeaders(path, session); + } catch (error) { + log.error(error); + return {}; + } + }); } diff --git a/modules/desktop/electron/libs/v1-client.ts b/modules/desktop/electron/libs/v1-client.ts index 5071e14..8974438 100644 --- a/modules/desktop/electron/libs/v1-client.ts +++ b/modules/desktop/electron/libs/v1-client.ts @@ -6,6 +6,7 @@ import bcrypt from "bcryptjs"; import { createReadStream, statSync } from "fs"; import { deepReadDir } from "./tea-dir"; import fetch from "node-fetch"; +import { hooks } from "@teaxyz/lib"; import { readSessionData, type Session } from "./auth"; @@ -65,20 +66,28 @@ export async function post(urlPath: string, data: { [key: string]: any }) { } } -async function getHeaders(path: string, session: Session) { +export async function getHeaders(path: string, session: Session) { const unixMs = new Date().getTime(); const unixHexSecs = Math.round(unixMs / 1000).toString(16); // hex const deviceId = session.device_id?.split("-")[0]; const preHash = [unixHexSecs, session.key, deviceId, path].join(""); const Authorization = bcrypt.hashSync(preHash, 10); + const { UserAgent } = hooks.useConfig(); - return { - Authorization, - ["tea-ts"]: unixMs.toString(), - ["tea-uid"]: session.user?.developer_id, - ["tea-gui_id"]: session.device_id - }; + log.info("tea/lib UserAgent", UserAgent); + return session?.device_id && session?.user + ? { + Authorization, + ["tea-ts"]: unixMs.toString(), + ["tea-uid"]: session.user?.developer_id, + ["tea-gui_id"]: session.device_id, + "User-Agent": UserAgent + } + : { + ...publicHeader, + "User-Agent": UserAgent + }; } export async function syncLogsAt(prefix: string) { diff --git a/modules/desktop/package.json b/modules/desktop/package.json index 1713114..45e9c15 100644 --- a/modules/desktop/package.json +++ b/modules/desktop/package.json @@ -1,6 +1,6 @@ { "name": "tea", - "version": "0.2.21", + "version": "0.2.22", "private": true, "description": "tea gui app", "author": "tea.xyz", @@ -89,6 +89,7 @@ "@sentry/electron": "^4.4.0", "@sentry/svelte": "^7.47.0", "@sentry/vite-plugin": "^0.7.2", + "@teaxyz/lib": "^0.1.9", "@types/electron": "^1.6.10", "@types/mousetrap": "^1.6.11", "@vitest/coverage-c8": "^0.27.1", diff --git a/modules/desktop/src/libs/native-electron.ts b/modules/desktop/src/libs/native-electron.ts index 1eb8a2d..5007e4e 100644 --- a/modules/desktop/src/libs/native-electron.ts +++ b/modules/desktop/src/libs/native-electron.ts @@ -301,3 +301,8 @@ export const stopMonitoringTeaDir = async () => { throw result; } }; + +export const getHeaders = async (path: string) => { + const headers = await ipcRenderer.invoke("get-api-headers", path); + return (headers || {}) as { [key: string]: string }; +}; diff --git a/modules/desktop/src/libs/native-mock.ts b/modules/desktop/src/libs/native-mock.ts index 3ba5c75..fc5516c 100644 --- a/modules/desktop/src/libs/native-mock.ts +++ b/modules/desktop/src/libs/native-mock.ts @@ -414,3 +414,5 @@ export const monitorTeaDir = async () => { export const stopMonitoringTeaDir = async () => { console.log("do nothing"); }; + +export const getHeaders = async (path: string) => ({}); diff --git a/modules/desktop/src/libs/v1-client.ts b/modules/desktop/src/libs/v1-client.ts index a86f99c..cf70952 100644 --- a/modules/desktop/src/libs/v1-client.ts +++ b/modules/desktop/src/libs/v1-client.ts @@ -2,6 +2,7 @@ import axios from "axios"; import type { Session } from "$libs/types"; import bcrypt from "bcryptjs"; import { getSession } from "$libs/stores/auth"; +import { getHeaders } from "@native"; export const baseUrl = "https://api.tea.xyz/v1"; @@ -11,12 +12,7 @@ export async function get( ): Promise { console.log(`GET /v1/${urlPath}`); - const [session] = await Promise.all([getSession()]); - - const headers = - session?.device_id && session?.user - ? await getHeaders(`GET/${urlPath}`, session) - : { Authorization: "public " }; + const headers = await getHeaders(`GET/${urlPath}`); const req = await axios.request({ method: "GET", @@ -29,19 +25,3 @@ export async function get( return req.data as T; } - -async function getHeaders(path: string, session: Session) { - const unixMs = new Date().getTime(); - const unixHexSecs = Math.round(unixMs / 1000).toString(16); // hex - const deviceId = session.device_id?.split("-")[0]; - const preHash = [unixHexSecs, session.key, deviceId, path].join(""); - - const Authorization = bcrypt.hashSync(preHash, 10); - - return { - Authorization, - ["tea-ts"]: unixMs.toString(), - ["tea-uid"]: session.user?.developer_id, - ["tea-gui_id"]: session.device_id - }; -} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bd0e144..2178fc7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -31,6 +31,7 @@ importers: '@sveltejs/kit': ^1.15.9 '@tea/libtea': workspace:* '@tea/ui': workspace:* + '@teaxyz/lib': ^0.1.9 '@testing-library/jest-dom': ^5.16.5 '@testing-library/svelte': ^3.2.2 '@testing-library/webdriverio': ^3.2.1 @@ -110,6 +111,7 @@ importers: '@sentry/electron': 4.5.0 '@sentry/svelte': 7.51.2_svelte@3.59.1 '@sentry/vite-plugin': 0.7.2 + '@teaxyz/lib': 0.1.9 '@types/electron': 1.6.10 '@types/mousetrap': 1.6.11 '@vitest/coverage-c8': 0.27.3_jsdom@21.1.2 @@ -1574,6 +1576,21 @@ packages: '@jridgewell/trace-mapping': 0.3.9 dev: true + /@deno/shim-crypto/0.3.1: + resolution: {integrity: sha512-ed4pNnfur6UbASEgF34gVxR9p7Mc3qF+Ygbmjiil8ws5IhNFhPDFy5vE5hQAUA9JmVsSxXPcVLM5Rf8LOZqQ5Q==} + dev: false + + /@deno/shim-deno-test/0.4.0: + resolution: {integrity: sha512-oYWcD7CpERZy/TXMTM9Tgh1HD/POHlbY9WpzmAk+5H8DohcxG415Qws8yLGlim3EaKBT2v3lJv01x4G0BosnaQ==} + dev: false + + /@deno/shim-deno/0.16.1: + resolution: {integrity: sha512-s9v0kzF5bm/o9TgdwvsraHx6QNllYrXXmKzgOG2lh4LFXnVMr2gpjK/c/ve6EflQn1MqImcWmVD8HAv5ahuuZQ==} + dependencies: + '@deno/shim-deno-test': 0.4.0 + which: 2.0.2 + dev: false + /@develar/schema-utils/2.6.5: resolution: {integrity: sha512-0cp4PsWQ/9avqTVMCtZ+GirikIA36ikvjtHweU4/j8yLtgObI0+JUPhYFScgwlteveGB1rt3Cm8UhN04XayDig==} engines: {node: '>= 8.9.0'} @@ -3626,6 +3643,17 @@ packages: tailwindcss: 3.3.2 dev: false + /@teaxyz/lib/0.1.9: + resolution: {integrity: sha512-LukZXiLo22bq5P81O4rvUPLpqvTt+aNwn73tUN3Fd2QLtPfsh70qBy/uHM1B5y0+IDfWjn/mvCXra6ZARA+VRA==} + dependencies: + '@deno/shim-crypto': 0.3.1 + '@deno/shim-deno': 0.16.1 + is-what: 4.1.11 + koffi: 2.3.20 + outdent: 0.8.0 + undici: 5.22.0 + dev: false + /@testing-library/dom/8.20.0: resolution: {integrity: sha512-d9ULIT+a4EXLX3UU8FBjauG9NnsZHkHztXoIcTsOKoOw030fyjheN9svkTULjJxtYag9DZz5Jz5qkWZDPxTFwA==} engines: {node: '>=12'} @@ -5186,7 +5214,6 @@ packages: engines: {node: '>=10.16.0'} dependencies: streamsearch: 1.1.0 - dev: true /bytes/3.0.0: resolution: {integrity: sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw==} @@ -8389,6 +8416,11 @@ packages: get-intrinsic: 1.2.0 dev: true + /is-what/4.1.11: + resolution: {integrity: sha512-gr9+qDrJvdwT4+N2TAACsZQIB4Ow9j2eefqlh3m9JUV41M1LoKhcE+/j+IVni/r6U8Jnc1PwhjdjVJr+Xmtb0A==} + engines: {node: '>=12.13'} + dev: false + /is-wsl/2.2.0: resolution: {integrity: sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==} engines: {node: '>=8'} @@ -8819,6 +8851,11 @@ packages: engines: {node: '>=6'} dev: true + /koffi/2.3.20: + resolution: {integrity: sha512-y1W9JikdswM/CLmumtibnCknGJ6bmCNpWWD9G6WzP6Zj3lEY7d0J7jO82uMVoD1udz/2ZYUw2twhsGUGqeZH1g==} + requiresBuild: true + dev: false + /ky/0.33.3: resolution: {integrity: sha512-CasD9OCEQSFIam2U8efFK81Yeg8vNMTBUqtMOHlrcWQHqUX3HeCl9Dr31u4toV7emlH8Mymk5+9p0lL6mKb/Xw==} engines: {node: '>=14.16'} @@ -9807,6 +9844,10 @@ packages: engines: {node: '>=0.10.0'} dev: true + /outdent/0.8.0: + resolution: {integrity: sha512-KiOAIsdpUTcAXuykya5fnVVT+/5uS0Q1mrkRHcF89tpieSmY33O/tmc54CqwA+bfhbtEfZUNLHaPUiB9X3jt1A==} + dev: false + /p-cancelable/2.1.1: resolution: {integrity: sha512-BZOr3nRQHOntUjTrH8+Lh54smKHoHyur8We1V8DSMVrl5A2malOOwuJRnKRDjSnkoeBh4at6BwEnb5I7Jl31wg==} engines: {node: '>=8'} @@ -11447,7 +11488,6 @@ packages: /streamsearch/1.1.0: resolution: {integrity: sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==} engines: {node: '>=10.0.0'} - dev: true /string-width/4.2.3: resolution: {integrity: sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==} @@ -12269,7 +12309,6 @@ packages: engines: {node: '>=14.0'} dependencies: busboy: 1.6.0 - dev: true /unfetch/4.2.0: resolution: {integrity: sha512-F9p7yYCn6cIW9El1zi0HI6vqpeIvBsr3dSuRO6Xuppb1u5rXpCPmMvLSyECLhybr9isec8Ohl0hPekMVrEinDA==}