Skip to content

Commit

Permalink
1. Update DNS Module
Browse files Browse the repository at this point in the history
  • Loading branch information
xpf0000 committed Sep 27, 2024
1 parent 142a075 commit a9dccd2
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 77 deletions.
37 changes: 31 additions & 6 deletions configs/esbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -25,7 +48,7 @@ const dev: BuildOptions = {
outfile: 'dist/electron/main.js',
minify: false,
bundle: true,
external: external,
external,
plugins: [BuildPlugin()]
}

Expand All @@ -35,7 +58,7 @@ const dist: BuildOptions = {
outfile: 'dist/electron/main.js',
minify: true,
bundle: true,
external: external,
external,
plugins: [BuildPlugin()],
drop: ['debugger', 'console']
}
Expand All @@ -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'],
Expand All @@ -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()]
}

Expand All @@ -77,7 +102,7 @@ const distDNSFork: BuildOptions = {
outfile: 'dist/electron/dns.js',
minify: true,
bundle: true,
external,
external: dnsExternal,
plugins: [BuildPlugin()]
}

Expand Down
2 changes: 2 additions & 0 deletions scripts/app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion scripts/dev-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
25 changes: 25 additions & 0 deletions scripts/fix.ts
Original file line number Diff line number Diff line change
@@ -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()
}
30 changes: 3 additions & 27 deletions src/fork/Fn.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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?.({
Expand Down Expand Up @@ -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<any>,
Expand Down
12 changes: 5 additions & 7 deletions src/fork/module/DNS.ts
Original file line number Diff line number Diff line change
@@ -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<string, string> = {}

constructor() {
super()
this.server = undefined
this.lastTime = 0
this.hosts = {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -124,7 +122,7 @@ class Manager extends Base {
port: 53,
address: LOCAL_IP
}
})
} as any)
.then()
.catch((e: any) => {
reject(e)
Expand Down
19 changes: 16 additions & 3 deletions src/main/core/DnsServerManager.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand Down
29 changes: 17 additions & 12 deletions src/render/components/DNS/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@
<template #header>
<div class="table-header">
<div class="left">
<template v-if="running">
<div class="status running" :class="{ disabled: fetching }">
<yb-icon :svg="import('@/svg/stop2.svg?raw')" @click.stop="dnsStore.dnsStop" />
</div>
<div class="status refresh" :class="{ disabled: fetching }">
<yb-icon
:svg="import('@/svg/icon_refresh.svg?raw')"
@click.stop="dnsStore.dnsStart"
/>
<template v-if="fetching">
<el-button :loading="true" link></el-button>
</template>
<template v-else>
<template v-if="running">
<div class="status running" :class="{ disabled: fetching }">
<yb-icon :svg="import('@/svg/stop2.svg?raw')" @click.stop="dnsStore.dnsStop" />
</div>
<div class="status refresh" :class="{ disabled: fetching }">
<yb-icon
:svg="import('@/svg/icon_refresh.svg?raw')"
@click.stop="dnsStore.dnsRestart"
/>
</div>
</template>
<div v-else class="status" :class="{ disabled: fetching }">
<yb-icon :svg="import('@/svg/play.svg?raw')" @click.stop="dnsStore.dnsStart" />
</div>
</template>
<div v-else class="status" :class="{ disabled: fetching }">
<yb-icon :svg="import('@/svg/play.svg?raw')" @click.stop="dnsStore.dnsStart" />
</div>
</div>
<el-button @click.stop="cleanLog">{{ $t('base.clean') }}</el-button>
</div>
Expand Down
10 changes: 1 addition & 9 deletions src/render/components/DNS/aside.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,9 @@
const switchChange = () => {
passwordCheck().then(() => {
let fn = null
let promise: Promise<any> | null = null
if (serviceFetching?.value) return
fn = serviceRunning?.value ? dnsStore.dnsStop : dnsStore.dnsStart
promise = fn()
promise?.then((res) => {
if (typeof res === 'string') {
MessageError(res)
} else {
MessageSuccess(I18nT('base.success'))
}
})
fn().then()
})
}
Expand Down
41 changes: 33 additions & 8 deletions src/render/components/DNS/dns.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { defineStore } from 'pinia'
import IPC from '@/util/IPC'
import { reactive } from 'vue'
import { MessageError, MessageSuccess } from '@/util/Element'
import { I18nT } from '@shared/lang'
const IP = require('ip')

export interface DNSLogItem {
Expand Down Expand Up @@ -40,22 +42,31 @@ export const DnsStore = defineStore('dns', {
IPC.off('App_DNS_Log')
},
dnsStop(): Promise<boolean> {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
if (!this.running) {
resolve(true)
return
}
this.fetching = true
IPC.send('DNS:stop').then((key: string, res: boolean) => {
IPC.send('DNS:stop').then((key: string, res: boolean | string) => {
IPC.off(key)
this.fetching = false
this.running = false
resolve(res)
if (typeof res === 'string') {
MessageError(res)
} else if (res) {
this.running = false
MessageSuccess(I18nT('base.success'))
resolve(true)
return
} else if (!res) {
MessageError(I18nT('base.fail'))
}
reject(new Error('fail'))
})
})
},
dnsStart(): Promise<boolean | string> {
return new Promise((resolve) => {
dnsStart(): Promise<boolean> {
return new Promise((resolve, reject) => {
if (this.running) {
resolve(true)
return
Expand All @@ -64,10 +75,24 @@ export const DnsStore = defineStore('dns', {
IPC.send('DNS:start').then((key: string, res: boolean | string) => {
IPC.off(key)
this.fetching = false
this.running = res === true
resolve(res)
if (typeof res === 'string') {
MessageError(res)
} else if (res) {
this.running = true
MessageSuccess(I18nT('base.success'))
resolve(true)
return
} else if (!res) {
MessageError(I18nT('base.fail'))
}
reject(new Error('fail'))
})
})
},
dnsRestart() {
this.dnsStop()
.then(() => this.dnsStart())
.catch()
}
}
})
Loading

0 comments on commit a9dccd2

Please sign in to comment.