outbound.js 36 KB

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