2020-04-24 14:10:37 +00:00
const electron = require ( "electron" ) ;
2020-05-29 18:30:38 +00:00
const express = require ( 'express' ) ;
2020-04-24 14:10:37 +00:00
const open = require ( 'open' ) ;
const log = require ( 'electron-log' ) ;
const fs = require ( 'fs' ) ;
const Menu = electron . Menu ;
const https = require ( 'https' ) ;
const autoUpdater = require ( './auto-updater' ) ;
const app = electron . app ;
const session = electron . session ;
const BrowserWindow = electron . BrowserWindow ;
const path = require ( "path" ) ;
const isDev = require ( "electron-is-dev" ) ;
const options = {
key : fs . readFileSync ( path . join ( _ _dirname , './ssl/server.key' ) ) ,
cert : fs . readFileSync ( path . join ( _ _dirname , './ssl/server.crt' ) ) ,
ca : fs . readFileSync ( path . join ( _ _dirname , './ssl/rootCA.crt' ) )
} ;
const PORT = 5000 ;
const createServer = ( ) => {
const app = express ( ) ;
2020-05-21 11:42:11 +00:00
const staticRoute = path . join ( _ _dirname , '../build' ) ;
2020-04-24 14:10:37 +00:00
app . use ( express . static ( staticRoute ) ) ;
https . createServer ( options , app ) . listen ( PORT ) ;
}
let mainWindow ;
function getOpenedWindow ( url , options ) {
let display = electron . screen . getPrimaryDisplay ( ) ;
let width = display . bounds . width ;
let height = display . bounds . height ;
// filter all requests to trezor-bridge and change origin to make it work
const filter = {
urls : [ 'http://127.0.0.1:21325/*' ]
} ;
options . webPreferences . affinity = 'main-window' ;
2020-05-11 21:36:39 +00:00
if ( url . includes ( 'trezor' ) ) {
2020-04-24 14:10:37 +00:00
session . defaultSession . webRequest . onBeforeSendHeaders ( filter , ( details , callback ) => {
2020-05-11 21:36:39 +00:00
details . requestHeaders [ 'Origin' ] = 'https://connect.trezor.io' ;
2020-04-24 14:10:37 +00:00
callback ( { cancel : false , requestHeaders : details . requestHeaders } ) ;
} ) ;
}
2020-05-11 21:36:39 +00:00
if ( url . includes ( 'wallet.portis' ) || url . includes ( 'trezor' ) || url . includes ( 'app.tor.us' ) ) {
2020-04-24 14:10:37 +00:00
const win = new BrowserWindow ( {
2020-05-11 21:36:39 +00:00
width : 350 ,
2020-04-24 14:10:37 +00:00
height : 700 ,
x : width - 1300 ,
parent : mainWindow ,
2020-06-03 23:33:44 +00:00
y : height - ( process . platform === 'win32' ? 750 : 200 ) ,
2020-04-24 14:10:37 +00:00
webContents : options . webContents , // use existing webContents if provided
fullscreen : false ,
show : false ,
} ) ;
2020-05-11 21:36:39 +00:00
win . webContents . on ( 'new-window' , function ( event , url ) {
if ( url . includes ( 'trezor' ) && url . includes ( 'bridge' ) )
open ( url ) ;
} ) ;
2020-04-24 14:10:37 +00:00
win . once ( 'ready-to-show' , ( ) => win . show ( ) ) ;
if ( ! options . webPreferences ) {
win . loadURL ( url ) ;
}
return win
}
return null ;
}
function createWindow ( ) {
mainWindow = new BrowserWindow ( {
show : false ,
width : 1024 ,
height : 768 ,
webPreferences : {
preload : path . join ( _ _dirname , '../scripts/preload.js' ) ,
allowRunningInsecureContent : true ,
nativeWindowOpen : true , // need to be set in order to display modal
} ,
icon : path . join ( _ _dirname , './build/safe.png' ) ,
} ) ;
mainWindow . once ( 'ready-to-show' , ( ) => {
mainWindow . show ( ) ;
} ) ;
mainWindow . loadURL (
isDev
? "http://localhost:3000"
: ` https://localhost: ${ PORT } `
)
if ( isDev ) {
// Open the DevTools.
mainWindow . webContents . openDevTools ( ) ;
//BrowserWindow.addDevToolsExtension('<location to your react chrome extension>');
}
mainWindow . setMenu ( null ) ;
mainWindow . setMenuBarVisibility ( false ) ;
mainWindow . webContents . on ( 'new-window' , function ( event , url , frameName , disposition , options ) {
event . preventDefault ( ) ;
const win = getOpenedWindow ( url , options ) ;
if ( win ) {
win . once ( 'ready-to-show' , ( ) => win . show ( ) ) ;
if ( ! options . webPreferences ) {
win . loadURL ( url ) ;
}
event . newGuest = win
} else open ( url ) ;
} ) ;
mainWindow . webContents . on ( 'did-finish-load' , ( ) => {
autoUpdater . init ( mainWindow ) ;
} ) ;
2020-05-04 13:33:12 +00:00
mainWindow . webContents . on ( 'crashed' , ( event ) => {
log . info ( ` App Crashed: ${ event } ` ) ;
2020-04-24 14:10:37 +00:00
mainWindow . reload ( ) ;
} ) ;
mainWindow . on ( "closed" , ( ) => ( mainWindow = null ) ) ;
}
2020-05-04 13:33:12 +00:00
process . on ( 'uncaughtException' , function ( error ) {
log . error ( error ) ;
} ) ;
2020-06-03 23:33:44 +00:00
app . userAgentFallback = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) old-airport-include/1.0.0 Chrome Electron/7.1.7 Safari/537.36' ;
2020-04-24 14:10:37 +00:00
2020-09-14 19:06:10 +00:00
// We have one non-context-aware module in node_modules/usb. This is used by @ledgerhq/hw-transport-node-hid
// This type of modules will be impossible to use after electron 10
app . allowRendererProcessReuse = false ;
2020-04-24 14:10:37 +00:00
app . commandLine . appendSwitch ( 'ignore-certificate-errors' ) ;
app . on ( "ready" , ( ) => {
// Hide the menu
2020-05-03 21:02:41 +00:00
Menu . setApplicationMenu ( null ) ;
2020-04-24 14:10:37 +00:00
if ( ! isDev ) createServer ( ) ;
createWindow ( ) ;
} ) ;
app . on ( "window-all-closed" , ( ) => {
if ( process . platform !== "darwin" ) {
app . quit ( ) ;
}
} ) ;
app . on ( "activate" , ( ) => {
if ( mainWindow === null ) {
createWindow ( ) ;
}
2020-05-29 18:30:38 +00:00
} ) ;