-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from wizardAEI/dev
Dev
- Loading branch information
Showing
18 changed files
with
493 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
provider: generic | ||
url: https://example.com/auto-updates | ||
updaterCacheDirName: Gomoon-updater | ||
provider: github | ||
owner: wizardAEI | ||
repo: Gomoon | ||
# url: https://example.com/auto-updates | ||
# updaterCacheDirName: Gomoon-updater |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import { | |
clipboard, | ||
globalShortcut, | ||
OnBeforeSendHeadersListenerDetails, | ||
BeforeSendResponse | ||
BeforeSendResponse, | ||
session | ||
} from 'electron' | ||
import { join } from 'path' | ||
import { is } from '@electron-toolkit/utils' | ||
|
@@ -16,6 +17,39 @@ import trayIcon from '../../resources/[email protected]?asset' | |
import { loadUserConfig } from './model' | ||
import { getResourcesPath, quitApp } from './lib' | ||
import { spawn } from 'child_process' | ||
import { autoUpdater } from 'electron-updater' | ||
|
||
let mainWindow: BrowserWindow | null = null | ||
let tray: Tray | null = null | ||
let preKeys = '' | ||
|
||
export function setQuicklyWakeUp(keys: string) { | ||
/** | ||
* FEAT: 按键监听 | ||
*/ | ||
globalShortcut.register(keys, () => { | ||
if (mainWindow?.isVisible()) { | ||
mainWindow?.hide() | ||
return | ||
} | ||
mainWindow?.webContents.send('show-window') | ||
mainWindow?.show() | ||
}) | ||
preKeys && globalShortcut.unregister(preKeys) | ||
preKeys = keys | ||
} | ||
|
||
export function hideWindow() { | ||
mainWindow?.hide() | ||
} | ||
|
||
export function minimize() { | ||
mainWindow?.minimize() | ||
} | ||
|
||
export function showWindow() { | ||
mainWindow?.show() | ||
} | ||
|
||
const cors: { | ||
defaultURLs: string[] | ||
|
@@ -36,7 +70,7 @@ const cors: { | |
const { origin, host } = new URL(details.url) | ||
details.requestHeaders['Origin'] = origin | ||
details.requestHeaders['Host'] = host | ||
callback({ requestHeaders: details.requestHeaders }) | ||
callback({ cancel: false, requestHeaders: details.requestHeaders }) | ||
} | ||
} | ||
type cspItem = 'default-src' | 'script-src' | 'style-src' | 'connect-src' | 'img-src' | 'worker-src' | ||
|
@@ -50,11 +84,12 @@ function csp(items?: { | |
'script-src': ['https://cdn.jsdelivr.net', "'unsafe-eval'"], | ||
'style-src': ["'unsafe-inline'"], | ||
'connect-src': [ | ||
'https://dashscope.aliyuncs.com', | ||
'https://api.openai.com', | ||
'https://tiktoken.pages.dev', | ||
'https://aip.baidubce.com', | ||
'https://cdn.jsdelivr.net', | ||
// 'https://dashscope.aliyuncs.com', | ||
// 'https://api.openai.com', | ||
// 'https://tiktoken.pages.dev', | ||
// 'https://aip.baidubce.com', | ||
// 'https://cdn.jsdelivr.net', | ||
'https:', | ||
'http://www.baidu.com', | ||
'data:' | ||
], | ||
|
@@ -66,6 +101,16 @@ function csp(items?: { | |
if (!item) { | ||
return '' | ||
} | ||
// 去除网址后的路径 | ||
item = item.map((url) => { | ||
try { | ||
const { origin } = new URL(url) | ||
if (origin === 'null') return url | ||
return origin || url | ||
} catch (e) { | ||
return url | ||
} | ||
}) | ||
return item.join(' ') | ||
} | ||
for (let item in defaultItem) { | ||
|
@@ -78,74 +123,33 @@ function csp(items?: { | |
'; ' | ||
} | ||
return cspStr | ||
// "default-src 'self'; script-src https://cdn.jsdelivr.net 'self' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; connect-src 'self' https://dashscope.aliyuncs.com https://api.openai.com https://api.chatanywhere.com.cn https://api.chatanywhere.tech https://tiktoken.pages.dev https://aip.baidubce.com https://cdn.jsdelivr.net http://www.baidu.com data:; img-src https: http: data: 'self'; worker-src 'self' blob:;" | ||
} | ||
|
||
let mainWindow: BrowserWindow | null = null | ||
let tray: Tray | null = null | ||
let preKeys = '' | ||
|
||
export function setQuicklyWakeUp(keys: string) { | ||
/** | ||
* FEAT: 按键监听 | ||
*/ | ||
globalShortcut.register(keys, () => { | ||
if (mainWindow?.isVisible()) { | ||
mainWindow?.hide() | ||
return | ||
} | ||
mainWindow?.webContents.send('show-window') | ||
mainWindow?.show() | ||
}) | ||
preKeys && globalShortcut.unregister(preKeys) | ||
preKeys = keys | ||
} | ||
|
||
export function hideWindow() { | ||
mainWindow?.hide() | ||
} | ||
|
||
export function minimize() { | ||
mainWindow?.minimize() | ||
} | ||
|
||
export function showWindow() { | ||
mainWindow?.show() | ||
} | ||
|
||
export function updateCors(urls: string[]) { | ||
mainWindow?.webContents.session.clearCache() | ||
mainWindow?.webContents.session.webRequest.onBeforeSendHeaders( | ||
export function updateSendHeaders(urls: string[] = []) { | ||
session.defaultSession.webRequest.onBeforeSendHeaders( | ||
{ urls: cors.defaultURLs.concat(urls.map((url) => (url.endsWith('/*') ? url : url + '/*'))) }, | ||
cors.handler | ||
) | ||
mainWindow?.webContents.session.webRequest.onHeadersReceived( | ||
} | ||
export function updateRespHeaders( | ||
urls: string[] = [], | ||
conf?: { cspItems?: { [key in cspItem]?: string[] } } | ||
) { | ||
session.defaultSession.webRequest.onHeadersReceived( | ||
{ urls: cors.defaultURLs.concat(urls.map((url) => (url.endsWith('/*') ? url : url + '/*'))) }, | ||
(details, callback) => { | ||
if (details.responseHeaders) { | ||
details.responseHeaders['Access-Control-Allow-Origin'] = [] | ||
details.responseHeaders['access-control-allow-origin'] = ['*'] | ||
details.responseHeaders['access-control-allow-headers'] = ['*'] | ||
details.responseHeaders['access-control-allow-methods'] = ['*'] | ||
details.responseHeaders['access-control-allow-credentials'] = ['true'] | ||
details.responseHeaders['Content-Security-Policy'] = [csp(conf?.cspItems)] | ||
} | ||
callback({ responseHeaders: details.responseHeaders }) | ||
} | ||
) | ||
} | ||
|
||
export function updateCSP(items: { [key in cspItem]?: string[] }) { | ||
mainWindow?.webContents.session.clearCache() | ||
mainWindow?.webContents.session.webRequest.onHeadersReceived((details, callback) => { | ||
callback({ | ||
responseHeaders: { | ||
...details.responseHeaders, | ||
'Content-Security-Policy': csp(items) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
export function createWindow(): void { | ||
const userConfig = loadUserConfig() | ||
|
||
|
@@ -167,6 +171,8 @@ export function createWindow(): void { | |
// preConfig | ||
mainWindow!.setAlwaysOnTop(userConfig.isOnTop, 'status') | ||
|
||
autoUpdater.checkForUpdatesAndNotify() | ||
|
||
// FEAT: 链接跳转,自动打开浏览器 | ||
mainWindow.webContents.on('will-frame-navigate', (event) => { | ||
if (event.url.includes('localhost')) { | ||
|
@@ -179,11 +185,10 @@ export function createWindow(): void { | |
// FEAT: 双击复制回答 | ||
if (userConfig.canMultiCopy) { | ||
let filename = 'eventTracker' | ||
if (process.arch === 'x64') { | ||
filename = 'eventTracker_x64' | ||
} | ||
if (process.platform === 'win32') { | ||
filename += '.exe' | ||
} else if (process.arch === 'x64') { | ||
filename = 'eventTracker_x64' | ||
} | ||
const eventTracker = spawn(getResourcesPath(filename)) | ||
eventTracker.stdout.on('data', (data) => { | ||
|
@@ -245,6 +250,9 @@ export function createWindow(): void { | |
return { action: 'deny' } | ||
}) | ||
|
||
updateSendHeaders() | ||
updateRespHeaders() | ||
|
||
// HMR for renderer base on electron-vite cli. | ||
// Load the remote URL for development or the local html file for production. | ||
if (is.dev && process.env['ELECTRON_RENDERER_URL']) { | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.