From a9dccd2967bff332be659665562d4824fcbfa087 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=B9=8F=E9=A3=9E?= <250881478@qq.com> Date: Fri, 27 Sep 2024 17:32:02 +0800 Subject: [PATCH] 1. Update DNS Module --- configs/esbuild.config.ts | 37 +++++++++++++++++++++----- scripts/app-builder.ts | 2 ++ scripts/dev-runner.ts | 4 ++- scripts/fix.ts | 25 ++++++++++++++++++ src/fork/Fn.ts | 30 +++------------------ src/fork/module/DNS.ts | 12 ++++----- src/main/core/DnsServerManager.ts | 19 ++++++++++--- src/render/components/DNS/Index.vue | 29 +++++++++++--------- src/render/components/DNS/aside.vue | 10 +------ src/render/components/DNS/dns.ts | 41 +++++++++++++++++++++++------ src/shared/Exec.ts | 27 +++++++++++++++++++ src/shared/utils.ts | 8 +++--- static/sh/node.sh | 7 +++++ 13 files changed, 174 insertions(+), 77 deletions(-) create mode 100644 scripts/fix.ts diff --git a/configs/esbuild.config.ts b/configs/esbuild.config.ts index a3480bec..a32517f7 100644 --- a/configs/esbuild.config.ts +++ b/configs/esbuild.config.ts @@ -16,7 +16,30 @@ const external = [ 'fs-extra', 'dns2', 'ip', - 'tangerine' + 'tangerine', + 'lodash', + 'axios', + 'iconv-lite', + 'compressing', + 'fast-xml-parser', + 'source-map', + 'source-map-js', + 'entities', + '@vue', + 'vue', + 'vue-i18n', + 'estree-walker', + 'serve-handler', + 'electron-updater', + 'js-yaml', + '@lzwme/get-physical-address', + '@electron/remote', + 'atomically', + 'electron-log', + 'jszip', + 'pako', + 'electron-devtools-installer', + 'conf' ] const dev: BuildOptions = { @@ -25,7 +48,7 @@ const dev: BuildOptions = { outfile: 'dist/electron/main.js', minify: false, bundle: true, - external: external, + external, plugins: [BuildPlugin()] } @@ -35,7 +58,7 @@ const dist: BuildOptions = { outfile: 'dist/electron/main.js', minify: true, bundle: true, - external: external, + external, plugins: [BuildPlugin()], drop: ['debugger', 'console'] } @@ -50,6 +73,8 @@ const devFork: BuildOptions = { plugins: [BuildPlugin()] } +const dnsExternal = ['path', 'fs', 'os', 'child_process'] + const distFork: BuildOptions = { platform: 'node', entryPoints: ['src/fork/index.ts'], @@ -65,9 +90,9 @@ const devDNSFork: BuildOptions = { platform: 'node', entryPoints: ['src/fork/dns.ts'], outfile: 'dist/electron/dns.js', - minify: true, + minify: false, bundle: true, - external, + external: dnsExternal, plugins: [BuildPlugin()] } @@ -77,7 +102,7 @@ const distDNSFork: BuildOptions = { outfile: 'dist/electron/dns.js', minify: true, bundle: true, - external, + external: dnsExternal, plugins: [BuildPlugin()] } diff --git a/scripts/app-builder.ts b/scripts/app-builder.ts index 9a23142b..aaf5c041 100644 --- a/scripts/app-builder.ts +++ b/scripts/app-builder.ts @@ -5,9 +5,11 @@ import { build as electronBuild, Platform, CliOptions } from 'electron-builder' import esbuildConfig from '../configs/esbuild.config' import viteConfig from '../configs/vite.config' import electronBuilderConfig from '../configs/electron-builder' +import { DoFix } from './fix' async function packMain() { try { + await DoFix() await esbuild(esbuildConfig.dist) await esbuild(esbuildConfig.distFork) await esbuild(esbuildConfig.distDNSFork) diff --git a/scripts/dev-runner.ts b/scripts/dev-runner.ts index cf6d9635..7a6f1512 100644 --- a/scripts/dev-runner.ts +++ b/scripts/dev-runner.ts @@ -8,6 +8,7 @@ import _md5 from 'md5' import viteConfig from '../configs/vite.config' import esbuildConfig from '../configs/esbuild.config' +import { DoFix } from './fix' let restart = false let electronProcess: ChildProcess | null @@ -22,7 +23,8 @@ async function launchViteDevServer(openInBrowser = false) { } function buildMainProcess() { - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { + await DoFix() Promise.all([ build(esbuildConfig.dev), build(esbuildConfig.devFork), diff --git a/scripts/fix.ts b/scripts/fix.ts new file mode 100644 index 00000000..2593e1ed --- /dev/null +++ b/scripts/fix.ts @@ -0,0 +1,25 @@ +import { resolve } from 'path' +import { existsSync, readFile, writeFile } from 'fs-extra' + +const dnsFix = async () => { + const file = resolve(__dirname, '../node_modules/dohdec/lib/doh.js') + if (!existsSync(file)) { + return + } + const search = `const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8'))` + let content = await readFile(file, 'utf-8') + if (!content.includes(search)) { + return + } + const jsonFile = resolve(__dirname, '../node_modules/dohdec/package.json') + const jsonObj = JSON.parse(await readFile(jsonFile, 'utf-8')) + content = content.replace( + search, + `const pkg = { name: '${jsonObj.name}', version: '${jsonObj.version}' }` + ) + await writeFile(file, content) +} + +export const DoFix = async () => { + await dnsFix() +} diff --git a/src/fork/Fn.ts b/src/fork/Fn.ts index bae62972..b7cc45ae 100644 --- a/src/fork/Fn.ts +++ b/src/fork/Fn.ts @@ -1,5 +1,4 @@ import { spawn, type ChildProcess } from 'child_process' -import { exec } from 'child-process-promise' import { merge } from 'lodash' import { statSync, readdirSync, mkdirSync, existsSync, createWriteStream, realpathSync } from 'fs' import path, { join, dirname } from 'path' @@ -10,6 +9,9 @@ import { readdir } from 'fs-extra' import type { AppHost, SoftInstalled } from '@shared/app' import { fixEnv } from '@shared/utils' import { compareVersions } from 'compare-versions' +import { execPromise } from '@shared/Exec' + +export { execPromise } export const ProcessSendSuccess = (key: string, data: any, on?: boolean) => { process?.send?.({ @@ -61,32 +63,6 @@ export function waitTime(time: number) { }) } -export function execPromise( - cammand: string, - opt?: { [k: string]: any } -): ForkPromise<{ - stdout: string - stderr: string -}> { - return new ForkPromise(async (resolve, reject) => { - try { - const env = await fixEnv() - const res = await exec( - cammand, - merge( - { - env - }, - opt - ) - ) - resolve(res) - } catch (e) { - reject(e) - } - }) -} - export function spawnPromise( cammand: string, params: Array, diff --git a/src/fork/module/DNS.ts b/src/fork/module/DNS.ts index 2d41e9a5..2110bac0 100644 --- a/src/fork/module/DNS.ts +++ b/src/fork/module/DNS.ts @@ -1,21 +1,19 @@ import { readFileSync } from 'fs' import { createServer, Packet } from 'dns2' import { address, isV6Format } from 'ip' -import * as DNS from 'dns2' -import { Base } from './Base' +import * as DNS2 from 'dns2' import { ForkPromise } from '@shared/ForkPromise' const Tangerine = require('tangerine') const tangerine = new Tangerine() -class Manager extends Base { - server?: DNS.DnsServer +class Manager { + server?: any lastTime = 0 hosts: Record = {} constructor() { - super() this.server = undefined this.lastTime = 0 this.hosts = {} @@ -48,7 +46,7 @@ class Manager extends Base { const IS_IPV6 = isV6Format(LOCAL_IP) const server = createServer({ udp: true, - handle: (request: DNS.DnsRequest, send: (response: DNS.DnsResponse) => void) => { + handle: (request: DNS2.DnsRequest, send: (response: DNS2.DnsResponse) => void) => { const response = Packet.createResponseFromRequest(request) const [question] = request.questions const { name } = question @@ -124,7 +122,7 @@ class Manager extends Base { port: 53, address: LOCAL_IP } - }) + } as any) .then() .catch((e: any) => { reject(e) diff --git a/src/main/core/DnsServerManager.ts b/src/main/core/DnsServerManager.ts index c19cd716..9bca3af8 100644 --- a/src/main/core/DnsServerManager.ts +++ b/src/main/core/DnsServerManager.ts @@ -1,5 +1,6 @@ -import { resolve } from 'path' -import { execPromiseRoot } from '@shared/Exec' +import { resolve, join } from 'path' +import { remove, copyFile } from 'fs-extra' +import { execPromise, execPromiseRoot } from '@shared/Exec' class DnsServer { _callbak?: Function @@ -12,7 +13,19 @@ class DnsServer { let timer: NodeJS.Timeout | undefined const file = resolve(__dirname, './dns.js') return new Promise(async (resolve, reject) => { - execPromiseRoot(`node ${file} App-DNS-Flag=${global.Server.BaseDir}`, { + try { + await execPromise('node -v') + } catch (e) { + reject(new Error('DNS Server Start Fail: Need NodeJS, Not Found NodeJS In System Env')) + return + } + const cacheFile = join(global.Server.Cache!, 'dns.js') + if (cacheFile) { + await remove(cacheFile) + } + await copyFile(file, cacheFile) + await execPromiseRoot(`chmod 777 ${cacheFile}`) + execPromiseRoot(`node ${cacheFile} App-DNS-Flag=${global.Server.BaseDir}`, { env: { PWSServer: global.Server } diff --git a/src/render/components/DNS/Index.vue b/src/render/components/DNS/Index.vue index 8e3391f4..f4da4c82 100644 --- a/src/render/components/DNS/Index.vue +++ b/src/render/components/DNS/Index.vue @@ -19,20 +19,25 @@