From 6a7c2e1e1274728b97c5dac45d4fd9bd321c0407 Mon Sep 17 00:00:00 2001 From: Ningyuan Li Date: Mon, 18 Nov 2024 23:17:18 +0900 Subject: [PATCH 1/2] installs jailer conf for native apps --- services/jail-conf.ts | 22 ++++++++++++++++++++++ services/service.ts | 27 ++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 services/jail-conf.ts diff --git a/services/jail-conf.ts b/services/jail-conf.ts new file mode 100644 index 0000000..dcaf68b --- /dev/null +++ b/services/jail-conf.ts @@ -0,0 +1,22 @@ +import { fetchWrapper } from './fetch-wrapper'; +import fs from 'fs'; +import { asyncPipeline } from './adapter'; +import path from 'path'; + +function fileUrl(sdkVersion: string, fileType: string) { + return `https://developer.lge.com/common/file/DownloadFile.dev?sdkVersion=${sdkVersion}&fileType=${fileType}`; +} + +async function downloadFileTo(url: string, targetPath: string) { + const res = await fetchWrapper(url); + if (!res.ok) { + throw new Error(`Failed to download jail.conf.sig: ${res.statusText}`); + } + const targetFile = fs.createWriteStream(targetPath); + await asyncPipeline(res.body, targetFile); +} + +export async function downloadJailConf(appDir: string, sdkVersion: string) { + await downloadFileTo(fileUrl(sdkVersion, 'conf'), path.join(appDir, 'jail_app.conf')); + await downloadFileTo(fileUrl(sdkVersion, 'sig'), path.join(appDir, 'jail_app.conf.sig')); +} diff --git a/services/service.ts b/services/service.ts index 2160fce..ce6fc2b 100644 --- a/services/service.ts +++ b/services/service.ts @@ -13,6 +13,7 @@ import Service, { Message } from 'webos-service'; import { asyncStat, asyncExecFile, asyncPipeline, asyncUnlink, asyncWriteFile, asyncReadFile, asyncChmod, asyncMkdir } from './adapter'; import { fetchWrapper } from './fetch-wrapper'; +import { downloadJailConf } from './jail-conf'; import rootAppInfo from '../appinfo.json'; import serviceInfo from './services.json'; @@ -406,8 +407,14 @@ function runService(): void { return serviceRemote as Service; } - async function getAppInfo(appId: string): Promise> { - const appList = await asyncCall<{ apps: { id: string }[] }>( + interface AppInfo { + id: string; + title: string; + type: 'web' | 'native' | string; + folderPath: string; + } + async function getAppInfo(appId: string): Promise { + const appList = await asyncCall<{ apps: AppInfo[] }>( getInstallerService(), 'luna://com.webos.applicationManager/dev/listApps', {}, @@ -417,14 +424,14 @@ function runService(): void { return appInfo; } - /** - * Installs the requested ipk from a URL. - */ interface InstallPayload { ipkUrl: string; ipkHash: string; id?: string; } + /** + * Installs the requested ipk from a URL. + */ service.register( 'install', tryRespond(async (message: Message) => { @@ -491,6 +498,16 @@ function runService(): void { try { const appInfo = await getAppInfo(installedPackageId); + if (appInfo.type === 'native') { + await createToast(`Updating jailer config for ${appInfo['title']}…`, service); + const sdkVersion = await asyncCall(service, 'luna://com.webos.service.tv.systemproperty/getSystemInfo', { + keys: ['sdkVersion'] + }).then((resp) => resp['sdkVersion']).catch(() => null); + if (sdkVersion) { + await downloadJailConf(appInfo.folderPath, sdkVersion) + .catch((err) => console.warn('jailer conf download failed:', err)); + } + } await createToast(`Application installed: ${appInfo['title']}`, service); } catch (err: unknown) { console.warn('appinfo fetch failed:', err); From ae26314648c356fb2d4de0b00a067ba6bcfbaa0c Mon Sep 17 00:00:00 2001 From: Ningyuan Li Date: Mon, 18 Nov 2024 23:24:05 +0900 Subject: [PATCH 2/2] code style fixes --- services/service.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/services/service.ts b/services/service.ts index ce6fc2b..5931f19 100644 --- a/services/service.ts +++ b/services/service.ts @@ -410,7 +410,7 @@ function runService(): void { interface AppInfo { id: string; title: string; - type: 'web' | 'native' | string; + type: string; folderPath: string; } async function getAppInfo(appId: string): Promise { @@ -499,16 +499,18 @@ function runService(): void { try { const appInfo = await getAppInfo(installedPackageId); if (appInfo.type === 'native') { - await createToast(`Updating jailer config for ${appInfo['title']}…`, service); - const sdkVersion = await asyncCall(service, 'luna://com.webos.service.tv.systemproperty/getSystemInfo', { - keys: ['sdkVersion'] - }).then((resp) => resp['sdkVersion']).catch(() => null); + await createToast(`Updating jailer config for ${appInfo.title}…`, service); + const sdkVersion = await asyncCall<{ + sdkVersion: string; + }>(service, 'luna://com.webos.service.tv.systemproperty/getSystemInfo', { + keys: ['sdkVersion'], + }).then((resp) => resp.sdkVersion).catch(() => null); if (sdkVersion) { await downloadJailConf(appInfo.folderPath, sdkVersion) .catch((err) => console.warn('jailer conf download failed:', err)); } } - await createToast(`Application installed: ${appInfo['title']}`, service); + await createToast(`Application installed: ${appInfo.title}`, service); } catch (err: unknown) { console.warn('appinfo fetch failed:', err); await createToast(`Application installed: ${installedPackageId}`, service);