From 324351f78d16f34acf46cc2ad2b6a297c6e9cf6d Mon Sep 17 00:00:00 2001 From: Mikhail Levin Date: Thu, 1 Oct 2026 03:54:50 +0300 Subject: [PATCH] Add RoutingA rule import and scheduled updates from URL --- gui/src/api/index.ts | 22 +- gui/src/dialogs/settings/RoutingA.spec.ts | 79 ++++++- gui/src/dialogs/settings/RoutingA.vue | 213 +++++++++++++++++- .../settings/routingA/RoutingEditor.vue | 8 + gui/src/locales/en.js | 14 ++ gui/src/locales/fa-ir.js | 14 ++ gui/src/locales/ko.js | 14 ++ gui/src/locales/pt-br.js | 14 ++ gui/src/locales/ru.js | 14 ++ gui/src/locales/zh.js | 13 ++ service/db/configure/configure.go | 4 + service/db/configure/routingA_source.go | 21 ++ service/pre_update.go | 21 ++ service/server/controller/routingA.go | 54 +++-- service/server/router/index.go | 1 + service/server/service/routingA.go | 179 +++++++++++++++ service/server/service/routingA_test.go | 83 +++++++ 17 files changed, 732 insertions(+), 36 deletions(-) create mode 100644 service/db/configure/routingA_source.go create mode 100644 service/server/service/routingA.go create mode 100644 service/server/service/routingA_test.go diff --git a/gui/src/api/index.ts b/gui/src/api/index.ts index 2124c740..bb18d118 100644 --- a/gui/src/api/index.ts +++ b/gui/src/api/index.ts @@ -140,9 +140,25 @@ export const postCustomInbound = (body: CustomInbound) => export const deleteCustomInbound = (body: { tag: string }) => call({ url: "customInbound", method: "delete", data: body }); export const getRoutingA = () => - call<{ routingA: string }>({ url: "routingA", method: "get" }); -export const putRoutingA = (body: { routingA: string }) => - call({ url: "routingA", method: "put", data: body }); + call<{ routingA: string; source: RoutingASource }>({ + url: "routingA", + method: "get", + }); +export interface RoutingASource { + url: string; + directUpdate: boolean; + intervalHours: number; +} +export const importRoutingAFromURL = (source: RoutingASource) => + call<{ routingA: string }>({ + url: "routingA/import", + method: "post", + data: source, + }); +export const putRoutingA = (body: { + routingA: string; + source?: RoutingASource; +}) => call({ url: "routingA", method: "put", data: body }); export const getDnsRules = () => call({ url: "dnsRules", method: "get" }); export const putDnsRules = (body: DnsRule[]) => diff --git a/gui/src/dialogs/settings/RoutingA.spec.ts b/gui/src/dialogs/settings/RoutingA.spec.ts index 73cdf195..9baffae0 100644 --- a/gui/src/dialogs/settings/RoutingA.spec.ts +++ b/gui/src/dialogs/settings/RoutingA.spec.ts @@ -19,7 +19,11 @@ import RuleDialog from "./routingA/RuleDialog.vue"; import { template } from "./routingA/template"; import { russia } from "./routingA/russia"; -const api = vi.hoisted(() => ({ getRoutingA: vi.fn(), putRoutingA: vi.fn() })); +const api = vi.hoisted(() => ({ + getRoutingA: vi.fn(), + putRoutingA: vi.fn(), + importRoutingAFromURL: vi.fn(), +})); vi.mock("@/api", () => api); const rules = "default: direct\n# inbound(http, 8080)\n"; const button = (wrapper: Pick, label: string) => @@ -40,6 +44,7 @@ beforeEach(() => { localStorage.clear(); api.getRoutingA.mockReset().mockResolvedValue({ routingA: rules }); api.putRoutingA.mockReset().mockResolvedValue(null); + api.importRoutingAFromURL.mockReset(); }); afterEach(() => { closeAllDialogs(); @@ -49,6 +54,78 @@ afterEach(() => { }); describe("RoutingA dialog", () => { + test("imports URL rules, previews them read-only, and saves the update schedule", async () => { + const imported = "default: proxy\n"; + api.importRoutingAFromURL.mockResolvedValue({ routingA: imported }); + const wrapper = mountWithApp(RoutingA); + await flushPromises(); + await button(wrapper, en.routingA.urlImport.title).trigger("click"); + await wrapper + .get('input[type="url"]') + .setValue("https://example.com/rules.txt"); + await button(wrapper, en.routingA.urlImport.import).trigger("click"); + await flushPromises(); + expect(api.importRoutingAFromURL).toHaveBeenCalledWith({ + url: "https://example.com/rules.txt", + directUpdate: false, + intervalHours: 24, + }); + const editor = wrapper.get("textarea"); + expect(editor.element.value).toBe(imported); + expect(editor.attributes("readonly")).toBeDefined(); + expect(wrapper.find(".routing-editor--source").exists()).toBe(true); + await button(wrapper, en.operations.save).trigger("click"); + await flushPromises(); + expect(api.putRoutingA).toHaveBeenCalledWith({ + routingA: imported, + source: { + url: "https://example.com/rules.txt", + directUpdate: false, + intervalHours: 24, + }, + }); + }); + + test("keeps the prior rules and shows a fetch error when URL import fails", async () => { + api.importRoutingAFromURL.mockRejectedValue( + new Error("invalid RoutingA rules"), + ); + const wrapper = mountWithApp(RoutingA); + await flushPromises(); + await button(wrapper, en.routingA.urlImport.title).trigger("click"); + await wrapper.get('input[type="url"]').setValue("https://example.com/page"); + await button(wrapper, en.routingA.urlImport.import).trigger("click"); + await flushPromises(); + expect(wrapper.get("textarea").element.value).toBe(rules); + expect(wrapper.get('[data-testid="routing-error"]').text()).toContain( + "invalid RoutingA rules", + ); + expect(api.putRoutingA).not.toHaveBeenCalled(); + }); + + test("shows import progress until validation finishes", async () => { + let finish!: (result: { routingA: string }) => void; + api.importRoutingAFromURL.mockReturnValue( + new Promise((resolve) => { + finish = resolve; + }), + ); + const wrapper = mountWithApp(RoutingA); + await flushPromises(); + await button(wrapper, en.routingA.urlImport.title).trigger("click"); + await wrapper.get('input[type="url"]').setValue("https://example.com/rules.txt"); + await button(wrapper, en.routingA.urlImport.import).trigger("click"); + expect(button(wrapper, en.routingA.urlImport.import).classes()).toContain( + "v-btn--loading", + ); + expect(button(wrapper, en.operations.save).attributes("disabled")).toBeDefined(); + finish({ routingA: "default: proxy\n" }); + await flushPromises(); + expect(button(wrapper, en.routingA.urlImport.import).classes()).not.toContain( + "v-btn--loading", + ); + }); + test("offers the Russia rule set as a confirmed replacement", async () => { const wrapper = mountWithApp(RoutingA); await flushPromises(); diff --git a/gui/src/dialogs/settings/RoutingA.vue b/gui/src/dialogs/settings/RoutingA.vue index 7451b28e..fc9e7106 100644 --- a/gui/src/dialogs/settings/RoutingA.vue +++ b/gui/src/dialogs/settings/RoutingA.vue @@ -8,8 +8,15 @@ import { mdiFormSelect, mdiMenuDown, mdiOpenInNew, + mdiLinkVariant, + mdiHelpCircleOutline, } from "@mdi/js"; -import { getRoutingA, putRoutingA } from "@/api"; +import { + getRoutingA, + importRoutingAFromURL, + putRoutingA, + type RoutingASource, +} from "@/api"; import { errorText } from "@/api/errors"; import { useConfirm, useNotify, useUnsavedGuard } from "@/composables"; import RoutingEditor from "./routingA/RoutingEditor.vue"; @@ -36,7 +43,27 @@ const confirm = useConfirm(); const notify = useNotify(); const routingA = ref(""); const original = ref(""); -useUnsavedGuard(() => routingA.value !== original.value); +const url = ref(""); +const directUpdate = ref(false); +const intervalHours = ref(24); +const validatedURL = ref(""); +const originalSource = ref({ + url: "", + directUpdate: false, + intervalHours: 24, +}); +const importing = ref(false); +const instructions = ref(false); +const source = computed(() => ({ + url: view.value === "url" ? url.value.trim() : "", + directUpdate: directUpdate.value, + intervalHours: Number(intervalHours.value), +})); +useUnsavedGuard( + () => + routingA.value !== original.value || + JSON.stringify(source.value) !== JSON.stringify(originalSource.value), +); const loading = ref(true); const saving = ref(false); const confirming = ref(false); @@ -46,7 +73,7 @@ const editor = ref>(); const referenceId = useId(); const wide = computed(() => width.value >= 840); const referencePreference = ref(null); -const view = ref<"form" | "text">("form"); +const view = ref<"form" | "text" | "url">("form"); const picker = ref(); try { referencePreference.value = localStorage.getItem("routingA.reference"); @@ -62,7 +89,9 @@ const showReference = computed(() => const hasInboundDef = computed(() => routingA.value.split("\n").some((line) => /^\s*inbound\s*\(/.test(line)), ); -const busy = computed(() => loading.value || saving.value || confirming.value); +const busy = computed( + () => loading.value || saving.value || confirming.value || importing.value, +); watch(routingA, () => { backendError.value = ""; warningDismissed.value = false; @@ -135,6 +164,20 @@ async function importRules(event: Event) { confirming.value = false; } } +async function importFromURL() { + if (busy.value || !url.value.trim()) return; + importing.value = true; + backendError.value = ""; + try { + const result = await importRoutingAFromURL(source.value); + routingA.value = result.routingA; + validatedURL.value = url.value.trim(); + } catch (err) { + backendError.value = errorText(err); + } finally { + importing.value = false; + } +} function toggleReference() { referencePreference.value = String(!showReference.value); try { @@ -147,6 +190,16 @@ onMounted(async () => { try { const res = await getRoutingA(); routingA.value = original.value = res.routingA; + const saved = res.source ?? { + url: "", + directUpdate: false, + intervalHours: 24, + }; + originalSource.value = saved; + url.value = validatedURL.value = saved.url; + directUpdate.value = saved.directUpdate; + intervalHours.value = saved.intervalHours; + if (saved.url) view.value = "url"; } catch (err) { notify.warning(errorText(err)); emit("close"); @@ -159,7 +212,9 @@ async function close() { confirming.value = true; try { if ( - routingA.value !== original.value && + (routingA.value !== original.value || + JSON.stringify(source.value) !== + JSON.stringify(originalSource.value)) && !(await confirm({ message: t("routingA.discard"), confirmText: t("operations.confirm"), @@ -190,6 +245,16 @@ async function reset() { } async function save() { if (busy.value) return; + if ( + view.value === "url" && + (url.value.trim() !== validatedURL.value || + !Number.isInteger(Number(intervalHours.value)) || + Number(intervalHours.value) < 1 || + Number(intervalHours.value) > 8760) + ) { + backendError.value = t("routingA.urlImport.importFirst"); + return; + } saving.value = true; try { if ( @@ -201,7 +266,11 @@ async function save() { })) ) return; - const res = await putRoutingA({ routingA: routingA.value }); + const res = await putRoutingA( + view.value === "url" || originalSource.value.url + ? { routingA: routingA.value, source: source.value } + : { routingA: routingA.value }, + ); if (res && typeof res === "object" && "warning" in res && res.warning) { notify.warning(t("routingA.savedWithWarning", { warning: res.warning }), { timeout: 8000, @@ -218,7 +287,7 @@ async function save() { diff --git a/gui/src/dialogs/settings/routingA/RoutingEditor.vue b/gui/src/dialogs/settings/routingA/RoutingEditor.vue index e7911cdd..51ae3bf8 100644 --- a/gui/src/dialogs/settings/routingA/RoutingEditor.vue +++ b/gui/src/dialogs/settings/routingA/RoutingEditor.vue @@ -9,6 +9,7 @@ const props = defineProps<{ modelValue: string; disabled?: boolean; readonly?: boolean; + sourcePreview?: boolean; }>(); const emit = defineEmits<{ "update:modelValue": [value: string]; save: [] }>(); const { t } = useI18n(); @@ -110,6 +111,7 @@ defineExpose({ insert });