sockopt.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { useTranslation } from 'react-i18next';
  2. import { Button, Form, Input, InputNumber, Select, Space, Switch } from 'antd';
  3. import {
  4. Address_Port_Strategy,
  5. DOMAIN_STRATEGY_OPTION,
  6. TCP_CONGESTION_OPTION,
  7. } from '@/schemas/primitives';
  8. import { HappyEyeballsSchema } from '@/schemas/protocols/stream/sockopt';
  9. export default function SockoptForm({
  10. toggleSockopt,
  11. }: {
  12. toggleSockopt: (on: boolean) => void;
  13. }) {
  14. const { t } = useTranslation();
  15. return (
  16. <Form.Item
  17. noStyle
  18. shouldUpdate={(prev, curr) => {
  19. const a = (prev.streamSettings as { sockopt?: object } | undefined)?.sockopt;
  20. const b = (curr.streamSettings as { sockopt?: object } | undefined)?.sockopt;
  21. return !!a !== !!b;
  22. }}
  23. >
  24. {({ getFieldValue }) => {
  25. const sock = getFieldValue(['streamSettings', 'sockopt']);
  26. const on = !!sock && typeof sock === 'object' && Object.keys(sock).length > 0;
  27. return (
  28. <>
  29. <Form.Item label="Sockopt">
  30. <Switch checked={on} onChange={toggleSockopt} />
  31. </Form.Item>
  32. {on && (
  33. <>
  34. <Form.Item name={['streamSettings', 'sockopt', 'mark']} label={t('pages.inbounds.form.routeMark')}>
  35. <InputNumber min={0} />
  36. </Form.Item>
  37. <Form.Item
  38. name={['streamSettings', 'sockopt', 'tcpKeepAliveInterval']}
  39. label={t('pages.inbounds.form.tcpKeepAliveInterval')}
  40. >
  41. <InputNumber min={0} />
  42. </Form.Item>
  43. <Form.Item
  44. name={['streamSettings', 'sockopt', 'tcpKeepAliveIdle']}
  45. label={t('pages.inbounds.form.tcpKeepAliveIdle')}
  46. >
  47. <InputNumber min={0} />
  48. </Form.Item>
  49. <Form.Item name={['streamSettings', 'sockopt', 'tcpMaxSeg']} label={t('pages.inbounds.form.tcpMaxSeg')}>
  50. <InputNumber min={0} />
  51. </Form.Item>
  52. <Form.Item
  53. name={['streamSettings', 'sockopt', 'tcpUserTimeout']}
  54. label={t('pages.inbounds.form.tcpUserTimeout')}
  55. >
  56. <InputNumber min={0} />
  57. </Form.Item>
  58. <Form.Item
  59. name={['streamSettings', 'sockopt', 'tcpWindowClamp']}
  60. label={t('pages.inbounds.form.tcpWindowClamp')}
  61. tooltip={t('pages.inbounds.form.tcpWindowClampHint')}
  62. >
  63. <InputNumber min={0} />
  64. </Form.Item>
  65. <Form.Item
  66. name={['streamSettings', 'sockopt', 'acceptProxyProtocol']}
  67. label={t('pages.inbounds.form.proxyProtocol')}
  68. valuePropName="checked"
  69. >
  70. <Switch />
  71. </Form.Item>
  72. <Form.Item
  73. name={['streamSettings', 'sockopt', 'tcpFastOpen']}
  74. label={t('pages.inbounds.form.tcpFastOpen')}
  75. valuePropName="checked"
  76. >
  77. <Switch />
  78. </Form.Item>
  79. <Form.Item
  80. name={['streamSettings', 'sockopt', 'tcpMptcp']}
  81. label={t('pages.inbounds.form.multipathTcp')}
  82. valuePropName="checked"
  83. >
  84. <Switch />
  85. </Form.Item>
  86. <Form.Item
  87. name={['streamSettings', 'sockopt', 'penetrate']}
  88. label={t('pages.inbounds.form.penetrate')}
  89. valuePropName="checked"
  90. >
  91. <Switch />
  92. </Form.Item>
  93. <Form.Item
  94. name={['streamSettings', 'sockopt', 'V6Only']}
  95. label={t('pages.inbounds.form.v6Only')}
  96. valuePropName="checked"
  97. >
  98. <Switch />
  99. </Form.Item>
  100. <Form.Item
  101. name={['streamSettings', 'sockopt', 'domainStrategy']}
  102. label={t('pages.xray.wireguard.domainStrategy')}
  103. >
  104. <Select
  105. style={{ width: '50%' }}
  106. options={Object.values(DOMAIN_STRATEGY_OPTION).map((d) => ({ value: d, label: d }))}
  107. />
  108. </Form.Item>
  109. <Form.Item
  110. name={['streamSettings', 'sockopt', 'tcpcongestion']}
  111. label={t('pages.inbounds.form.tcpCongestion')}
  112. >
  113. <Select
  114. style={{ width: '50%' }}
  115. options={Object.values(TCP_CONGESTION_OPTION).map((c) => ({ value: c, label: c }))}
  116. />
  117. </Form.Item>
  118. <Form.Item name={['streamSettings', 'sockopt', 'tproxy']} label="TProxy">
  119. <Select
  120. style={{ width: '50%' }}
  121. options={[
  122. { value: 'off', label: 'Off' },
  123. { value: 'redirect', label: 'Redirect' },
  124. { value: 'tproxy', label: 'TProxy' },
  125. ]}
  126. />
  127. </Form.Item>
  128. <Form.Item name={['streamSettings', 'sockopt', 'dialerProxy']} label={t('pages.inbounds.form.dialerProxy')}>
  129. <Input />
  130. </Form.Item>
  131. <Form.Item
  132. name={['streamSettings', 'sockopt', 'interface']}
  133. label={t('pages.inbounds.info.interfaceName')}
  134. >
  135. <Input />
  136. </Form.Item>
  137. <Form.Item
  138. name={['streamSettings', 'sockopt', 'trustedXForwardedFor']}
  139. label={t('pages.inbounds.form.trustedXForwardedFor')}
  140. >
  141. <Select
  142. mode="tags"
  143. style={{ width: '100%' }}
  144. tokenSeparators={[',']}
  145. options={[
  146. { value: 'CF-Connecting-IP', label: 'CF-Connecting-IP' },
  147. { value: 'X-Real-IP', label: 'X-Real-IP' },
  148. { value: 'True-Client-IP', label: 'True-Client-IP' },
  149. { value: 'X-Client-IP', label: 'X-Client-IP' },
  150. ]}
  151. />
  152. </Form.Item>
  153. <Form.Item
  154. name={['streamSettings', 'sockopt', 'addressPortStrategy']}
  155. label={t('pages.inbounds.form.addressPortStrategy')}
  156. >
  157. <Select
  158. style={{ width: '50%' }}
  159. options={Object.values(Address_Port_Strategy).map((v) => ({ value: v, label: v }))}
  160. />
  161. </Form.Item>
  162. <Form.Item shouldUpdate noStyle>
  163. {({ getFieldValue, setFieldValue }) => {
  164. const he = getFieldValue(['streamSettings', 'sockopt', 'happyEyeballs']);
  165. const hasHe = he != null;
  166. return (
  167. <>
  168. <Form.Item label="Happy Eyeballs">
  169. <Switch
  170. checked={hasHe}
  171. onChange={(v) => {
  172. setFieldValue(
  173. ['streamSettings', 'sockopt', 'happyEyeballs'],
  174. v ? HappyEyeballsSchema.parse({}) : undefined,
  175. );
  176. }}
  177. />
  178. </Form.Item>
  179. {hasHe && (
  180. <>
  181. <Form.Item
  182. name={['streamSettings', 'sockopt', 'happyEyeballs', 'tryDelayMs']}
  183. label={t('pages.inbounds.form.tryDelayMs')}
  184. >
  185. <InputNumber min={0} placeholder="0 disabled — 250 recommended" />
  186. </Form.Item>
  187. <Form.Item
  188. name={['streamSettings', 'sockopt', 'happyEyeballs', 'prioritizeIPv6']}
  189. label={t('pages.inbounds.form.prioritizeIPv6')}
  190. valuePropName="checked"
  191. >
  192. <Switch />
  193. </Form.Item>
  194. <Form.Item
  195. name={['streamSettings', 'sockopt', 'happyEyeballs', 'interleave']}
  196. label={t('pages.inbounds.form.interleave')}
  197. >
  198. <InputNumber min={1} />
  199. </Form.Item>
  200. <Form.Item
  201. name={['streamSettings', 'sockopt', 'happyEyeballs', 'maxConcurrentTry']}
  202. label={t('pages.inbounds.form.maxConcurrentTry')}
  203. >
  204. <InputNumber min={0} />
  205. </Form.Item>
  206. </>
  207. )}
  208. </>
  209. );
  210. }}
  211. </Form.Item>
  212. <Form.List name={['streamSettings', 'sockopt', 'customSockopt']}>
  213. {(fields, { add, remove }) => (
  214. <>
  215. <Form.Item label={t('pages.inbounds.form.customSockopt')}>
  216. <Button
  217. type="dashed"
  218. size="small"
  219. onClick={() => add({ type: 'int', level: '6', opt: '', value: '' })}
  220. >
  221. + {t('pages.inbounds.form.addCustomOption')}
  222. </Button>
  223. </Form.Item>
  224. {fields.map((field) => (
  225. <Space.Compact key={field.key} style={{ display: 'flex', marginBottom: 8 }}>
  226. <Form.Item name={[field.name, 'system']} noStyle>
  227. <Select
  228. placeholder="all"
  229. allowClear
  230. style={{ width: 100 }}
  231. options={[
  232. { value: 'linux', label: 'linux' },
  233. { value: 'windows', label: 'windows' },
  234. { value: 'darwin', label: 'darwin' },
  235. ]}
  236. />
  237. </Form.Item>
  238. <Form.Item name={[field.name, 'type']} noStyle>
  239. <Select
  240. style={{ width: 80 }}
  241. options={[
  242. { value: 'int', label: 'int' },
  243. { value: 'str', label: 'str' },
  244. ]}
  245. />
  246. </Form.Item>
  247. <Form.Item name={[field.name, 'level']} noStyle>
  248. <Input placeholder="level (6=TCP)" style={{ width: 100 }} />
  249. </Form.Item>
  250. <Form.Item name={[field.name, 'opt']} noStyle>
  251. <Input placeholder="opt" style={{ width: 120 }} />
  252. </Form.Item>
  253. <Form.Item name={[field.name, 'value']} noStyle>
  254. <Input placeholder="value" style={{ flex: 1 }} />
  255. </Form.Item>
  256. <Button danger onClick={() => remove(field.name)}>−</Button>
  257. </Space.Compact>
  258. ))}
  259. </>
  260. )}
  261. </Form.List>
  262. </>
  263. )}
  264. </>
  265. );
  266. }}
  267. </Form.Item>
  268. );
  269. }