outbound.js 34 KB

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