From db6804e25a0be123ada127106e5bfb347ce8b58b Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Thu, 8 Aug 2024 19:30:33 +0800 Subject: [PATCH] geo data config --- scripts/prepare.mjs | 12 +- src/main/core/mihomoApi.ts | 7 + src/main/resolve/init.ts | 2 +- src/main/utils/ipc.ts | 10 +- src/main/utils/template.ts | 9 + src/renderer/src/App.tsx | 23 ++- .../src/components/sider/config-viewer.tsx | 4 +- ...core-card.tsx.tsx => mihomo-core-card.tsx} | 0 .../src/components/sider/profile-card.tsx | 2 +- .../{test-card.tsx => resource-card.tsx} | 14 +- src/renderer/src/pages/resources.tsx | 163 ++++++++++++++++++ src/renderer/src/pages/tests.tsx | 7 - src/renderer/src/routes/index.tsx | 6 +- src/renderer/src/utils/ipc.ts | 10 +- src/shared/types.d.ts | 9 + 15 files changed, 248 insertions(+), 30 deletions(-) rename src/renderer/src/components/sider/{mihomo-core-card.tsx.tsx => mihomo-core-card.tsx} (100%) rename src/renderer/src/components/sider/{test-card.tsx => resource-card.tsx} (78%) create mode 100644 src/renderer/src/pages/resources.tsx delete mode 100644 src/renderer/src/pages/tests.tsx diff --git a/scripts/prepare.mjs b/scripts/prepare.mjs index 48c1916..1eb9091 100644 --- a/scripts/prepare.mjs +++ b/scripts/prepare.mjs @@ -238,8 +238,8 @@ async function downloadFile(url, path) { const resolveMmdb = () => resolveResource({ - file: 'Country.mmdb', - downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country.mmdb` + file: 'country.mmdb', + downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country-lite.mmdb` }) const resolveGeosite = () => resolveResource({ @@ -249,7 +249,12 @@ const resolveGeosite = () => const resolveGeoIP = () => resolveResource({ file: 'geoip.dat', - downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip.dat` + downloadURL: `https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip-lite.dat` + }) +const resolveASN = () => + resolveResource({ + file: 'ASN.mmdb', + downloadURL: `https://github.com/xishang0128/geoip/releases/download/latest/GeoLite2-ASN.mmdb` }) const resolveEnableLoopback = () => resolveResource({ @@ -285,6 +290,7 @@ const tasks = [ { name: 'mmdb', func: resolveMmdb, retry: 5 }, { name: 'geosite', func: resolveGeosite, retry: 5 }, { name: 'geoip', func: resolveGeoIP, retry: 5 }, + { name: 'asn', func: resolveASN, retry: 5 }, { name: 'font', func: resolveFont, diff --git a/src/main/core/mihomoApi.ts b/src/main/core/mihomoApi.ts index 4a29538..8c1bed5 100644 --- a/src/main/core/mihomoApi.ts +++ b/src/main/core/mihomoApi.ts @@ -85,6 +85,13 @@ export const mihomoChangeProxy = async (group: string, proxy: string): Promise => { + const instance = await getAxios() + return instance.post('/configs/geo').catch((e) => { + return e.response.data + }) +} + export const mihomoProxyDelay = async (proxy: string, url?: string): Promise => { const appConfig = getAppConfig() const { delayTestUrl, delayTestTimeout } = appConfig diff --git a/src/main/resolve/init.ts b/src/main/resolve/init.ts index ee15ddb..4eff0c5 100644 --- a/src/main/resolve/init.ts +++ b/src/main/resolve/init.ts @@ -58,7 +58,7 @@ function initConfig(): void { } function initFiles(): void { - const fileList = ['Country.mmdb', 'geoip.dat', 'geosite.dat'] + const fileList = ['country.mmdb', 'geoip.dat', 'geosite.dat', 'ASN.mmdb'] for (const file of fileList) { const targetPath = path.join(mihomoWorkDir(), file) const testTargrtPath = path.join(mihomoTestDir(), file) diff --git a/src/main/utils/ipc.ts b/src/main/utils/ipc.ts index 984c556..b7fc7db 100644 --- a/src/main/utils/ipc.ts +++ b/src/main/utils/ipc.ts @@ -6,6 +6,7 @@ import { mihomoProxies, mihomoProxyDelay, mihomoRules, + mihomoUpgradeGeo, mihomoVersion, patchMihomoConfig, startMihomoConnections, @@ -34,6 +35,7 @@ import { triggerSysProxy } from '../resolve/sysproxy' import { checkUpdate } from '../resolve/autoUpdater' import { exePath, mihomoCorePath, mihomoWorkConfigPath } from './dirs' import { execSync } from 'child_process' +import yaml from 'yaml' import fs from 'fs' export function registerIpcMainHandlers(): void { @@ -43,6 +45,7 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('mihomoRules', mihomoRules) ipcMain.handle('mihomoProxies', mihomoProxies) ipcMain.handle('mihomoChangeProxy', (_e, group, proxy) => mihomoChangeProxy(group, proxy)) + ipcMain.handle('mihomoUpgradeGeo', mihomoUpgradeGeo) ipcMain.handle('mihomoProxyDelay', (_e, proxy, url) => mihomoProxyDelay(proxy, url)) ipcMain.handle('startMihomoLogs', startMihomoLogs) ipcMain.handle('stopMihomoLogs', stopMihomoLogs) @@ -71,6 +74,7 @@ export function registerIpcMainHandlers(): void { ipcMain.handle('encryptString', (_e, str) => safeStorage.encryptString(str)) ipcMain.handle('getFilePath', getFilePath) ipcMain.handle('readTextFile', (_e, filePath) => readTextFile(filePath)) + ipcMain.handle('getRuntimeConfigStr', getRuntimeConfigStr) ipcMain.handle('getRuntimeConfig', getRuntimeConfig) ipcMain.handle('checkUpdate', () => checkUpdate()) ipcMain.handle('getVersion', () => app.getVersion()) @@ -91,10 +95,14 @@ function readTextFile(filePath: string): string { return fs.readFileSync(filePath, 'utf8') } -function getRuntimeConfig(): string { +function getRuntimeConfigStr(): string { return fs.readFileSync(mihomoWorkConfigPath(), 'utf8') } +function getRuntimeConfig(): Record { + return yaml.parse(getRuntimeConfigStr()) +} + async function setupFirewall(): Promise { return new Promise((resolve, reject) => { const removeCommand = ` diff --git a/src/main/utils/template.ts b/src/main/utils/template.ts index 7684a4b..ad23664 100644 --- a/src/main/utils/template.ts +++ b/src/main/utils/template.ts @@ -58,6 +58,15 @@ export const defaultControledMihomoConfig: Partial = { } }, 'skip-domain': ['+.push.apple.com'] + }, + 'geo-auto-update': false, + 'geo-update-interval': 24, + 'geodata-mode': false, + 'geox-url': { + geoip: 'https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip-lite.dat', + geosite: 'https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat', + mmdb: 'https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country-lite.mmdb', + asn: 'https://github.com/xishang0128/geoip/releases/download/latest/GeoLite2-ASN.mmdb' } } diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 8334a63..8c3b50e 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -15,9 +15,9 @@ import SniffCard from '@renderer/components/sider/sniff-card' import OverrideCard from '@renderer/components/sider/override-card' import ConnCard from '@renderer/components/sider/conn-card' import LogCard from '@renderer/components/sider/log-card' -import MihomoCoreCard from './components/sider/mihomo-core-card.tsx' -import TestCard from './components/sider/test-card.js' -import UpdaterButton from './components/updater/updater-button.js' +import MihomoCoreCard from '@renderer/components/sider/mihomo-core-card' +import ResourceCard from '@renderer/components/sider/resource-card' +import UpdaterButton from '@renderer/components/updater/updater-button' const App: React.FC = () => { const { setTheme } = useTheme() @@ -63,9 +63,24 @@ const App: React.FC = () => { /> +
+ {/*
+ + + + + + + + + + + + +
*/}
@@ -86,7 +101,7 @@ const App: React.FC = () => {
- +
diff --git a/src/renderer/src/components/sider/config-viewer.tsx b/src/renderer/src/components/sider/config-viewer.tsx index e22ad0b..a55f79c 100644 --- a/src/renderer/src/components/sider/config-viewer.tsx +++ b/src/renderer/src/components/sider/config-viewer.tsx @@ -2,7 +2,7 @@ import { Modal, ModalContent, ModalHeader, ModalBody, ModalFooter, Button } from import React, { useEffect, useState } from 'react' import MonacoEditor, { monaco } from 'react-monaco-editor' import { useTheme } from 'next-themes' -import { getRuntimeConfig } from '@renderer/utils/ipc' +import { getRuntimeConfigStr } from '@renderer/utils/ipc' interface Props { onClose: () => void } @@ -23,7 +23,7 @@ const ConfigViewer: React.FC = (props) => { } const getContent = async (): Promise => { - setCurrData(await getRuntimeConfig()) + setCurrData(await getRuntimeConfigStr()) } useEffect(() => { diff --git a/src/renderer/src/components/sider/mihomo-core-card.tsx.tsx b/src/renderer/src/components/sider/mihomo-core-card.tsx similarity index 100% rename from src/renderer/src/components/sider/mihomo-core-card.tsx.tsx rename to src/renderer/src/components/sider/mihomo-core-card.tsx diff --git a/src/renderer/src/components/sider/profile-card.tsx b/src/renderer/src/components/sider/profile-card.tsx index 3ff174a..0bf69e2 100644 --- a/src/renderer/src/components/sider/profile-card.tsx +++ b/src/renderer/src/components/sider/profile-card.tsx @@ -46,7 +46,7 @@ const ProfileCard: React.FC = () => { > {info?.name} -
+
+ )} + +
+ + +
+ {geositeInput !== geoxUrl.geosite && ( + + )} + +
+
+ +
+ {mmdbInput !== geoxUrl.mmdb && ( + + )} + +
+
+ +
+ {asnInput !== geoxUrl.asn && ( + + )} + +
+
+ + { + patchControledMihomoConfig({ 'geodata-mode': key === 'dat' }) + }} + > + + + + + { + setUpdating(true) + mihomoUpgradeGeo() + .catch((e) => { + new Notification('更新失败', { body: e.message }) + }) + .finally(() => { + new Notification('Geo 数据库更新成功') + setUpdating(false) + }) + }} + > + + + } + divider={geoAutoUpdate} + > + { + patchControledMihomoConfig({ 'geo-auto-update': v }) + }} + /> + + {geoAutoUpdate && ( + + { + patchControledMihomoConfig({ 'geo-update-interval': parseInt(v) }) + }} + /> + + )} + + + ) +} + +export default Resources diff --git a/src/renderer/src/pages/tests.tsx b/src/renderer/src/pages/tests.tsx deleted file mode 100644 index 4041f54..0000000 --- a/src/renderer/src/pages/tests.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import BasePage from '@renderer/components/base/base-page' - -const Tests: React.FC = () => { - return -} - -export default Tests diff --git a/src/renderer/src/routes/index.tsx b/src/renderer/src/routes/index.tsx index abb3420..ddaa27b 100644 --- a/src/renderer/src/routes/index.tsx +++ b/src/renderer/src/routes/index.tsx @@ -9,7 +9,7 @@ import Connections from '@renderer/pages/connections' import Mihomo from '@renderer/pages/mihomo' import Sysproxy from '@renderer/pages/syspeoxy' import Tun from '@renderer/pages/tun' -import Tests from '@renderer/pages/tests' +import Resources from '@renderer/pages/resources' import DNS from '@renderer/pages/dns' import Sniffer from '@renderer/pages/sniffer' @@ -35,8 +35,8 @@ const routes = [ element: }, { - path: '/tests', - element: + path: '/resources', + element: }, { path: '/dns', diff --git a/src/renderer/src/utils/ipc.ts b/src/renderer/src/utils/ipc.ts index 138e5bc..442ca07 100644 --- a/src/renderer/src/utils/ipc.ts +++ b/src/renderer/src/utils/ipc.ts @@ -22,6 +22,10 @@ export async function mihomoChangeProxy(group: string, proxy: string): Promise { + return await window.electron.ipcRenderer.invoke('mihomoUpgradeGeo') +} + export async function mihomoProxyDelay(proxy: string, url?: string): Promise { const res = await window.electron.ipcRenderer.invoke('mihomoProxyDelay', proxy, url) return res @@ -135,7 +139,11 @@ export async function readTextFile(filePath: string): Promise { return await window.electron.ipcRenderer.invoke('readTextFile', filePath) } -export async function getRuntimeConfig(): Promise { +export async function getRuntimeConfigStr(): Promise { + return await window.electron.ipcRenderer.invoke('getRuntimeConfigStr') +} + +export async function getRuntimeConfig(): Promise { return await window.electron.ipcRenderer.invoke('getRuntimeConfig') } diff --git a/src/shared/types.d.ts b/src/shared/types.d.ts index 6fc6f3d..0c9c290 100644 --- a/src/shared/types.d.ts +++ b/src/shared/types.d.ts @@ -246,6 +246,15 @@ interface IMihomoConfig { 'proxy-groups'?: [] rules?: [] hosts?: { [key: string]: string | string[] } + 'geodata-mode'?: boolean + 'geo-auto-update'?: boolean + 'geo-update-interval'?: number + 'geox-url'?: { + geoip?: string + geosite?: string + mmdb?: string + asn?: string + } tun: IMihomoTunConfig dns: IMihomoDNSConfig sniffer: IMihomoSnifferConfig