outbound.js 37 KB

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