-
Notifications
You must be signed in to change notification settings - Fork 864
/
index.js
164 lines (135 loc) · 4.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const { screen, BrowserWindow, ipcMain, app, session } = require('electron')
const { join } = require('path')
const { URL } = require('url')
const toUri = require('multiaddr-to-uri')
const serve = require('electron-serve')
const os = require('os')
const openExternal = require('./open-external')
const logger = require('../common/logger')
const store = require('../common/store')
const { IS_MAC, IS_WIN } = require('../common/consts')
const dock = require('../utils/dock')
const { VERSION } = require('../common/consts')
const createToggler = require('../utils/create-toggler')
serve({ scheme: 'webui', directory: join(__dirname, '../../assets/webui') })
const CONFIG_KEY = 'openWebUIAtLaunch'
const createWindow = () => {
const dimensions = screen.getPrimaryDisplay()
const window = new BrowserWindow({
title: 'IPFS Desktop',
show: false,
autoHideMenuBar: true,
titleBarStyle: 'hiddenInset',
fullscreenWindowTitle: true,
width: store.get('window.width', dimensions.width < 1440 ? dimensions.width : 1440),
height: store.get('window.height', dimensions.height < 900 ? dimensions.height : 900),
webPreferences: {
preload: join(__dirname, 'preload.js'),
webSecurity: false,
allowRunningInsecureContent: false,
nodeIntegration: process.env.NODE_ENV === 'test'
}
})
window.webContents.on('crashed', event => {
logger.error(`[web ui] crashed: ${event.toString()}`)
})
window.webContents.on('unresponsive', event => {
logger.error(`[web ui] unresponsive: ${event.toString()}`)
})
window.on('resize', () => {
const dim = window.getSize()
store.set('window.width', dim[0])
store.set('window.height', dim[1])
})
window.on('close', (event) => {
event.preventDefault()
window.hide()
dock.hide()
logger.info('[web ui] window hidden')
})
app.on('before-quit', () => {
// Makes sure the app quits even though we prevent
// the closing of this window.
window.removeAllListeners('close')
})
return window
}
// Converts a Multiaddr to a valid value for Origin HTTP header
const apiOrigin = (apiMultiaddr) => {
// Return opaque origin when there is no API yet
// https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque
if (!apiMultiaddr) return 'null'
// Return the Origin of HTTP API
const apiUri = toUri(apiMultiaddr, { assumeHttp: true })
return new URL(apiUri).origin
}
module.exports = async function (ctx) {
if (store.get(CONFIG_KEY, null) === null) {
// First time running this. If it's not macOS, nor Windows,
// enable opening ipfs-webui at app launch.
// This is the best we can do to mitigate Tray issues on Linux:
// https://github.com/ipfs-shipyard/ipfs-desktop/issues/1153
store.set(CONFIG_KEY, !IS_MAC && !IS_WIN)
}
createToggler(CONFIG_KEY, async ({ newValue }) => {
store.set(CONFIG_KEY, newValue)
return true
})
openExternal()
const window = createWindow(ctx)
let apiAddress = null
ctx.webui = window
const url = new URL('/', 'webui://-')
url.hash = '/blank'
url.searchParams.set('deviceId', ctx.countlyDeviceId)
ctx.launchWebUI = (path, { focus = true } = {}) => {
if (!path) {
logger.info('[web ui] launching web ui')
} else {
logger.info(`[web ui] navigate to ${path}`)
window.webContents.send('updatedPage', path)
url.hash = path
}
if (focus) {
window.show()
window.focus()
dock.show()
}
}
function updateLanguage () {
url.searchParams.set('lng', store.get('language'))
}
ipcMain.on('ipfsd', async () => {
const ipfsd = await ctx.getIpfsd(true)
if (ipfsd && ipfsd.apiAddr !== apiAddress) {
apiAddress = ipfsd.apiAddr
url.searchParams.set('api', apiAddress)
updateLanguage()
window.loadURL(url.toString())
}
})
ipcMain.on('config.get', () => {
window.webContents.send('config.changed', {
platform: os.platform(),
config: store.store
})
})
session.defaultSession.webRequest.onBeforeSendHeaders((details, callback) => {
// Avoid setting CORS by acting like /webui loaded from API port
details.requestHeaders.Origin = apiOrigin(apiAddress)
details.requestHeaders['User-Agent'] = `ipfs-desktop/${VERSION}`
callback({ cancel: false, requestHeaders: details.requestHeaders }) // eslint-disable-line
})
return new Promise(resolve => {
window.once('ready-to-show', () => {
logger.info('[web ui] window ready')
if (store.get(CONFIG_KEY)) {
ctx.launchWebUI('/')
}
resolve()
})
updateLanguage()
window.loadURL(url.toString())
})
}
module.exports.CONFIG_KEY = CONFIG_KEY