outbound.js 33 KB

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