outbound.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  1. const Protocols = {
  2. Freedom: "freedom",
  3. Blackhole: "blackhole",
  4. DNS: "dns",
  5. VMess: "vmess",
  6. VLESS: "vless",
  7. Trojan: "trojan",
  8. Shadowsocks: "shadowsocks",
  9. Socks: "socks",
  10. HTTP: "http",
  11. Wireguard: "wireguard",
  12. Hysteria: "hysteria"
  13. };
  14. const SSMethods = {
  15. AES_256_GCM: 'aes-256-gcm',
  16. AES_128_GCM: 'aes-128-gcm',
  17. CHACHA20_POLY1305: 'chacha20-poly1305',
  18. CHACHA20_IETF_POLY1305: 'chacha20-ietf-poly1305',
  19. XCHACHA20_POLY1305: 'xchacha20-poly1305',
  20. XCHACHA20_IETF_POLY1305: 'xchacha20-ietf-poly1305',
  21. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  22. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  23. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  24. };
  25. const TLS_FLOW_CONTROL = {
  26. VISION: "xtls-rprx-vision",
  27. VISION_UDP443: "xtls-rprx-vision-udp443",
  28. };
  29. const UTLS_FINGERPRINT = {
  30. UTLS_CHROME: "chrome",
  31. UTLS_FIREFOX: "firefox",
  32. UTLS_SAFARI: "safari",
  33. UTLS_IOS: "ios",
  34. UTLS_android: "android",
  35. UTLS_EDGE: "edge",
  36. UTLS_360: "360",
  37. UTLS_QQ: "qq",
  38. UTLS_RANDOM: "random",
  39. UTLS_RANDOMIZED: "randomized",
  40. UTLS_RONDOMIZEDNOALPN: "randomizednoalpn",
  41. UTLS_UNSAFE: "unsafe",
  42. };
  43. const ALPN_OPTION = {
  44. H3: "h3",
  45. H2: "h2",
  46. HTTP1: "http/1.1",
  47. };
  48. const OutboundDomainStrategies = [
  49. "AsIs",
  50. "UseIP",
  51. "UseIPv4",
  52. "UseIPv6",
  53. "UseIPv6v4",
  54. "UseIPv4v6",
  55. "ForceIP",
  56. "ForceIPv6v4",
  57. "ForceIPv6",
  58. "ForceIPv4v6",
  59. "ForceIPv4"
  60. ];
  61. const WireguardDomainStrategy = [
  62. "ForceIP",
  63. "ForceIPv4",
  64. "ForceIPv4v6",
  65. "ForceIPv6",
  66. "ForceIPv6v4"
  67. ];
  68. const USERS_SECURITY = {
  69. AES_128_GCM: "aes-128-gcm",
  70. CHACHA20_POLY1305: "chacha20-poly1305",
  71. AUTO: "auto",
  72. NONE: "none",
  73. ZERO: "zero",
  74. };
  75. const MODE_OPTION = {
  76. AUTO: "auto",
  77. PACKET_UP: "packet-up",
  78. STREAM_UP: "stream-up",
  79. STREAM_ONE: "stream-one",
  80. };
  81. const Address_Port_Strategy = {
  82. NONE: "none",
  83. SrvPortOnly: "srvportonly",
  84. SrvAddressOnly: "srvaddressonly",
  85. SrvPortAndAddress: "srvportandaddress",
  86. TxtPortOnly: "txtportonly",
  87. TxtAddressOnly: "txtaddressonly",
  88. TxtPortAndAddress: "txtportandaddress"
  89. };
  90. Object.freeze(Protocols);
  91. Object.freeze(SSMethods);
  92. Object.freeze(TLS_FLOW_CONTROL);
  93. Object.freeze(UTLS_FINGERPRINT);
  94. Object.freeze(ALPN_OPTION);
  95. Object.freeze(OutboundDomainStrategies);
  96. Object.freeze(WireguardDomainStrategy);
  97. Object.freeze(USERS_SECURITY);
  98. Object.freeze(MODE_OPTION);
  99. Object.freeze(Address_Port_Strategy);
  100. class CommonClass {
  101. static toJsonArray(arr) {
  102. return arr.map(obj => obj.toJson());
  103. }
  104. static fromJson() {
  105. return new CommonClass();
  106. }
  107. toJson() {
  108. return this;
  109. }
  110. toString(format = true) {
  111. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  112. }
  113. }
  114. class TcpStreamSettings extends CommonClass {
  115. constructor(type = 'none', host, path) {
  116. super();
  117. this.type = type;
  118. this.host = host;
  119. this.path = path;
  120. }
  121. static fromJson(json = {}) {
  122. let header = json.header;
  123. if (!header) return new TcpStreamSettings();
  124. if (header.type == 'http' && header.request) {
  125. return new TcpStreamSettings(
  126. header.type,
  127. header.request.headers.Host.join(','),
  128. header.request.path.join(','),
  129. );
  130. }
  131. return new TcpStreamSettings(header.type, '', '');
  132. }
  133. toJson() {
  134. return {
  135. header: {
  136. type: this.type,
  137. request: this.type === 'http' ? {
  138. headers: {
  139. Host: ObjectUtil.isEmpty(this.host) ? [] : this.host.split(',')
  140. },
  141. path: ObjectUtil.isEmpty(this.path) ? ["/"] : this.path.split(',')
  142. } : undefined,
  143. }
  144. };
  145. }
  146. }
  147. class KcpStreamSettings extends CommonClass {
  148. constructor(
  149. mtu = 1250,
  150. tti = 50,
  151. uplinkCapacity = 5,
  152. downlinkCapacity = 20,
  153. congestion = false,
  154. readBufferSize = 2,
  155. writeBufferSize = 2,
  156. type = 'none',
  157. seed = '',
  158. ) {
  159. super();
  160. this.mtu = mtu;
  161. this.tti = tti;
  162. this.upCap = uplinkCapacity;
  163. this.downCap = downlinkCapacity;
  164. this.congestion = congestion;
  165. this.readBuffer = readBufferSize;
  166. this.writeBuffer = writeBufferSize;
  167. this.type = type;
  168. this.seed = seed;
  169. }
  170. static fromJson(json = {}) {
  171. return new KcpStreamSettings(
  172. json.mtu,
  173. json.tti,
  174. json.uplinkCapacity,
  175. json.downlinkCapacity,
  176. json.congestion,
  177. json.readBufferSize,
  178. json.writeBufferSize,
  179. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  180. json.seed,
  181. );
  182. }
  183. toJson() {
  184. return {
  185. mtu: this.mtu,
  186. tti: this.tti,
  187. uplinkCapacity: this.upCap,
  188. downlinkCapacity: this.downCap,
  189. congestion: this.congestion,
  190. readBufferSize: this.readBuffer,
  191. writeBufferSize: this.writeBuffer,
  192. header: {
  193. type: this.type,
  194. },
  195. seed: this.seed,
  196. };
  197. }
  198. }
  199. class WsStreamSettings extends CommonClass {
  200. constructor(
  201. path = '/',
  202. host = '',
  203. heartbeatPeriod = 0,
  204. ) {
  205. super();
  206. this.path = path;
  207. this.host = host;
  208. this.heartbeatPeriod = heartbeatPeriod;
  209. }
  210. static fromJson(json = {}) {
  211. return new WsStreamSettings(
  212. json.path,
  213. json.host,
  214. json.heartbeatPeriod,
  215. );
  216. }
  217. toJson() {
  218. return {
  219. path: this.path,
  220. host: this.host,
  221. heartbeatPeriod: this.heartbeatPeriod
  222. };
  223. }
  224. }
  225. class GrpcStreamSettings extends CommonClass {
  226. constructor(
  227. serviceName = "",
  228. authority = "",
  229. multiMode = false
  230. ) {
  231. super();
  232. this.serviceName = serviceName;
  233. this.authority = authority;
  234. this.multiMode = multiMode;
  235. }
  236. static fromJson(json = {}) {
  237. return new GrpcStreamSettings(json.serviceName, json.authority, json.multiMode);
  238. }
  239. toJson() {
  240. return {
  241. serviceName: this.serviceName,
  242. authority: this.authority,
  243. multiMode: this.multiMode
  244. }
  245. }
  246. }
  247. class HttpUpgradeStreamSettings extends CommonClass {
  248. constructor(path = '/', host = '') {
  249. super();
  250. this.path = path;
  251. this.host = host;
  252. }
  253. static fromJson(json = {}) {
  254. return new HttpUpgradeStreamSettings(
  255. json.path,
  256. json.host,
  257. );
  258. }
  259. toJson() {
  260. return {
  261. path: this.path,
  262. host: this.host,
  263. };
  264. }
  265. }
  266. class xHTTPStreamSettings extends CommonClass {
  267. constructor(
  268. path = '/',
  269. host = '',
  270. mode = '',
  271. noGRPCHeader = false,
  272. scMinPostsIntervalMs = "30",
  273. xmux = {
  274. maxConcurrency: "16-32",
  275. maxConnections: 0,
  276. cMaxReuseTimes: 0,
  277. hMaxRequestTimes: "600-900",
  278. hMaxReusableSecs: "1800-3000",
  279. hKeepAlivePeriod: 0,
  280. },
  281. ) {
  282. super();
  283. this.path = path;
  284. this.host = host;
  285. this.mode = mode;
  286. this.noGRPCHeader = noGRPCHeader;
  287. this.scMinPostsIntervalMs = scMinPostsIntervalMs;
  288. this.xmux = xmux;
  289. }
  290. static fromJson(json = {}) {
  291. return new xHTTPStreamSettings(
  292. json.path,
  293. json.host,
  294. json.mode,
  295. json.noGRPCHeader,
  296. json.scMinPostsIntervalMs,
  297. json.xmux
  298. );
  299. }
  300. toJson() {
  301. return {
  302. path: this.path,
  303. host: this.host,
  304. mode: this.mode,
  305. noGRPCHeader: this.noGRPCHeader,
  306. scMinPostsIntervalMs: this.scMinPostsIntervalMs,
  307. xmux: {
  308. maxConcurrency: this.xmux.maxConcurrency,
  309. maxConnections: this.xmux.maxConnections,
  310. cMaxReuseTimes: this.xmux.cMaxReuseTimes,
  311. hMaxRequestTimes: this.xmux.hMaxRequestTimes,
  312. hMaxReusableSecs: this.xmux.hMaxReusableSecs,
  313. hKeepAlivePeriod: this.xmux.hKeepAlivePeriod,
  314. },
  315. };
  316. }
  317. }
  318. class TlsStreamSettings extends CommonClass {
  319. constructor(
  320. serverName = '',
  321. alpn = [],
  322. fingerprint = '',
  323. allowInsecure = false,
  324. echConfigList = '',
  325. ) {
  326. super();
  327. this.serverName = serverName;
  328. this.alpn = alpn;
  329. this.fingerprint = fingerprint;
  330. this.allowInsecure = allowInsecure;
  331. this.echConfigList = echConfigList;
  332. }
  333. static fromJson(json = {}) {
  334. return new TlsStreamSettings(
  335. json.serverName,
  336. json.alpn,
  337. json.fingerprint,
  338. json.allowInsecure,
  339. json.echConfigList,
  340. );
  341. }
  342. toJson() {
  343. return {
  344. serverName: this.serverName,
  345. alpn: this.alpn,
  346. fingerprint: this.fingerprint,
  347. allowInsecure: this.allowInsecure,
  348. echConfigList: this.echConfigList
  349. };
  350. }
  351. }
  352. class RealityStreamSettings extends CommonClass {
  353. constructor(
  354. publicKey = '',
  355. fingerprint = '',
  356. serverName = '',
  357. shortId = '',
  358. spiderX = '',
  359. mldsa65Verify = ''
  360. ) {
  361. super();
  362. this.publicKey = publicKey;
  363. this.fingerprint = fingerprint;
  364. this.serverName = serverName;
  365. this.shortId = shortId
  366. this.spiderX = spiderX;
  367. this.mldsa65Verify = mldsa65Verify;
  368. }
  369. static fromJson(json = {}) {
  370. return new RealityStreamSettings(
  371. json.publicKey,
  372. json.fingerprint,
  373. json.serverName,
  374. json.shortId,
  375. json.spiderX,
  376. json.mldsa65Verify
  377. );
  378. }
  379. toJson() {
  380. return {
  381. publicKey: this.publicKey,
  382. fingerprint: this.fingerprint,
  383. serverName: this.serverName,
  384. shortId: this.shortId,
  385. spiderX: this.spiderX,
  386. mldsa65Verify: this.mldsa65Verify
  387. };
  388. }
  389. };
  390. class HysteriaStreamSettings extends CommonClass {
  391. constructor(
  392. version = 2,
  393. auth = '',
  394. congestion = '',
  395. up = '0',
  396. down = '0',
  397. udphopPort = '',
  398. udphopInterval = 30,
  399. initStreamReceiveWindow = 8388608,
  400. maxStreamReceiveWindow = 8388608,
  401. initConnectionReceiveWindow = 20971520,
  402. maxConnectionReceiveWindow = 20971520,
  403. maxIdleTimeout = 30,
  404. keepAlivePeriod = 0,
  405. disablePathMTUDiscovery = false
  406. ) {
  407. super();
  408. this.version = version;
  409. this.auth = auth;
  410. this.congestion = congestion;
  411. this.up = up;
  412. this.down = down;
  413. this.udphopPort = udphopPort;
  414. this.udphopInterval = udphopInterval;
  415. this.initStreamReceiveWindow = initStreamReceiveWindow;
  416. this.maxStreamReceiveWindow = maxStreamReceiveWindow;
  417. this.initConnectionReceiveWindow = initConnectionReceiveWindow;
  418. this.maxConnectionReceiveWindow = maxConnectionReceiveWindow;
  419. this.maxIdleTimeout = maxIdleTimeout;
  420. this.keepAlivePeriod = keepAlivePeriod;
  421. this.disablePathMTUDiscovery = disablePathMTUDiscovery;
  422. }
  423. static fromJson(json = {}) {
  424. let udphopPort = '';
  425. let udphopInterval = 30;
  426. if (json.udphop) {
  427. udphopPort = json.udphop.port || '';
  428. udphopInterval = json.udphop.interval || 30;
  429. }
  430. return new HysteriaStreamSettings(
  431. json.version,
  432. json.auth,
  433. json.congestion,
  434. json.up,
  435. json.down,
  436. udphopPort,
  437. udphopInterval,
  438. json.initStreamReceiveWindow,
  439. json.maxStreamReceiveWindow,
  440. json.initConnectionReceiveWindow,
  441. json.maxConnectionReceiveWindow,
  442. json.maxIdleTimeout,
  443. json.keepAlivePeriod,
  444. json.disablePathMTUDiscovery
  445. );
  446. }
  447. toJson() {
  448. const result = {
  449. version: this.version,
  450. auth: this.auth,
  451. congestion: this.congestion,
  452. up: this.up,
  453. down: this.down,
  454. initStreamReceiveWindow: this.initStreamReceiveWindow,
  455. maxStreamReceiveWindow: this.maxStreamReceiveWindow,
  456. initConnectionReceiveWindow: this.initConnectionReceiveWindow,
  457. maxConnectionReceiveWindow: this.maxConnectionReceiveWindow,
  458. maxIdleTimeout: this.maxIdleTimeout,
  459. keepAlivePeriod: this.keepAlivePeriod,
  460. disablePathMTUDiscovery: this.disablePathMTUDiscovery
  461. };
  462. if (this.udphopPort) {
  463. result.udphop = {
  464. port: this.udphopPort,
  465. interval: this.udphopInterval
  466. };
  467. }
  468. return result;
  469. }
  470. };
  471. class SockoptStreamSettings extends CommonClass {
  472. constructor(
  473. dialerProxy = "",
  474. tcpFastOpen = false,
  475. tcpKeepAliveInterval = 0,
  476. tcpMptcp = false,
  477. penetrate = false,
  478. addressPortStrategy = Address_Port_Strategy.NONE,
  479. trustedXForwardedFor = [],
  480. ) {
  481. super();
  482. this.dialerProxy = dialerProxy;
  483. this.tcpFastOpen = tcpFastOpen;
  484. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  485. this.tcpMptcp = tcpMptcp;
  486. this.penetrate = penetrate;
  487. this.addressPortStrategy = addressPortStrategy;
  488. this.trustedXForwardedFor = trustedXForwardedFor;
  489. }
  490. static fromJson(json = {}) {
  491. if (Object.keys(json).length === 0) return undefined;
  492. return new SockoptStreamSettings(
  493. json.dialerProxy,
  494. json.tcpFastOpen,
  495. json.tcpKeepAliveInterval,
  496. json.tcpMptcp,
  497. json.penetrate,
  498. json.addressPortStrategy,
  499. json.trustedXForwardedFor || []
  500. );
  501. }
  502. toJson() {
  503. const result = {
  504. dialerProxy: this.dialerProxy,
  505. tcpFastOpen: this.tcpFastOpen,
  506. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  507. tcpMptcp: this.tcpMptcp,
  508. penetrate: this.penetrate,
  509. addressPortStrategy: this.addressPortStrategy
  510. };
  511. if (this.trustedXForwardedFor && this.trustedXForwardedFor.length > 0) {
  512. result.trustedXForwardedFor = this.trustedXForwardedFor;
  513. }
  514. return result;
  515. }
  516. }
  517. class UdpMask extends CommonClass {
  518. constructor(type = 'salamander', password = '') {
  519. super();
  520. this.type = type;
  521. this.password = password;
  522. }
  523. static fromJson(json = {}) {
  524. return new UdpMask(
  525. json.type,
  526. json.settings?.password || ''
  527. );
  528. }
  529. toJson() {
  530. return {
  531. type: this.type,
  532. settings: {
  533. password: this.password
  534. }
  535. };
  536. }
  537. }
  538. class StreamSettings extends CommonClass {
  539. constructor(
  540. network = 'tcp',
  541. security = 'none',
  542. tlsSettings = new TlsStreamSettings(),
  543. realitySettings = new RealityStreamSettings(),
  544. tcpSettings = new TcpStreamSettings(),
  545. kcpSettings = new KcpStreamSettings(),
  546. wsSettings = new WsStreamSettings(),
  547. grpcSettings = new GrpcStreamSettings(),
  548. httpupgradeSettings = new HttpUpgradeStreamSettings(),
  549. xhttpSettings = new xHTTPStreamSettings(),
  550. hysteriaSettings = new HysteriaStreamSettings(),
  551. udpmasks = [],
  552. sockopt = undefined,
  553. ) {
  554. super();
  555. this.network = network;
  556. this.security = security;
  557. this.tls = tlsSettings;
  558. this.reality = realitySettings;
  559. this.tcp = tcpSettings;
  560. this.kcp = kcpSettings;
  561. this.ws = wsSettings;
  562. this.grpc = grpcSettings;
  563. this.httpupgrade = httpupgradeSettings;
  564. this.xhttp = xhttpSettings;
  565. this.hysteria = hysteriaSettings;
  566. this.udpmasks = udpmasks;
  567. this.sockopt = sockopt;
  568. }
  569. addUdpMask() {
  570. this.udpmasks.push(new UdpMask());
  571. }
  572. delUdpMask(index) {
  573. this.udpmasks.splice(index, 1);
  574. }
  575. get isTls() {
  576. return this.security === 'tls';
  577. }
  578. get isReality() {
  579. return this.security === "reality";
  580. }
  581. get sockoptSwitch() {
  582. return this.sockopt != undefined;
  583. }
  584. set sockoptSwitch(value) {
  585. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  586. }
  587. static fromJson(json = {}) {
  588. const udpmasks = json.udpmasks ? json.udpmasks.map(mask => UdpMask.fromJson(mask)) : [];
  589. return new StreamSettings(
  590. json.network,
  591. json.security,
  592. TlsStreamSettings.fromJson(json.tlsSettings),
  593. RealityStreamSettings.fromJson(json.realitySettings),
  594. TcpStreamSettings.fromJson(json.tcpSettings),
  595. KcpStreamSettings.fromJson(json.kcpSettings),
  596. WsStreamSettings.fromJson(json.wsSettings),
  597. GrpcStreamSettings.fromJson(json.grpcSettings),
  598. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  599. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  600. HysteriaStreamSettings.fromJson(json.hysteriaSettings),
  601. udpmasks,
  602. SockoptStreamSettings.fromJson(json.sockopt),
  603. );
  604. }
  605. toJson() {
  606. const network = this.network;
  607. return {
  608. network: network,
  609. security: this.security,
  610. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  611. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  612. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  613. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  614. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  615. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  616. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  617. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  618. hysteriaSettings: network === 'hysteria' ? this.hysteria.toJson() : undefined,
  619. udpmasks: this.udpmasks.length > 0 ? this.udpmasks.map(mask => mask.toJson()) : undefined,
  620. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  621. };
  622. }
  623. }
  624. class Mux extends CommonClass {
  625. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  626. super();
  627. this.enabled = enabled;
  628. this.concurrency = concurrency;
  629. this.xudpConcurrency = xudpConcurrency;
  630. this.xudpProxyUDP443 = xudpProxyUDP443;
  631. }
  632. static fromJson(json = {}) {
  633. if (Object.keys(json).length === 0) return undefined;
  634. return new Mux(
  635. json.enabled,
  636. json.concurrency,
  637. json.xudpConcurrency,
  638. json.xudpProxyUDP443,
  639. );
  640. }
  641. toJson() {
  642. return {
  643. enabled: this.enabled,
  644. concurrency: this.concurrency,
  645. xudpConcurrency: this.xudpConcurrency,
  646. xudpProxyUDP443: this.xudpProxyUDP443,
  647. };
  648. }
  649. }
  650. class Outbound extends CommonClass {
  651. constructor(
  652. tag = '',
  653. protocol = Protocols.VLESS,
  654. settings = null,
  655. streamSettings = new StreamSettings(),
  656. sendThrough,
  657. mux = new Mux(),
  658. ) {
  659. super();
  660. this.tag = tag;
  661. this._protocol = protocol;
  662. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  663. this.stream = streamSettings;
  664. this.sendThrough = sendThrough;
  665. this.mux = mux;
  666. }
  667. get protocol() {
  668. return this._protocol;
  669. }
  670. set protocol(protocol) {
  671. this._protocol = protocol;
  672. this.settings = Outbound.Settings.getSettings(protocol);
  673. this.stream = new StreamSettings();
  674. }
  675. canEnableTls() {
  676. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.Hysteria].includes(this.protocol)) return false;
  677. if (this.protocol === Protocols.Hysteria) return this.stream.network === 'hysteria';
  678. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.stream.network);
  679. }
  680. //this is used for xtls-rprx-vision
  681. canEnableTlsFlow() {
  682. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  683. return this.protocol === Protocols.VLESS;
  684. }
  685. return false;
  686. }
  687. // Vision seed applies only when vision flow is selected
  688. canEnableVisionSeed() {
  689. if (!this.canEnableTlsFlow()) return false;
  690. const flow = this.settings?.flow;
  691. return flow === TLS_FLOW_CONTROL.VISION || flow === TLS_FLOW_CONTROL.VISION_UDP443;
  692. }
  693. canEnableReality() {
  694. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  695. return ["tcp", "http", "grpc", "xhttp"].includes(this.stream.network);
  696. }
  697. canEnableStream() {
  698. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks, Protocols.Hysteria].includes(this.protocol);
  699. }
  700. canEnableMux() {
  701. // Disable Mux if flow is set
  702. if (this.settings.flow && this.settings.flow !== '') {
  703. this.mux.enabled = false;
  704. return false;
  705. }
  706. // Disable Mux if network is xhttp
  707. if (this.stream.network === 'xhttp') {
  708. this.mux.enabled = false;
  709. return false;
  710. }
  711. // Allow Mux only for these protocols
  712. return [
  713. Protocols.VMess,
  714. Protocols.VLESS,
  715. Protocols.Trojan,
  716. Protocols.Shadowsocks,
  717. Protocols.HTTP,
  718. Protocols.Socks
  719. ].includes(this.protocol);
  720. }
  721. hasServers() {
  722. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  723. }
  724. hasAddressPort() {
  725. return [
  726. Protocols.DNS,
  727. Protocols.VMess,
  728. Protocols.VLESS,
  729. Protocols.Trojan,
  730. Protocols.Shadowsocks,
  731. Protocols.Socks,
  732. Protocols.HTTP,
  733. Protocols.Hysteria
  734. ].includes(this.protocol);
  735. }
  736. hasUsername() {
  737. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  738. }
  739. static fromJson(json = {}) {
  740. return new Outbound(
  741. json.tag,
  742. json.protocol,
  743. Outbound.Settings.fromJson(json.protocol, json.settings),
  744. StreamSettings.fromJson(json.streamSettings),
  745. json.sendThrough,
  746. Mux.fromJson(json.mux),
  747. )
  748. }
  749. toJson() {
  750. var stream;
  751. if (this.canEnableStream()) {
  752. stream = this.stream.toJson();
  753. } else {
  754. if (this.stream?.sockopt)
  755. stream = { sockopt: this.stream.sockopt.toJson() };
  756. }
  757. let settingsOut = this.settings instanceof CommonClass ? this.settings.toJson() : this.settings;
  758. return {
  759. protocol: this.protocol,
  760. settings: settingsOut,
  761. // Only include tag, streamSettings, sendThrough, mux if present and not empty
  762. ...(this.tag ? { tag: this.tag } : {}),
  763. ...(stream ? { streamSettings: stream } : {}),
  764. ...(this.sendThrough ? { sendThrough: this.sendThrough } : {}),
  765. ...(this.mux?.enabled ? { mux: this.mux } : {}),
  766. };
  767. }
  768. static fromLink(link) {
  769. data = link.split('://');
  770. if (data.length != 2) return null;
  771. switch (data[0].toLowerCase()) {
  772. case Protocols.VMess:
  773. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  774. case Protocols.VLESS:
  775. case Protocols.Trojan:
  776. case 'ss':
  777. return this.fromParamLink(link);
  778. case 'hysteria2':
  779. case Protocols.Hysteria:
  780. return this.fromHysteriaLink(link);
  781. default:
  782. return null;
  783. }
  784. }
  785. static fromVmessLink(json = {}) {
  786. let stream = new StreamSettings(json.net, json.tls);
  787. let network = json.net;
  788. if (network === 'tcp') {
  789. stream.tcp = new TcpStreamSettings(
  790. json.type,
  791. json.host ?? '',
  792. json.path ?? '');
  793. } else if (network === 'kcp') {
  794. stream.kcp = new KcpStreamSettings();
  795. stream.type = json.type;
  796. stream.seed = json.path;
  797. } else if (network === 'ws') {
  798. stream.ws = new WsStreamSettings(json.path, json.host);
  799. } else if (network === 'grpc') {
  800. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  801. } else if (network === 'httpupgrade') {
  802. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  803. } else if (network === 'xhttp') {
  804. stream.xhttp = new xHTTPStreamSettings(json.path, json.host, json.mode);
  805. }
  806. if (json.tls && json.tls == 'tls') {
  807. stream.tls = new TlsStreamSettings(
  808. json.sni,
  809. json.alpn ? json.alpn.split(',') : [],
  810. json.fp,
  811. json.allowInsecure);
  812. }
  813. const port = json.port * 1;
  814. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id, json.scy), stream);
  815. }
  816. static fromParamLink(link) {
  817. const url = new URL(link);
  818. let type = url.searchParams.get('type') ?? 'tcp';
  819. let security = url.searchParams.get('security') ?? 'none';
  820. let stream = new StreamSettings(type, security);
  821. let headerType = url.searchParams.get('headerType') ?? undefined;
  822. let host = url.searchParams.get('host') ?? undefined;
  823. let path = url.searchParams.get('path') ?? undefined;
  824. let mode = url.searchParams.get('mode') ?? undefined;
  825. if (type === 'tcp' || type === 'none') {
  826. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  827. } else if (type === 'kcp') {
  828. stream.kcp = new KcpStreamSettings();
  829. stream.kcp.type = headerType ?? 'none';
  830. stream.kcp.seed = path;
  831. } else if (type === 'ws') {
  832. stream.ws = new WsStreamSettings(path, host);
  833. } else if (type === 'grpc') {
  834. stream.grpc = new GrpcStreamSettings(
  835. url.searchParams.get('serviceName') ?? '',
  836. url.searchParams.get('authority') ?? '',
  837. url.searchParams.get('mode') == 'multi');
  838. } else if (type === 'httpupgrade') {
  839. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  840. } else if (type === 'xhttp') {
  841. stream.xhttp = new xHTTPStreamSettings(path, host, mode);
  842. }
  843. if (security == 'tls') {
  844. let fp = url.searchParams.get('fp') ?? 'none';
  845. let alpn = url.searchParams.get('alpn');
  846. let allowInsecure = url.searchParams.get('allowInsecure');
  847. let sni = url.searchParams.get('sni') ?? '';
  848. let ech = url.searchParams.get('ech') ?? '';
  849. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1, ech);
  850. }
  851. if (security == 'reality') {
  852. let pbk = url.searchParams.get('pbk');
  853. let fp = url.searchParams.get('fp');
  854. let sni = url.searchParams.get('sni') ?? '';
  855. let sid = url.searchParams.get('sid') ?? '';
  856. let spx = url.searchParams.get('spx') ?? '';
  857. let pqv = url.searchParams.get('pqv') ?? '';
  858. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx, pqv);
  859. }
  860. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  861. const match = link.match(regex);
  862. if (!match) return null;
  863. let [, protocol, userData, address, port,] = match;
  864. port *= 1;
  865. if (protocol == 'ss') {
  866. protocol = 'shadowsocks';
  867. userData = atob(userData).split(':');
  868. }
  869. var settings;
  870. switch (protocol) {
  871. case Protocols.VLESS:
  872. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '', url.searchParams.get('encryption') ?? 'none');
  873. break;
  874. case Protocols.Trojan:
  875. settings = new Outbound.TrojanSettings(address, port, userData);
  876. break;
  877. case Protocols.Shadowsocks:
  878. let method = userData.splice(0, 1)[0];
  879. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  880. break;
  881. default:
  882. return null;
  883. }
  884. let remark = decodeURIComponent(url.hash);
  885. // Remove '#' from url.hash
  886. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  887. return new Outbound(remark, protocol, settings, stream);
  888. }
  889. static fromHysteriaLink(link) {
  890. // Parse hysteria2://password@address:port[?param1=value1&param2=value2...][#remarks]
  891. const regex = /^hysteria2?:\/\/([^@]+)@([^:?#]+):(\d+)([^#]*)(#.*)?$/;
  892. const match = link.match(regex);
  893. if (!match) return null;
  894. let [, password, address, port, params, hash] = match;
  895. port = parseInt(port);
  896. // Parse URL parameters if present
  897. let urlParams = new URLSearchParams(params);
  898. // Create stream settings with hysteria network
  899. let stream = new StreamSettings('hysteria', 'none');
  900. // Set hysteria stream settings
  901. stream.hysteria.auth = password;
  902. stream.hysteria.congestion = urlParams.get('congestion') ?? '';
  903. stream.hysteria.up = urlParams.get('up') ?? '0';
  904. stream.hysteria.down = urlParams.get('down') ?? '0';
  905. stream.hysteria.udphopPort = urlParams.get('udphopPort') ?? '';
  906. stream.hysteria.udphopInterval = parseInt(urlParams.get('udphopInterval') ?? '30');
  907. // Optional QUIC parameters
  908. if (urlParams.has('initStreamReceiveWindow')) {
  909. stream.hysteria.initStreamReceiveWindow = parseInt(urlParams.get('initStreamReceiveWindow'));
  910. }
  911. if (urlParams.has('maxStreamReceiveWindow')) {
  912. stream.hysteria.maxStreamReceiveWindow = parseInt(urlParams.get('maxStreamReceiveWindow'));
  913. }
  914. if (urlParams.has('initConnectionReceiveWindow')) {
  915. stream.hysteria.initConnectionReceiveWindow = parseInt(urlParams.get('initConnectionReceiveWindow'));
  916. }
  917. if (urlParams.has('maxConnectionReceiveWindow')) {
  918. stream.hysteria.maxConnectionReceiveWindow = parseInt(urlParams.get('maxConnectionReceiveWindow'));
  919. }
  920. if (urlParams.has('maxIdleTimeout')) {
  921. stream.hysteria.maxIdleTimeout = parseInt(urlParams.get('maxIdleTimeout'));
  922. }
  923. if (urlParams.has('keepAlivePeriod')) {
  924. stream.hysteria.keepAlivePeriod = parseInt(urlParams.get('keepAlivePeriod'));
  925. }
  926. if (urlParams.has('disablePathMTUDiscovery')) {
  927. stream.hysteria.disablePathMTUDiscovery = urlParams.get('disablePathMTUDiscovery') === 'true';
  928. }
  929. // Create settings
  930. let settings = new Outbound.HysteriaSettings(address, port, 2);
  931. // Extract remark from hash
  932. let remark = hash ? decodeURIComponent(hash.substring(1)) : `out-hysteria-${port}`;
  933. return new Outbound(remark, Protocols.Hysteria, settings, stream);
  934. }
  935. }
  936. Outbound.Settings = class extends CommonClass {
  937. constructor(protocol) {
  938. super();
  939. this.protocol = protocol;
  940. }
  941. static getSettings(protocol) {
  942. switch (protocol) {
  943. case Protocols.Freedom: return new Outbound.FreedomSettings();
  944. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  945. case Protocols.DNS: return new Outbound.DNSSettings();
  946. case Protocols.VMess: return new Outbound.VmessSettings();
  947. case Protocols.VLESS: return new Outbound.VLESSSettings();
  948. case Protocols.Trojan: return new Outbound.TrojanSettings();
  949. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  950. case Protocols.Socks: return new Outbound.SocksSettings();
  951. case Protocols.HTTP: return new Outbound.HttpSettings();
  952. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  953. case Protocols.Hysteria: return new Outbound.HysteriaSettings();
  954. default: return null;
  955. }
  956. }
  957. static fromJson(protocol, json) {
  958. switch (protocol) {
  959. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  960. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  961. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  962. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  963. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  964. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  965. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  966. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  967. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  968. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  969. case Protocols.Hysteria: return Outbound.HysteriaSettings.fromJson(json);
  970. default: return null;
  971. }
  972. }
  973. toJson() {
  974. return {};
  975. }
  976. };
  977. Outbound.FreedomSettings = class extends CommonClass {
  978. constructor(
  979. domainStrategy = '',
  980. redirect = '',
  981. fragment = {},
  982. noises = []
  983. ) {
  984. super();
  985. this.domainStrategy = domainStrategy;
  986. this.redirect = redirect;
  987. this.fragment = fragment;
  988. this.noises = noises;
  989. }
  990. addNoise() {
  991. this.noises.push(new Outbound.FreedomSettings.Noise());
  992. }
  993. delNoise(index) {
  994. this.noises.splice(index, 1);
  995. }
  996. static fromJson(json = {}) {
  997. return new Outbound.FreedomSettings(
  998. json.domainStrategy,
  999. json.redirect,
  1000. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  1001. json.noises ? json.noises.map(noise => Outbound.FreedomSettings.Noise.fromJson(noise)) : undefined,
  1002. );
  1003. }
  1004. toJson() {
  1005. return {
  1006. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  1007. redirect: ObjectUtil.isEmpty(this.redirect) ? undefined : this.redirect,
  1008. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  1009. noises: this.noises.length === 0 ? undefined : Outbound.FreedomSettings.Noise.toJsonArray(this.noises),
  1010. };
  1011. }
  1012. };
  1013. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  1014. constructor(
  1015. packets = '1-3',
  1016. length = '',
  1017. interval = '',
  1018. maxSplit = ''
  1019. ) {
  1020. super();
  1021. this.packets = packets;
  1022. this.length = length;
  1023. this.interval = interval;
  1024. this.maxSplit = maxSplit;
  1025. }
  1026. static fromJson(json = {}) {
  1027. return new Outbound.FreedomSettings.Fragment(
  1028. json.packets,
  1029. json.length,
  1030. json.interval,
  1031. json.maxSplit
  1032. );
  1033. }
  1034. };
  1035. Outbound.FreedomSettings.Noise = class extends CommonClass {
  1036. constructor(
  1037. type = 'rand',
  1038. packet = '10-20',
  1039. delay = '10-16',
  1040. applyTo = 'ip'
  1041. ) {
  1042. super();
  1043. this.type = type;
  1044. this.packet = packet;
  1045. this.delay = delay;
  1046. this.applyTo = applyTo;
  1047. }
  1048. static fromJson(json = {}) {
  1049. return new Outbound.FreedomSettings.Noise(
  1050. json.type,
  1051. json.packet,
  1052. json.delay,
  1053. json.applyTo
  1054. );
  1055. }
  1056. toJson() {
  1057. return {
  1058. type: this.type,
  1059. packet: this.packet,
  1060. delay: this.delay,
  1061. applyTo: this.applyTo
  1062. };
  1063. }
  1064. };
  1065. Outbound.BlackholeSettings = class extends CommonClass {
  1066. constructor(type) {
  1067. super();
  1068. this.type = type;
  1069. }
  1070. static fromJson(json = {}) {
  1071. return new Outbound.BlackholeSettings(
  1072. json.response ? json.response.type : undefined,
  1073. );
  1074. }
  1075. toJson() {
  1076. return {
  1077. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  1078. };
  1079. }
  1080. };
  1081. Outbound.DNSSettings = class extends CommonClass {
  1082. constructor(
  1083. network = 'udp',
  1084. address = '',
  1085. port = 53,
  1086. nonIPQuery = 'reject',
  1087. blockTypes = []
  1088. ) {
  1089. super();
  1090. this.network = network;
  1091. this.address = address;
  1092. this.port = port;
  1093. this.nonIPQuery = nonIPQuery;
  1094. this.blockTypes = blockTypes;
  1095. }
  1096. static fromJson(json = {}) {
  1097. return new Outbound.DNSSettings(
  1098. json.network,
  1099. json.address,
  1100. json.port,
  1101. json.nonIPQuery,
  1102. json.blockTypes,
  1103. );
  1104. }
  1105. };
  1106. Outbound.VmessSettings = class extends CommonClass {
  1107. constructor(address, port, id, security) {
  1108. super();
  1109. this.address = address;
  1110. this.port = port;
  1111. this.id = id;
  1112. this.security = security;
  1113. }
  1114. static fromJson(json = {}) {
  1115. if (!ObjectUtil.isArrEmpty(json.vnext)) {
  1116. const v = json.vnext[0] || {};
  1117. const u = ObjectUtil.isArrEmpty(v.users) ? {} : v.users[0];
  1118. return new Outbound.VmessSettings(
  1119. v.address,
  1120. v.port,
  1121. u.id,
  1122. u.security,
  1123. );
  1124. }
  1125. }
  1126. toJson() {
  1127. return {
  1128. vnext: [{
  1129. address: this.address,
  1130. port: this.port,
  1131. users: [{
  1132. id: this.id,
  1133. security: this.security
  1134. }]
  1135. }]
  1136. };
  1137. }
  1138. };
  1139. Outbound.VLESSSettings = class extends CommonClass {
  1140. constructor(address, port, id, flow, encryption, testpre = 0, testseed = [900, 500, 900, 256]) {
  1141. super();
  1142. this.address = address;
  1143. this.port = port;
  1144. this.id = id;
  1145. this.flow = flow;
  1146. this.encryption = encryption;
  1147. this.testpre = testpre;
  1148. this.testseed = testseed;
  1149. }
  1150. static fromJson(json = {}) {
  1151. if (ObjectUtil.isEmpty(json.address) || ObjectUtil.isEmpty(json.port)) return new Outbound.VLESSSettings();
  1152. return new Outbound.VLESSSettings(
  1153. json.address,
  1154. json.port,
  1155. json.id,
  1156. json.flow,
  1157. json.encryption,
  1158. json.testpre || 0,
  1159. json.testseed && json.testseed.length >= 4 ? json.testseed : [900, 500, 900, 256]
  1160. );
  1161. }
  1162. toJson() {
  1163. const result = {
  1164. address: this.address,
  1165. port: this.port,
  1166. id: this.id,
  1167. flow: this.flow,
  1168. encryption: this.encryption,
  1169. };
  1170. if (this.testpre > 0) {
  1171. result.testpre = this.testpre;
  1172. }
  1173. if (this.testseed && this.testseed.length >= 4) {
  1174. result.testseed = this.testseed;
  1175. }
  1176. return result;
  1177. }
  1178. };
  1179. Outbound.TrojanSettings = class extends CommonClass {
  1180. constructor(address, port, password) {
  1181. super();
  1182. this.address = address;
  1183. this.port = port;
  1184. this.password = password;
  1185. }
  1186. static fromJson(json = {}) {
  1187. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  1188. return new Outbound.TrojanSettings(
  1189. json.servers[0].address,
  1190. json.servers[0].port,
  1191. json.servers[0].password,
  1192. );
  1193. }
  1194. toJson() {
  1195. return {
  1196. servers: [{
  1197. address: this.address,
  1198. port: this.port,
  1199. password: this.password,
  1200. }],
  1201. };
  1202. }
  1203. };
  1204. Outbound.ShadowsocksSettings = class extends CommonClass {
  1205. constructor(address, port, password, method, uot, UoTVersion) {
  1206. super();
  1207. this.address = address;
  1208. this.port = port;
  1209. this.password = password;
  1210. this.method = method;
  1211. this.uot = uot;
  1212. this.UoTVersion = UoTVersion;
  1213. }
  1214. static fromJson(json = {}) {
  1215. let servers = json.servers;
  1216. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  1217. return new Outbound.ShadowsocksSettings(
  1218. servers[0].address,
  1219. servers[0].port,
  1220. servers[0].password,
  1221. servers[0].method,
  1222. servers[0].uot,
  1223. servers[0].UoTVersion,
  1224. );
  1225. }
  1226. toJson() {
  1227. return {
  1228. servers: [{
  1229. address: this.address,
  1230. port: this.port,
  1231. password: this.password,
  1232. method: this.method,
  1233. uot: this.uot,
  1234. UoTVersion: this.UoTVersion,
  1235. }],
  1236. };
  1237. }
  1238. };
  1239. Outbound.SocksSettings = class extends CommonClass {
  1240. constructor(address, port, user, pass) {
  1241. super();
  1242. this.address = address;
  1243. this.port = port;
  1244. this.user = user;
  1245. this.pass = pass;
  1246. }
  1247. static fromJson(json = {}) {
  1248. let servers = json.servers;
  1249. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1250. return new Outbound.SocksSettings(
  1251. servers[0].address,
  1252. servers[0].port,
  1253. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1254. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1255. );
  1256. }
  1257. toJson() {
  1258. return {
  1259. servers: [{
  1260. address: this.address,
  1261. port: this.port,
  1262. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1263. }],
  1264. };
  1265. }
  1266. };
  1267. Outbound.HttpSettings = class extends CommonClass {
  1268. constructor(address, port, user, pass) {
  1269. super();
  1270. this.address = address;
  1271. this.port = port;
  1272. this.user = user;
  1273. this.pass = pass;
  1274. }
  1275. static fromJson(json = {}) {
  1276. let servers = json.servers;
  1277. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1278. return new Outbound.HttpSettings(
  1279. servers[0].address,
  1280. servers[0].port,
  1281. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1282. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1283. );
  1284. }
  1285. toJson() {
  1286. return {
  1287. servers: [{
  1288. address: this.address,
  1289. port: this.port,
  1290. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1291. }],
  1292. };
  1293. }
  1294. };
  1295. Outbound.WireguardSettings = class extends CommonClass {
  1296. constructor(
  1297. mtu = 1250,
  1298. secretKey = '',
  1299. address = [''],
  1300. workers = 2,
  1301. domainStrategy = '',
  1302. reserved = '',
  1303. peers = [new Outbound.WireguardSettings.Peer()],
  1304. noKernelTun = false,
  1305. ) {
  1306. super();
  1307. this.mtu = mtu;
  1308. this.secretKey = secretKey;
  1309. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1310. this.address = Array.isArray(address) ? address.join(',') : address;
  1311. this.workers = workers;
  1312. this.domainStrategy = domainStrategy;
  1313. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1314. this.peers = peers;
  1315. this.noKernelTun = noKernelTun;
  1316. }
  1317. addPeer() {
  1318. this.peers.push(new Outbound.WireguardSettings.Peer());
  1319. }
  1320. delPeer(index) {
  1321. this.peers.splice(index, 1);
  1322. }
  1323. static fromJson(json = {}) {
  1324. return new Outbound.WireguardSettings(
  1325. json.mtu,
  1326. json.secretKey,
  1327. json.address,
  1328. json.workers,
  1329. json.domainStrategy,
  1330. json.reserved,
  1331. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1332. json.noKernelTun,
  1333. );
  1334. }
  1335. toJson() {
  1336. return {
  1337. mtu: this.mtu ?? undefined,
  1338. secretKey: this.secretKey,
  1339. address: this.address ? this.address.split(",") : [],
  1340. workers: this.workers ?? undefined,
  1341. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1342. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1343. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1344. noKernelTun: this.noKernelTun,
  1345. };
  1346. }
  1347. };
  1348. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1349. constructor(
  1350. publicKey = '',
  1351. psk = '',
  1352. allowedIPs = ['0.0.0.0/0', '::/0'],
  1353. endpoint = '',
  1354. keepAlive = 0
  1355. ) {
  1356. super();
  1357. this.publicKey = publicKey;
  1358. this.psk = psk;
  1359. this.allowedIPs = allowedIPs;
  1360. this.endpoint = endpoint;
  1361. this.keepAlive = keepAlive;
  1362. }
  1363. static fromJson(json = {}) {
  1364. return new Outbound.WireguardSettings.Peer(
  1365. json.publicKey,
  1366. json.preSharedKey,
  1367. json.allowedIPs,
  1368. json.endpoint,
  1369. json.keepAlive
  1370. );
  1371. }
  1372. toJson() {
  1373. return {
  1374. publicKey: this.publicKey,
  1375. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1376. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1377. endpoint: this.endpoint,
  1378. keepAlive: this.keepAlive ?? undefined,
  1379. };
  1380. }
  1381. };
  1382. Outbound.HysteriaSettings = class extends CommonClass {
  1383. constructor(address = '', port = 443, version = 2) {
  1384. super();
  1385. this.address = address;
  1386. this.port = port;
  1387. this.version = version;
  1388. }
  1389. static fromJson(json = {}) {
  1390. if (Object.keys(json).length === 0) return new Outbound.HysteriaSettings();
  1391. return new Outbound.HysteriaSettings(
  1392. json.address,
  1393. json.port,
  1394. json.version
  1395. );
  1396. }
  1397. toJson() {
  1398. return {
  1399. address: this.address,
  1400. port: this.port,
  1401. version: this.version
  1402. };
  1403. }
  1404. };