1
0

outbound-generator.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. 'use client';
  2. import { useState } from 'react';
  3. import {
  4. buildOutboundJson,
  5. type OutboundInput,
  6. type OutboundKind,
  7. type Network,
  8. type Security,
  9. type ProxyServerInput,
  10. type StreamInput,
  11. type WireguardInput,
  12. } from '@/lib/xray/outbounds';
  13. import { ToolFrame } from './tool-frame';
  14. import { TextField, SelectField } from './shared/fields';
  15. import { OutputBlock } from './shared/output-block';
  16. const KINDS: readonly OutboundKind[] = [
  17. 'freedom',
  18. 'blackhole',
  19. 'vless',
  20. 'vmess',
  21. 'trojan',
  22. 'shadowsocks',
  23. 'socks',
  24. 'http',
  25. 'wireguard',
  26. 'warp',
  27. ];
  28. const NETWORKS: readonly Network[] = ['tcp', 'kcp', 'ws', 'grpc', 'httpupgrade', 'xhttp'];
  29. const SECURITIES: readonly Security[] = ['none', 'tls', 'reality'];
  30. const FINGERPRINTS = ['chrome', 'firefox', 'safari', 'ios', 'android', 'edge', 'random'];
  31. const DOMAIN_STRATEGIES = ['AsIs', 'UseIP', 'UseIPv4', 'UseIPv6', 'ForceIP'];
  32. const PROXY_KINDS = new Set<OutboundKind>([
  33. 'vless',
  34. 'vmess',
  35. 'trojan',
  36. 'shadowsocks',
  37. 'socks',
  38. 'http',
  39. ]);
  40. const STREAM_KINDS = new Set<OutboundKind>(['vless', 'vmess', 'trojan', 'shadowsocks']);
  41. const WG_KINDS = new Set<OutboundKind>(['wireguard', 'warp']);
  42. export function OutboundGenerator() {
  43. const [kind, setKind] = useState<OutboundKind>('vless');
  44. const [tag, setTag] = useState('proxy');
  45. // proxy server
  46. const [address, setAddress] = useState('example.com');
  47. const [port, setPort] = useState('443');
  48. const [id, setId] = useState('');
  49. const [password, setPassword] = useState('');
  50. const [method, setMethod] = useState('2022-blake3-aes-128-gcm');
  51. const [flow, setFlow] = useState('');
  52. const [username, setUsername] = useState('');
  53. // stream
  54. const [network, setNetwork] = useState<Network>('tcp');
  55. const [security, setSecurity] = useState<Security>('reality');
  56. const [host, setHost] = useState('');
  57. const [path, setPath] = useState('/');
  58. const [serviceName, setServiceName] = useState('');
  59. const [sni, setSni] = useState('www.microsoft.com');
  60. const [fingerprint, setFingerprint] = useState('chrome');
  61. const [publicKey, setPublicKey] = useState('');
  62. const [shortId, setShortId] = useState('');
  63. // freedom
  64. const [domainStrategy, setDomainStrategy] = useState('AsIs');
  65. // wireguard
  66. const [wgSecretKey, setWgSecretKey] = useState('');
  67. const [wgAddress, setWgAddress] = useState('172.16.0.2/32');
  68. const [wgPublicKey, setWgPublicKey] = useState('');
  69. const [wgEndpoint, setWgEndpoint] = useState('');
  70. const isProxy = PROXY_KINDS.has(kind);
  71. const hasStream = STREAM_KINDS.has(kind);
  72. const isWg = WG_KINDS.has(kind);
  73. const hasPath = network === 'ws' || network === 'httpupgrade' || network === 'xhttp';
  74. const server: ProxyServerInput = {
  75. address,
  76. port: Number(port),
  77. id,
  78. password,
  79. method,
  80. flow,
  81. username,
  82. };
  83. const stream: StreamInput = {
  84. network,
  85. security,
  86. host,
  87. path,
  88. serviceName,
  89. sni,
  90. fingerprint,
  91. publicKey,
  92. shortId,
  93. };
  94. const wireguard: WireguardInput = {
  95. secretKey: wgSecretKey,
  96. address: wgAddress
  97. .split(',')
  98. .map((a) => a.trim())
  99. .filter(Boolean),
  100. publicKey: wgPublicKey,
  101. endpoint: wgEndpoint,
  102. };
  103. const input: OutboundInput = {
  104. kind,
  105. tag,
  106. server: isProxy ? server : undefined,
  107. wireguard: isWg ? wireguard : undefined,
  108. stream: hasStream ? stream : undefined,
  109. domainStrategy: kind === 'freedom' ? domainStrategy : undefined,
  110. };
  111. function reset() {
  112. setKind('vless');
  113. setTag('proxy');
  114. setAddress('example.com');
  115. setPort('443');
  116. setId('');
  117. setPassword('');
  118. setMethod('2022-blake3-aes-128-gcm');
  119. setFlow('');
  120. setUsername('');
  121. setNetwork('tcp');
  122. setSecurity('reality');
  123. setHost('');
  124. setPath('/');
  125. setServiceName('');
  126. setSni('www.microsoft.com');
  127. setFingerprint('chrome');
  128. setPublicKey('');
  129. setShortId('');
  130. setDomainStrategy('AsIs');
  131. setWgSecretKey('');
  132. setWgAddress('172.16.0.2/32');
  133. setWgPublicKey('');
  134. setWgEndpoint('');
  135. }
  136. return (
  137. <ToolFrame
  138. title="Outbound config generator"
  139. description="Build an Xray outbound object — freedom, blackhole, a proxy protocol, WireGuard, or WARP — to paste into your Xray configuration."
  140. onReset={reset}
  141. >
  142. <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
  143. <SelectField
  144. label="Kind"
  145. value={kind}
  146. onChange={(v) => setKind(v as OutboundKind)}
  147. options={KINDS}
  148. />
  149. <TextField label="Tag" value={kind === 'warp' ? 'warp' : tag} onChange={setTag} />
  150. {kind === 'freedom' ? (
  151. <SelectField
  152. label="Domain strategy"
  153. value={domainStrategy}
  154. onChange={setDomainStrategy}
  155. options={DOMAIN_STRATEGIES}
  156. />
  157. ) : null}
  158. {isProxy ? (
  159. <>
  160. <TextField label="Address" value={address} onChange={setAddress} />
  161. <TextField label="Port" value={port} onChange={setPort} inputMode="numeric" />
  162. {(kind === 'vless' || kind === 'vmess') && (
  163. <TextField label="UUID (id)" value={id} onChange={setId} />
  164. )}
  165. {kind === 'vless' && (
  166. <TextField
  167. label="Flow"
  168. value={flow}
  169. onChange={setFlow}
  170. placeholder="xtls-rprx-vision (optional)"
  171. />
  172. )}
  173. {(kind === 'trojan' || kind === 'shadowsocks') && (
  174. <TextField label="Password" value={password} onChange={setPassword} />
  175. )}
  176. {kind === 'shadowsocks' && (
  177. <TextField label="Method (cipher)" value={method} onChange={setMethod} />
  178. )}
  179. {(kind === 'socks' || kind === 'http') && (
  180. <>
  181. <TextField
  182. label="Username"
  183. value={username}
  184. onChange={setUsername}
  185. placeholder="optional"
  186. />
  187. <TextField label="Password" value={password} onChange={setPassword} />
  188. </>
  189. )}
  190. </>
  191. ) : null}
  192. {isWg ? (
  193. <>
  194. <TextField
  195. label="Private key (secretKey)"
  196. value={wgSecretKey}
  197. onChange={setWgSecretKey}
  198. />
  199. <TextField label="Local address" value={wgAddress} onChange={setWgAddress} />
  200. <TextField label="Peer public key" value={wgPublicKey} onChange={setWgPublicKey} />
  201. <TextField
  202. label="Peer endpoint"
  203. value={wgEndpoint}
  204. onChange={setWgEndpoint}
  205. placeholder={kind === 'warp' ? 'engage.cloudflareclient.com:2408' : 'host:51820'}
  206. />
  207. </>
  208. ) : null}
  209. </div>
  210. {hasStream ? (
  211. <div className="mt-4 grid grid-cols-1 gap-4 sm:grid-cols-2">
  212. <SelectField
  213. label="Transport"
  214. value={network}
  215. onChange={(v) => setNetwork(v as Network)}
  216. options={NETWORKS}
  217. />
  218. <SelectField
  219. label="Security"
  220. value={security}
  221. onChange={(v) => setSecurity(v as Security)}
  222. options={SECURITIES}
  223. />
  224. {hasPath ? (
  225. <>
  226. <TextField label="Path" value={path} onChange={setPath} />
  227. <TextField label="Host" value={host} onChange={setHost} placeholder="optional" />
  228. </>
  229. ) : null}
  230. {network === 'grpc' ? (
  231. <TextField label="serviceName" value={serviceName} onChange={setServiceName} />
  232. ) : null}
  233. {security !== 'none' ? (
  234. <>
  235. <TextField label="SNI (serverName)" value={sni} onChange={setSni} />
  236. <SelectField
  237. label="Fingerprint"
  238. value={fingerprint}
  239. onChange={setFingerprint}
  240. options={FINGERPRINTS}
  241. />
  242. </>
  243. ) : null}
  244. {security === 'reality' ? (
  245. <>
  246. <TextField label="Public key (pbk)" value={publicKey} onChange={setPublicKey} />
  247. <TextField label="Short ID (sid)" value={shortId} onChange={setShortId} />
  248. </>
  249. ) : null}
  250. </div>
  251. ) : null}
  252. <div className="mt-4">
  253. <OutputBlock label="Outbound (Xray JSON)" value={buildOutboundJson(input)} />
  254. </div>
  255. </ToolFrame>
  256. );
  257. }