|
| 1 | +import { overrideConfigPath, overridePath } from '../utils/dirs' |
| 2 | +import yaml from 'yaml' |
| 3 | +import fs from 'fs' |
| 4 | +import { dialog } from 'electron' |
| 5 | +import axios from 'axios' |
| 6 | +import { getControledMihomoConfig } from './controledMihomo' |
| 7 | + |
| 8 | +let overrideConfig: IOverrideConfig // override.yaml |
| 9 | + |
| 10 | +export function getOverrideConfig(force = false): IOverrideConfig { |
| 11 | + if (force || !overrideConfig) { |
| 12 | + overrideConfig = yaml.parse(fs.readFileSync(overrideConfigPath(), 'utf-8')) |
| 13 | + } |
| 14 | + return overrideConfig |
| 15 | +} |
| 16 | + |
| 17 | +export function setOverrideConfig(config: IOverrideConfig): void { |
| 18 | + overrideConfig = config |
| 19 | + fs.writeFileSync(overrideConfigPath(), yaml.stringify(overrideConfig)) |
| 20 | +} |
| 21 | + |
| 22 | +export function getOverrideItem(id: string): IOverrideItem | undefined { |
| 23 | + return overrideConfig.items.find((item) => item.id === id) |
| 24 | +} |
| 25 | +export function updateOverrideItem(item: IOverrideItem): void { |
| 26 | + const index = overrideConfig.items.findIndex((i) => i.id === item.id) |
| 27 | + overrideConfig.items[index] = item |
| 28 | + fs.writeFileSync(overrideConfigPath(), yaml.stringify(overrideConfig)) |
| 29 | +} |
| 30 | + |
| 31 | +export async function addOverrideItem(item: Partial<IOverrideItem>): Promise<void> { |
| 32 | + const newItem = await createOverride(item) |
| 33 | + if (overrideConfig.items.find((i) => i.id === newItem.id)) { |
| 34 | + updateOverrideItem(newItem) |
| 35 | + } else { |
| 36 | + overrideConfig.items.push(newItem) |
| 37 | + } |
| 38 | + fs.writeFileSync(overrideConfigPath(), yaml.stringify(overrideConfig)) |
| 39 | +} |
| 40 | + |
| 41 | +export function removeOverrideItem(id: string): void { |
| 42 | + overrideConfig.items = overrideConfig.items?.filter((item) => item.id !== id) |
| 43 | + fs.writeFileSync(overrideConfigPath(), yaml.stringify(overrideConfig)) |
| 44 | + fs.rmSync(overridePath(id)) |
| 45 | +} |
| 46 | + |
| 47 | +export async function createOverride(item: Partial<IOverrideItem>): Promise<IOverrideItem> { |
| 48 | + const id = item.id || new Date().getTime().toString(16) |
| 49 | + const newItem = { |
| 50 | + id, |
| 51 | + name: item.name || (item.type === 'remote' ? 'Remote File' : 'Local File'), |
| 52 | + type: item.type, |
| 53 | + url: item.url, |
| 54 | + updated: new Date().getTime() |
| 55 | + } as IOverrideItem |
| 56 | + switch (newItem.type) { |
| 57 | + case 'remote': { |
| 58 | + if (!item.url) { |
| 59 | + dialog.showErrorBox( |
| 60 | + 'URL is required for remote script', |
| 61 | + 'URL is required for remote script' |
| 62 | + ) |
| 63 | + throw new Error('URL is required for remote script') |
| 64 | + } |
| 65 | + try { |
| 66 | + const res = await axios.get(item.url, { |
| 67 | + proxy: { |
| 68 | + protocol: 'http', |
| 69 | + host: '127.0.0.1', |
| 70 | + port: getControledMihomoConfig()['mixed-port'] || 7890 |
| 71 | + }, |
| 72 | + responseType: 'text' |
| 73 | + }) |
| 74 | + const data = res.data |
| 75 | + setOverride(id, data) |
| 76 | + } catch (e) { |
| 77 | + dialog.showErrorBox('Failed to fetch remote script', `${e}\nurl: ${item.url}`) |
| 78 | + throw new Error(`Failed to fetch remote script ${e}`) |
| 79 | + } |
| 80 | + break |
| 81 | + } |
| 82 | + case 'local': { |
| 83 | + if (!item.file) { |
| 84 | + dialog.showErrorBox( |
| 85 | + 'File is required for local script', |
| 86 | + 'File is required for local script' |
| 87 | + ) |
| 88 | + throw new Error('File is required for local script') |
| 89 | + } |
| 90 | + const data = item.file |
| 91 | + setOverride(id, data) |
| 92 | + break |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return newItem |
| 97 | +} |
| 98 | + |
| 99 | +export function getOverride(id: string): string { |
| 100 | + if (!fs.existsSync(overridePath(id))) { |
| 101 | + return `function main(config){ return config }` |
| 102 | + } |
| 103 | + return fs.readFileSync(overridePath(id), 'utf-8') |
| 104 | +} |
| 105 | + |
| 106 | +export function setOverride(id: string, content: string): void { |
| 107 | + fs.writeFileSync(overridePath(id), content, 'utf-8') |
| 108 | +} |
0 commit comments