-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.js
More file actions
195 lines (162 loc) · 5.3 KB
/
main.js
File metadata and controls
195 lines (162 loc) · 5.3 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const { app, dialog, shell, ipcMain, BrowserWindow } = require('electron')
const { autoUpdater } = require('electron-updater')
const unhandled = require('electron-unhandled')
const Store = require('electron-store')
const fetch = require('node-fetch')
const path = require('path')
// Catch unhandled errors and promise rejections
unhandled()
const store = new Store({ encryptionKey: 'encryption-key-here' })
const accountId = '1fddcec8-8dd3-4d8d-9b16-215cac0f9b52'
const productId = '858e0235-3237-46e4-a86c-ef01ae0b2c21'
const isDev = process.env.NODE_ENV === 'development'
store.set('app.version', app.getVersion())
// Validate a license key for the product. Returns the validation result and a license object.
async function validateLicenseKey(key) {
const validation = await fetch(`https://api.keygen.sh/v1/accounts/${accountId}/licenses/actions/validate-key`, {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/json',
},
body: JSON.stringify({
meta: {
scope: { product: productId },
key,
}
}),
})
const { meta, data, errors } = await validation.json()
if (errors) {
return { status: validation.status, errors }
}
return {
status: validation.status,
meta,
data,
}
}
// Wrap the main application window in a license gate. The main window will
// only be launched when a valid license is provided.
async function gateAppLaunchWithLicense(appLauncher) {
const gateWindow = new BrowserWindow({
resizable: false,
frame: false,
width: 420,
height: 200,
webPreferences: {
preload: path.join(__dirname, 'gate.js'),
devTools: isDev,
},
})
gateWindow.loadFile('gate.html')
if (isDev) {
gateWindow.webContents.openDevTools({ mode: 'detach' })
}
ipcMain.on('GATE_SUBMIT', async (_event, { key }) => {
// Validate the license key
const res = await validateLicenseKey(key)
if (res.errors) {
const [{ code }] = res.errors
const choice = await dialog.showMessageBox(gateWindow, {
type: 'error',
title: 'Your license is invalid',
message: 'The license key you entered does not exist for this product. Would you like to buy a license?',
detail: `Error code: ${code ?? res.status}`,
buttons: [
'Continue evaluation',
'Try again',
'Buy a license',
],
})
switch (choice.response) {
case 0:
// Set to evaluation mode
store.set('app.mode', 'EVALUATION')
store.delete('license')
// Close the license gate window
gateWindow.close()
// Launch our main app
appLauncher(key)
break
case 1:
// noop (dismiss and try again)
break
case 2:
// TODO(ezekg) Open a link to purchase page
shell.openExternal('https://keygen.sh/for-electron-apps/')
break
}
return
}
// Branch on the license's validation code
const { valid, code } = res.meta
switch (code) {
// License is valid. All is well.
case 'VALID':
// For expired licenses, we still want to allow the app to be used, but automatic
// updates will not be allowed.
case 'EXPIRED': {
const license = res.data
store.set('license.expiry', license.attributes.expiry)
store.set('license.key', license.attributes.key)
store.set('license.status', code)
store.set('app.mode', 'LICENSED')
await dialog.showMessageBox(gateWindow, {
type: valid ? 'info' : 'warning',
title: 'Thanks for your business!',
message: `Your license ID is ${res.data.id.substring(0, 8)}. It is ${code.toLowerCase()}.`,
detail: valid ? 'Automatic updates are enabled.' : 'Automatic updates are disabled.',
buttons: [
'Continue',
],
})
// Close the license gate window
gateWindow.close()
// Launch our main app
appLauncher(key)
break
}
// All other validation codes, e.g. SUSPENDED, etc. are treated as invalid.
default: {
store.set('app.mode', 'UNLICENSED')
store.delete('license')
await dialog.showMessageBox(gateWindow, {
type: 'error',
title: 'Your license is invalid',
message: 'That license key is no longer valid.',
detail: `Validation code: ${code}`,
buttons: [
'Exit',
],
})
app.exit(1)
break
}
}
})
}
// Launch the main application window and configure automatic updates.
function launchAppWithLicenseKey(key) {
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
devTools: isDev,
},
})
mainWindow.loadFile('index.html')
if (!isDev) {
// Check for updates right away using license key authentication
autoUpdater.addAuthHeader(`License ${key}`)
autoUpdater.checkForUpdatesAndNotify()
// Check for updates periodically
setInterval(
autoUpdater.checkForUpdatesAndNotify,
1000 * 60 * 60 * 3, // 3 hours in ms
)
}
}
app.whenReady().then(() => gateAppLaunchWithLicense(launchAppWithLicenseKey))
app.on('window-all-closed', () => app.quit())