Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 128 additions & 3 deletions .github/workflows/build-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ jobs:
fs.mkdirSync('electron', { recursive: true });
}

const mainJsContent = 'const { app, BrowserWindow, Menu, shell, globalShortcut } = require(\\'electron\\');\\n' +
const mainJsContent = 'const { app, BrowserWindow, Menu, shell, globalShortcut, ipcMain } = require(\\'electron\\');\\n' +
'const path = require(\\'path\\');\\n' +
'const fs = require(\\'fs\\');\\n' +
'const isDev = process.env.NODE_ENV === \\'development\\';\\n\\n' +
Expand All @@ -204,7 +204,8 @@ jobs:
' enableRemoteModule: false,\\n' +
' webSecurity: false,\\n' +
' allowRunningInsecureContent: true,\\n' +
' devTools: true // 生产环境也允许 DevTools 便于排障\\n' +
' devTools: true, // 生产环境也允许 DevTools 便于排障\\n' +
' preload: path.join(__dirname, \\'preload.js\\')\\n' +
' },\\n' +
' icon: path.join(__dirname, \\'../build/icon.png\\'),\\n' +
' titleBarStyle: \\'default\\', // 使用默认标题栏,避免重叠问题\\n' +
Expand Down Expand Up @@ -375,8 +376,123 @@ jobs:
' mainWindow = null;\\n' +
' });\\n' +
'}\\n\\n' +
'const PROXY_CONFIG_PATH = path.join(app.getPath(\\'userData\\'), \\'proxy-config.json\\');\\n\\n' +
'function loadProxyConfig() {\\n' +
' try {\\n' +
' if (fs.existsSync(PROXY_CONFIG_PATH)) {\\n' +
' return JSON.parse(fs.readFileSync(PROXY_CONFIG_PATH, \\'utf-8\\'));\\n' +
' }\\n' +
' } catch (e) { console.error(\\'Failed to load proxy config:\\', e); }\\n' +
' return { enabled: false, type: \\'http\\', host: \\'\\', port: 7890 };\\n' +
'}\\n\\n' +
'function saveProxyConfig(config) {\\n' +
' fs.writeFileSync(PROXY_CONFIG_PATH, JSON.stringify(config, null, 2));\\n' +
'}\\n\\n' +
'async function applyProxy(config) {\\n' +
' if (!mainWindow || mainWindow.isDestroyed()) return;\\n' +
' if (config.enabled && config.host && config.port) {\\n' +
' let auth = \\'\\';\\n' +
' if (config.username) {\\n' +
' auth = config.password\\n' +
' ? encodeURIComponent(config.username) + \\':\\' + encodeURIComponent(config.password) + \\'@\\'\\n' +
' : encodeURIComponent(config.username) + \\'@\\';\\n' +
' }\\n' +
' const proxyUrl = config.type === \\'socks5\\'\\n' +
' ? \\'socks5://\\' + auth + config.host + \\':\\' + config.port\\n' +
' : \\'http://\\' + auth + config.host + \\':\\' + config.port;\\n' +
' await mainWindow.webContents.session.setProxy({\\n' +
' proxyRules: proxyUrl,\\n' +
' proxyBypassRules: \\'<local>;localhost;127.0.0.1\\'\\n' +
' });\\n' +
' console.log(\\'[Proxy] Applied:\\', proxyUrl);\\n' +
' } else {\\n' +
' await mainWindow.webContents.session.setProxy({ proxyRules: \\'direct://\\' });\\n' +
' console.log(\\'[Proxy] Disabled, using direct connection\\');\\n' +
' }\\n' +
'}\\n\\n' +
'ipcMain.handle(\\'set-proxy\\', async (event, config) => {\\n' +
' saveProxyConfig(config);\\n' +
' await applyProxy(config);\\n' +
' return { success: true };\\n' +
'});\\n\\n' +
'ipcMain.handle(\\'get-proxy\\', () => {\\n' +
' return loadProxyConfig();\\n' +
'});\\n\\n' +
'ipcMain.handle(\\'test-proxy\\', async (event, config) => {\\n' +
' const net = require(\\'net\\');\\n' +
' const connectToProxy = () => new Promise((resolve, reject) => {\\n' +
' const socket = new net.Socket();\\n' +
' socket.setTimeout(5000);\\n' +
' socket.on(\\'connect\\', () => resolve(socket));\\n' +
' socket.on(\\'timeout\\', () => { socket.destroy(); reject(new Error(\\'Connection timeout\\')); });\\n' +
' socket.on(\\'error\\', (err) => reject(err));\\n' +
' socket.connect(config.port, config.host);\\n' +
' });\\n' +
' try {\\n' +
' if (config.type === \\'socks5\\') {\\n' +
' const socket = await connectToProxy();\\n' +
' return await new Promise((resolve) => {\\n' +
' const greeting = config.username\\n' +
' ? Buffer.from([0x05, 0x02, 0x00, 0x02])\\n' +
' : Buffer.from([0x05, 0x01, 0x00]);\\n' +
' socket.setTimeout(5000);\\n' +
' socket.write(greeting);\\n' +
' let step = 0;\\n' +
' socket.on(\\'data\\', (data) => {\\n' +
' if (step === 0) {\\n' +
' if (data[0] !== 0x05) { socket.destroy(); resolve({ success: false, error: \\'Invalid SOCKS5 version\\' }); return; }\\n' +
' if (data[1] === 0xFF) { socket.destroy(); resolve({ success: false, error: \\'No acceptable auth method\\' }); return; }\\n' +
' if (data[1] === 0x02 && config.username && config.password) {\\n' +
' step = 1;\\n' +
' const userBuf = Buffer.from(config.username, \\'utf8\\');\\n' +
' const passBuf = Buffer.from(config.password, \\'utf8\\');\\n' +
' const authReq = Buffer.alloc(3 + userBuf.length + passBuf.length);\\n' +
' authReq[0] = 0x01; authReq[1] = userBuf.length;\\n' +
' userBuf.copy(authReq, 2);\\n' +
' authReq[2 + userBuf.length] = passBuf.length;\\n' +
' passBuf.copy(authReq, 3 + userBuf.length);\\n' +
' socket.write(authReq);\\n' +
' } else { socket.destroy(); resolve({ success: true }); }\\n' +
' } else if (step === 1) {\\n' +
' socket.destroy();\\n' +
' resolve(data[0] === 0x01 && data[1] === 0x00\\n' +
' ? { success: true }\\n' +
' : { success: false, error: \\'SOCKS5 authentication failed\\' });\\n' +
' }\\n' +
' });\\n' +
' socket.on(\\'timeout\\', () => { socket.destroy(); resolve({ success: false, error: \\'SOCKS5 handshake timeout\\' }); });\\n' +
' socket.on(\\'error\\', (err) => resolve({ success: false, error: err.message }));\\n' +
' });\\n' +
' } else {\\n' +
' const socket = await connectToProxy();\\n' +
' return await new Promise((resolve) => {\\n' +
' socket.setTimeout(5000);\\n' +
' const authHeader = config.username && config.password\\n' +
' ? \\'Proxy-Authorization: Basic \\' + Buffer.from(config.username + \\':\\' + config.password).toString(\\'base64\\') + \\'\\\\r\\\\n\\'\\n' +
' : \\'\\';\\n' +
' socket.write(\\'CONNECT httpbin.org:443 HTTP/1.1\\\\r\\\\nHost: httpbin.org:443\\\\r\\\\n\\' + authHeader + \\'\\\\r\\\\n\\');\\n' +
' let responseData = \\'\\';\\n' +
' socket.on(\\'data\\', (data) => {\\n' +
' responseData += data.toString();\\n' +
' if (responseData.includes(\\'\\\\r\\\\n\\\\r\\\\n\\')) {\\n' +
' socket.destroy();\\n' +
' if (responseData.includes(\\'200\\')) resolve({ success: true });\\n' +
' else if (responseData.includes(\\'407\\')) resolve({ success: false, error: \\'Proxy authentication required\\' });\\n' +
' else resolve({ success: false, error: \\'Proxy rejected: \\' + (responseData.split(\\'\\\\r\\\\n\\')[0] || \\'Unknown\\') });\\n' +
' }\\n' +
' });\\n' +
' socket.on(\\'timeout\\', () => { socket.destroy(); resolve({ success: false, error: \\'HTTP proxy handshake timeout\\' }); });\\n' +
' socket.on(\\'error\\', (err) => resolve({ success: false, error: err.message }));\\n' +
' });\\n' +
' }\\n' +
' } catch (e) { return { success: false, error: e.message }; }\\n' +
'});\\n\\n' +
'app.whenReady().then(() => {\\n' +
' createWindow();\\n' +
' const savedProxy = loadProxyConfig();\\n' +
' if (savedProxy.enabled && savedProxy.host && savedProxy.port) {\\n' +
' applyProxy(savedProxy);\\n' +
' }\\n' +
' globalShortcut.register(\\'CommandOrControl+Shift+I\\', () => {\\n' +
' const focused = BrowserWindow.getFocusedWindow();\\n' +
' if (focused && !focused.isDestroyed()) {\\n' +
Expand All @@ -399,7 +515,16 @@ jobs:
'});';

fs.writeFileSync('electron/main.js', mainJsContent);


const preloadJsContent = 'const { contextBridge, ipcRenderer } = require(\\'electron\\');\\n' +
'\\n' +
'contextBridge.exposeInMainWorld(\\'electronAPI\\', {\\n' +
' setProxy: (config) => ipcRenderer.invoke(\\'set-proxy\\', config),\\n' +
' getProxy: () => ipcRenderer.invoke(\\'get-proxy\\'),\\n' +
' testProxy: (config) => ipcRenderer.invoke(\\'test-proxy\\', config),\\n' +
'});\\n';
fs.writeFileSync('electron/preload.js', preloadJsContent);

const electronPackageJson = {
name: 'github-stars-manager-desktop',
version: '1.0.0',
Expand Down
Loading
Loading