outbound.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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 HttpUpgradeStreamSettings extends CommonClass {
  241. constructor(path='/', host='') {
  242. super();
  243. this.path = path;
  244. this.host = host;
  245. }
  246. static fromJson(json={}) {
  247. return new HttpUpgradeStreamSettings(
  248. json.path,
  249. json.Host,
  250. );
  251. }
  252. toJson() {
  253. return {
  254. path: this.path,
  255. host: this.host,
  256. };
  257. }
  258. }
  259. class TlsStreamSettings extends CommonClass {
  260. constructor(serverName='',
  261. alpn=[],
  262. fingerprint = '',
  263. allowInsecure = false) {
  264. super();
  265. this.serverName = serverName;
  266. this.alpn = alpn;
  267. this.fingerprint = fingerprint;
  268. this.allowInsecure = allowInsecure;
  269. }
  270. static fromJson(json={}) {
  271. return new TlsStreamSettings(
  272. json.serverName,
  273. json.alpn,
  274. json.fingerprint,
  275. json.allowInsecure,
  276. );
  277. }
  278. toJson() {
  279. return {
  280. serverName: this.serverName,
  281. alpn: this.alpn,
  282. fingerprint: this.fingerprint,
  283. allowInsecure: this.allowInsecure,
  284. };
  285. }
  286. }
  287. class RealityStreamSettings extends CommonClass {
  288. constructor(publicKey = '', fingerprint = '', serverName = '', shortId = '', spiderX = '/') {
  289. super();
  290. this.publicKey = publicKey;
  291. this.fingerprint = fingerprint;
  292. this.serverName = serverName;
  293. this.shortId = shortId
  294. this.spiderX = spiderX;
  295. }
  296. static fromJson(json = {}) {
  297. return new RealityStreamSettings(
  298. json.publicKey,
  299. json.fingerprint,
  300. json.serverName,
  301. json.shortId,
  302. json.spiderX,
  303. );
  304. }
  305. toJson() {
  306. return {
  307. publicKey: this.publicKey,
  308. fingerprint: this.fingerprint,
  309. serverName: this.serverName,
  310. shortId: this.shortId,
  311. spiderX: this.spiderX,
  312. };
  313. }
  314. };
  315. class StreamSettings extends CommonClass {
  316. constructor(network='tcp',
  317. security='none',
  318. tlsSettings=new TlsStreamSettings(),
  319. realitySettings = new RealityStreamSettings(),
  320. tcpSettings=new TcpStreamSettings(),
  321. kcpSettings=new KcpStreamSettings(),
  322. wsSettings=new WsStreamSettings(),
  323. httpSettings=new HttpStreamSettings(),
  324. quicSettings=new QuicStreamSettings(),
  325. grpcSettings=new GrpcStreamSettings(),
  326. httpupgradeSettings=new HttpUpgradeStreamSettings(),
  327. ) {
  328. super();
  329. this.network = network;
  330. this.security = security;
  331. this.tls = tlsSettings;
  332. this.reality = realitySettings;
  333. this.tcp = tcpSettings;
  334. this.kcp = kcpSettings;
  335. this.ws = wsSettings;
  336. this.http = httpSettings;
  337. this.quic = quicSettings;
  338. this.grpc = grpcSettings;
  339. this.httpupgrade = httpupgradeSettings;
  340. }
  341. get isTls() {
  342. return this.security === 'tls';
  343. }
  344. get isReality() {
  345. return this.security === "reality";
  346. }
  347. static fromJson(json={}) {
  348. return new StreamSettings(
  349. json.network,
  350. json.security,
  351. TlsStreamSettings.fromJson(json.tlsSettings),
  352. RealityStreamSettings.fromJson(json.realitySettings),
  353. TcpStreamSettings.fromJson(json.tcpSettings),
  354. KcpStreamSettings.fromJson(json.kcpSettings),
  355. WsStreamSettings.fromJson(json.wsSettings),
  356. HttpStreamSettings.fromJson(json.httpSettings),
  357. QuicStreamSettings.fromJson(json.quicSettings),
  358. GrpcStreamSettings.fromJson(json.grpcSettings),
  359. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  360. );
  361. }
  362. toJson() {
  363. const network = this.network;
  364. return {
  365. network: network,
  366. security: this.security,
  367. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  368. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  369. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  370. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  371. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  372. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  373. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  374. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  375. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  376. };
  377. }
  378. }
  379. class Outbound extends CommonClass {
  380. constructor(
  381. tag='',
  382. protocol=Protocols.VMess,
  383. settings=null,
  384. streamSettings = new StreamSettings(),
  385. ) {
  386. super();
  387. this.tag = tag;
  388. this._protocol = protocol;
  389. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  390. this.stream = streamSettings;
  391. }
  392. get protocol() {
  393. return this._protocol;
  394. }
  395. set protocol(protocol) {
  396. this._protocol = protocol;
  397. this.settings = Outbound.Settings.getSettings(protocol);
  398. this.stream = new StreamSettings();
  399. }
  400. canEnableTls() {
  401. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol)) return false;
  402. return ["tcp", "ws", "http", "quic", "grpc", "httpupgrade"].includes(this.stream.network);
  403. }
  404. //this is used for xtls-rprx-vision
  405. canEnableTlsFlow() {
  406. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  407. return this.protocol === Protocols.VLESS;
  408. }
  409. return false;
  410. }
  411. canEnableReality() {
  412. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  413. return ["tcp", "http", "grpc"].includes(this.stream.network);
  414. }
  415. canEnableStream() {
  416. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol);
  417. }
  418. hasVnext() {
  419. return [Protocols.VMess, Protocols.VLESS].includes(this.protocol);
  420. }
  421. hasServers() {
  422. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  423. }
  424. hasAddressPort() {
  425. return [
  426. Protocols.DNS,
  427. Protocols.VMess,
  428. Protocols.VLESS,
  429. Protocols.Trojan,
  430. Protocols.Shadowsocks,
  431. Protocols.Socks,
  432. Protocols.HTTP
  433. ].includes(this.protocol);
  434. }
  435. hasUsername() {
  436. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  437. }
  438. static fromJson(json={}) {
  439. return new Outbound(
  440. json.tag,
  441. json.protocol,
  442. Outbound.Settings.fromJson(json.protocol, json.settings),
  443. StreamSettings.fromJson(json.streamSettings),
  444. )
  445. }
  446. toJson() {
  447. return {
  448. tag: this.tag == '' ? undefined : this.tag,
  449. protocol: this.protocol,
  450. settings: this.settings instanceof CommonClass ? this.settings.toJson() : this.settings,
  451. streamSettings: this.canEnableStream() ? this.stream.toJson() : undefined,
  452. };
  453. }
  454. static fromLink(link) {
  455. data = link.split('://');
  456. if(data.length !=2) return null;
  457. switch(data[0].toLowerCase()){
  458. case Protocols.VMess:
  459. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  460. case Protocols.VLESS:
  461. case Protocols.Trojan:
  462. case 'ss':
  463. return this.fromParamLink(link);
  464. default:
  465. return null;
  466. }
  467. }
  468. static fromVmessLink(json={}){
  469. let stream = new StreamSettings(json.net, json.tls);
  470. let network = json.net;
  471. if (network === 'tcp') {
  472. stream.tcp = new TcpStreamSettings(
  473. json.type,
  474. json.host ?? '',
  475. json.path ?? '');
  476. } else if (network === 'kcp') {
  477. stream.kcp = new KcpStreamSettings();
  478. stream.type = json.type;
  479. stream.seed = json.path;
  480. } else if (network === 'ws') {
  481. stream.ws = new WsStreamSettings(json.path,json.host);
  482. } else if (network === 'http' || network == 'h2') {
  483. stream.network = 'http'
  484. stream.http = new HttpStreamSettings(
  485. json.path,
  486. json.host);
  487. } else if (network === 'quic') {
  488. stream.quic = new QuicStreamSettings(
  489. json.host ? json.host : 'none',
  490. json.path,
  491. json.type ? json.type : 'none');
  492. } else if (network === 'grpc') {
  493. stream.grpc = new GrpcStreamSettings(json.path, json.type == 'multi');
  494. } else if (network === 'httpupgrade') {
  495. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path,json.host);
  496. }
  497. if(json.tls && json.tls == 'tls'){
  498. stream.tls = new TlsStreamSettings(
  499. json.sni,
  500. json.alpn ? json.alpn.split(',') : [],
  501. json.fp,
  502. json.allowInsecure);
  503. }
  504. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, json.port, json.id), stream);
  505. }
  506. static fromParamLink(link){
  507. const url = new URL(link);
  508. let type = url.searchParams.get('type');
  509. let security = url.searchParams.get('security') ?? 'none';
  510. let stream = new StreamSettings(type, security);
  511. let headerType = url.searchParams.get('headerType');
  512. let host = url.searchParams.get('host');
  513. let path = url.searchParams.get('path');
  514. if (type === 'tcp') {
  515. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  516. } else if (type === 'kcp') {
  517. stream.kcp = new KcpStreamSettings();
  518. stream.kcp.type = headerType ?? 'none';
  519. stream.kcp.seed = path;
  520. } else if (type === 'ws') {
  521. stream.ws = new WsStreamSettings(path,host);
  522. } else if (type === 'http' || type == 'h2') {
  523. stream.http = new HttpStreamSettings(path,host);
  524. } else if (type === 'quic') {
  525. stream.quic = new QuicStreamSettings(
  526. url.searchParams.get('quicSecurity') ?? 'none',
  527. url.searchParams.get('key') ?? '',
  528. headerType ?? 'none');
  529. } else if (type === 'grpc') {
  530. stream.grpc = new GrpcStreamSettings(url.searchParams.get('serviceName') ?? '', url.searchParams.get('mode') == 'multi');
  531. } else if (type === 'httpupgrade') {
  532. stream.httpupgrade = new HttpUpgradeStreamSettings(path,host);
  533. }
  534. if(security == 'tls'){
  535. let fp=url.searchParams.get('fp') ?? 'none';
  536. let alpn=url.searchParams.get('alpn');
  537. let allowInsecure=url.searchParams.get('allowInsecure');
  538. let sni=url.searchParams.get('sni') ?? '';
  539. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1);
  540. }
  541. if(security == 'reality'){
  542. let pbk=url.searchParams.get('pbk');
  543. let fp=url.searchParams.get('fp');
  544. let sni=url.searchParams.get('sni') ?? '';
  545. let sid=url.searchParams.get('sid') ?? '';
  546. let spx=url.searchParams.get('spx') ?? '';
  547. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx);
  548. }
  549. let data = link.split('?');
  550. if(data.length != 2) return null;
  551. const regex = /([^@]+):\/\/([^@]+)@([^:]+):(\d+)\?(.*)$/;
  552. const match = link.match(regex);
  553. if (!match) return null;
  554. let [, protocol, userData, address, port, ] = match;
  555. port *= 1;
  556. if(protocol == 'ss') {
  557. protocol = 'shadowsocks';
  558. userData = atob(userData).split(':');
  559. }
  560. var settings;
  561. switch(protocol){
  562. case Protocols.VLESS:
  563. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '');
  564. break;
  565. case Protocols.Trojan:
  566. settings = new Outbound.TrojanSettings(address, port, userData);
  567. break;
  568. case Protocols.Shadowsocks:
  569. let method = userData.splice(0,1)[0];
  570. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  571. break;
  572. default:
  573. return null;
  574. }
  575. let remark = decodeURIComponent(url.hash);
  576. // Remove '#' from url.hash
  577. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  578. return new Outbound(remark, protocol, settings, stream);
  579. }
  580. }
  581. Outbound.Settings = class extends CommonClass {
  582. constructor(protocol) {
  583. super();
  584. this.protocol = protocol;
  585. }
  586. static getSettings(protocol) {
  587. switch (protocol) {
  588. case Protocols.Freedom: return new Outbound.FreedomSettings();
  589. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  590. case Protocols.DNS: return new Outbound.DNSSettings();
  591. case Protocols.VMess: return new Outbound.VmessSettings();
  592. case Protocols.VLESS: return new Outbound.VLESSSettings();
  593. case Protocols.Trojan: return new Outbound.TrojanSettings();
  594. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  595. case Protocols.Socks: return new Outbound.SocksSettings();
  596. case Protocols.HTTP: return new Outbound.HttpSettings();
  597. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  598. default: return null;
  599. }
  600. }
  601. static fromJson(protocol, json) {
  602. switch (protocol) {
  603. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  604. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  605. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  606. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  607. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  608. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  609. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  610. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  611. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  612. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  613. default: return null;
  614. }
  615. }
  616. toJson() {
  617. return {};
  618. }
  619. };
  620. Outbound.FreedomSettings = class extends CommonClass {
  621. constructor(domainStrategy='', fragment={}) {
  622. super();
  623. this.domainStrategy = domainStrategy;
  624. this.fragment = fragment;
  625. }
  626. static fromJson(json={}) {
  627. return new Outbound.FreedomSettings(
  628. json.domainStrategy,
  629. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  630. );
  631. }
  632. toJson() {
  633. return {
  634. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  635. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  636. };
  637. }
  638. };
  639. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  640. constructor(packets='1-3',length='',interval=''){
  641. super();
  642. this.packets = packets;
  643. this.length = length;
  644. this.interval = interval;
  645. }
  646. static fromJson(json={}) {
  647. return new Outbound.FreedomSettings.Fragment(
  648. json.packets,
  649. json.length,
  650. json.interval,
  651. );
  652. }
  653. };
  654. Outbound.BlackholeSettings = class extends CommonClass {
  655. constructor(type) {
  656. super();
  657. this.type;
  658. }
  659. static fromJson(json={}) {
  660. return new Outbound.BlackholeSettings(
  661. json.response ? json.response.type : undefined,
  662. );
  663. }
  664. toJson() {
  665. return {
  666. response: ObjectUtil.isEmpty(this.type) ? undefined : {type: this.type},
  667. };
  668. }
  669. };
  670. Outbound.DNSSettings = class extends CommonClass {
  671. constructor(network='udp', address='1.1.1.1', port=53) {
  672. super();
  673. this.network = network;
  674. this.address = address;
  675. this.port = port;
  676. }
  677. static fromJson(json={}){
  678. return new Outbound.DNSSettings(
  679. json.network,
  680. json.address,
  681. json.port,
  682. );
  683. }
  684. };
  685. Outbound.VmessSettings = class extends CommonClass {
  686. constructor(address, port, id) {
  687. super();
  688. this.address = address;
  689. this.port = port;
  690. this.id = id;
  691. }
  692. static fromJson(json={}) {
  693. if(ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  694. return new Outbound.VmessSettings(
  695. json.vnext[0].address,
  696. json.vnext[0].port,
  697. json.vnext[0].users[0].id,
  698. );
  699. }
  700. toJson() {
  701. return {
  702. vnext: [{
  703. address: this.address,
  704. port: this.port,
  705. users: [{id: this.id}],
  706. }],
  707. };
  708. }
  709. };
  710. Outbound.VLESSSettings = class extends CommonClass {
  711. constructor(address, port, id, flow, encryption='none') {
  712. super();
  713. this.address = address;
  714. this.port = port;
  715. this.id = id;
  716. this.flow = flow;
  717. this.encryption = encryption
  718. }
  719. static fromJson(json={}) {
  720. if(ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  721. return new Outbound.VLESSSettings(
  722. json.vnext[0].address,
  723. json.vnext[0].port,
  724. json.vnext[0].users[0].id,
  725. json.vnext[0].users[0].flow,
  726. json.vnext[0].users[0].encryption,
  727. );
  728. }
  729. toJson() {
  730. return {
  731. vnext: [{
  732. address: this.address,
  733. port: this.port,
  734. users: [{id: this.id, flow: this.flow, encryption: 'none',}],
  735. }],
  736. };
  737. }
  738. };
  739. Outbound.TrojanSettings = class extends CommonClass {
  740. constructor(address, port, password) {
  741. super();
  742. this.address = address;
  743. this.port = port;
  744. this.password = password;
  745. }
  746. static fromJson(json={}) {
  747. if(ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  748. return new Outbound.TrojanSettings(
  749. json.servers[0].address,
  750. json.servers[0].port,
  751. json.servers[0].password,
  752. );
  753. }
  754. toJson() {
  755. return {
  756. servers: [{
  757. address: this.address,
  758. port: this.port,
  759. password: this.password,
  760. }],
  761. };
  762. }
  763. };
  764. Outbound.ShadowsocksSettings = class extends CommonClass {
  765. constructor(address, port, password, method, uot) {
  766. super();
  767. this.address = address;
  768. this.port = port;
  769. this.password = password;
  770. this.method = method;
  771. this.uot = uot;
  772. }
  773. static fromJson(json={}) {
  774. let servers = json.servers;
  775. if(ObjectUtil.isArrEmpty(servers)) servers=[{}];
  776. return new Outbound.ShadowsocksSettings(
  777. servers[0].address,
  778. servers[0].port,
  779. servers[0].password,
  780. servers[0].method,
  781. servers[0].uot,
  782. );
  783. }
  784. toJson() {
  785. return {
  786. servers: [{
  787. address: this.address,
  788. port: this.port,
  789. password: this.password,
  790. method: this.method,
  791. uot: this.uot,
  792. }],
  793. };
  794. }
  795. };
  796. Outbound.SocksSettings = class extends CommonClass {
  797. constructor(address, port, user, pass) {
  798. super();
  799. this.address = address;
  800. this.port = port;
  801. this.user = user;
  802. this.pass = pass;
  803. }
  804. static fromJson(json={}) {
  805. let servers = json.servers;
  806. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  807. return new Outbound.SocksSettings(
  808. servers[0].address,
  809. servers[0].port,
  810. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  811. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  812. );
  813. }
  814. toJson() {
  815. return {
  816. servers: [{
  817. address: this.address,
  818. port: this.port,
  819. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  820. }],
  821. };
  822. }
  823. };
  824. Outbound.HttpSettings = class extends CommonClass {
  825. constructor(address, port, user, pass) {
  826. super();
  827. this.address = address;
  828. this.port = port;
  829. this.user = user;
  830. this.pass = pass;
  831. }
  832. static fromJson(json={}) {
  833. let servers = json.servers;
  834. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  835. return new Outbound.HttpSettings(
  836. servers[0].address,
  837. servers[0].port,
  838. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  839. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  840. );
  841. }
  842. toJson() {
  843. return {
  844. servers: [{
  845. address: this.address,
  846. port: this.port,
  847. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  848. }],
  849. };
  850. }
  851. };
  852. Outbound.WireguardSettings = class extends CommonClass {
  853. constructor(
  854. mtu=1420, secretKey='',
  855. address=[''], workers=2, domainStrategy='', reserved='',
  856. peers=[new Outbound.WireguardSettings.Peer()], kernelMode=false) {
  857. super();
  858. this.mtu = mtu;
  859. this.secretKey = secretKey;
  860. this.pubKey = secretKey.length>0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  861. this.address = address instanceof Array ? address.join(',') : address;
  862. this.workers = workers;
  863. this.domainStrategy = domainStrategy;
  864. this.reserved = reserved instanceof Array ? reserved.join(',') : reserved;
  865. this.peers = peers;
  866. this.kernelMode = kernelMode;
  867. }
  868. addPeer() {
  869. this.peers.push(new Outbound.WireguardSettings.Peer());
  870. }
  871. delPeer(index) {
  872. this.peers.splice(index, 1);
  873. }
  874. static fromJson(json={}){
  875. return new Outbound.WireguardSettings(
  876. json.mtu,
  877. json.secretKey,
  878. json.address,
  879. json.workers,
  880. json.domainStrategy,
  881. json.reserved,
  882. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  883. json.kernelMode,
  884. );
  885. }
  886. toJson() {
  887. return {
  888. mtu: this.mtu?? undefined,
  889. secretKey: this.secretKey,
  890. address: this.address ? this.address.split(",") : [],
  891. workers: this.workers?? undefined,
  892. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  893. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  894. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  895. kernelMode: this.kernelMode,
  896. };
  897. }
  898. };
  899. Outbound.WireguardSettings.Peer = class extends CommonClass {
  900. constructor(publicKey='', psk='', allowedIPs=['0.0.0.0/0','::/0'], endpoint='', keepAlive=0) {
  901. super();
  902. this.publicKey = publicKey;
  903. this.psk = psk;
  904. this.allowedIPs = allowedIPs;
  905. this.endpoint = endpoint;
  906. this.keepAlive = keepAlive;
  907. }
  908. static fromJson(json={}){
  909. return new Outbound.WireguardSettings.Peer(
  910. json.publicKey,
  911. json.preSharedKey,
  912. json.allowedIPs,
  913. json.endpoint,
  914. json.keepAlive
  915. );
  916. }
  917. toJson() {
  918. return {
  919. publicKey: this.publicKey,
  920. preSharedKey: this.psk.length>0 ? this.psk : undefined,
  921. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  922. endpoint: this.endpoint,
  923. keepAlive: this.keepAlive?? undefined,
  924. };
  925. }
  926. };