Skip to content

Commit

Permalink
✨ feat(node): Add protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
web-ppanel committed Dec 11, 2024
1 parent 1c61966 commit 301b635
Show file tree
Hide file tree
Showing 10 changed files with 8,767 additions and 4,102 deletions.
98 changes: 98 additions & 0 deletions apps/admin/app/dashboard/server/form-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { z } from '@shadcn/ui/lib/zod';

export const protocols = ['shadowsocks', 'vmess', 'vless', 'trojan', 'hysteria2', 'tuic'];

const nullableString = z.string().nullish();
const portSchema = z.number().min(1).max(65535);
const securityConfigSchema = z
.object({
sni: nullableString,
allow_insecure: z.boolean().nullable().default(false),
fingerprint: nullableString,
reality_private_key: nullableString,
reality_public_key: nullableString,
reality_short_id: nullableString,
})
.nullish();

const transportConfigSchema = z
.object({
path: nullableString,
host: nullableString,
server_name: nullableString,
})
.nullish();

const baseProtocolSchema = z.object({
port: portSchema,
transport: z.string(),
transport_config: transportConfigSchema,
security: z.string(),
security_config: securityConfigSchema,
});

const shadowsocksSchema = z.object({
method: z.string(),
port: portSchema,
});

const vmessSchema = baseProtocolSchema;

const vlessSchema = baseProtocolSchema.extend({
flow: nullableString,
});

const trojanSchema = baseProtocolSchema;

const hysteria2Schema = z.object({
port: portSchema,
hop_ports: nullableString,
hop_interval: z.number().nullish(),
obfs_password: nullableString,
security_config: securityConfigSchema,
});

const tuicSchema = z.object({
port: portSchema,
security_config: securityConfigSchema,
});

const protocolConfigSchema = z.discriminatedUnion('protocol', [
z.object({
protocol: z.literal('shadowsocks'),
config: shadowsocksSchema,
}),
z.object({
protocol: z.literal('vmess'),
config: vmessSchema,
}),
z.object({
protocol: z.literal('vless'),
config: vlessSchema,
}),
z.object({
protocol: z.literal('trojan'),
config: trojanSchema,
}),
z.object({
protocol: z.literal('hysteria2'),
config: hysteria2Schema,
}),
z.object({
protocol: z.literal('tuic'),
config: tuicSchema,
}),
]);

const baseFormSchema = z.object({
name: z.string(),
server_addr: z.string(),
speed_limit: z.number().nullish(),
traffic_ratio: z.number().default(1),
group_id: z.number().nullish(),
enable_relay: z.boolean().nullish().default(false),
relay_host: nullableString,
relay_port: z.number().nullish(),
});

export const formSchema = z.intersection(baseFormSchema, protocolConfigSchema);
Loading

0 comments on commit 301b635

Please sign in to comment.