outbound.js 33 KB

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