outbound.js 27 KB

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