outbound.js 31 KB

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