outbound.js 34 KB

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