outbound.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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, Protocols.Shadowsocks].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.SocksSettings = class extends CommonClass {
  770. constructor(address, port, user, pass) {
  771. super();
  772. this.address = address;
  773. this.port = port;
  774. this.user = user;
  775. this.pass = pass;
  776. }
  777. static fromJson(json={}) {
  778. let servers = json.servers;
  779. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  780. return new Outbound.SocksSettings(
  781. servers[0].address,
  782. servers[0].port,
  783. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  784. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  785. );
  786. }
  787. toJson() {
  788. return {
  789. servers: [{
  790. address: this.address,
  791. port: this.port,
  792. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  793. }],
  794. };
  795. }
  796. };
  797. Outbound.HttpSettings = class extends CommonClass {
  798. constructor(address, port, user, pass) {
  799. super();
  800. this.address = address;
  801. this.port = port;
  802. this.user = user;
  803. this.pass = pass;
  804. }
  805. static fromJson(json={}) {
  806. let servers = json.servers;
  807. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  808. return new Outbound.HttpSettings(
  809. servers[0].address,
  810. servers[0].port,
  811. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  812. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  813. );
  814. }
  815. toJson() {
  816. return {
  817. servers: [{
  818. address: this.address,
  819. port: this.port,
  820. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  821. }],
  822. };
  823. }
  824. };
  825. Outbound.WireguardSettings = class extends CommonClass {
  826. constructor(
  827. mtu=1420, secretKey='',
  828. address=[''], workers=2, domainStrategy='', reserved='',
  829. peers=[new Outbound.WireguardSettings.Peer()], kernelMode=false) {
  830. super();
  831. this.mtu = mtu;
  832. this.secretKey = secretKey;
  833. this.pubKey = secretKey.length>0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  834. this.address = address instanceof Array ? address.join(',') : address;
  835. this.workers = workers;
  836. this.domainStrategy = domainStrategy;
  837. this.reserved = reserved instanceof Array ? reserved.join(',') : reserved;
  838. this.peers = peers;
  839. this.kernelMode = kernelMode;
  840. }
  841. addPeer() {
  842. this.peers.push(new Outbound.WireguardSettings.Peer());
  843. }
  844. delPeer(index) {
  845. this.peers.splice(index, 1);
  846. }
  847. static fromJson(json={}){
  848. return new Outbound.WireguardSettings(
  849. json.mtu,
  850. json.secretKey,
  851. json.address,
  852. json.workers,
  853. json.domainStrategy,
  854. json.reserved,
  855. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  856. json.kernelMode,
  857. );
  858. }
  859. toJson() {
  860. return {
  861. mtu: this.mtu?? undefined,
  862. secretKey: this.secretKey,
  863. address: this.address ? this.address.split(",") : [],
  864. workers: this.workers?? undefined,
  865. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  866. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  867. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  868. kernelMode: this.kernelMode,
  869. };
  870. }
  871. };
  872. Outbound.WireguardSettings.Peer = class extends CommonClass {
  873. constructor(publicKey='', psk='', allowedIPs=['0.0.0.0/0','::/0'], endpoint='', keepAlive=0) {
  874. super();
  875. this.publicKey = publicKey;
  876. this.psk = psk;
  877. this.allowedIPs = allowedIPs;
  878. this.endpoint = endpoint;
  879. this.keepAlive = keepAlive;
  880. }
  881. static fromJson(json={}){
  882. return new Outbound.WireguardSettings.Peer(
  883. json.publicKey,
  884. json.preSharedKey,
  885. json.allowedIPs,
  886. json.endpoint,
  887. json.keepAlive
  888. );
  889. }
  890. toJson() {
  891. return {
  892. publicKey: this.publicKey,
  893. preSharedKey: this.psk.length>0 ? this.psk : undefined,
  894. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  895. endpoint: this.endpoint,
  896. keepAlive: this.keepAlive?? undefined,
  897. };
  898. }
  899. };