outbound.js 33 KB

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