2023-06-20 08:45:01 +02:00
|
|
|
import { BrowserWindow, ipcMain, IpcMainEvent, nativeImage, Tray, Menu, dialog } from 'electron';
|
2023-12-06 11:50:26 +01:00
|
|
|
import { TcpListenerService } from './TcpListenerService';
|
2024-01-04 12:38:39 +01:00
|
|
|
import { PlayMessage, PlaybackErrorMessage, PlaybackUpdateMessage, VolumeUpdateMessage } from './Packets';
|
2023-06-20 08:45:01 +02:00
|
|
|
import { DiscoveryService } from './DiscoveryService';
|
|
|
|
import { Updater } from './Updater';
|
2023-12-06 11:50:26 +01:00
|
|
|
import { WebSocketListenerService } from './WebSocketListenerService';
|
2023-12-30 10:55:30 +01:00
|
|
|
import { Opcode } from './FCastSession';
|
2024-01-04 12:38:39 +01:00
|
|
|
import * as os from 'os';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as http from 'http';
|
|
|
|
import * as url from 'url';
|
|
|
|
import { AddressInfo } from 'ws';
|
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2023-06-20 08:45:01 +02:00
|
|
|
|
|
|
|
export default class Main {
|
2023-12-06 13:03:23 +01:00
|
|
|
static shouldOpenMainWindow = true;
|
|
|
|
static playerWindow: Electron.BrowserWindow;
|
2023-06-20 08:45:01 +02:00
|
|
|
static mainWindow: Electron.BrowserWindow;
|
|
|
|
static application: Electron.App;
|
2023-12-06 11:50:26 +01:00
|
|
|
static tcpListenerService: TcpListenerService;
|
|
|
|
static webSocketListenerService: WebSocketListenerService;
|
2023-06-20 08:45:01 +02:00
|
|
|
static discoveryService: DiscoveryService;
|
|
|
|
static tray: Tray;
|
2023-12-30 10:55:30 +01:00
|
|
|
static key: string = null;
|
|
|
|
static cert: string = null;
|
2024-01-04 12:38:39 +01:00
|
|
|
static proxyServer: http.Server;
|
|
|
|
static proxyServerAddress: AddressInfo;
|
|
|
|
static proxiedFiles: Map<string, { url: string, headers: { [key: string]: string } }> = new Map();
|
2023-06-20 08:45:01 +02:00
|
|
|
|
|
|
|
private static createTray() {
|
|
|
|
const icon = (process.platform === 'win32') ? path.join(__dirname, 'app.ico') : path.join(__dirname, 'app.png');
|
|
|
|
const trayicon = nativeImage.createFromPath(icon)
|
|
|
|
const tray = new Tray(trayicon.resize({ width: 16 }));
|
|
|
|
const contextMenu = Menu.buildFromTemplate([
|
2023-12-06 13:03:23 +01:00
|
|
|
{
|
|
|
|
label: 'Open window',
|
|
|
|
click: () => Main.openMainWindow()
|
|
|
|
},
|
2023-06-20 08:45:01 +02:00
|
|
|
{
|
|
|
|
label: 'Check for updates',
|
|
|
|
click: async () => {
|
|
|
|
try {
|
|
|
|
const updater = new Updater(path.join(__dirname, '../'), 'https://releases.grayjay.app/fcastreceiver');
|
|
|
|
if (await updater.update()) {
|
|
|
|
const restartPrompt = await dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
title: 'Update completed',
|
|
|
|
message: 'The application has been updated. Restart now to apply the changes.',
|
|
|
|
buttons: ['Restart'],
|
|
|
|
defaultId: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log('Update completed');
|
|
|
|
|
|
|
|
// Restart the app if the user clicks the 'Restart' button
|
|
|
|
if (restartPrompt.response === 0) {
|
|
|
|
Main.application.relaunch();
|
|
|
|
Main.application.exit(0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await dialog.showMessageBox({
|
|
|
|
type: 'info',
|
|
|
|
title: 'Already up-to-date',
|
|
|
|
message: 'The application is already on the latest version.',
|
|
|
|
buttons: ['OK'],
|
|
|
|
defaultId: 0
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
await dialog.showMessageBox({
|
|
|
|
type: 'error',
|
|
|
|
title: 'Failed to update',
|
|
|
|
message: 'The application failed to update.',
|
|
|
|
buttons: ['OK'],
|
|
|
|
defaultId: 0
|
|
|
|
});
|
|
|
|
|
|
|
|
console.error('Failed to update:', err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: 'separator',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Restart',
|
|
|
|
click: () => {
|
|
|
|
this.application.relaunch();
|
|
|
|
this.application.exit(0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: 'Quit',
|
|
|
|
click: () => {
|
|
|
|
this.application.quit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
])
|
|
|
|
|
|
|
|
tray.setContextMenu(contextMenu);
|
|
|
|
this.tray = tray;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static onReady() {
|
|
|
|
Main.createTray();
|
|
|
|
|
|
|
|
Main.discoveryService = new DiscoveryService();
|
|
|
|
Main.discoveryService.start();
|
|
|
|
|
2023-12-06 11:50:26 +01:00
|
|
|
Main.tcpListenerService = new TcpListenerService();
|
|
|
|
Main.webSocketListenerService = new WebSocketListenerService();
|
|
|
|
|
2023-12-30 11:28:36 +01:00
|
|
|
const listeners = [Main.tcpListenerService, Main.webSocketListenerService];
|
2023-12-06 11:50:26 +01:00
|
|
|
listeners.forEach(l => {
|
2024-01-04 12:38:39 +01:00
|
|
|
l.emitter.on("play", async (message) => {
|
2023-12-06 13:03:23 +01:00
|
|
|
if (Main.playerWindow == null) {
|
|
|
|
Main.playerWindow = new BrowserWindow({
|
2023-12-06 11:50:26 +01:00
|
|
|
fullscreen: true,
|
|
|
|
autoHideMenuBar: true,
|
|
|
|
webPreferences: {
|
2023-12-06 13:03:23 +01:00
|
|
|
preload: path.join(__dirname, 'player/preload.js')
|
2023-12-06 11:50:26 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-06 13:03:23 +01:00
|
|
|
Main.playerWindow.setAlwaysOnTop(false, 'pop-up-menu');
|
|
|
|
Main.playerWindow.show();
|
2023-12-06 11:50:26 +01:00
|
|
|
|
2023-12-06 13:03:23 +01:00
|
|
|
Main.playerWindow.loadFile(path.join(__dirname, 'player/index.html'));
|
2024-01-04 12:38:39 +01:00
|
|
|
Main.playerWindow.on('ready-to-show', async () => {
|
|
|
|
Main.playerWindow?.webContents?.send("play", await Main.proxyPlayIfRequired(message));
|
2023-12-06 13:03:23 +01:00
|
|
|
});
|
|
|
|
Main.playerWindow.on('closed', () => {
|
|
|
|
Main.playerWindow = null;
|
2023-12-06 11:50:26 +01:00
|
|
|
});
|
|
|
|
} else {
|
2024-01-04 12:38:39 +01:00
|
|
|
Main.playerWindow?.webContents?.send("play", await Main.proxyPlayIfRequired(message));
|
2023-12-06 11:50:26 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-06 13:03:23 +01:00
|
|
|
l.emitter.on("pause", () => Main.playerWindow?.webContents?.send("pause"));
|
|
|
|
l.emitter.on("resume", () => Main.playerWindow?.webContents?.send("resume"));
|
2023-12-06 11:50:26 +01:00
|
|
|
|
|
|
|
l.emitter.on("stop", () => {
|
2023-12-06 13:03:23 +01:00
|
|
|
Main.playerWindow.close();
|
|
|
|
Main.playerWindow = null;
|
2023-12-06 11:50:26 +01:00
|
|
|
});
|
|
|
|
|
2023-12-06 13:03:23 +01:00
|
|
|
l.emitter.on("seek", (message) => Main.playerWindow?.webContents?.send("seek", message));
|
|
|
|
l.emitter.on("setvolume", (message) => Main.playerWindow?.webContents?.send("setvolume", message));
|
2023-12-07 16:10:18 +01:00
|
|
|
l.emitter.on("setspeed", (message) => Main.playerWindow?.webContents?.send("setspeed", message));
|
2023-12-06 11:50:26 +01:00
|
|
|
l.start();
|
2023-06-20 08:45:01 +02:00
|
|
|
|
2023-12-07 16:10:18 +01:00
|
|
|
ipcMain.on('send-playback-error', (event: IpcMainEvent, value: PlaybackErrorMessage) => {
|
2023-12-30 10:55:30 +01:00
|
|
|
l.send(Opcode.PlaybackError, value);
|
2023-12-07 16:10:18 +01:00
|
|
|
});
|
|
|
|
|
2023-12-06 11:50:26 +01:00
|
|
|
ipcMain.on('send-playback-update', (event: IpcMainEvent, value: PlaybackUpdateMessage) => {
|
2023-12-30 10:55:30 +01:00
|
|
|
l.send(Opcode.PlaybackUpdate, value);
|
2023-12-06 11:50:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.on('send-volume-update', (event: IpcMainEvent, value: VolumeUpdateMessage) => {
|
2023-12-30 10:55:30 +01:00
|
|
|
l.send(Opcode.VolumeUpdate, value);
|
2023-12-06 11:50:26 +01:00
|
|
|
});
|
2023-06-20 08:45:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.on('toggle-full-screen', () => {
|
2023-12-06 13:03:23 +01:00
|
|
|
const window = Main.playerWindow;
|
2023-06-20 08:45:01 +02:00
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.setFullScreen(!window.isFullScreen());
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.on('exit-full-screen', () => {
|
2023-12-06 13:03:23 +01:00
|
|
|
const window = Main.playerWindow;
|
2023-06-20 08:45:01 +02:00
|
|
|
if (!window) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.setFullScreen(false);
|
|
|
|
});
|
2023-12-06 13:03:23 +01:00
|
|
|
|
|
|
|
if (Main.shouldOpenMainWindow) {
|
|
|
|
Main.openMainWindow();
|
|
|
|
}
|
2023-06-20 08:45:01 +02:00
|
|
|
}
|
|
|
|
|
2024-01-04 12:38:39 +01:00
|
|
|
|
|
|
|
private static setupProxyServer(): Promise<void> {
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
try {
|
|
|
|
console.log(`Proxy server starting`);
|
|
|
|
|
|
|
|
const port = 0;
|
|
|
|
Main.proxyServer = http.createServer((req, res) => {
|
|
|
|
console.log(`Request received`);
|
|
|
|
const requestUrl = `http://${req.headers.host}${req.url}`;
|
|
|
|
|
|
|
|
const proxyInfo = Main.proxiedFiles.get(requestUrl);
|
|
|
|
|
|
|
|
if (!proxyInfo) {
|
|
|
|
res.writeHead(404);
|
|
|
|
res.end('Not found');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const omitHeaders = new Set([
|
|
|
|
'host',
|
|
|
|
'connection',
|
|
|
|
'keep-alive',
|
|
|
|
'proxy-authenticate',
|
|
|
|
'proxy-authorization',
|
|
|
|
'te',
|
|
|
|
'trailers',
|
|
|
|
'transfer-encoding',
|
|
|
|
'upgrade'
|
|
|
|
]);
|
|
|
|
|
|
|
|
const filteredHeaders = Object.fromEntries(Object.entries(req.headers)
|
|
|
|
.filter(([key]) => !omitHeaders.has(key.toLowerCase()))
|
|
|
|
.map(([key, value]) => [key, Array.isArray(value) ? value.join(', ') : value]));
|
|
|
|
|
|
|
|
const parsedUrl = url.parse(proxyInfo.url);
|
|
|
|
const options: http.RequestOptions = {
|
|
|
|
... parsedUrl,
|
|
|
|
method: req.method,
|
|
|
|
headers: { ...filteredHeaders, ...proxyInfo.headers }
|
|
|
|
};
|
|
|
|
|
|
|
|
const proxyReq = http.request(options, (proxyRes) => {
|
|
|
|
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
|
|
|
proxyRes.pipe(res, { end: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
req.pipe(proxyReq, { end: true });
|
|
|
|
proxyReq.on('error', (e) => {
|
|
|
|
console.error(`Problem with request: ${e.message}`);
|
|
|
|
res.writeHead(500);
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
Main.proxyServer.on('error', e => {
|
|
|
|
reject(e);
|
|
|
|
});
|
|
|
|
Main.proxyServer.listen(port, '127.0.0.1', () => {
|
|
|
|
Main.proxyServerAddress = Main.proxyServer.address() as AddressInfo;
|
|
|
|
console.log(`Proxy server running at http://127.0.0.1:${Main.proxyServerAddress.port}/`);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
reject(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
static streamingMediaTypes = [
|
|
|
|
"application/vnd.apple.mpegurl",
|
|
|
|
"application/x-mpegURL",
|
|
|
|
"application/dash+xml"
|
|
|
|
];
|
|
|
|
|
|
|
|
static async proxyPlayIfRequired(message: PlayMessage): Promise<PlayMessage> {
|
|
|
|
if (message.headers && message.url && !Main.streamingMediaTypes.find(v => v === message.container.toLocaleLowerCase())) {
|
|
|
|
return { ...message, url: await Main.proxyFile(message.url, message.headers) };
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async proxyFile(url: string, headers: { [key: string]: string }): Promise<string> {
|
|
|
|
if (!Main.proxyServer) {
|
|
|
|
await Main.setupProxyServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
const proxiedUrl = `http://127.0.0.1:${Main.proxyServerAddress.port}/${uuidv4()}`;
|
|
|
|
console.log("Proxied url", { proxiedUrl, url, headers });
|
|
|
|
Main.proxiedFiles.set(proxiedUrl, { url: url, headers: headers });
|
|
|
|
return proxiedUrl;
|
|
|
|
}
|
|
|
|
|
2023-12-06 13:03:23 +01:00
|
|
|
static getAllIPv4Addresses() {
|
|
|
|
const interfaces = os.networkInterfaces();
|
|
|
|
const ipv4Addresses: string[] = [];
|
|
|
|
|
|
|
|
for (const interfaceName in interfaces) {
|
|
|
|
const addresses = interfaces[interfaceName];
|
|
|
|
if (!addresses) continue;
|
|
|
|
|
|
|
|
for (const addressInfo of addresses) {
|
|
|
|
if (addressInfo.family === 'IPv4' && !addressInfo.internal) {
|
|
|
|
ipv4Addresses.push(addressInfo.address);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipv4Addresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
static openMainWindow() {
|
|
|
|
if (Main.mainWindow) {
|
|
|
|
Main.mainWindow.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Main.mainWindow = new BrowserWindow({
|
2023-12-06 13:08:56 +01:00
|
|
|
fullscreen: true,
|
2023-12-06 13:03:23 +01:00
|
|
|
autoHideMenuBar: true,
|
2023-12-07 16:10:18 +01:00
|
|
|
minWidth: 500,
|
|
|
|
minHeight: 920,
|
2023-12-06 13:03:23 +01:00
|
|
|
webPreferences: {
|
|
|
|
preload: path.join(__dirname, 'main/preload.js')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Main.mainWindow.loadFile(path.join(__dirname, 'main/index.html'));
|
|
|
|
Main.mainWindow.on('closed', () => {
|
|
|
|
Main.mainWindow = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
Main.mainWindow.show();
|
|
|
|
|
|
|
|
Main.mainWindow.on('ready-to-show', () => {
|
|
|
|
Main.mainWindow.webContents.send("device-info", {name: os.hostname(), addresses: Main.getAllIPv4Addresses()});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:45:01 +02:00
|
|
|
static main(app: Electron.App) {
|
|
|
|
Main.application = app;
|
2023-12-06 13:03:23 +01:00
|
|
|
const argv = process.argv;
|
|
|
|
if (argv.includes('--no-main-window')) {
|
|
|
|
Main.shouldOpenMainWindow = false;
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:45:01 +02:00
|
|
|
Main.application.on('ready', Main.onReady);
|
|
|
|
Main.application.on('window-all-closed', () => { });
|
|
|
|
}
|
|
|
|
}
|