1
0

outbound.js 35 KB

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