mirror of
https://github.com/luckjiawei/podux.git
synced 2026-12-02 15:56:50 +00:00
✨ Add SOCKS5 Plugin
This commit is contained in:
1 parent
32e26c322d
commit
fce9272cf0
12 files changed
+200
-102
No files matched your search
@@ -169,6 +169,13 @@ func (fs *Service) genProxyCfgs(serverId *string) ([]v1.ProxyConfigurer, error)
|
||||
}
|
||||
proxyData["name"] = proxyMap.Name + "-" + proxyMap.Id
|
||||
|
||||
// Remove empty plugin map so frp doesn't try to parse a typeless plugin
|
||||
if plugin, ok := proxyData["plugin"].(map[string]interface{}); ok {
|
||||
if _, hasType := plugin["type"]; !hasType {
|
||||
delete(proxyData, "plugin")
|
||||
}
|
||||
}
|
||||
|
||||
jsonData, err = json.Marshal(proxyData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -14,6 +14,7 @@ type Proxy struct {
|
||||
Subdomain string `db:"subdomain" json:"subdomain"`
|
||||
CustomDomains types.JSONStringArray `db:"customDomains" json:"customDomains"`
|
||||
Transport types.JSONMap `db:"transport" json:"transport"`
|
||||
Plugin types.JSONMap `db:"plugin" json:"plugin,omitempty"`
|
||||
Description string `db:"description" json:"description"`
|
||||
Status string `db:"status" json:"status"`
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ export interface RadioCardOption {
|
||||
label: string;
|
||||
icon?: string;
|
||||
comingSoon?: boolean;
|
||||
incompatible?: boolean;
|
||||
incompatibleLabel?: string;
|
||||
}
|
||||
|
||||
interface RadioCardGroupProps {
|
||||
@@ -21,7 +23,7 @@ export function RadioCardGroup({ options, value, onChange, comingSoonLabel = "Co
|
||||
<Flex gap="2" wrap="wrap">
|
||||
{options.map((opt) => {
|
||||
const isActive = value === opt.value;
|
||||
const disabled = !!opt.comingSoon;
|
||||
const disabled = !!opt.comingSoon || !!opt.incompatible;
|
||||
return (
|
||||
<button
|
||||
key={opt.value}
|
||||
@@ -63,19 +65,23 @@ export function RadioCardGroup({ options, value, onChange, comingSoonLabel = "Co
|
||||
|
||||
{/* Label — bold ghost reserves width to prevent jitter */}
|
||||
<span className="relative">
|
||||
{/* invisible bold copy to pre-reserve width */}
|
||||
<span aria-hidden className="invisible block h-0 font-bold">{opt.label}</span>
|
||||
<span className={`block ${isActive ? "font-bold text-[var(--accent-11)]" : "font-normal text-[var(--gray-11)]"}`}>
|
||||
{opt.label}
|
||||
</span>
|
||||
</span>
|
||||
|
||||
{/* Coming soon badge */}
|
||||
{/* Badges */}
|
||||
{opt.comingSoon && (
|
||||
<span className="rounded bg-[var(--gray-4)] px-1 py-0.5 text-[10px] text-[var(--gray-9)]">
|
||||
{comingSoonLabel}
|
||||
</span>
|
||||
)}
|
||||
{opt.incompatible && opt.incompatibleLabel && (
|
||||
<span className="rounded bg-[var(--amber-3)] px-1 py-0.5 text-[10px] text-[var(--amber-11)]">
|
||||
{opt.incompatibleLabel}
|
||||
</span>
|
||||
)}
|
||||
</Flex>
|
||||
</button>
|
||||
);
|
||||
|
||||
@@ -296,6 +296,7 @@
|
||||
"sectionPlugin": "Plugin",
|
||||
"pluginEnable": "Enable Plugin",
|
||||
"pluginEnableDesc": "Use a frp plugin to extend proxy functionality",
|
||||
"pluginTcpOnly": "TCP only",
|
||||
"pluginType": "Plugin Type",
|
||||
"pluginSupported": "Supported",
|
||||
"pluginUsername": "Username",
|
||||
@@ -315,7 +316,9 @@
|
||||
"disabled": "Disabled",
|
||||
"errorInvalidName": "Invalid name (use alphanumeric, - or _)",
|
||||
"errorInvalidIP": "Invalid IP or hostname",
|
||||
"errorInvalidPort": "Invalid port (1-65535)"
|
||||
"errorInvalidPort": "Invalid port (1-65535)",
|
||||
"errorRequired": "Required",
|
||||
"errorSubdomainRequired": "Subdomain or custom domains required"
|
||||
},
|
||||
"settings": {
|
||||
"title": "Settings",
|
||||
@@ -459,4 +462,4 @@
|
||||
"settingsCreateFailed": "Failed to initialize system settings"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,6 +296,7 @@
|
||||
"sectionPlugin": "插件",
|
||||
"pluginEnable": "启用插件",
|
||||
"pluginEnableDesc": "使用 frp 插件扩展代理功能",
|
||||
"pluginTcpOnly": "仅限 TCP",
|
||||
"pluginType": "插件类型",
|
||||
"pluginSupported": "已支持",
|
||||
"pluginUsername": "用户名",
|
||||
@@ -315,7 +316,9 @@
|
||||
"disabled": "已禁用",
|
||||
"errorInvalidName": "无效的名称(使用字母数字、- 或 _)",
|
||||
"errorInvalidIP": "无效的 IP 或主机名",
|
||||
"errorInvalidPort": "无效的端口(1-65535)"
|
||||
"errorInvalidPort": "无效的端口(1-65535)",
|
||||
"errorRequired": "必填",
|
||||
"errorSubdomainRequired": "子域名或自定义域名为必填项"
|
||||
},
|
||||
"settings": {
|
||||
"title": "设置",
|
||||
@@ -459,4 +462,4 @@
|
||||
"settingsCreateFailed": "初始化系统设置失败"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,7 +171,7 @@ export function ProxiesView({
|
||||
{proxies.map((proxy) => (
|
||||
<Table.Row key={proxy.id}>
|
||||
<Table.Cell>
|
||||
<Text weight="medium">{proxy.name}</Text>
|
||||
<Text weight="medium">{proxy.name || <Text color="gray">-</Text>}</Text>
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
<Badge variant="surface">{proxy.proxyType.toUpperCase()}</Badge>
|
||||
@@ -191,11 +191,52 @@ export function ProxiesView({
|
||||
)}
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
<Text size="2" color="gray">
|
||||
{proxy.localIP || "127.0.0.1"}
|
||||
</Text>
|
||||
{(() => {
|
||||
const ip = proxy.localIP || "127.0.0.1";
|
||||
const copyKey = `${proxy.id}-localIP`;
|
||||
const isCopied = copiedId === copyKey;
|
||||
return (
|
||||
<Flex
|
||||
align="center"
|
||||
gap="1"
|
||||
style={{ cursor: "pointer" }}
|
||||
onClick={() => handleCopy(ip, copyKey)}
|
||||
title={isCopied ? t("common.copied") : t("common.clickToCopy")}
|
||||
>
|
||||
<Text size="2" color="gray">{ip}</Text>
|
||||
<Icon
|
||||
icon={isCopied ? "lucide:check" : "lucide:copy"}
|
||||
width="12"
|
||||
height="12"
|
||||
color={isCopied ? "var(--green-9)" : "var(--gray-8)"}
|
||||
/>
|
||||
</Flex>
|
||||
);
|
||||
})()}
|
||||
</Table.Cell>
|
||||
<Table.Cell>
|
||||
{proxy.localPort ? (() => {
|
||||
const copyKey = `${proxy.id}-localPort`;
|
||||
const isCopied = copiedId === copyKey;
|
||||
return (
|
||||
<Flex
|
||||
align="center"
|
||||
gap="1"
|
||||
style={{ cursor: "pointer" }}
|
||||
onClick={() => handleCopy(String(proxy.localPort), copyKey)}
|
||||
title={isCopied ? t("common.copied") : t("common.clickToCopy")}
|
||||
>
|
||||
<Text size="2">{proxy.localPort}</Text>
|
||||
<Icon
|
||||
icon={isCopied ? "lucide:check" : "lucide:copy"}
|
||||
width="12"
|
||||
height="12"
|
||||
color={isCopied ? "var(--green-9)" : "var(--gray-8)"}
|
||||
/>
|
||||
</Flex>
|
||||
);
|
||||
})() : <Text size="2" color="gray">-</Text>}
|
||||
</Table.Cell>
|
||||
<Table.Cell>{proxy.localPort}</Table.Cell>
|
||||
<Table.Cell>
|
||||
{(() => {
|
||||
if (proxy.proxyType === "http" || proxy.proxyType === "https") {
|
||||
|
||||
@@ -34,16 +34,16 @@ const proxyTypes = [
|
||||
{ value: "xtcp", label: "XTCP", icon: "lucide:share-2", comingSoon: true },
|
||||
];
|
||||
|
||||
const pluginTypes = [
|
||||
{ value: "socks5", label: "SOCKS5", icon: "lucide:globe", comingSoon: false },
|
||||
{ value: "http_proxy", label: "HTTPProxy", icon: "lucide:waypoints", comingSoon: true },
|
||||
{ value: "static_file", label: "StaticFile", icon: "lucide:folder-open", comingSoon: true },
|
||||
{ value: "unix_domain_socket", label: "UnixDomainSocket", icon: "lucide:cable", comingSoon: true },
|
||||
{ value: "http2https", label: "HTTP2HTTPS", icon: "lucide:lock", comingSoon: true },
|
||||
{ value: "https2http", label: "HTTPS2HTTP", icon: "lucide:lock-open", comingSoon: true },
|
||||
{ value: "https2https", label: "HTTPS2HTTPS", icon: "lucide:shield-check", comingSoon: true },
|
||||
{ value: "tls2raw", label: "TLS2Raw", icon: "lucide:zap", comingSoon: true },
|
||||
{ value: "virtual_net", label: "VirtualNet", icon: "lucide:network", comingSoon: true },
|
||||
const BASE_PLUGIN_TYPES = [
|
||||
{ value: "socks5", label: "SOCKS5", icon: "lucide:globe", comingSoon: false, tcpOnly: true },
|
||||
{ value: "http_proxy", label: "HTTPProxy", icon: "lucide:waypoints", comingSoon: true, tcpOnly: false },
|
||||
{ value: "static_file", label: "StaticFile", icon: "lucide:folder-open", comingSoon: true, tcpOnly: false },
|
||||
{ value: "unix_domain_socket", label: "UnixDomainSocket", icon: "lucide:cable", comingSoon: true, tcpOnly: false },
|
||||
{ value: "http2https", label: "HTTP2HTTPS", icon: "lucide:lock", comingSoon: true, tcpOnly: false },
|
||||
{ value: "https2http", label: "HTTPS2HTTP", icon: "lucide:lock-open", comingSoon: true, tcpOnly: false },
|
||||
{ value: "https2https", label: "HTTPS2HTTPS", icon: "lucide:shield-check", comingSoon: true, tcpOnly: false },
|
||||
{ value: "tls2raw", label: "TLS2Raw", icon: "lucide:zap", comingSoon: true, tcpOnly: false },
|
||||
{ value: "virtual_net", label: "VirtualNet", icon: "lucide:network", comingSoon: true, tcpOnly: false },
|
||||
];
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ interface ProxyFormPageViewProps {
|
||||
submitting: boolean;
|
||||
errors: Partial<Record<keyof ProxyFormData, string>>;
|
||||
isHttpType: boolean;
|
||||
isSubmitDisabled: boolean;
|
||||
isSocks5Plugin: boolean;
|
||||
mounted: boolean;
|
||||
onChange: (field: keyof ProxyFormData, value: string | boolean) => void;
|
||||
onSubmit: () => void;
|
||||
@@ -73,7 +73,7 @@ export function ProxyFormPageView({
|
||||
submitting,
|
||||
errors,
|
||||
isHttpType,
|
||||
isSubmitDisabled,
|
||||
isSocks5Plugin,
|
||||
mounted,
|
||||
onChange,
|
||||
onSubmit,
|
||||
@@ -198,31 +198,44 @@ export function ProxyFormPageView({
|
||||
<section>
|
||||
<SectionHeading id="section-network" title={t("proxy.sectionNetwork")} icon="lucide:network" />
|
||||
<Flex direction="column" gap="4">
|
||||
<Flex gap="3">
|
||||
<Box className="flex-[2]">
|
||||
<FormItem label={t("proxy.localAddress")} required error={errors.localIp}>
|
||||
<TextField.Root
|
||||
size="2"
|
||||
placeholder={t("proxy.localAddressPlaceholder")}
|
||||
value={formData.localIp}
|
||||
onChange={(e) => onChange("localIp", e.target.value)}
|
||||
color={errors.localIp ? "red" : undefined}
|
||||
/>
|
||||
</FormItem>
|
||||
</Box>
|
||||
<Box className="flex-1">
|
||||
<FormItem label={t("proxy.localPort")} required error={errors.localPort}>
|
||||
<TextField.Root
|
||||
size="2"
|
||||
placeholder={t("proxy.localPortPlaceholder")}
|
||||
type="number"
|
||||
value={formData.localPort}
|
||||
onChange={(e) => onChange("localPort", e.target.value)}
|
||||
color={errors.localPort ? "red" : undefined}
|
||||
/>
|
||||
</FormItem>
|
||||
</Box>
|
||||
</Flex>
|
||||
<AnimatePresence>
|
||||
{!isSocks5Plugin && (
|
||||
<motion.div
|
||||
key="local-addr"
|
||||
initial={{ opacity: 0, height: 0 }}
|
||||
animate={{ opacity: 1, height: "auto" }}
|
||||
exit={{ opacity: 0, height: 0 }}
|
||||
transition={{ duration: 0.2, ease: "easeInOut" }}
|
||||
style={{ overflow: "hidden" }}
|
||||
>
|
||||
<Flex gap="3">
|
||||
<Box className="flex-[2]">
|
||||
<FormItem label={t("proxy.localAddress")} required error={errors.localIp}>
|
||||
<TextField.Root
|
||||
size="2"
|
||||
placeholder={t("proxy.localAddressPlaceholder")}
|
||||
value={formData.localIp}
|
||||
onChange={(e) => onChange("localIp", e.target.value)}
|
||||
color={errors.localIp ? "red" : undefined}
|
||||
/>
|
||||
</FormItem>
|
||||
</Box>
|
||||
<Box className="flex-1">
|
||||
<FormItem label={t("proxy.localPort")} required error={errors.localPort}>
|
||||
<TextField.Root
|
||||
size="2"
|
||||
placeholder={t("proxy.localPortPlaceholder")}
|
||||
type="number"
|
||||
value={formData.localPort}
|
||||
onChange={(e) => onChange("localPort", e.target.value)}
|
||||
color={errors.localPort ? "red" : undefined}
|
||||
/>
|
||||
</FormItem>
|
||||
</Box>
|
||||
</Flex>
|
||||
</motion.div>
|
||||
)}
|
||||
</AnimatePresence>
|
||||
|
||||
{isHttpType ? (
|
||||
<>
|
||||
@@ -334,7 +347,11 @@ export function ProxyFormPageView({
|
||||
>
|
||||
<FormItem label={t("proxy.pluginType")} required>
|
||||
<RadioCardGroup
|
||||
options={pluginTypes}
|
||||
options={BASE_PLUGIN_TYPES.map((pt) => ({
|
||||
...pt,
|
||||
incompatible: pt.tcpOnly && formData.type !== "tcp",
|
||||
incompatibleLabel: t("proxy.pluginTcpOnly"),
|
||||
}))}
|
||||
value={formData.pluginType}
|
||||
onChange={(v) => onChange("pluginType", v as ProxyFormData["pluginType"])}
|
||||
comingSoonLabel={t("common.comingSoon")}
|
||||
@@ -392,7 +409,7 @@ export function ProxyFormPageView({
|
||||
</Button>
|
||||
<Button
|
||||
size="2"
|
||||
disabled={isSubmitDisabled}
|
||||
disabled={submitting}
|
||||
onClick={onSubmit}
|
||||
className="[background:linear-gradient(135deg,var(--accent-9)_0%,var(--accent-10)_100%)] text-white"
|
||||
>
|
||||
|
||||
@@ -21,7 +21,7 @@ export function ProxyFormPage() {
|
||||
submitting={form.submitting}
|
||||
errors={form.errors}
|
||||
isHttpType={form.isHttpType}
|
||||
isSubmitDisabled={form.isSubmitDisabled}
|
||||
isSocks5Plugin={form.isSocks5Plugin}
|
||||
mounted={mounted}
|
||||
onChange={form.handleChange}
|
||||
onSubmit={form.handleSubmit}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import pb from "../../../lib/pocketbase";
|
||||
import { toast } from "sonner";
|
||||
import { REGEX } from "../../../lib/regex";
|
||||
@@ -54,6 +55,7 @@ export function useProxyForm() {
|
||||
const navigate = useNavigate();
|
||||
const { id } = useParams<{ id: string }>();
|
||||
const isEditing = !!id;
|
||||
const { t } = useTranslation();
|
||||
|
||||
const [formData, setFormData] = useState<ProxyFormData>(DEFAULT_FORM);
|
||||
const [servers, setServers] = useState<ServerOption[]>([]);
|
||||
@@ -129,14 +131,14 @@ export function useProxyForm() {
|
||||
if (value) {
|
||||
switch (field) {
|
||||
case "name":
|
||||
if (!REGEX.PROXY_NAME.test(value)) error = "Invalid proxy name";
|
||||
if (!REGEX.PROXY_NAME.test(value)) error = t("proxy.errorInvalidName");
|
||||
break;
|
||||
case "localIp":
|
||||
if (!REGEX.IP_OR_HOSTNAME.test(value)) error = "Invalid IP or hostname";
|
||||
if (!REGEX.IP_OR_HOSTNAME.test(value)) error = t("proxy.errorInvalidIP");
|
||||
break;
|
||||
case "localPort":
|
||||
case "remotePort":
|
||||
if (!REGEX.PORT.test(value)) error = "Invalid port number";
|
||||
if (!REGEX.PORT.test(value)) error = t("proxy.errorInvalidPort");
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -144,17 +146,55 @@ export function useProxyForm() {
|
||||
};
|
||||
|
||||
const handleChange = (field: keyof ProxyFormData, value: string | boolean) => {
|
||||
setFormData((prev) => ({ ...prev, [field]: value }));
|
||||
setFormData((prev) => {
|
||||
const next = { ...prev, [field]: value };
|
||||
if (field === "type" && value !== "tcp" && prev.pluginEnabled && prev.pluginType === "socks5") {
|
||||
next.pluginEnabled = false;
|
||||
}
|
||||
return next;
|
||||
});
|
||||
if (typeof value === "string") validateField(field, value);
|
||||
};
|
||||
|
||||
const validate = (data: ProxyFormData): boolean => {
|
||||
const isHttp = data.type === "http" || data.type === "https";
|
||||
const isSocks5 = data.pluginEnabled && data.pluginType === "socks5";
|
||||
const newErrors: Partial<Record<keyof ProxyFormData, string>> = {};
|
||||
|
||||
if (!data.serverId) newErrors.serverId = t("proxy.errorRequired");
|
||||
if (!data.name) newErrors.name = t("proxy.errorRequired");
|
||||
else if (!REGEX.PROXY_NAME.test(data.name)) newErrors.name = t("proxy.errorInvalidName");
|
||||
|
||||
if (!isSocks5) {
|
||||
if (!data.localIp) newErrors.localIp = t("proxy.errorRequired");
|
||||
else if (!REGEX.IP_OR_HOSTNAME.test(data.localIp)) newErrors.localIp = t("proxy.errorInvalidIP");
|
||||
|
||||
if (!data.localPort) newErrors.localPort = t("proxy.errorRequired");
|
||||
else if (!REGEX.PORT.test(data.localPort)) newErrors.localPort = t("proxy.errorInvalidPort");
|
||||
}
|
||||
|
||||
if (!isHttp && !isSocks5) {
|
||||
if (!data.remotePort) newErrors.remotePort = t("proxy.errorRequired");
|
||||
else if (!REGEX.PORT.test(data.remotePort)) newErrors.remotePort = t("proxy.errorInvalidPort");
|
||||
}
|
||||
|
||||
if (isHttp && !data.subdomain && !data.customDomains) {
|
||||
newErrors.subdomain = t("proxy.errorSubdomainRequired");
|
||||
}
|
||||
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!validate(formData)) return;
|
||||
|
||||
const plugin = formData.pluginEnabled
|
||||
? {
|
||||
type: formData.pluginType,
|
||||
...(formData.pluginUsername ? { username: formData.pluginUsername } : {}),
|
||||
...(formData.pluginPassword ? { password: formData.pluginPassword } : {}),
|
||||
}
|
||||
type: formData.pluginType,
|
||||
...(formData.pluginUsername ? { username: formData.pluginUsername } : {}),
|
||||
...(formData.pluginPassword ? { password: formData.pluginPassword } : {}),
|
||||
}
|
||||
: null;
|
||||
|
||||
const payload = {
|
||||
@@ -195,20 +235,7 @@ export function useProxyForm() {
|
||||
};
|
||||
|
||||
const isHttpType = formData.type === "http" || formData.type === "https";
|
||||
|
||||
const isSubmitDisabled =
|
||||
!formData.serverId ||
|
||||
!formData.name ||
|
||||
!formData.type ||
|
||||
!formData.localIp ||
|
||||
!formData.localPort ||
|
||||
(!isHttpType && !formData.remotePort) ||
|
||||
(isHttpType && !formData.subdomain && !formData.customDomains) ||
|
||||
!!errors.name ||
|
||||
!!errors.localIp ||
|
||||
!!errors.localPort ||
|
||||
(!isHttpType && !!errors.remotePort) ||
|
||||
submitting;
|
||||
const isSocks5Plugin = formData.pluginEnabled && formData.pluginType === "socks5";
|
||||
|
||||
return {
|
||||
isEditing,
|
||||
@@ -219,7 +246,7 @@ export function useProxyForm() {
|
||||
submitting,
|
||||
errors,
|
||||
isHttpType,
|
||||
isSubmitDisabled,
|
||||
isSocks5Plugin,
|
||||
handleChange,
|
||||
handleSubmit,
|
||||
navigate,
|
||||
|
||||
@@ -29,7 +29,6 @@ interface ServerFormPageViewProps {
|
||||
submitting: boolean;
|
||||
loadingServer: boolean;
|
||||
frpVersion: string;
|
||||
isSubmitDisabled: boolean;
|
||||
mounted: boolean;
|
||||
onChange: (field: keyof Omit<ServerFormData, "auth" | "log" | "transport" | "metadatas">, value: string | number | boolean) => void;
|
||||
onAuthChange: (field: keyof ServerFormData["auth"], value: string) => void;
|
||||
@@ -50,7 +49,6 @@ export function ServerFormPageView({
|
||||
submitting,
|
||||
loadingServer,
|
||||
frpVersion,
|
||||
isSubmitDisabled,
|
||||
mounted,
|
||||
onChange,
|
||||
onAuthChange,
|
||||
@@ -505,7 +503,7 @@ export function ServerFormPageView({
|
||||
</Button>
|
||||
<Button
|
||||
size="2"
|
||||
disabled={isSubmitDisabled}
|
||||
disabled={submitting}
|
||||
onClick={onSubmit}
|
||||
className="[background:linear-gradient(135deg,var(--accent-9)_0%,var(--accent-10)_100%)] text-white"
|
||||
>
|
||||
|
||||
@@ -19,7 +19,6 @@ export function ServerFormPage() {
|
||||
submitting={form.submitting}
|
||||
loadingServer={form.loadingServer}
|
||||
frpVersion={form.frpVersion}
|
||||
isSubmitDisabled={form.isSubmitDisabled}
|
||||
mounted={mounted}
|
||||
onChange={form.handleChange}
|
||||
onAuthChange={form.handleAuthChange}
|
||||
|
||||
@@ -130,18 +130,19 @@ export function useServerForm() {
|
||||
value: string | number | boolean
|
||||
) => {
|
||||
setFormData((prev) => ({ ...prev, [field]: value }));
|
||||
// Clear error on change, re-validate format
|
||||
if (field === "serverName") {
|
||||
const error = value && !REGEX.SERVER_NAME.test(value as string) ? "Invalid server name" : "";
|
||||
setErrors((prev) => ({ ...prev, serverName: error }));
|
||||
}
|
||||
if (field === "serverAddr") {
|
||||
const error = value && !REGEX.IP_OR_HOSTNAME.test(value as string) ? "Invalid IP or hostname" : "";
|
||||
setErrors((prev) => ({ ...prev, serverAddr: error }));
|
||||
}
|
||||
if (field === "serverPort") {
|
||||
const error = !value || !REGEX.PORT.test((value as number).toString()) ? "Invalid port" : "";
|
||||
const error = value && !REGEX.PORT.test((value as number).toString()) ? "Invalid port" : "";
|
||||
setErrors((prev) => ({ ...prev, serverPort: error }));
|
||||
}
|
||||
if (field === "serverName") {
|
||||
const error = value && !REGEX.SERVER_NAME.test(value as string) ? "Invalid server name" : "";
|
||||
setErrors((prev) => ({ ...prev, serverName: error }));
|
||||
}
|
||||
};
|
||||
|
||||
const handleAuthChange = (field: keyof ServerFormData["auth"], value: string) => {
|
||||
@@ -182,15 +183,20 @@ export function useServerForm() {
|
||||
e.target.value = "";
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
// Basic validation
|
||||
const validate = (data: ServerFormData): boolean => {
|
||||
const newErrors: Record<string, string> = {};
|
||||
if (!formData.serverName) newErrors.serverName = "Required";
|
||||
else if (!REGEX.SERVER_NAME.test(formData.serverName)) newErrors.serverName = "Invalid server name";
|
||||
if (!formData.serverAddr) newErrors.serverAddr = "Required";
|
||||
else if (!REGEX.IP_OR_HOSTNAME.test(formData.serverAddr)) newErrors.serverAddr = "Invalid IP or hostname";
|
||||
if (!formData.serverPort || !REGEX.PORT.test(formData.serverPort.toString())) newErrors.serverPort = "Invalid port";
|
||||
if (Object.keys(newErrors).length > 0) { setErrors(newErrors); return; }
|
||||
if (!data.serverName) newErrors.serverName = "Required";
|
||||
else if (!REGEX.SERVER_NAME.test(data.serverName)) newErrors.serverName = "Invalid server name";
|
||||
if (!data.serverAddr) newErrors.serverAddr = "Required";
|
||||
else if (!REGEX.IP_OR_HOSTNAME.test(data.serverAddr)) newErrors.serverAddr = "Invalid IP or hostname";
|
||||
if (!data.serverPort) newErrors.serverPort = "Required";
|
||||
else if (!REGEX.PORT.test(data.serverPort.toString())) newErrors.serverPort = "Invalid port";
|
||||
setErrors(newErrors);
|
||||
return Object.keys(newErrors).length === 0;
|
||||
};
|
||||
|
||||
const handleSubmit = async () => {
|
||||
if (!validate(formData)) return;
|
||||
|
||||
try {
|
||||
setSubmitting(true);
|
||||
@@ -210,15 +216,6 @@ export function useServerForm() {
|
||||
}
|
||||
};
|
||||
|
||||
const isSubmitDisabled =
|
||||
!formData.serverName ||
|
||||
!formData.serverAddr ||
|
||||
!formData.serverPort ||
|
||||
!!errors.serverName ||
|
||||
!!errors.serverAddr ||
|
||||
!!errors.serverPort ||
|
||||
submitting;
|
||||
|
||||
return {
|
||||
isEditing,
|
||||
formData,
|
||||
@@ -226,7 +223,6 @@ export function useServerForm() {
|
||||
submitting,
|
||||
loadingServer,
|
||||
frpVersion,
|
||||
isSubmitDisabled,
|
||||
handleChange,
|
||||
handleAuthChange,
|
||||
handleLogChange,
|
||||
|
||||
Reference in new issue
Block a user