outbound.js 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. };
  13. const SSMethods = {
  14. AES_256_GCM: 'aes-256-gcm',
  15. AES_128_GCM: 'aes-128-gcm',
  16. CHACHA20_POLY1305: 'chacha20-poly1305',
  17. CHACHA20_IETF_POLY1305: 'chacha20-ietf-poly1305',
  18. XCHACHA20_POLY1305: 'xchacha20-poly1305',
  19. XCHACHA20_IETF_POLY1305: 'xchacha20-ietf-poly1305',
  20. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  21. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  22. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  23. };
  24. const TLS_FLOW_CONTROL = {
  25. VISION: "xtls-rprx-vision",
  26. VISION_UDP443: "xtls-rprx-vision-udp443",
  27. };
  28. const UTLS_FINGERPRINT = {
  29. UTLS_CHROME: "chrome",
  30. UTLS_FIREFOX: "firefox",
  31. UTLS_SAFARI: "safari",
  32. UTLS_IOS: "ios",
  33. UTLS_android: "android",
  34. UTLS_EDGE: "edge",
  35. UTLS_360: "360",
  36. UTLS_QQ: "qq",
  37. UTLS_RANDOM: "random",
  38. UTLS_RANDOMIZED: "randomized",
  39. };
  40. const ALPN_OPTION = {
  41. H3: "h3",
  42. H2: "h2",
  43. HTTP1: "http/1.1",
  44. };
  45. const outboundDomainStrategies = [
  46. "AsIs",
  47. "UseIP",
  48. "UseIPv4",
  49. "UseIPv6"
  50. ]
  51. const WireguardDomainStrategy = [
  52. "ForceIP",
  53. "ForceIPv4",
  54. "ForceIPv4v6",
  55. "ForceIPv6",
  56. "ForceIPv6v4"
  57. ];
  58. Object.freeze(Protocols);
  59. Object.freeze(SSMethods);
  60. Object.freeze(TLS_FLOW_CONTROL);
  61. Object.freeze(ALPN_OPTION);
  62. Object.freeze(outboundDomainStrategies);
  63. Object.freeze(WireguardDomainStrategy);
  64. class CommonClass {
  65. static toJsonArray(arr) {
  66. return arr.map(obj => obj.toJson());
  67. }
  68. static fromJson() {
  69. return new CommonClass();
  70. }
  71. toJson() {
  72. return this;
  73. }
  74. toString(format=true) {
  75. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  76. }
  77. }
  78. class TcpStreamSettings extends CommonClass {
  79. constructor(type='none', host, path) {
  80. super();
  81. this.type = type;
  82. this.host = host;
  83. this.path = path;
  84. }
  85. static fromJson(json={}) {
  86. let header = json.header;
  87. if (!header) return new TcpStreamSettings();
  88. if(header.type == 'http' && header.request){
  89. return new TcpStreamSettings(
  90. header.type,
  91. header.request.headers.Host.join(','),
  92. header.request.path.join(','),
  93. );
  94. }
  95. return new TcpStreamSettings(header.type,'','');
  96. }
  97. toJson() {
  98. return {
  99. header: {
  100. type: this.type,
  101. request: this.type === 'http' ? {
  102. headers: {
  103. Host: ObjectUtil.isEmpty(this.host) ? [] : this.host.split(',')
  104. },
  105. path: ObjectUtil.isEmpty(this.path) ? ["/"] : this.path.split(',')
  106. } : undefined,
  107. }
  108. };
  109. }
  110. }
  111. class KcpStreamSettings extends CommonClass {
  112. constructor(mtu=1350, tti=20,
  113. uplinkCapacity=5,
  114. downlinkCapacity=20,
  115. congestion=false,
  116. readBufferSize=2,
  117. writeBufferSize=2,
  118. type='none',
  119. seed='',
  120. ) {
  121. super();
  122. this.mtu = mtu;
  123. this.tti = tti;
  124. this.upCap = uplinkCapacity;
  125. this.downCap = downlinkCapacity;
  126. this.congestion = congestion;
  127. this.readBuffer = readBufferSize;
  128. this.writeBuffer = writeBufferSize;
  129. this.type = type;
  130. this.seed = seed;
  131. }
  132. static fromJson(json={}) {
  133. return new KcpStreamSettings(
  134. json.mtu,
  135. json.tti,
  136. json.uplinkCapacity,
  137. json.downlinkCapacity,
  138. json.congestion,
  139. json.readBufferSize,
  140. json.writeBufferSize,
  141. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  142. json.seed,
  143. );
  144. }
  145. toJson() {
  146. return {
  147. mtu: this.mtu,
  148. tti: this.tti,
  149. uplinkCapacity: this.upCap,
  150. downlinkCapacity: this.downCap,
  151. congestion: this.congestion,
  152. readBufferSize: this.readBuffer,
  153. writeBufferSize: this.writeBuffer,
  154. header: {
  155. type: this.type,
  156. },
  157. seed: this.seed,
  158. };
  159. }
  160. }
  161. class WsStreamSettings extends CommonClass {
  162. constructor(path='/', host='') {
  163. super();
  164. this.path = path;
  165. this.host = host;
  166. }
  167. static fromJson(json={}) {
  168. return new WsStreamSettings(
  169. json.path,
  170. json.headers && !ObjectUtil.isEmpty(json.headers.Host) ? json.headers.Host : '',
  171. );
  172. }
  173. toJson() {
  174. return {
  175. path: this.path,
  176. headers: ObjectUtil.isEmpty(this.host) ? undefined : {Host: this.host},
  177. };
  178. }
  179. }
  180. class HttpStreamSettings extends CommonClass {
  181. constructor(path='/', host='') {
  182. super();
  183. this.path = path;
  184. this.host = host;
  185. }
  186. static fromJson(json={}) {
  187. return new HttpStreamSettings(
  188. json.path,
  189. json.host ? json.host.join(',') : '',
  190. );
  191. }
  192. toJson() {
  193. return {
  194. path: this.path,
  195. host: ObjectUtil.isEmpty(this.host) ? [''] : this.host.split(','),
  196. }
  197. }
  198. }
  199. class QuicStreamSettings extends CommonClass {
  200. constructor(security='none',
  201. key='', type='none') {
  202. super();
  203. this.security = security;
  204. this.key = key;
  205. this.type = type;
  206. }
  207. static fromJson(json={}) {
  208. return new QuicStreamSettings(
  209. json.security,
  210. json.key,
  211. json.header ? json.header.type : 'none',
  212. );
  213. }
  214. toJson() {
  215. return {
  216. security: this.security,
  217. key: this.key,
  218. header: {
  219. type: this.type,
  220. }
  221. }
  222. }
  223. }
  224. class GrpcStreamSettings extends CommonClass {
  225. constructor(serviceName="", multiMode=false) {
  226. super();
  227. this.serviceName = serviceName;
  228. this.multiMode = multiMode;
  229. }
  230. static fromJson(json={}) {
  231. return new GrpcStreamSettings(json.serviceName, json.multiMode);
  232. }
  233. toJson() {
  234. return {
  235. serviceName: this.serviceName,
  236. multiMode: this.multiMode,
  237. }
  238. }
  239. }
  240. class TlsStreamSettings extends CommonClass {
  241. constructor(serverName='',
  242. alpn=[],
  243. fingerprint = '',
  244. allowInsecure = false) {
  245. super();
  246. this.serverName = serverName;
  247. this.alpn = alpn;
  248. this.fingerprint = fingerprint;
  249. this.allowInsecure = allowInsecure;
  250. }
  251. static fromJson(json={}) {
  252. return new TlsStreamSettings(
  253. json.serverName,
  254. json.alpn,
  255. json.fingerprint,
  256. json.allowInsecure,
  257. );
  258. }
  259. toJson() {
  260. return {
  261. serverName: this.serverName,
  262. alpn: this.alpn,
  263. fingerprint: this.fingerprint,
  264. allowInsecure: this.allowInsecure,
  265. };
  266. }
  267. }
  268. class RealityStreamSettings extends CommonClass {
  269. constructor(publicKey = '', fingerprint = '', serverName = '', shortId = '', spiderX = '/') {
  270. super();
  271. this.publicKey = publicKey;
  272. this.fingerprint = fingerprint;
  273. this.serverName = serverName;
  274. this.shortId = shortId
  275. this.spiderX = spiderX;
  276. }
  277. static fromJson(json = {}) {
  278. return new RealityStreamSettings(
  279. json.publicKey,
  280. json.fingerprint,
  281. json.serverName,
  282. json.shortId,
  283. json.spiderX,
  284. );
  285. }
  286. toJson() {
  287. return {
  288. publicKey: this.publicKey,
  289. fingerprint: this.fingerprint,
  290. serverName: this.serverName,
  291. shortId: this.shortId,
  292. spiderX: this.spiderX,
  293. };
  294. }
  295. };
  296. class StreamSettings extends CommonClass {
  297. constructor(network='tcp',
  298. security='none',
  299. tlsSettings=new TlsStreamSettings(),
  300. realitySettings = new RealityStreamSettings(),
  301. tcpSettings=new TcpStreamSettings(),
  302. kcpSettings=new KcpStreamSettings(),
  303. wsSettings=new WsStreamSettings(),
  304. httpSettings=new HttpStreamSettings(),
  305. quicSettings=new QuicStreamSettings(),
  306. grpcSettings=new GrpcStreamSettings(),
  307. ) {
  308. super();
  309. this.network = network;
  310. this.security = security;
  311. this.tls = tlsSettings;
  312. this.reality = realitySettings;
  313. this.tcp = tcpSettings;
  314. this.kcp = kcpSettings;
  315. this.ws = wsSettings;
  316. this.http = httpSettings;
  317. this.quic = quicSettings;
  318. this.grpc = grpcSettings;
  319. }
  320. get isTls() {
  321. return this.security === 'tls';
  322. }
  323. get isReality() {
  324. return this.security === "reality";
  325. }
  326. static fromJson(json={}) {
  327. return new StreamSettings(
  328. json.network,
  329. json.security,
  330. TlsStreamSettings.fromJson(json.tlsSettings),
  331. RealityStreamSettings.fromJson(json.realitySettings),
  332. TcpStreamSettings.fromJson(json.tcpSettings),
  333. KcpStreamSettings.fromJson(json.kcpSettings),
  334. WsStreamSettings.fromJson(json.wsSettings),
  335. HttpStreamSettings.fromJson(json.httpSettings),
  336. QuicStreamSettings.fromJson(json.quicSettings),
  337. GrpcStreamSettings.fromJson(json.grpcSettings),
  338. );
  339. }
  340. toJson() {
  341. const network = this.network;
  342. return {
  343. network: network,
  344. security: this.security,
  345. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  346. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  347. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  348. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  349. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  350. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  351. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  352. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  353. };
  354. }
  355. }
  356. class Outbound extends CommonClass {
  357. constructor(
  358. tag='',
  359. protocol=Protocols.VMess,
  360. settings=null,
  361. streamSettings = new StreamSettings(),
  362. ) {
  363. super();
  364. this.tag = tag;
  365. this._protocol = protocol;
  366. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  367. this.stream = streamSettings;
  368. }
  369. get protocol() {
  370. return this._protocol;
  371. }
  372. set protocol(protocol) {
  373. this._protocol = protocol;
  374. this.settings = Outbound.Settings.getSettings(protocol);
  375. this.stream = new StreamSettings();
  376. }
  377. canEnableTls() {
  378. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  379. return ["tcp", "ws", "http", "quic", "grpc"].includes(this.stream.network);
  380. }
  381. //this is used for xtls-rprx-vision
  382. canEnableTlsFlow() {
  383. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  384. return this.protocol === Protocols.VLESS;
  385. }
  386. return false;
  387. }
  388. canEnableReality() {
  389. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  390. return ["tcp", "http", "grpc"].includes(this.stream.network);
  391. }
  392. canEnableStream() {
  393. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol);
  394. }
  395. hasVnext() {
  396. return [Protocols.VMess, Protocols.VLESS].includes(this.protocol);
  397. }
  398. hasServers() {
  399. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  400. }
  401. hasAddressPort() {
  402. return [
  403. Protocols.DNS,
  404. Protocols.VMess,
  405. Protocols.VLESS,
  406. Protocols.Trojan,
  407. Protocols.Shadowsocks,
  408. Protocols.Socks,
  409. Protocols.HTTP
  410. ].includes(this.protocol);
  411. }
  412. hasUsername() {
  413. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  414. }
  415. static fromJson(json={}) {
  416. return new Outbound(
  417. json.tag,
  418. json.protocol,
  419. Outbound.Settings.fromJson(json.protocol, json.settings),
  420. StreamSettings.fromJson(json.streamSettings),
  421. )
  422. }
  423. toJson() {
  424. return {
  425. tag: this.tag == '' ? undefined : this.tag,
  426. protocol: this.protocol,
  427. settings: this.settings instanceof CommonClass ? this.settings.toJson() : this.settings,
  428. streamSettings: this.canEnableStream() ? this.stream.toJson() : undefined,
  429. };
  430. }
  431. static fromLink(link) {
  432. data = link.split('://');
  433. if(data.length !=2) return null;
  434. switch(data[0].toLowerCase()){
  435. case Protocols.VMess:
  436. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  437. case Protocols.VLESS:
  438. case Protocols.Trojan:
  439. case 'ss':
  440. return this.fromParamLink(link);
  441. default:
  442. return null;
  443. }
  444. }
  445. static fromVmessLink(json={}){
  446. let stream = new StreamSettings(json.net, json.tls);
  447. let network = json.net;
  448. if (network === 'tcp') {
  449. stream.tcp = new TcpStreamSettings(
  450. json.type,
  451. json.host ?? '',
  452. json.path ?? '');
  453. } else if (network === 'kcp') {
  454. stream.kcp = new KcpStreamSettings();
  455. stream.type = json.type;
  456. stream.seed = json.path;
  457. } else if (network === 'ws') {
  458. stream.ws = new WsStreamSettings(json.path,json.host);
  459. } else if (network === 'http' || network == 'h2') {
  460. stream.network = 'http'
  461. stream.http = new HttpStreamSettings(
  462. json.path,
  463. json.host);
  464. } else if (network === 'quic') {
  465. stream.quic = new QuicStreamSettings(
  466. json.host ? json.host : 'none',
  467. json.path,
  468. json.type ? json.type : 'none');
  469. } else if (network === 'grpc') {
  470. stream.grpc = new GrpcStreamSettings(json.path, json.type == 'multi');
  471. }
  472. if(json.tls && json.tls == 'tls'){
  473. stream.tls = new TlsStreamSettings(
  474. json.sni,
  475. json.alpn ? json.alpn.split(',') : [],
  476. json.fp,
  477. json.allowInsecure);
  478. }
  479. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, json.port, json.id), stream);
  480. }
  481. static fromParamLink(link){
  482. const url = new URL(link);
  483. let type = url.searchParams.get('type');
  484. let security = url.searchParams.get('security') ?? 'none';
  485. let stream = new StreamSettings(type, security);
  486. let headerType = url.searchParams.get('headerType');
  487. let host = url.searchParams.get('host');
  488. let path = url.searchParams.get('path');
  489. if (type === 'tcp') {
  490. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  491. } else if (type === 'kcp') {
  492. stream.kcp = new KcpStreamSettings();
  493. stream.kcp.type = headerType ?? 'none';
  494. stream.kcp.seed = path;
  495. } else if (type === 'ws') {
  496. stream.ws = new WsStreamSettings(path,host);
  497. } else if (type === 'http' || type == 'h2') {
  498. stream.http = new HttpStreamSettings(path,host);
  499. } else if (type === 'quic') {
  500. stream.quic = new QuicStreamSettings(
  501. url.searchParams.get('quicSecurity') ?? 'none',
  502. url.searchParams.get('key') ?? '',
  503. headerType ?? 'none');
  504. } else if (type === 'grpc') {
  505. stream.grpc = new GrpcStreamSettings(url.searchParams.get('serviceName') ?? '', url.searchParams.get('mode') == 'multi');
  506. }
  507. if(security == 'tls'){
  508. let fp=url.searchParams.get('fp') ?? 'none';
  509. let alpn=url.searchParams.get('alpn');
  510. let allowInsecure=url.searchParams.get('allowInsecure');
  511. let sni=url.searchParams.get('sni') ?? '';
  512. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1);
  513. }
  514. if(security == 'reality'){
  515. let pbk=url.searchParams.get('pbk');
  516. let fp=url.searchParams.get('fp');
  517. let sni=url.searchParams.get('sni') ?? '';
  518. let sid=url.searchParams.get('sid') ?? '';
  519. let spx=url.searchParams.get('spx') ?? '';
  520. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx);
  521. }
  522. let data = link.split('?');
  523. if(data.length != 2) return null;
  524. const regex = /([^@]+):\/\/([^@]+)@([^:]+):(\d+)\?(.*)$/;
  525. const match = link.match(regex);
  526. if (!match) return null;
  527. let [, protocol, userData, address, port, ] = match;
  528. port *= 1;
  529. if(protocol == 'ss') {
  530. protocol = 'shadowsocks';
  531. userData = atob(userData).split(':');
  532. }
  533. var settings;
  534. switch(protocol){
  535. case Protocols.VLESS:
  536. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '');
  537. break;
  538. case Protocols.Trojan:
  539. settings = new Outbound.TrojanSettings(address, port, userData);
  540. break;
  541. case Protocols.Shadowsocks:
  542. let method = userData.splice(0,1)[0];
  543. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  544. break;
  545. default:
  546. return null;
  547. }
  548. let remark = decodeURIComponent(url.hash);
  549. // Remove '#' from url.hash
  550. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  551. return new Outbound(remark, protocol, settings, stream);
  552. }
  553. }
  554. Outbound.Settings = class extends CommonClass {
  555. constructor(protocol) {
  556. super();
  557. this.protocol = protocol;
  558. }
  559. static getSettings(protocol) {
  560. switch (protocol) {
  561. case Protocols.Freedom: return new Outbound.FreedomSettings();
  562. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  563. case Protocols.DNS: return new Outbound.DNSSettings();
  564. case Protocols.VMess: return new Outbound.VmessSettings();
  565. case Protocols.VLESS: return new Outbound.VLESSSettings();
  566. case Protocols.Trojan: return new Outbound.TrojanSettings();
  567. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  568. case Protocols.Socks: return new Outbound.SocksSettings();
  569. case Protocols.HTTP: return new Outbound.HttpSettings();
  570. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  571. default: return null;
  572. }
  573. }
  574. static fromJson(protocol, json) {
  575. switch (protocol) {
  576. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  577. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  578. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  579. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  580. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  581. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  582. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  583. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  584. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  585. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  586. default: return null;
  587. }
  588. }
  589. toJson() {
  590. return {};
  591. }
  592. };
  593. Outbound.FreedomSettings = class extends CommonClass {
  594. constructor(domainStrategy='', fragment={}) {
  595. super();
  596. this.domainStrategy = domainStrategy;
  597. this.fragment = fragment;
  598. }
  599. static fromJson(json={}) {
  600. return new Outbound.FreedomSettings(
  601. json.domainStrategy,
  602. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  603. );
  604. }
  605. toJson() {
  606. return {
  607. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  608. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  609. };
  610. }
  611. };
  612. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  613. constructor(packets='1-3',length='',interval=''){
  614. super();
  615. this.packets = packets;
  616. this.length = length;
  617. this.interval = interval;
  618. }
  619. static fromJson(json={}) {
  620. return new Outbound.FreedomSettings.Fragment(
  621. json.packets,
  622. json.length,
  623. json.interval,
  624. );
  625. }
  626. };
  627. Outbound.BlackholeSettings = class extends CommonClass {
  628. constructor(type) {
  629. super();
  630. this.type;
  631. }
  632. static fromJson(json={}) {
  633. return new Outbound.BlackholeSettings(
  634. json.response ? json.response.type : undefined,
  635. );
  636. }
  637. toJson() {
  638. return {
  639. response: ObjectUtil.isEmpty(this.type) ? undefined : {type: this.type},
  640. };
  641. }
  642. };
  643. Outbound.DNSSettings = class extends CommonClass {
  644. constructor(network='udp', address='1.1.1.1', port=53) {
  645. super();
  646. this.network = network;
  647. this.address = address;
  648. this.port = port;
  649. }
  650. static fromJson(json={}){
  651. return new Outbound.DNSSettings(
  652. json.network,
  653. json.address,
  654. json.port,
  655. );
  656. }
  657. };
  658. Outbound.VmessSettings = class extends CommonClass {
  659. constructor(address, port, id) {
  660. super();
  661. this.address = address;
  662. this.port = port;
  663. this.id = id;
  664. }
  665. static fromJson(json={}) {
  666. if(ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  667. return new Outbound.VmessSettings(
  668. json.vnext[0].address,
  669. json.vnext[0].port,
  670. json.vnext[0].users[0].id,
  671. );
  672. }
  673. toJson() {
  674. return {
  675. vnext: [{
  676. address: this.address,
  677. port: this.port,
  678. users: [{id: this.id}],
  679. }],
  680. };
  681. }
  682. };
  683. Outbound.VLESSSettings = class extends CommonClass {
  684. constructor(address, port, id, flow, encryption='none') {
  685. super();
  686. this.address = address;
  687. this.port = port;
  688. this.id = id;
  689. this.flow = flow;
  690. this.encryption = encryption
  691. }
  692. static fromJson(json={}) {
  693. if(ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  694. return new Outbound.VLESSSettings(
  695. json.vnext[0].address,
  696. json.vnext[0].port,
  697. json.vnext[0].users[0].id,
  698. json.vnext[0].users[0].flow,
  699. json.vnext[0].users[0].encryption,
  700. );
  701. }
  702. toJson() {
  703. return {
  704. vnext: [{
  705. address: this.address,
  706. port: this.port,
  707. users: [{id: this.id, flow: this.flow, encryption: 'none',}],
  708. }],
  709. };
  710. }
  711. };
  712. Outbound.TrojanSettings = class extends CommonClass {
  713. constructor(address, port, password) {
  714. super();
  715. this.address = address;
  716. this.port = port;
  717. this.password = password;
  718. }
  719. static fromJson(json={}) {
  720. if(ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  721. return new Outbound.TrojanSettings(
  722. json.servers[0].address,
  723. json.servers[0].port,
  724. json.servers[0].password,
  725. );
  726. }
  727. toJson() {
  728. return {
  729. servers: [{
  730. address: this.address,
  731. port: this.port,
  732. password: this.password,
  733. }],
  734. };
  735. }
  736. };
  737. Outbound.ShadowsocksSettings = class extends CommonClass {
  738. constructor(address, port, password, method, uot) {
  739. super();
  740. this.address = address;
  741. this.port = port;
  742. this.password = password;
  743. this.method = method;
  744. this.uot = uot;
  745. }
  746. static fromJson(json={}) {
  747. let servers = json.servers;
  748. if(ObjectUtil.isArrEmpty(servers)) servers=[{}];
  749. return new Outbound.ShadowsocksSettings(
  750. servers[0].address,
  751. servers[0].port,
  752. servers[0].password,
  753. servers[0].method,
  754. servers[0].uot,
  755. );
  756. }
  757. toJson() {
  758. return {
  759. servers: [{
  760. address: this.address,
  761. port: this.port,
  762. password: this.password,
  763. method: this.method,
  764. uot: this.uot,
  765. }],
  766. };
  767. }
  768. };
  769. Outbound.WireguardSettings = class extends CommonClass {
  770. constructor(secretKey, address, peers, mtu, workers, domainStrategy, reserved) {
  771. super();
  772. this.secretKey = secretKey || '';
  773. this.address = address ? [...address] : [];
  774. this.peers = peers ? peers.map((p) => ({
  775. ...p,
  776. allowedIPs: p.allowedIPs ? [...p.allowedIPs] : [],
  777. })) : [];
  778. this.mtu = mtu;
  779. this.workers = workers;
  780. this.domainStrategy = domainStrategy;
  781. this.reserved = reserved;
  782. }
  783. static fromJson(json={}) {
  784. return new Outbound.WireguardSettings(
  785. json.secretKey,
  786. json.address,
  787. json.peers,
  788. json.mtu,
  789. json.workers,
  790. json.domainStrategy,
  791. json.reserved,
  792. );
  793. }
  794. addAddress() {
  795. this.address.push('');
  796. }
  797. delAddress(index) {
  798. this.address.splice(index, 1);
  799. }
  800. addPeer() {
  801. this.peers.push({
  802. endpoint: '',
  803. publicKey: '',
  804. allowedIPs: [],
  805. });
  806. }
  807. delPeer(index) {
  808. this.peers.splice(index, 1);
  809. }
  810. addAllowedIP(index) {
  811. if (!this.peers[index].allowedIPs) {
  812. this.peers[index].allowedIPs = [];
  813. }
  814. this.peers[index].allowedIPs.push('');
  815. }
  816. delAllowedIP(index, ipIndex) {
  817. this.peers[index].allowedIPs.splice(ipIndex, 1);
  818. }
  819. optionalFields = ['mtu', 'workers', 'domainStrategy', 'address', 'reserved'];
  820. optionalPeerFields = ['allowedIPs', 'keepAlive', 'preSharedKey'];
  821. cleanUpOptionalFields(obj) {
  822. const isEmpty = (v) => ObjectUtil.isEmpty(v) || ObjectUtil.isArrEmpty(v);
  823. return Object.entries(obj).reduce((memo, [key, value]) => {
  824. if (key === 'peers') {
  825. memo[key] = value.map((peer) => {
  826. return Object.entries(peer).reduce((pMemo, [pKey, pValue]) => {
  827. if (this.optionalPeerFields.includes(pKey) && isEmpty(pValue)) {
  828. return pMemo;
  829. }
  830. pMemo[pKey] = pValue;
  831. return pMemo;
  832. }, {});
  833. });
  834. } else if (this.optionalFields.includes(key) && isEmpty(value)) {
  835. return memo;
  836. } else {
  837. memo[key] = value;
  838. }
  839. return memo;
  840. }, {});
  841. }
  842. toJson() {
  843. return this.cleanUpOptionalFields({
  844. secretKey: this.secretKey,
  845. address: this.address,
  846. peers: this.peers,
  847. mtu: this.mtu,
  848. workers: this.workers,
  849. domainStrategy: this.domainStrategy,
  850. reserved: this.reserved,
  851. });
  852. }
  853. };
  854. Outbound.SocksSettings = class extends CommonClass {
  855. constructor(address, port, user, pass) {
  856. super();
  857. this.address = address;
  858. this.port = port;
  859. this.user = user;
  860. this.pass = pass;
  861. }
  862. static fromJson(json={}) {
  863. servers = json.servers;
  864. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  865. return new Outbound.SocksSettings(
  866. servers[0].address,
  867. servers[0].port,
  868. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  869. ObjectUtil.isArrEmpty(servers[0].pass) ? '' : servers[0].users[0].pass,
  870. );
  871. }
  872. toJson() {
  873. return {
  874. servers: [{
  875. address: this.address,
  876. port: this.port,
  877. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  878. }],
  879. };
  880. }
  881. };
  882. Outbound.HttpSettings = class extends CommonClass {
  883. constructor(address, port, user, pass) {
  884. super();
  885. this.address = address;
  886. this.port = port;
  887. this.user = user;
  888. this.pass = pass;
  889. }
  890. static fromJson(json={}) {
  891. servers = json.servers;
  892. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  893. return new Outbound.HttpSettings(
  894. servers[0].address,
  895. servers[0].port,
  896. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  897. ObjectUtil.isArrEmpty(servers[0].pass) ? '' : servers[0].users[0].pass,
  898. );
  899. }
  900. toJson() {
  901. return {
  902. servers: [{
  903. address: this.address,
  904. port: this.port,
  905. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  906. }],
  907. };
  908. }
  909. };