outbound.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 Mux(
  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. var stream;
  520. if (this.canEnableStream()) {
  521. stream = this.stream.toJson();
  522. } else {
  523. if (this.stream?.sockopt)
  524. stream = { sockopt: this.stream.sockopt.toJson() };
  525. }
  526. return {
  527. tag: this.tag == '' ? undefined : this.tag,
  528. protocol: this.protocol,
  529. settings: this.settings instanceof CommonClass ? this.settings.toJson() : this.settings,
  530. streamSettings: stream,
  531. mux: this.mux?.enabled ? this.mux : undefined,
  532. };
  533. }
  534. static fromLink(link) {
  535. data = link.split('://');
  536. if(data.length !=2) return null;
  537. switch(data[0].toLowerCase()){
  538. case Protocols.VMess:
  539. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  540. case Protocols.VLESS:
  541. case Protocols.Trojan:
  542. case 'ss':
  543. return this.fromParamLink(link);
  544. default:
  545. return null;
  546. }
  547. }
  548. static fromVmessLink(json={}){
  549. let stream = new StreamSettings(json.net, json.tls);
  550. let network = json.net;
  551. if (network === 'tcp') {
  552. stream.tcp = new TcpStreamSettings(
  553. json.type,
  554. json.host ?? '',
  555. json.path ?? '');
  556. } else if (network === 'kcp') {
  557. stream.kcp = new KcpStreamSettings();
  558. stream.type = json.type;
  559. stream.seed = json.path;
  560. } else if (network === 'ws') {
  561. stream.ws = new WsStreamSettings(json.path,json.host);
  562. } else if (network === 'http' || network == 'h2') {
  563. stream.network = 'http'
  564. stream.http = new HttpStreamSettings(
  565. json.path,
  566. json.host);
  567. } else if (network === 'quic') {
  568. stream.quic = new QuicStreamSettings(
  569. json.host ? json.host : 'none',
  570. json.path,
  571. json.type ? json.type : 'none');
  572. } else if (network === 'grpc') {
  573. stream.grpc = new GrpcStreamSettings(json.path, json.type == 'multi');
  574. } else if (network === 'httpupgrade') {
  575. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path,json.host);
  576. }
  577. if(json.tls && json.tls == 'tls'){
  578. stream.tls = new TlsStreamSettings(
  579. json.sni,
  580. json.alpn ? json.alpn.split(',') : [],
  581. json.fp,
  582. json.allowInsecure);
  583. }
  584. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, json.port, json.id), stream);
  585. }
  586. static fromParamLink(link){
  587. const url = new URL(link);
  588. let type = url.searchParams.get('type');
  589. let security = url.searchParams.get('security') ?? 'none';
  590. let stream = new StreamSettings(type, security);
  591. let headerType = url.searchParams.get('headerType');
  592. let host = url.searchParams.get('host');
  593. let path = url.searchParams.get('path');
  594. if (type === 'tcp') {
  595. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  596. } else if (type === 'kcp') {
  597. stream.kcp = new KcpStreamSettings();
  598. stream.kcp.type = headerType ?? 'none';
  599. stream.kcp.seed = path;
  600. } else if (type === 'ws') {
  601. stream.ws = new WsStreamSettings(path,host);
  602. } else if (type === 'http' || type == 'h2') {
  603. stream.http = new HttpStreamSettings(path,host);
  604. } else if (type === 'quic') {
  605. stream.quic = new QuicStreamSettings(
  606. url.searchParams.get('quicSecurity') ?? 'none',
  607. url.searchParams.get('key') ?? '',
  608. headerType ?? 'none');
  609. } else if (type === 'grpc') {
  610. stream.grpc = new GrpcStreamSettings(url.searchParams.get('serviceName') ?? '', url.searchParams.get('mode') == 'multi');
  611. } else if (type === 'httpupgrade') {
  612. stream.httpupgrade = new HttpUpgradeStreamSettings(path,host);
  613. }
  614. if(security == 'tls'){
  615. let fp=url.searchParams.get('fp') ?? 'none';
  616. let alpn=url.searchParams.get('alpn');
  617. let allowInsecure=url.searchParams.get('allowInsecure');
  618. let sni=url.searchParams.get('sni') ?? '';
  619. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1);
  620. }
  621. if(security == 'reality'){
  622. let pbk=url.searchParams.get('pbk');
  623. let fp=url.searchParams.get('fp');
  624. let sni=url.searchParams.get('sni') ?? '';
  625. let sid=url.searchParams.get('sid') ?? '';
  626. let spx=url.searchParams.get('spx') ?? '';
  627. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx);
  628. }
  629. let data = link.split('?');
  630. if(data.length != 2) return null;
  631. const regex = /([^@]+):\/\/([^@]+)@([^:]+):(\d+)\?(.*)$/;
  632. const match = link.match(regex);
  633. if (!match) return null;
  634. let [, protocol, userData, address, port, ] = match;
  635. port *= 1;
  636. if(protocol == 'ss') {
  637. protocol = 'shadowsocks';
  638. userData = atob(userData).split(':');
  639. }
  640. var settings;
  641. switch(protocol){
  642. case Protocols.VLESS:
  643. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '');
  644. break;
  645. case Protocols.Trojan:
  646. settings = new Outbound.TrojanSettings(address, port, userData);
  647. break;
  648. case Protocols.Shadowsocks:
  649. let method = userData.splice(0,1)[0];
  650. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  651. break;
  652. default:
  653. return null;
  654. }
  655. let remark = decodeURIComponent(url.hash);
  656. // Remove '#' from url.hash
  657. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  658. return new Outbound(remark, protocol, settings, stream);
  659. }
  660. }
  661. Outbound.Settings = class extends CommonClass {
  662. constructor(protocol) {
  663. super();
  664. this.protocol = protocol;
  665. }
  666. static getSettings(protocol) {
  667. switch (protocol) {
  668. case Protocols.Freedom: return new Outbound.FreedomSettings();
  669. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  670. case Protocols.DNS: return new Outbound.DNSSettings();
  671. case Protocols.VMess: return new Outbound.VmessSettings();
  672. case Protocols.VLESS: return new Outbound.VLESSSettings();
  673. case Protocols.Trojan: return new Outbound.TrojanSettings();
  674. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  675. case Protocols.Socks: return new Outbound.SocksSettings();
  676. case Protocols.HTTP: return new Outbound.HttpSettings();
  677. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  678. default: return null;
  679. }
  680. }
  681. static fromJson(protocol, json) {
  682. switch (protocol) {
  683. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  684. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  685. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  686. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  687. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  688. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  689. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  690. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  691. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  692. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  693. default: return null;
  694. }
  695. }
  696. toJson() {
  697. return {};
  698. }
  699. };
  700. Outbound.FreedomSettings = class extends CommonClass {
  701. constructor(domainStrategy='', fragment={}) {
  702. super();
  703. this.domainStrategy = domainStrategy;
  704. this.fragment = fragment;
  705. }
  706. static fromJson(json={}) {
  707. return new Outbound.FreedomSettings(
  708. json.domainStrategy,
  709. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  710. );
  711. }
  712. toJson() {
  713. return {
  714. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  715. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  716. };
  717. }
  718. };
  719. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  720. constructor(packets='1-3',length='',interval=''){
  721. super();
  722. this.packets = packets;
  723. this.length = length;
  724. this.interval = interval;
  725. }
  726. static fromJson(json={}) {
  727. return new Outbound.FreedomSettings.Fragment(
  728. json.packets,
  729. json.length,
  730. json.interval,
  731. );
  732. }
  733. };
  734. Outbound.BlackholeSettings = class extends CommonClass {
  735. constructor(type) {
  736. super();
  737. this.type;
  738. }
  739. static fromJson(json={}) {
  740. return new Outbound.BlackholeSettings(
  741. json.response ? json.response.type : undefined,
  742. );
  743. }
  744. toJson() {
  745. return {
  746. response: ObjectUtil.isEmpty(this.type) ? undefined : {type: this.type},
  747. };
  748. }
  749. };
  750. Outbound.DNSSettings = class extends CommonClass {
  751. constructor(network='udp', address='1.1.1.1', port=53) {
  752. super();
  753. this.network = network;
  754. this.address = address;
  755. this.port = port;
  756. }
  757. static fromJson(json={}){
  758. return new Outbound.DNSSettings(
  759. json.network,
  760. json.address,
  761. json.port,
  762. );
  763. }
  764. };
  765. Outbound.VmessSettings = class extends CommonClass {
  766. constructor(address, port, id) {
  767. super();
  768. this.address = address;
  769. this.port = port;
  770. this.id = id;
  771. }
  772. static fromJson(json={}) {
  773. if(ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  774. return new Outbound.VmessSettings(
  775. json.vnext[0].address,
  776. json.vnext[0].port,
  777. json.vnext[0].users[0].id,
  778. );
  779. }
  780. toJson() {
  781. return {
  782. vnext: [{
  783. address: this.address,
  784. port: this.port,
  785. users: [{id: this.id}],
  786. }],
  787. };
  788. }
  789. };
  790. Outbound.VLESSSettings = class extends CommonClass {
  791. constructor(address, port, id, flow, encryption='none') {
  792. super();
  793. this.address = address;
  794. this.port = port;
  795. this.id = id;
  796. this.flow = flow;
  797. this.encryption = encryption
  798. }
  799. static fromJson(json={}) {
  800. if(ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  801. return new Outbound.VLESSSettings(
  802. json.vnext[0].address,
  803. json.vnext[0].port,
  804. json.vnext[0].users[0].id,
  805. json.vnext[0].users[0].flow,
  806. json.vnext[0].users[0].encryption,
  807. );
  808. }
  809. toJson() {
  810. return {
  811. vnext: [{
  812. address: this.address,
  813. port: this.port,
  814. users: [{id: this.id, flow: this.flow, encryption: 'none',}],
  815. }],
  816. };
  817. }
  818. };
  819. Outbound.TrojanSettings = class extends CommonClass {
  820. constructor(address, port, password) {
  821. super();
  822. this.address = address;
  823. this.port = port;
  824. this.password = password;
  825. }
  826. static fromJson(json={}) {
  827. if(ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  828. return new Outbound.TrojanSettings(
  829. json.servers[0].address,
  830. json.servers[0].port,
  831. json.servers[0].password,
  832. );
  833. }
  834. toJson() {
  835. return {
  836. servers: [{
  837. address: this.address,
  838. port: this.port,
  839. password: this.password,
  840. }],
  841. };
  842. }
  843. };
  844. Outbound.ShadowsocksSettings = class extends CommonClass {
  845. constructor(address, port, password, method, uot) {
  846. super();
  847. this.address = address;
  848. this.port = port;
  849. this.password = password;
  850. this.method = method;
  851. this.uot = uot;
  852. }
  853. static fromJson(json={}) {
  854. let servers = json.servers;
  855. if(ObjectUtil.isArrEmpty(servers)) servers=[{}];
  856. return new Outbound.ShadowsocksSettings(
  857. servers[0].address,
  858. servers[0].port,
  859. servers[0].password,
  860. servers[0].method,
  861. servers[0].uot,
  862. );
  863. }
  864. toJson() {
  865. return {
  866. servers: [{
  867. address: this.address,
  868. port: this.port,
  869. password: this.password,
  870. method: this.method,
  871. uot: this.uot,
  872. }],
  873. };
  874. }
  875. };
  876. Outbound.SocksSettings = class extends CommonClass {
  877. constructor(address, port, user, pass) {
  878. super();
  879. this.address = address;
  880. this.port = port;
  881. this.user = user;
  882. this.pass = pass;
  883. }
  884. static fromJson(json={}) {
  885. let servers = json.servers;
  886. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  887. return new Outbound.SocksSettings(
  888. servers[0].address,
  889. servers[0].port,
  890. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  891. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  892. );
  893. }
  894. toJson() {
  895. return {
  896. servers: [{
  897. address: this.address,
  898. port: this.port,
  899. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  900. }],
  901. };
  902. }
  903. };
  904. Outbound.HttpSettings = class extends CommonClass {
  905. constructor(address, port, user, pass) {
  906. super();
  907. this.address = address;
  908. this.port = port;
  909. this.user = user;
  910. this.pass = pass;
  911. }
  912. static fromJson(json={}) {
  913. let servers = json.servers;
  914. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  915. return new Outbound.HttpSettings(
  916. servers[0].address,
  917. servers[0].port,
  918. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  919. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  920. );
  921. }
  922. toJson() {
  923. return {
  924. servers: [{
  925. address: this.address,
  926. port: this.port,
  927. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  928. }],
  929. };
  930. }
  931. };
  932. Outbound.WireguardSettings = class extends CommonClass {
  933. constructor(
  934. mtu=1420, secretKey='',
  935. address=[''], workers=2, domainStrategy='', reserved='',
  936. peers=[new Outbound.WireguardSettings.Peer()], kernelMode=false) {
  937. super();
  938. this.mtu = mtu;
  939. this.secretKey = secretKey;
  940. this.pubKey = secretKey.length>0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  941. this.address = address instanceof Array ? address.join(',') : address;
  942. this.workers = workers;
  943. this.domainStrategy = domainStrategy;
  944. this.reserved = reserved instanceof Array ? reserved.join(',') : reserved;
  945. this.peers = peers;
  946. this.kernelMode = kernelMode;
  947. }
  948. addPeer() {
  949. this.peers.push(new Outbound.WireguardSettings.Peer());
  950. }
  951. delPeer(index) {
  952. this.peers.splice(index, 1);
  953. }
  954. static fromJson(json={}){
  955. return new Outbound.WireguardSettings(
  956. json.mtu,
  957. json.secretKey,
  958. json.address,
  959. json.workers,
  960. json.domainStrategy,
  961. json.reserved,
  962. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  963. json.kernelMode,
  964. );
  965. }
  966. toJson() {
  967. return {
  968. mtu: this.mtu?? undefined,
  969. secretKey: this.secretKey,
  970. address: this.address ? this.address.split(",") : [],
  971. workers: this.workers?? undefined,
  972. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  973. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  974. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  975. kernelMode: this.kernelMode,
  976. };
  977. }
  978. };
  979. Outbound.WireguardSettings.Peer = class extends CommonClass {
  980. constructor(publicKey='', psk='', allowedIPs=['0.0.0.0/0','::/0'], endpoint='', keepAlive=0) {
  981. super();
  982. this.publicKey = publicKey;
  983. this.psk = psk;
  984. this.allowedIPs = allowedIPs;
  985. this.endpoint = endpoint;
  986. this.keepAlive = keepAlive;
  987. }
  988. static fromJson(json={}){
  989. return new Outbound.WireguardSettings.Peer(
  990. json.publicKey,
  991. json.preSharedKey,
  992. json.allowedIPs,
  993. json.endpoint,
  994. json.keepAlive
  995. );
  996. }
  997. toJson() {
  998. return {
  999. publicKey: this.publicKey,
  1000. preSharedKey: this.psk.length>0 ? this.psk : undefined,
  1001. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1002. endpoint: this.endpoint,
  1003. keepAlive: this.keepAlive?? undefined,
  1004. };
  1005. }
  1006. };