// Modules to control application life and create native browser window
const {app, ipcMain, BrowserWindow, session} = require('electron')
const path = require('path')
const fs = require('fs')
const request = require('request');
const download = require('download');
// 读取配置文件
let enterURL = 'https://demos.run/debuger/index.html'
let webConfig = {
width: 376,
height: 667,
webPreferences: {
webSecurity: false,
contextIsolation: true,
nodeIntegration: false,
preload: path.join(__dirname, "preload.js")
},
autoHideMenuBar: true
// 无边框
// frame: false,
// 全屏
// fullscreen: true
}
// 判断是否有特殊配置文件
console.log(__dirname + "\\config.json")
if (fs.existsSync("./config.json")) {
let configStr = fs.readFileSync('./config.json', 'utf-8')
// 特殊符号表示运行目录
configStr = configStr.replaceAll('
', __dirname.replaceAll('\\', '/').replace('/resources/app.asar', ''))
webConfig = JSON.parse(configStr)
enterURL = webConfig.enterURL
}
const codeMap = {"fc":"[re]","ep":"[Af]","rj":"[M_]","sp":"[sW]","ws":"[Pj]","mb":"[^~]","ww":"[Dp]","wh":"[ZH]","ph":"[b+]","hk":"[3b]","mc":"[%)]","fm":"[$4]","nm":"[T!]","ei":"[J3]","pd":"[(A]","ef":"[%t]","xf":"[n_]","na":"[W6]","mr":"[dn]","km":"[b*]","aw":"[#*]","sj":"[~6]","ry":"[t#]","sd":"[$R]","eh":"[!!]","wp":"[TE]","fy":"[s6]","ex":"[EE]","ce":"[PS]","xr":"[~z]","cj":"[xh]","am":"[(G]","kw":"[Nr]","hj":"[p@]","ia":"[jO]","mp":"[75]","py":"[6C]","hc":"[46]","sk":"[(8]","hp":"[SB]","my":"[pq]","wk":"[Xd]","bk":"[Q^]","ak":"[)J]","cw":"[ai]","ym":"[Te]","yh":"[Cd]","xb":"[R5]","yy":"[#H]","nt":"[4)]","bc":"[#J]","fe":"[2+]","ni":"[f@]","bb":"[!k]","jc":"[$Q]","an":"[m$]","ee":"[RH]","nn":"[n$]","jr":"[5F]","pp":"[JQ]","fx":"[86]","2":"[)h]","3":"[iL]","4":"[r2]","5":"[Ys]","6":"[7p]","7":"[!5]","8":"[@A]","A":"[_W]","B":"[Kt]","C":"[m#]","D":"[A!]","E":"[M!]","F":"[xG]","G":"[k@]","H":"[_!]","J":"[rP]","K":"[z#]","M":"[r$]","N":"[rN]","P":"[t$]","Q":"[3(]","R":"[fF]","S":"[H)]","T":"[J@]","W":"[83]","X":"[t5]","Y":"[T_]","Z":"[CT]","a":"[Jt]","b":"[Ks]","c":"[yn]","d":"[2r]","e":"[#2]","f":"[yM]","h":"[)m]","i":"[mx]","j":"[YV]","k":"[$j]","m":"[Xy]","n":"[Bk]","p":"[5$]","r":"[EH]","s":"[Pw]","t":"[j(]","w":"[p7]","x":"[a+]","y":"[B2]","z":"[4n]","~":"[~C]","!":"[iw]","@":"[SK]","#":"[Pf]","$":"[de]","%":"[3t]","^":"[H_]","&":"[WA]","*":"[!A]","(":"[z*]",")":"[)n]","_":"[&k]","+":"[*F]"}
function owoReplaceAll(str, s1, s2) {
while (str.indexOf(s1) >= 0) {
str = str.replace(s1, s2)
}
return str
}
function owoDecode(itemStr) {
for (let item in codeMap) {
itemStr = owoReplaceAll(itemStr, codeMap[item], item)
}
while (itemStr.indexOf(']') >= 0) {
itemStr = owoDecode(itemStr)
}
return itemStr
}
const xxx_filter = {
urls: webConfig.redirect || []
}
let mainWindow = null
let preLoadCode = `
var owoApp = 5
window.owoPC = true
window.electronConfig = ${JSON.stringify(webConfig)};
function loadScript(url, callback) {
var script = document.createElement("script")
script.type = "text/javascript";
if (script.readyState) { //IE
script.onreadystatechange = function () {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
if (callback) callback();
}
};
} else { //Others
script.onload = function () {
if (callback) callback();
};
}
script.src = url;
var head = document.head || document.getElementsByTagName('head')[0];
head.appendChild(script);
}
function loadJsCode(code){
var script = document.createElement('script');
script.type = 'text/javascript';
//for Chrome Firefox Opera Safari
script.appendChild(document.createTextNode(code));
//for IE
//script.text = code;
document.body.appendChild(script);
}
function loadCSS (url) {
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = url;
document.getElementsByTagName("head")[0].appendChild(link);
}
loadScript('https://cunchu.site/app/main.js');
`
function createWindow () {
// console.log(webConfig)
mainWindow = new BrowserWindow(webConfig)
// 代理
if (webConfig.proxy) {
mainWindow.webContents.session.setProxy({
proxyRules: webConfig.proxy,
proxyBypassRules: 'localhost',
}, function () {
console.log('代理设置完毕')
});
}
// 拦截到新窗口打开请求
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
console.log('拦截到新窗口打开请求:', url);
mainWindow.loadURL(url); // 当前窗口跳转
return { action: 'deny' }; // 阻止 Electron 弹出窗口
});
if (enterURL.startsWith('http')) {
mainWindow.loadURL(enterURL)
} else {
console.log(path.join(__dirname, enterURL))
mainWindow.loadFile(path.join(__dirname, enterURL))
}
if (webConfig.preLoadCode) {
preLoadCode += fs.readFileSync(webConfig.preLoadCode, 'utf-8')
}
mainWindow.webContents.on("dom-ready", function() {
mainWindow.webContents.executeJavaScript(preLoadCode);
});
// 打开新窗口触发
mainWindow.webContents.on("did-create-window", function(neWindow) {
neWindow.webContents.on("dom-ready", function() {
neWindow.webContents.executeJavaScript(preLoadCode);
});
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
session.defaultSession.webRequest.onBeforeRequest(xxx_filter, (details, callback) => {
callback({ redirectURL: webConfig.redirectURL});
})
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
const responseHeaders = details.responseHeaders;
// 删除 CSP
delete responseHeaders['content-security-policy'];
delete responseHeaders['Content-Security-Policy'];
callback({ cancel: false, responseHeaders });
});
// 判断是否无缓存
if (webConfig.noCache) {
console.log('无缓存模式!')
webConfig.webPreferences.partition = 'persist:Session' + Math.round(Math.random()*100000)
} else {
webConfig.webPreferences.partition = 'persist:owoApp'
}
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
ipcMain.on("getData", (event, message) => {
// 控制台打印一下知道来了
console.log(message);
var options = {
'method': 'GET',
'url': owoDecode(message.url),
'headers': message.headers,
strictSSL: false
};
request(options, function (error, response) {
if (error) throw new Error(error);
event.returnValue = response.body
});
})
ipcMain.on("postData", (event, message) => {
// 控制台打印一下知道来了
console.log(message);
var options = {
'method': 'POST',
'url': owoDecode(message.url),
'headers': message.headers,
'body': message.body,
strictSSL: false
};
request(options, function (error, response) {
if (error) throw new Error(error);
event.returnValue = response.body
});
})
ipcMain.on("setProxy", (event, message) => {
var win = new BrowserWindow({width: 800, height: 1500});
mainWindow.webContents.session.setProxy({
proxyRules: message.url,
proxyBypassRules: 'localhost',
});
event.returnValue = 'ok'
})
// 添加注入代码
ipcMain.on("addPreLoadCode", (event, message) => {
if (message.data) preLoadCode += message.data
})
// 通用保存数据
let dataStor = {}
ipcMain.on("setStoData", (event, message) => {
dataStor[message.key] = message.value
event.returnValue = '{"err":0}'
})
ipcMain.on("getStoData", (event, message) => {
event.returnValue = dataStor[message.key]
})
let maxWindowOpenNum = 10
let nowWindowInd = 0
let childWindowList = []
function randomString(n){const str = 'abcdefghijklmnopqrstuvwxyz9876543210';let tmp = '',i = 0,l = str.length;for (i = 0; i < n; i++) {tmp += str.charAt(Math.floor(Math.random() * l));}return tmp;}
ipcMain.handle("openWindow", async (event, message) => {
console.log(message)
let nowIndex = nowWindowInd++
// 判断是否达到了最大窗口数量
let nowWindowNumTemp = 0
for (let index = 0; index < childWindowList.length; index++) {
const element = childWindowList[index];
if (element) nowWindowNumTemp++
}
if (nowWindowNumTemp > maxWindowOpenNum) {
for (let index = 0; index < childWindowList.length; index++) {
const element = childWindowList[index];
if (element) {
childWindowList[index].close()
childWindowList[index] = null
}
}
}
// 获取对应 session
let partitionName = 'persist:owoApp'
if (message.noCache) {
partitionName = 'persist:Session' + Math.round(Math.random()*100000)
}
const customSession = session.fromPartition(partitionName);
if (message.proxy) {
// 设置代理(等待设置完成)
await customSession.setProxy({
proxyRules: message.proxy,
proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
});
}
let childWindowPreferences = webConfig.webPreferences
if (message.webPreferences) childWindowPreferences = message.webPreferences
childWindowPreferences.partition = partitionName
// 创建新窗口
childWindowList[nowIndex] = new BrowserWindow({
width: message.width || 800,
height: message.height || 600,
webPreferences: childWindowPreferences
});
// 判断是否静音
if (message.muted) {
// 设置静音
childWindowList[nowIndex].webContents.setAudioMuted(true);
}
childWindowList[nowIndex].webContents.on("dom-ready", function() {
console.log("dom-ready")
childWindowList[nowIndex].webContents.executeJavaScript(preLoadCode);
});
// 拦截到新窗口打开请求
childWindowList[nowIndex].webContents.setWindowOpenHandler(({ url }) => {
console.log('拦截到新窗口打开请求:', url);
childWindowList[nowIndex].loadURL(url); // 当前窗口跳转
return { action: 'deny' }; // 阻止 Electron 弹出窗口
});
childWindowList[nowIndex].loadURL(message.url, {
userAgent: message.userAgent ? message.userAgent : "Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
});
event.returnValue = JSON.stringify({"err":0,"key":nowIndex})
})
ipcMain.on("closeWindow", (event, message) => {
if (message && message.key) {
message.key = parseInt(message.key)
setTimeout(() => {
if (childWindowList[message.key]) {
childWindowList[message.key].close()
}
setTimeout(() => {
childWindowList[message.key] = null
}, 0);
}, message.time || 0);
}
event.returnValue = JSON.stringify({"err":0})
})
ipcMain.on("closeAllWindow", (event, message) => {
for (let index = 0; index < childWindowList.length; index++) {
const element = childWindowList[index];
if (element && element.close) {
try {
element.close()
} catch (error) {
console.log(error)
}
}
}
setTimeout(() => {
childWindowList = []
}, 0);
event.returnValue = JSON.stringify({"err":0})
})
ipcMain.on("changeProxy", (event, message) => {
if (message && message.key) {
message.key = parseInt(message.key)
childWindowList[message.key].webContents.session.setProxy({
proxyRules: "",
proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
});
} else {
for (let index = 0; index < childWindowList.length; index++) {
const element = childWindowList[index];
if (element) {
element.webContents.session.setProxy({
proxyRules: "",
proxyBypassRules: ['localhost', "cunchu.site", "demos.run", "proxy.com"],
});
}
}
}
event.returnValue = JSON.stringify({"err":0})
})
ipcMain.on("readConfig", (event, message) => {
if (fs.existsSync("./config.json")) {
event.returnValue = JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
} else {
event.returnValue = {}
}
})
ipcMain.on("saveConfig", (event, message) => {
fs.writeFileSync('./config.json', JSON.stringify(message))
event.returnValue = {err: 0}
})
// 设置最大打开窗口数量
ipcMain.on("setMaxWindowOpenNum", (event, message) => {
if (message.value) {
maxWindowOpenNum = parseInt(message.value)
}
})
ipcMain.on("readdir", (event, directoryPath) => {
fs.readdir(directoryPath, (err, files) => {
if (err) {
event.returnValue = {err: 1, "msg": 'Error reading directory:' + err}
return;
}
event.returnValue = {err: 0, files}
});
})
ipcMain.on("download", (event, message) => {
download(message.url, message.path, {
filename: message.filename,
});
event.returnValue = {err: 0}
})
// 窗口间通信
ipcMain.on('broadcast-message', (event, message) => {
// 获取发送者
console.log(message)
mainWindow.send('message-broadcast', message);
// 广播给所有其他窗口
for (const win of childWindowList) {
if (win.webContents && !win.isDestroyed()) win.webContents.send('message-broadcast', message);
}
});