This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
139 lines (113 loc) · 3.75 KB
/
app.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
const { app, BrowserWindow, shell, ipcMain } = require('electron')
const path = require('path')
var express = require("express")
const request = require('request');
var wapp = express();
var wserver
var win
var authToken
var callbackUri = "http://localhost:12971/callback"
var idmsaUri = "https://idmsa.idledev.org"
var idmsaId = "BMHpIDSRBGSxRhfA1cCAfKQEnksQRvl71Gn3x7g4"
var idmsaSecret = "0M4frXRRaiMyVEu0Wcmwp8hnyEzt0xF1I67ZPnsb"
function createWindow () {
if(process.platform === 'darwin'){
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
},
titleBarStyle: 'hidden'
})
} else {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
}
win.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
ipcMain.on('init',function(e,a){
if(authToken === undefined){
initWASession()
var _flagCheck = setInterval(function() {
if (authToken !== undefined) {
clearInterval(_flagCheck);
win.webContents.send('auth',authToken)
}
}, 100);
} else {
win.webContents.send('auth',authToken)
}
})
ipcMain.on('back', function(e,a){
if(a == 'home'){
win.loadFile('index.html')
}
})
ipcMain.on('loadPtero', function(e,a){
let url = "https://my.idley.gg/api/createAutoLoginToken?access_token="+authToken;
let options = {json: true};
request(url, options, (error, res, body) => {
if (error) {
return console.log(error)
};
if (!error && res.statusCode == 200) {
win.loadURL("https://control.idley.gg/auth/login?idle_ias="+body.token)
.then(function(){
win.loadURL("https://control.idley.gg/server/"+a.userData.servers.find(x => x.id === a.server_id).uuidShort)
.then(function(){
win.webContents.executeJavaScript(`
function goBackToMain(){
const ipc = require('electron').ipcRenderer;
ipc.send('back','home')
}
document.body.innerHTML = "<div style='position:absolute; right: 20px; top: 20px;'><button style='height: 40px; width: 60px; border: none; border-radius: 10px; background: white; color:black;' onclick='goBackToMain()'>Back</button></div>"+document.body.innerHTML;
`)
})
})
};
});
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
async function initWebAuth() {
wapp.get("/callback", (req, res) => {
res.send("Authorisation complete, you may safely return back to the app.")
request.post({
url: idmsaUri+"/oauth/token",
form: {
grant_type: 'authorization_code',
code: req.query.code,
client_id: idmsaId,
client_secret: idmsaSecret,
redirect_uri: callbackUri
}
}, function (err, httpResponse, body) {
authToken = JSON.parse(body).access_token
})
wserver.close()
})
wserver = wapp.listen(12971)
}
async function initWASession(){
await initWebAuth();
shell.openExternal(`${idmsaUri}/oauth/authorize?client_id=${idmsaId}&redirect_uri=${encodeURIComponent(callbackUri)}&response_type=code`)
}