From c561bb758944603c2b5cca20ea61ecd73abb77ef Mon Sep 17 00:00:00 2001 From: Core <64542347+coredev-uk@users.noreply.github.com> Date: Tue, 4 Jan 2022 23:27:44 +0000 Subject: [PATCH] temporary store --- package.json | 1 + src/main/base/win.ts | 160 +++++++++++++++++++++++++++++++++++++ src/main/index.ts | 183 +++++++++++++++++++++++-------------------- tsconfig.json | 2 +- 4 files changed, 258 insertions(+), 88 deletions(-) create mode 100644 src/main/base/win.ts diff --git a/package.json b/package.json index 83eebac8..3e6ec71e 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "youtube-search-without-api-key": "^1.0.7" }, "devDependencies": { + "@types/express": "^4.17.13", "electron": "https://github.com/castlabs/electron-releases.git", "electron-builder": "^22.14.5", "electron-webpack": "^2.8.2", diff --git a/src/main/base/win.ts b/src/main/base/win.ts new file mode 100644 index 00000000..8caffad9 --- /dev/null +++ b/src/main/base/win.ts @@ -0,0 +1,160 @@ +import * as path from "path"; +import {app, ipcMain} from "electron"; +import {join} from "path"; +import * as windowStateKeeper from "electron-window-state"; +import * as express from "express"; +import * as getPort from "get-port"; + +export default class Win { + public win: Electron.BrowserWindow | undefined; + public app: Electron.App | undefined; + + private static port: number = 0; + private static envVars: object = { + "env": { + // @ts-ignore + platform: process.platform, + // @ts-ignore + dev: app.isPackaged + } + }; + private static options: any = { + icon: join(__dirname, `../../../resources/icons/icon.ico`), + width: 1024, + height: 600, + x: undefined, + y: undefined, + minWidth: 850, + minHeight: 400, + frame: false, + title: "Cider", + vibrancy: 'dark', + transparent: process.platform === "darwin", + hasShadow: false, + webPreferences: { + webviewTag: true, + plugins: true, + nodeIntegration: true, + nodeIntegrationInWorker: false, + webSecurity: false, + allowRunningInsecureContent: true, + enableRemoteModule: true, + sandbox: true, + nativeWindowOpen: true, + contextIsolation: false, + preload: join(__dirname, '../../preload/cider-preload.js') + } + }; + + get options() { + return Win.options; + } + + /** + * Creates the browser window + */ + public createWindow(): Electron.BrowserWindow { + let BrowserWindow; + if (process.platform === "win32") { + BrowserWindow = require("electron-acrylic-window").BrowserWindow; + } else { + BrowserWindow = require("electron").BrowserWindow; + } + + // Load the previous state with fallback to defaults + const windowState = windowStateKeeper({ + defaultWidth: 1024, + defaultHeight: 600 + }); + this.options.width = windowState.width; + this.options.height = windowState.height; + + // Create the browser window. + const win = new BrowserWindow(this.options); + + // and load the renderer. + this.startWebServer(win) + .then((url) => { + console.log(url) + this.startSession(win, url); + }) + .catch(console.error); + + + + + this.win = win; + return win; + } + + /** + * Starts the webserver for the renderer process. + * @param win The window to use + */ + private async startWebServer(win: Electron.BrowserWindow): Promise { + Win.port = await getPort({port: 9000}); + + const webapp = express(), + webRemotePath = path.join(__dirname, '../../renderer/'); + + webapp.set("views", path.join(webRemotePath, "views")); + webapp.set("view engine", "ejs"); + + webapp.use((req, res, next) => { + // if not localhost + // @ts-ignore + if (req.headers.host.includes("localhost") && req.headers["user-agent"].includes("Cider")) { + next(); + } + }); + + webapp.use(express.static(webRemotePath)); + webapp.get('/', function (req, res) { + //res.sendFile(path.join(webRemotePath, 'index_old.html')); + res.render("main", Win.envVars) + }); + webapp.listen(Win.port, function () { + console.debug(`Cider client port: ${Win.port}`); + }); + + return "http://localhost:" + Win.port; + } + + /** + * Starts the session for the renderer process. + * @param win The window to use + * @param location The location of the renderer + */ + private startSession(win: Electron.BrowserWindow, location: string) { + // intercept "https://js-cdn.music.apple.com/hls.js/2.141.0/hls.js/hls.js" and redirect to local file "./apple-hls.js" instead + win.webContents.session.webRequest.onBeforeRequest( + { + urls: ["https://*/*.js"] + }, + (details, callback) => { + if (details.url.includes("hls.js")) { + callback({ + redirectURL: `http://localhost:${Win.port}/apple-hls.js` + }) + } else { + callback({ + cancel: false + }) + } + } + ) + + win.webContents.session.webRequest.onBeforeSendHeaders(async (details, callback) => { + if (details.url === "https://buy.itunes.apple.com/account/web/info") { + details.requestHeaders['sec-fetch-site'] = 'same-site'; + details.requestHeaders['DNT'] = '1'; + let ItsPod = await win.webContents.executeJavaScript(`window.localStorage.getItem("music.ampwebplay.itspod")`) + if (ItsPod != null) + details.requestHeaders['Cookie'] = `itspod=${ItsPod}` + } + callback({requestHeaders: details.requestHeaders}) + }) + + win.loadURL(location).catch(console.error); + } +} \ No newline at end of file diff --git a/src/main/index.ts b/src/main/index.ts index 435f39be..770d2f3c 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -14,94 +14,103 @@ import * as path from "path"; // // const {Init} = require("./src/main/cider-base"); // // Init() // + +import Win from "./base/win"; + +const Cider = new Win() + +app.on("ready", () => { + Cider.createWindow(); +}); + /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * App Event Handlers * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ -app.on('ready', () => { - if (app.isQuiting) { app.quit(); return; } - - console.log('[Cider] Application is Ready. Creating Window.') - if (!app.isPackaged) { - console.info('[Cider] Running in development mode.') - require('vue-devtools').install() - } - - // CiderBase.Start() -}); - -app.on('before-quit', () => { - app.isQuiting = true; - console.warn(`${app.getName()} exited.`); -}); - -// Widevine Stuff -app.on('widevine-ready', (version, lastVersion) => { - if (null !== lastVersion) { - console.log('[Cider][Widevine] Widevine ' + version + ', upgraded from ' + lastVersion + ', is ready to be used!') - } else { - console.log('[Cider][Widevine] Widevine ' + version + ' is ready to be used!') - } -}) - -app.on('widevine-update-pending', (currentVersion, pendingVersion) => { - console.log('[Cider][Widevine] Widevine ' + currentVersion + ' is ready to be upgraded to ' + pendingVersion + '!') -}) - -app.on('widevine-error', (error) => { - console.log('[Cider][Widevine] Widevine installation encountered an error: ' + error) - app.exit() -}) - -if (process.defaultApp) { - if (process.argv.length >= 2) { - app.setAsDefaultProtocolClient('cider', process.execPath, [resolve(process.argv[1])]) - app.setAsDefaultProtocolClient('ame', process.execPath, [resolve(process.argv[1])]) - app.setAsDefaultProtocolClient('itms', process.execPath, [resolve(process.argv[1])]) - app.setAsDefaultProtocolClient('itmss', process.execPath, [resolve(process.argv[1])]) - app.setAsDefaultProtocolClient('musics', process.execPath, [resolve(process.argv[1])]) - app.setAsDefaultProtocolClient('music', process.execPath, [resolve(process.argv[1])]) - } -} else { - app.setAsDefaultProtocolClient('cider') // Custom AME Protocol - app.setAsDefaultProtocolClient('ame') // Custom AME Protocol - app.setAsDefaultProtocolClient('itms') // iTunes HTTP Protocol - app.setAsDefaultProtocolClient('itmss') // iTunes HTTPS Protocol - app.setAsDefaultProtocolClient('musics') // macOS Client Protocol - app.setAsDefaultProtocolClient('music') // macOS Client Protocol -} - -app.on('open-url', (event, url) => { - event.preventDefault() - if (url.includes('ame://') || url.includes('itms://') || url.includes('itmss://') || url.includes('musics://') || url.includes('music://')) { - CiderBase.LinkHandler(url) - } -}) - -app.on('second-instance', (_e, argv) => { - console.warn(`[InstanceHandler][SecondInstanceHandler] Second Instance Started with args: [${argv.join(', ')}]`) - - // Checks if first instance is authorized and if second instance has protocol args - argv.forEach((value) => { - if (value.includes('ame://') || value.includes('itms://') || value.includes('itmss://') || value.includes('musics://') || value.includes('music://')) { - console.warn(`[InstanceHandler][SecondInstanceHandler] Found Protocol!`) - CiderBase.LinkHandler(value); - } - }) - - if (argv.includes("--force-quit")) { - console.warn('[InstanceHandler][SecondInstanceHandler] Force Quit found. Quitting App.'); - app.isQuiting = true - app.quit() - } else if (app.win && !app.cfg.get('advanced.allowMultipleInstances')) { // If a Second Instance has Been Started - console.warn('[InstanceHandler][SecondInstanceHandler] Showing window.'); - app.win.show() - app.win.focus() - } -}) - -if (!app.requestSingleInstanceLock() && !app.cfg.get('advanced.allowMultipleInstances')) { - console.warn("[InstanceHandler] Existing Instance is Blocking Second Instance."); - app.quit(); - app.isQuiting = true -} \ No newline at end of file +// app.on('ready', () => { +// if (app.isQuiting) { app.quit(); return; } +// +// console.log('[Cider] Application is Ready. Creating Window.') +// if (!app.isPackaged) { +// console.info('[Cider] Running in development mode.') +// require('vue-devtools').install() +// } +// +// // CiderBase.Start() +// }); +// +// app.on('before-quit', () => { +// app.isQuiting = true; +// console.warn(`${app.getName()} exited.`); +// }); +// +// // Widevine Stuff +// app.on('widevine-ready', (version, lastVersion) => { +// if (null !== lastVersion) { +// console.log('[Cider][Widevine] Widevine ' + version + ', upgraded from ' + lastVersion + ', is ready to be used!') +// } else { +// console.log('[Cider][Widevine] Widevine ' + version + ' is ready to be used!') +// } +// }) +// +// app.on('widevine-update-pending', (currentVersion, pendingVersion) => { +// console.log('[Cider][Widevine] Widevine ' + currentVersion + ' is ready to be upgraded to ' + pendingVersion + '!') +// }) +// +// app.on('widevine-error', (error) => { +// console.log('[Cider][Widevine] Widevine installation encountered an error: ' + error) +// app.exit() +// }) +// +// if (process.defaultApp) { +// if (process.argv.length >= 2) { +// app.setAsDefaultProtocolClient('cider', process.execPath, [resolve(process.argv[1])]) +// app.setAsDefaultProtocolClient('ame', process.execPath, [resolve(process.argv[1])]) +// app.setAsDefaultProtocolClient('itms', process.execPath, [resolve(process.argv[1])]) +// app.setAsDefaultProtocolClient('itmss', process.execPath, [resolve(process.argv[1])]) +// app.setAsDefaultProtocolClient('musics', process.execPath, [resolve(process.argv[1])]) +// app.setAsDefaultProtocolClient('music', process.execPath, [resolve(process.argv[1])]) +// } +// } else { +// app.setAsDefaultProtocolClient('cider') // Custom AME Protocol +// app.setAsDefaultProtocolClient('ame') // Custom AME Protocol +// app.setAsDefaultProtocolClient('itms') // iTunes HTTP Protocol +// app.setAsDefaultProtocolClient('itmss') // iTunes HTTPS Protocol +// app.setAsDefaultProtocolClient('musics') // macOS Client Protocol +// app.setAsDefaultProtocolClient('music') // macOS Client Protocol +// } +// +// app.on('open-url', (event, url) => { +// event.preventDefault() +// if (url.includes('ame://') || url.includes('itms://') || url.includes('itmss://') || url.includes('musics://') || url.includes('music://')) { +// CiderBase.LinkHandler(url) +// } +// }) +// +// app.on('second-instance', (_e, argv) => { +// console.warn(`[InstanceHandler][SecondInstanceHandler] Second Instance Started with args: [${argv.join(', ')}]`) +// +// // Checks if first instance is authorized and if second instance has protocol args +// argv.forEach((value) => { +// if (value.includes('ame://') || value.includes('itms://') || value.includes('itmss://') || value.includes('musics://') || value.includes('music://')) { +// console.warn(`[InstanceHandler][SecondInstanceHandler] Found Protocol!`) +// CiderBase.LinkHandler(value); +// } +// }) +// +// if (argv.includes("--force-quit")) { +// console.warn('[InstanceHandler][SecondInstanceHandler] Force Quit found. Quitting App.'); +// app.isQuiting = true +// app.quit() +// } else if (app.win && !app.cfg.get('advanced.allowMultipleInstances')) { // If a Second Instance has Been Started +// console.warn('[InstanceHandler][SecondInstanceHandler] Showing window.'); +// app.win.show() +// app.win.focus() +// } +// }) +// +// if (!app.requestSingleInstanceLock() && !app.cfg.get('advanced.allowMultipleInstances')) { +// console.warn("[InstanceHandler] Existing Instance is Blocking Second Instance."); +// app.quit(); +// app.isQuiting = true +// } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index a4db4c0d..416aa2c7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es6", + "target": "esnext", "module": "commonjs", "noImplicitAny": true, "strict": true,