outbound.js 34 KB

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