sockopt.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { z } from 'zod';
  2. export const SockoptDomainStrategySchema = z.enum([
  3. 'AsIs',
  4. 'UseIP',
  5. 'UseIPv6v4',
  6. 'UseIPv6',
  7. 'UseIPv4v6',
  8. 'UseIPv4',
  9. 'ForceIP',
  10. 'ForceIPv6v4',
  11. 'ForceIPv6',
  12. 'ForceIPv4v6',
  13. 'ForceIPv4',
  14. ]);
  15. export type SockoptDomainStrategy = z.infer<typeof SockoptDomainStrategySchema>;
  16. export const TcpCongestionSchema = z.enum(['bbr', 'cubic', 'reno']);
  17. export type TcpCongestion = z.infer<typeof TcpCongestionSchema>;
  18. export const TproxyModeSchema = z.enum(['off', 'redirect', 'tproxy']);
  19. export type TproxyMode = z.infer<typeof TproxyModeSchema>;
  20. // Sockopt knobs are an orthogonal layer on streamSettings — they tune
  21. // the underlying socket (TCP keepalive, TFO, mark, tproxy, dialer proxy,
  22. // IPv6-only, MPTCP). The wire field is `interface` (single word) but the
  23. // panel class names it `interfaceName` internally to avoid the JS
  24. // reserved keyword. We use `interfaceName` here too and document the
  25. // renames; serializers writing back to wire must rename.
  26. //
  27. // trustedXForwardedFor is omitted from the wire payload when empty
  28. // (legacy toJson() filters it); our default([]) lets parsing succeed but
  29. // the shadow canonicalize step treats [] and absence as equivalent.
  30. export const SockoptStreamSettingsSchema = z.object({
  31. acceptProxyProtocol: z.boolean().default(false),
  32. tcpFastOpen: z.boolean().default(false),
  33. mark: z.number().int().min(0).default(0),
  34. tproxy: TproxyModeSchema.default('off'),
  35. tcpMptcp: z.boolean().default(false),
  36. penetrate: z.boolean().default(false),
  37. domainStrategy: SockoptDomainStrategySchema.default('UseIP'),
  38. tcpMaxSeg: z.number().int().min(0).default(1440),
  39. dialerProxy: z.string().default(''),
  40. tcpKeepAliveInterval: z.number().int().min(0).default(0),
  41. tcpKeepAliveIdle: z.number().int().min(0).default(300),
  42. tcpUserTimeout: z.number().int().min(0).default(10000),
  43. tcpcongestion: TcpCongestionSchema.default('bbr'),
  44. V6Only: z.boolean().default(false),
  45. tcpWindowClamp: z.number().int().min(0).default(600),
  46. interfaceName: z.string().default(''),
  47. trustedXForwardedFor: z.array(z.string()).default([]),
  48. });
  49. export type SockoptStreamSettings = z.infer<typeof SockoptStreamSettingsSchema>;