wireguard.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { useTranslation } from 'react-i18next';
  2. import { Button, Divider, Form, Input, InputNumber, Space, Switch } from 'antd';
  3. import { MinusOutlined, PlusOutlined, ReloadOutlined } from '@ant-design/icons';
  4. import { Wireguard } from '@/utils';
  5. interface WireguardFieldsProps {
  6. wgPubKey: string;
  7. regenInboundWg: () => void;
  8. regenWgPeerKeypair: (name: number) => void;
  9. }
  10. export default function WireguardFields({ wgPubKey, regenInboundWg, regenWgPeerKeypair }: WireguardFieldsProps) {
  11. const { t } = useTranslation();
  12. return (
  13. <>
  14. <Form.Item label={t('pages.xray.wireguard.secretKey')}>
  15. <Space.Compact block>
  16. <Form.Item name={['settings', 'secretKey']} noStyle>
  17. <Input style={{ width: 'calc(100% - 32px)' }} />
  18. </Form.Item>
  19. <Button icon={<ReloadOutlined />} onClick={regenInboundWg} />
  20. </Space.Compact>
  21. </Form.Item>
  22. <Form.Item label={t('pages.xray.wireguard.publicKey')}>
  23. <Input value={wgPubKey} disabled />
  24. </Form.Item>
  25. <Form.Item name={['settings', 'mtu']} label="MTU">
  26. <InputNumber />
  27. </Form.Item>
  28. <Form.Item
  29. name={['settings', 'noKernelTun']}
  30. label={t('pages.inbounds.info.noKernelTun')}
  31. valuePropName="checked"
  32. >
  33. <Switch />
  34. </Form.Item>
  35. <Form.List name={['settings', 'peers']}>
  36. {(fields, { add, remove }) => (
  37. <>
  38. <Form.Item label={t('pages.inbounds.form.peers')}>
  39. <Button
  40. size="small"
  41. onClick={() => {
  42. const kp = Wireguard.generateKeypair();
  43. add({
  44. privateKey: kp.privateKey,
  45. publicKey: kp.publicKey,
  46. allowedIPs: ['10.0.0.2/32'],
  47. keepAlive: 0,
  48. });
  49. }}
  50. >
  51. <PlusOutlined /> {t('pages.inbounds.form.addPeer')}
  52. </Button>
  53. </Form.Item>
  54. {fields.map((field, idx) => (
  55. <div key={field.key} className="wg-peer">
  56. <Divider titlePlacement="center">
  57. <Space>
  58. <span>{t('pages.inbounds.info.peerNumber', { n: idx + 1 })}</span>
  59. {fields.length > 1 && (
  60. <Button
  61. size="small"
  62. danger
  63. icon={<MinusOutlined />}
  64. onClick={() => remove(field.name)}
  65. />
  66. )}
  67. </Space>
  68. </Divider>
  69. <Form.Item label={t('pages.xray.wireguard.secretKey')}>
  70. <Space.Compact block>
  71. <Form.Item name={[field.name, 'privateKey']} noStyle>
  72. <Input style={{ width: 'calc(100% - 32px)' }} />
  73. </Form.Item>
  74. <Button
  75. icon={<ReloadOutlined />}
  76. onClick={() => regenWgPeerKeypair(field.name)}
  77. />
  78. </Space.Compact>
  79. </Form.Item>
  80. <Form.Item name={[field.name, 'publicKey']} label={t('pages.xray.wireguard.publicKey')}>
  81. <Input />
  82. </Form.Item>
  83. <Form.Item name={[field.name, 'preSharedKey']} label="PSK">
  84. <Input />
  85. </Form.Item>
  86. <Form.List name={[field.name, 'allowedIPs']}>
  87. {(ipFields, { add: addIp, remove: removeIp }) => (
  88. <Form.Item label={t('pages.xray.wireguard.allowedIPs')}>
  89. <Button size="small" onClick={() => addIp('')}>
  90. <PlusOutlined />
  91. </Button>
  92. {ipFields.map((ipField) => (
  93. <Space.Compact key={ipField.key} block className="mt-4">
  94. <Form.Item name={ipField.name} noStyle>
  95. <Input />
  96. </Form.Item>
  97. {ipFields.length > 1 && (
  98. <Button size="small" onClick={() => removeIp(ipField.name)}>
  99. <MinusOutlined />
  100. </Button>
  101. )}
  102. </Space.Compact>
  103. ))}
  104. </Form.Item>
  105. )}
  106. </Form.List>
  107. <Form.Item name={[field.name, 'keepAlive']} label={t('pages.inbounds.form.keepAlive')}>
  108. <InputNumber min={0} />
  109. </Form.Item>
  110. </div>
  111. ))}
  112. </>
  113. )}
  114. </Form.List>
  115. </>
  116. );
  117. }