subscription-builder.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use client';
  2. import { useState } from 'react';
  3. import {
  4. buildSubscriptionUrls,
  5. buildShareLinks,
  6. buildBase64Subscription,
  7. buildJsonSubscription,
  8. type SubClient,
  9. type SubUrlInput,
  10. } from '@/lib/xray/subscription';
  11. import type { Network, Security } from '@/lib/xray/outbounds';
  12. import { ToolFrame } from './tool-frame';
  13. import { TextField, SelectField, CheckboxField } from './shared/fields';
  14. import { OutputBlock } from './shared/output-block';
  15. type ClientProtocol = 'vless' | 'vmess' | 'trojan' | 'ss';
  16. interface ClientRow {
  17. protocol: ClientProtocol;
  18. remark: string;
  19. address: string;
  20. port: string;
  21. credential: string; // id (vless/vmess) or password (trojan/ss)
  22. method: string; // ss
  23. network: Network;
  24. security: Security;
  25. sni: string;
  26. }
  27. const PROTOCOLS: readonly ClientProtocol[] = ['vless', 'vmess', 'trojan', 'ss'];
  28. const NETWORKS: readonly Network[] = ['tcp', 'kcp', 'ws', 'grpc', 'httpupgrade', 'xhttp'];
  29. const SECURITIES: readonly Security[] = ['none', 'tls', 'reality'];
  30. const addBtn =
  31. 'inline-flex items-center gap-1.5 rounded-lg border px-2.5 py-1.5 text-xs font-medium transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground';
  32. const DEFAULT_CLIENTS: ClientRow[] = [
  33. {
  34. protocol: 'vless',
  35. remark: 'HK-01',
  36. address: 'a.example.com',
  37. port: '443',
  38. credential: '11111111-2222-3333-4444-555555555555',
  39. method: '',
  40. network: 'tcp',
  41. security: 'reality',
  42. sni: 'www.microsoft.com',
  43. },
  44. ];
  45. function toClient(r: ClientRow): SubClient {
  46. const isUuid = r.protocol === 'vless' || r.protocol === 'vmess';
  47. return {
  48. protocol: r.protocol,
  49. remark: r.remark,
  50. address: r.address,
  51. port: Number(r.port),
  52. id: isUuid ? r.credential : undefined,
  53. password: isUuid ? undefined : r.credential,
  54. method: r.protocol === 'ss' ? r.method : undefined,
  55. network: r.network,
  56. security: r.security,
  57. sni: r.sni || undefined,
  58. };
  59. }
  60. export function SubscriptionBuilder() {
  61. const [scheme, setScheme] = useState<'http' | 'https'>('https');
  62. const [host, setHost] = useState('sub.example.com');
  63. const [port, setPort] = useState('2096');
  64. const [subPath, setSubPath] = useState('/sub/');
  65. const [jsonPath, setJsonPath] = useState('/json/');
  66. const [subId, setSubId] = useState('user-1');
  67. const [behindProxy, setBehindProxy] = useState(false);
  68. const [clients, setClients] = useState<ClientRow[]>(DEFAULT_CLIENTS);
  69. function patch(i: number, p: Partial<ClientRow>) {
  70. setClients((prev) => prev.map((c, j) => (i === j ? { ...c, ...p } : c)));
  71. }
  72. const urlInput: SubUrlInput = { scheme, host, port: Number(port), subPath, jsonPath, subId, behindProxy };
  73. const urls = buildSubscriptionUrls(urlInput);
  74. const subClients = clients.filter((c) => c.address.trim()).map(toClient);
  75. function reset() {
  76. setScheme('https');
  77. setHost('sub.example.com');
  78. setPort('2096');
  79. setSubPath('/sub/');
  80. setJsonPath('/json/');
  81. setSubId('user-1');
  82. setBehindProxy(false);
  83. setClients(DEFAULT_CLIENTS);
  84. }
  85. return (
  86. <ToolFrame
  87. title="Subscription & sub-JSON builder"
  88. description="Build the subscription URLs and preview both body formats — the Base64 link list and the JSON (Xray-json) config."
  89. onReset={reset}
  90. >
  91. <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
  92. <SelectField
  93. label="Scheme"
  94. value={scheme}
  95. onChange={(v) => setScheme(v as 'http' | 'https')}
  96. options={['https', 'http']}
  97. />
  98. <TextField label="Host" value={host} onChange={setHost} />
  99. <TextField label="Port" value={port} onChange={setPort} inputMode="numeric" />
  100. <TextField label="Sub ID" value={subId} onChange={setSubId} />
  101. <TextField label="Sub path" value={subPath} onChange={setSubPath} />
  102. <TextField label="JSON path" value={jsonPath} onChange={setJsonPath} />
  103. <CheckboxField
  104. label="Behind a reverse proxy (omit the port)"
  105. checked={behindProxy}
  106. onChange={setBehindProxy}
  107. />
  108. </div>
  109. <div className="mt-4 grid grid-cols-1 gap-4">
  110. <OutputBlock label="Base64 subscription URL" value={urls.base64} qr />
  111. <OutputBlock label="JSON subscription URL" value={urls.json} />
  112. </div>
  113. <div className="mt-5 flex items-center justify-between">
  114. <h4 className="text-sm font-semibold">Clients in this subscription</h4>
  115. <button
  116. type="button"
  117. className={addBtn}
  118. onClick={() =>
  119. setClients((p) => [
  120. ...p,
  121. {
  122. protocol: 'vless',
  123. remark: '',
  124. address: '',
  125. port: '443',
  126. credential: '',
  127. method: '',
  128. network: 'tcp',
  129. security: 'reality',
  130. sni: '',
  131. },
  132. ])
  133. }
  134. >
  135. Add client
  136. </button>
  137. </div>
  138. <div className="mt-2 flex flex-col gap-3">
  139. {clients.map((c, i) => (
  140. <div key={i} className="rounded-xl border p-3">
  141. <div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
  142. <SelectField
  143. label="Protocol"
  144. value={c.protocol}
  145. onChange={(v) => patch(i, { protocol: v as ClientProtocol })}
  146. options={PROTOCOLS}
  147. />
  148. <TextField label="Remark" value={c.remark} onChange={(v) => patch(i, { remark: v })} />
  149. <TextField label="Address" value={c.address} onChange={(v) => patch(i, { address: v })} />
  150. <TextField label="Port" value={c.port} onChange={(v) => patch(i, { port: v })} inputMode="numeric" />
  151. <TextField
  152. label={c.protocol === 'vless' || c.protocol === 'vmess' ? 'UUID (id)' : 'Password'}
  153. value={c.credential}
  154. onChange={(v) => patch(i, { credential: v })}
  155. />
  156. {c.protocol === 'ss' ? (
  157. <TextField label="Method" value={c.method} onChange={(v) => patch(i, { method: v })} />
  158. ) : null}
  159. <SelectField
  160. label="Transport"
  161. value={c.network}
  162. onChange={(v) => patch(i, { network: v as Network })}
  163. options={NETWORKS}
  164. />
  165. <SelectField
  166. label="Security"
  167. value={c.security}
  168. onChange={(v) => patch(i, { security: v as Security })}
  169. options={SECURITIES}
  170. />
  171. {c.security !== 'none' ? (
  172. <TextField label="SNI" value={c.sni} onChange={(v) => patch(i, { sni: v })} />
  173. ) : null}
  174. </div>
  175. <div className="mt-2 flex justify-end">
  176. <button
  177. type="button"
  178. className={addBtn}
  179. onClick={() => setClients((p) => p.filter((_, j) => j !== i))}
  180. >
  181. Remove
  182. </button>
  183. </div>
  184. </div>
  185. ))}
  186. </div>
  187. <div className="mt-4 grid grid-cols-1 gap-4">
  188. <OutputBlock label="Subscription links (decoded body)" value={buildShareLinks(subClients).join('\n')} />
  189. <OutputBlock label="Base64 body" value={buildBase64Subscription(subClients)} />
  190. <OutputBlock label="JSON subscription (preview)" value={buildJsonSubscription(subClients)} />
  191. </div>
  192. </ToolFrame>
  193. );
  194. }