From 7f158ddc2eb33fbd5ecbb91138efefd6c3e9d6dd Mon Sep 17 00:00:00 2001 From: Timothy Date: Sat, 24 Feb 2024 18:03:59 +0800 Subject: [PATCH] fetch network settings --- web/api/network.ts | 55 ++++++++++++++++++++++++++++++++++++++ web/app/domains/page.tsx | 3 --- web/app/network/page.tsx | 31 ++++++++++++++++++++- web/components/ip-mode.tsx | 21 ++++++++++++--- 4 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 web/api/network.ts diff --git a/web/api/network.ts b/web/api/network.ts new file mode 100644 index 00000000..0bcf44a2 --- /dev/null +++ b/web/api/network.ts @@ -0,0 +1,55 @@ +import { get_api_server } from "./env"; + +export interface WebHook { + enabled: boolean; + url: string; + request_body: string; +} + +export interface NetworkSettings { + ip_mode: string; + ip_urls: string[]; + ipv6_urls: string[]; + use_proxy: boolean; + skip_ssl_verify: boolean; + socks5_proxy: string; + webhook: WebHook; + resolver: string; + ip_interface: string; +} + +export async function get_network_settings(credentials: string): Promise { + if (credentials) { + const resp = await fetch(get_api_server() + '/api/v1/network', { + method: 'GET', + headers: { + 'Authorization': `Basic ${credentials}` + } + }) + + if (resp.status === 200) { + return resp.json(); + } + } + + return {} as NetworkSettings; +} + +export async function update_network_settings(credentials: string, settings: NetworkSettings): Promise { + if (credentials) { + const resp = await fetch(get_api_server() + '/api/v1/network', { + method: 'PUT', + headers: { + 'Authorization': `Basic ${credentials}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(settings) + }) + + if (resp.status === 200) { + return true; + } + } + + return false; +} \ No newline at end of file diff --git a/web/app/domains/page.tsx b/web/app/domains/page.tsx index 45c61345..1d5478a1 100644 --- a/web/app/domains/page.tsx +++ b/web/app/domains/page.tsx @@ -30,9 +30,6 @@ export default function Domains() {
- {/*
*/} - {/* Provider */} - {/*
*/}
diff --git a/web/app/network/page.tsx b/web/app/network/page.tsx index 28a1b927..1553a31f 100644 --- a/web/app/network/page.tsx +++ b/web/app/network/page.tsx @@ -1,15 +1,44 @@ +'use client'; + import { IpMode } from "@/components/ip-mode"; import { Proxy } from "@/components/proxy"; import { WebHook } from "@/components/webhook"; import { Resolver } from "@/components/resolver"; import { IPInterface } from "@/components/ip-interface"; +import { useRouter } from "next/navigation"; +import { CommonContext } from "@/components/user"; +import { useEffect, useState, useContext } from "react"; +import { get_network_settings, NetworkSettings, WebHook as WebHookSetting, update_network_settings } from "@/api/network"; +import { get_info } from "@/api/info"; export default function Network() { + const router = useRouter(); + const userStore = useContext(CommonContext); + const { credentials, setCurrentPage, saveVersion } = userStore; + const [settings, setSettings] = useState({} as NetworkSettings); + + useEffect(() => { + if (!credentials) { + router.push('/login'); + return; + } + setCurrentPage('Network'); + get_info(credentials).then((info) => { + saveVersion(info.version); + }); + + get_network_settings(credentials).then((settings) => { + console.log('settings', settings); + setSettings(settings); + }); + }, [credentials, router, setCurrentPage, saveVersion]); + + return (
- + diff --git a/web/components/ip-mode.tsx b/web/components/ip-mode.tsx index de9b96fd..0c7bb63a 100644 --- a/web/components/ip-mode.tsx +++ b/web/components/ip-mode.tsx @@ -1,6 +1,15 @@ import { SettingsIcon } from "@/components/icons"; +import { useState } from "react"; + +interface IpModeProps { + IPMode: string; + IPUrls?: string[]; + IPV6Urls?: string[]; +} + +export const IpMode = (props: IpModeProps) => { + const [isIPV6, setIsIPV6] = useState(props.IPMode === 'IPV6' ? true : false); -export const IpMode = () => { return (
@@ -8,18 +17,22 @@ export const IpMode = () => {
IP Mode
-
IPV4
+
{props.IPMode}
The current IP mode
Enable IPv6 - +
URLs
- +