outbound.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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. echConfigList = '',
  324. ) {
  325. super();
  326. this.serverName = serverName;
  327. this.alpn = alpn;
  328. this.fingerprint = fingerprint;
  329. this.allowInsecure = allowInsecure;
  330. this.echConfigList = echConfigList;
  331. }
  332. static fromJson(json = {}) {
  333. return new TlsStreamSettings(
  334. json.serverName,
  335. json.alpn,
  336. json.fingerprint,
  337. json.allowInsecure,
  338. json.echConfigList,
  339. );
  340. }
  341. toJson() {
  342. return {
  343. serverName: this.serverName,
  344. alpn: this.alpn,
  345. fingerprint: this.fingerprint,
  346. allowInsecure: this.allowInsecure,
  347. echConfigList: this.echConfigList
  348. };
  349. }
  350. }
  351. class RealityStreamSettings extends CommonClass {
  352. constructor(
  353. publicKey = '',
  354. fingerprint = '',
  355. serverName = '',
  356. shortId = '',
  357. spiderX = '',
  358. mldsa65Verify = ''
  359. ) {
  360. super();
  361. this.publicKey = publicKey;
  362. this.fingerprint = fingerprint;
  363. this.serverName = serverName;
  364. this.shortId = shortId
  365. this.spiderX = spiderX;
  366. this.mldsa65Verify = mldsa65Verify;
  367. }
  368. static fromJson(json = {}) {
  369. return new RealityStreamSettings(
  370. json.publicKey,
  371. json.fingerprint,
  372. json.serverName,
  373. json.shortId,
  374. json.spiderX,
  375. json.mldsa65Verify
  376. );
  377. }
  378. toJson() {
  379. return {
  380. publicKey: this.publicKey,
  381. fingerprint: this.fingerprint,
  382. serverName: this.serverName,
  383. shortId: this.shortId,
  384. spiderX: this.spiderX,
  385. mldsa65Verify: this.mldsa65Verify
  386. };
  387. }
  388. };
  389. class SockoptStreamSettings extends CommonClass {
  390. constructor(
  391. dialerProxy = "",
  392. tcpFastOpen = false,
  393. tcpKeepAliveInterval = 0,
  394. tcpMptcp = false,
  395. penetrate = false,
  396. addressPortStrategy = Address_Port_Strategy.NONE,
  397. ) {
  398. super();
  399. this.dialerProxy = dialerProxy;
  400. this.tcpFastOpen = tcpFastOpen;
  401. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  402. this.tcpMptcp = tcpMptcp;
  403. this.penetrate = penetrate;
  404. this.addressPortStrategy = addressPortStrategy;
  405. }
  406. static fromJson(json = {}) {
  407. if (Object.keys(json).length === 0) return undefined;
  408. return new SockoptStreamSettings(
  409. json.dialerProxy,
  410. json.tcpFastOpen,
  411. json.tcpKeepAliveInterval,
  412. json.tcpMptcp,
  413. json.penetrate,
  414. json.addressPortStrategy
  415. );
  416. }
  417. toJson() {
  418. return {
  419. dialerProxy: this.dialerProxy,
  420. tcpFastOpen: this.tcpFastOpen,
  421. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  422. tcpMptcp: this.tcpMptcp,
  423. penetrate: this.penetrate,
  424. addressPortStrategy: this.addressPortStrategy
  425. };
  426. }
  427. }
  428. class StreamSettings extends CommonClass {
  429. constructor(
  430. network = 'tcp',
  431. security = 'none',
  432. tlsSettings = new TlsStreamSettings(),
  433. realitySettings = new RealityStreamSettings(),
  434. tcpSettings = new TcpStreamSettings(),
  435. kcpSettings = new KcpStreamSettings(),
  436. wsSettings = new WsStreamSettings(),
  437. grpcSettings = new GrpcStreamSettings(),
  438. httpupgradeSettings = new HttpUpgradeStreamSettings(),
  439. xhttpSettings = new xHTTPStreamSettings(),
  440. sockopt = undefined,
  441. ) {
  442. super();
  443. this.network = network;
  444. this.security = security;
  445. this.tls = tlsSettings;
  446. this.reality = realitySettings;
  447. this.tcp = tcpSettings;
  448. this.kcp = kcpSettings;
  449. this.ws = wsSettings;
  450. this.grpc = grpcSettings;
  451. this.httpupgrade = httpupgradeSettings;
  452. this.xhttp = xhttpSettings;
  453. this.sockopt = sockopt;
  454. }
  455. get isTls() {
  456. return this.security === 'tls';
  457. }
  458. get isReality() {
  459. return this.security === "reality";
  460. }
  461. get sockoptSwitch() {
  462. return this.sockopt != undefined;
  463. }
  464. set sockoptSwitch(value) {
  465. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  466. }
  467. static fromJson(json = {}) {
  468. return new StreamSettings(
  469. json.network,
  470. json.security,
  471. TlsStreamSettings.fromJson(json.tlsSettings),
  472. RealityStreamSettings.fromJson(json.realitySettings),
  473. TcpStreamSettings.fromJson(json.tcpSettings),
  474. KcpStreamSettings.fromJson(json.kcpSettings),
  475. WsStreamSettings.fromJson(json.wsSettings),
  476. GrpcStreamSettings.fromJson(json.grpcSettings),
  477. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  478. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  479. SockoptStreamSettings.fromJson(json.sockopt),
  480. );
  481. }
  482. toJson() {
  483. const network = this.network;
  484. return {
  485. network: network,
  486. security: this.security,
  487. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  488. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  489. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  490. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  491. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  492. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  493. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  494. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  495. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  496. };
  497. }
  498. }
  499. class Mux extends CommonClass {
  500. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  501. super();
  502. this.enabled = enabled;
  503. this.concurrency = concurrency;
  504. this.xudpConcurrency = xudpConcurrency;
  505. this.xudpProxyUDP443 = xudpProxyUDP443;
  506. }
  507. static fromJson(json = {}) {
  508. if (Object.keys(json).length === 0) return undefined;
  509. return new Mux(
  510. json.enabled,
  511. json.concurrency,
  512. json.xudpConcurrency,
  513. json.xudpProxyUDP443,
  514. );
  515. }
  516. toJson() {
  517. return {
  518. enabled: this.enabled,
  519. concurrency: this.concurrency,
  520. xudpConcurrency: this.xudpConcurrency,
  521. xudpProxyUDP443: this.xudpProxyUDP443,
  522. };
  523. }
  524. }
  525. class Outbound extends CommonClass {
  526. constructor(
  527. tag = '',
  528. protocol = Protocols.VLESS,
  529. settings = null,
  530. streamSettings = new StreamSettings(),
  531. sendThrough,
  532. mux = new Mux(),
  533. ) {
  534. super();
  535. this.tag = tag;
  536. this._protocol = protocol;
  537. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  538. this.stream = streamSettings;
  539. this.sendThrough = sendThrough;
  540. this.mux = mux;
  541. }
  542. get protocol() {
  543. return this._protocol;
  544. }
  545. set protocol(protocol) {
  546. this._protocol = protocol;
  547. this.settings = Outbound.Settings.getSettings(protocol);
  548. this.stream = new StreamSettings();
  549. }
  550. canEnableTls() {
  551. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol)) return false;
  552. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.stream.network);
  553. }
  554. //this is used for xtls-rprx-vision
  555. canEnableTlsFlow() {
  556. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  557. return this.protocol === Protocols.VLESS;
  558. }
  559. return false;
  560. }
  561. canEnableReality() {
  562. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  563. return ["tcp", "http", "grpc", "xhttp"].includes(this.stream.network);
  564. }
  565. canEnableStream() {
  566. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol);
  567. }
  568. canEnableMux() {
  569. // Disable Mux if flow is set
  570. if (this.settings.flow && this.settings.flow !== '') {
  571. this.mux.enabled = false;
  572. return false;
  573. }
  574. // Disable Mux if network is xhttp
  575. if (this.stream.network === 'xhttp') {
  576. this.mux.enabled = false;
  577. return false;
  578. }
  579. // Allow Mux only for these protocols
  580. return [
  581. Protocols.VMess,
  582. Protocols.VLESS,
  583. Protocols.Trojan,
  584. Protocols.Shadowsocks,
  585. Protocols.HTTP,
  586. Protocols.Socks
  587. ].includes(this.protocol);
  588. }
  589. hasVnext() {
  590. return [Protocols.VMess, Protocols.VLESS].includes(this.protocol);
  591. }
  592. hasServers() {
  593. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  594. }
  595. hasAddressPort() {
  596. return [
  597. Protocols.DNS,
  598. Protocols.VMess,
  599. Protocols.VLESS,
  600. Protocols.Trojan,
  601. Protocols.Shadowsocks,
  602. Protocols.Socks,
  603. Protocols.HTTP
  604. ].includes(this.protocol);
  605. }
  606. hasUsername() {
  607. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  608. }
  609. static fromJson(json = {}) {
  610. return new Outbound(
  611. json.tag,
  612. json.protocol,
  613. Outbound.Settings.fromJson(json.protocol, json.settings),
  614. StreamSettings.fromJson(json.streamSettings),
  615. json.sendThrough,
  616. Mux.fromJson(json.mux),
  617. )
  618. }
  619. toJson() {
  620. var stream;
  621. if (this.canEnableStream()) {
  622. stream = this.stream.toJson();
  623. } else {
  624. if (this.stream?.sockopt)
  625. stream = { sockopt: this.stream.sockopt.toJson() };
  626. }
  627. return {
  628. tag: this.tag == '' ? undefined : this.tag,
  629. protocol: this.protocol,
  630. settings: this.settings instanceof CommonClass ? this.settings.toJson() : this.settings,
  631. streamSettings: stream,
  632. sendThrough: this.sendThrough != "" ? this.sendThrough : undefined,
  633. mux: this.mux?.enabled ? this.mux : undefined,
  634. };
  635. }
  636. static fromLink(link) {
  637. data = link.split('://');
  638. if (data.length != 2) return null;
  639. switch (data[0].toLowerCase()) {
  640. case Protocols.VMess:
  641. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  642. case Protocols.VLESS:
  643. case Protocols.Trojan:
  644. case 'ss':
  645. return this.fromParamLink(link);
  646. default:
  647. return null;
  648. }
  649. }
  650. static fromVmessLink(json = {}) {
  651. let stream = new StreamSettings(json.net, json.tls);
  652. let network = json.net;
  653. if (network === 'tcp') {
  654. stream.tcp = new TcpStreamSettings(
  655. json.type,
  656. json.host ?? '',
  657. json.path ?? '');
  658. } else if (network === 'kcp') {
  659. stream.kcp = new KcpStreamSettings();
  660. stream.type = json.type;
  661. stream.seed = json.path;
  662. } else if (network === 'ws') {
  663. stream.ws = new WsStreamSettings(json.path, json.host);
  664. } else if (network === 'grpc') {
  665. stream.grpc = new GrpcStreamSettings(json.path, json.authority, json.type == 'multi');
  666. } else if (network === 'httpupgrade') {
  667. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path, json.host);
  668. } else if (network === 'xhttp') {
  669. stream.xhttp = new xHTTPStreamSettings(json.path, json.host, json.mode);
  670. }
  671. if (json.tls && json.tls == 'tls') {
  672. stream.tls = new TlsStreamSettings(
  673. json.sni,
  674. json.alpn ? json.alpn.split(',') : [],
  675. json.fp,
  676. json.allowInsecure);
  677. }
  678. const port = json.port * 1;
  679. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, port, json.id, json.scy), stream);
  680. }
  681. static fromParamLink(link) {
  682. const url = new URL(link);
  683. let type = url.searchParams.get('type') ?? 'tcp';
  684. let security = url.searchParams.get('security') ?? 'none';
  685. let stream = new StreamSettings(type, security);
  686. let headerType = url.searchParams.get('headerType') ?? undefined;
  687. let host = url.searchParams.get('host') ?? undefined;
  688. let path = url.searchParams.get('path') ?? undefined;
  689. let mode = url.searchParams.get('mode') ?? undefined;
  690. if (type === 'tcp' || type === 'none') {
  691. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  692. } else if (type === 'kcp') {
  693. stream.kcp = new KcpStreamSettings();
  694. stream.kcp.type = headerType ?? 'none';
  695. stream.kcp.seed = path;
  696. } else if (type === 'ws') {
  697. stream.ws = new WsStreamSettings(path, host);
  698. } else if (type === 'grpc') {
  699. stream.grpc = new GrpcStreamSettings(
  700. url.searchParams.get('serviceName') ?? '',
  701. url.searchParams.get('authority') ?? '',
  702. url.searchParams.get('mode') == 'multi');
  703. } else if (type === 'httpupgrade') {
  704. stream.httpupgrade = new HttpUpgradeStreamSettings(path, host);
  705. } else if (type === 'xhttp') {
  706. stream.xhttp = new xHTTPStreamSettings(path, host, mode);
  707. }
  708. if (security == 'tls') {
  709. let fp = url.searchParams.get('fp') ?? 'none';
  710. let alpn = url.searchParams.get('alpn');
  711. let allowInsecure = url.searchParams.get('allowInsecure');
  712. let sni = url.searchParams.get('sni') ?? '';
  713. let ech = url.searchParams.get('ech') ?? '';
  714. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1, ech);
  715. }
  716. if (security == 'reality') {
  717. let pbk = url.searchParams.get('pbk');
  718. let fp = url.searchParams.get('fp');
  719. let sni = url.searchParams.get('sni') ?? '';
  720. let sid = url.searchParams.get('sid') ?? '';
  721. let spx = url.searchParams.get('spx') ?? '';
  722. let pqv = url.searchParams.get('pqv') ?? '';
  723. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx, pqv);
  724. }
  725. const regex = /([^@]+):\/\/([^@]+)@(.+):(\d+)(.*)$/;
  726. const match = link.match(regex);
  727. if (!match) return null;
  728. let [, protocol, userData, address, port,] = match;
  729. port *= 1;
  730. if (protocol == 'ss') {
  731. protocol = 'shadowsocks';
  732. userData = atob(userData).split(':');
  733. }
  734. var settings;
  735. switch (protocol) {
  736. case Protocols.VLESS:
  737. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '');
  738. break;
  739. case Protocols.Trojan:
  740. settings = new Outbound.TrojanSettings(address, port, userData);
  741. break;
  742. case Protocols.Shadowsocks:
  743. let method = userData.splice(0, 1)[0];
  744. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  745. break;
  746. default:
  747. return null;
  748. }
  749. let remark = decodeURIComponent(url.hash);
  750. // Remove '#' from url.hash
  751. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  752. return new Outbound(remark, protocol, settings, stream);
  753. }
  754. }
  755. Outbound.Settings = class extends CommonClass {
  756. constructor(protocol) {
  757. super();
  758. this.protocol = protocol;
  759. }
  760. static getSettings(protocol) {
  761. switch (protocol) {
  762. case Protocols.Freedom: return new Outbound.FreedomSettings();
  763. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  764. case Protocols.DNS: return new Outbound.DNSSettings();
  765. case Protocols.VMess: return new Outbound.VmessSettings();
  766. case Protocols.VLESS: return new Outbound.VLESSSettings();
  767. case Protocols.Trojan: return new Outbound.TrojanSettings();
  768. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  769. case Protocols.Socks: return new Outbound.SocksSettings();
  770. case Protocols.HTTP: return new Outbound.HttpSettings();
  771. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  772. default: return null;
  773. }
  774. }
  775. static fromJson(protocol, json) {
  776. switch (protocol) {
  777. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  778. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  779. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  780. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  781. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  782. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  783. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  784. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  785. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  786. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  787. default: return null;
  788. }
  789. }
  790. toJson() {
  791. return {};
  792. }
  793. };
  794. Outbound.FreedomSettings = class extends CommonClass {
  795. constructor(
  796. domainStrategy = '',
  797. redirect = '',
  798. fragment = {},
  799. noises = []
  800. ) {
  801. super();
  802. this.domainStrategy = domainStrategy;
  803. this.redirect = redirect;
  804. this.fragment = fragment;
  805. this.noises = noises;
  806. }
  807. addNoise() {
  808. this.noises.push(new Outbound.FreedomSettings.Noise());
  809. }
  810. delNoise(index) {
  811. this.noises.splice(index, 1);
  812. }
  813. static fromJson(json = {}) {
  814. return new Outbound.FreedomSettings(
  815. json.domainStrategy,
  816. json.redirect,
  817. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  818. json.noises ? json.noises.map(noise => Outbound.FreedomSettings.Noise.fromJson(noise)) : undefined,
  819. );
  820. }
  821. toJson() {
  822. return {
  823. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  824. redirect: ObjectUtil.isEmpty(this.redirect) ? undefined: this.redirect,
  825. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  826. noises: this.noises.length === 0 ? undefined : Outbound.FreedomSettings.Noise.toJsonArray(this.noises),
  827. };
  828. }
  829. };
  830. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  831. constructor(
  832. packets = '1-3',
  833. length = '',
  834. interval = '',
  835. maxSplit = ''
  836. ) {
  837. super();
  838. this.packets = packets;
  839. this.length = length;
  840. this.interval = interval;
  841. this.maxSplit = maxSplit;
  842. }
  843. static fromJson(json = {}) {
  844. return new Outbound.FreedomSettings.Fragment(
  845. json.packets,
  846. json.length,
  847. json.interval,
  848. json.maxSplit
  849. );
  850. }
  851. };
  852. Outbound.FreedomSettings.Noise = class extends CommonClass {
  853. constructor(
  854. type = 'rand',
  855. packet = '10-20',
  856. delay = '10-16',
  857. applyTo = 'ip'
  858. ) {
  859. super();
  860. this.type = type;
  861. this.packet = packet;
  862. this.delay = delay;
  863. this.applyTo = applyTo;
  864. }
  865. static fromJson(json = {}) {
  866. return new Outbound.FreedomSettings.Noise(
  867. json.type,
  868. json.packet,
  869. json.delay,
  870. json.applyTo
  871. );
  872. }
  873. toJson() {
  874. return {
  875. type: this.type,
  876. packet: this.packet,
  877. delay: this.delay,
  878. applyTo: this.applyTo
  879. };
  880. }
  881. };
  882. Outbound.BlackholeSettings = class extends CommonClass {
  883. constructor(type) {
  884. super();
  885. this.type = type;
  886. }
  887. static fromJson(json = {}) {
  888. return new Outbound.BlackholeSettings(
  889. json.response ? json.response.type : undefined,
  890. );
  891. }
  892. toJson() {
  893. return {
  894. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  895. };
  896. }
  897. };
  898. Outbound.DNSSettings = class extends CommonClass {
  899. constructor(
  900. network = 'udp',
  901. address = '',
  902. port = 53,
  903. nonIPQuery = 'drop',
  904. blockTypes = []
  905. ) {
  906. super();
  907. this.network = network;
  908. this.address = address;
  909. this.port = port;
  910. this.nonIPQuery = nonIPQuery;
  911. this.blockTypes = blockTypes;
  912. }
  913. static fromJson(json = {}) {
  914. return new Outbound.DNSSettings(
  915. json.network,
  916. json.address,
  917. json.port,
  918. json.nonIPQuery,
  919. json.blockTypes,
  920. );
  921. }
  922. };
  923. Outbound.VmessSettings = class extends CommonClass {
  924. constructor(address, port, id, security) {
  925. super();
  926. this.address = address;
  927. this.port = port;
  928. this.id = id;
  929. this.security = security;
  930. }
  931. static fromJson(json = {}) {
  932. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  933. return new Outbound.VmessSettings(
  934. json.vnext[0].address,
  935. json.vnext[0].port,
  936. json.vnext[0].users[0].id,
  937. json.vnext[0].users[0].security,
  938. );
  939. }
  940. toJson() {
  941. return {
  942. vnext: [{
  943. address: this.address,
  944. port: this.port,
  945. users: [{ id: this.id, security: this.security }],
  946. }],
  947. };
  948. }
  949. };
  950. Outbound.VLESSSettings = class extends CommonClass {
  951. constructor(address, port, id, flow, encryption = 'none') {
  952. super();
  953. this.address = address;
  954. this.port = port;
  955. this.id = id;
  956. this.flow = flow;
  957. this.encryption = encryption
  958. }
  959. static fromJson(json = {}) {
  960. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  961. return new Outbound.VLESSSettings(
  962. json.vnext[0].address,
  963. json.vnext[0].port,
  964. json.vnext[0].users[0].id,
  965. json.vnext[0].users[0].flow,
  966. json.vnext[0].users[0].encryption,
  967. );
  968. }
  969. toJson() {
  970. return {
  971. vnext: [{
  972. address: this.address,
  973. port: this.port,
  974. users: [{ id: this.id, flow: this.flow, encryption: 'none', }],
  975. }],
  976. };
  977. }
  978. };
  979. Outbound.TrojanSettings = class extends CommonClass {
  980. constructor(address, port, password) {
  981. super();
  982. this.address = address;
  983. this.port = port;
  984. this.password = password;
  985. }
  986. static fromJson(json = {}) {
  987. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  988. return new Outbound.TrojanSettings(
  989. json.servers[0].address,
  990. json.servers[0].port,
  991. json.servers[0].password,
  992. );
  993. }
  994. toJson() {
  995. return {
  996. servers: [{
  997. address: this.address,
  998. port: this.port,
  999. password: this.password,
  1000. }],
  1001. };
  1002. }
  1003. };
  1004. Outbound.ShadowsocksSettings = class extends CommonClass {
  1005. constructor(address, port, password, method, uot, UoTVersion) {
  1006. super();
  1007. this.address = address;
  1008. this.port = port;
  1009. this.password = password;
  1010. this.method = method;
  1011. this.uot = uot;
  1012. this.UoTVersion = UoTVersion;
  1013. }
  1014. static fromJson(json = {}) {
  1015. let servers = json.servers;
  1016. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  1017. return new Outbound.ShadowsocksSettings(
  1018. servers[0].address,
  1019. servers[0].port,
  1020. servers[0].password,
  1021. servers[0].method,
  1022. servers[0].uot,
  1023. servers[0].UoTVersion,
  1024. );
  1025. }
  1026. toJson() {
  1027. return {
  1028. servers: [{
  1029. address: this.address,
  1030. port: this.port,
  1031. password: this.password,
  1032. method: this.method,
  1033. uot: this.uot,
  1034. UoTVersion: this.UoTVersion,
  1035. }],
  1036. };
  1037. }
  1038. };
  1039. Outbound.SocksSettings = class extends CommonClass {
  1040. constructor(address, port, user, pass) {
  1041. super();
  1042. this.address = address;
  1043. this.port = port;
  1044. this.user = user;
  1045. this.pass = pass;
  1046. }
  1047. static fromJson(json = {}) {
  1048. let servers = json.servers;
  1049. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1050. return new Outbound.SocksSettings(
  1051. servers[0].address,
  1052. servers[0].port,
  1053. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1054. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1055. );
  1056. }
  1057. toJson() {
  1058. return {
  1059. servers: [{
  1060. address: this.address,
  1061. port: this.port,
  1062. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1063. }],
  1064. };
  1065. }
  1066. };
  1067. Outbound.HttpSettings = class extends CommonClass {
  1068. constructor(address, port, user, pass) {
  1069. super();
  1070. this.address = address;
  1071. this.port = port;
  1072. this.user = user;
  1073. this.pass = pass;
  1074. }
  1075. static fromJson(json = {}) {
  1076. let servers = json.servers;
  1077. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1078. return new Outbound.HttpSettings(
  1079. servers[0].address,
  1080. servers[0].port,
  1081. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1082. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1083. );
  1084. }
  1085. toJson() {
  1086. return {
  1087. servers: [{
  1088. address: this.address,
  1089. port: this.port,
  1090. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1091. }],
  1092. };
  1093. }
  1094. };
  1095. Outbound.WireguardSettings = class extends CommonClass {
  1096. constructor(
  1097. mtu = 1420,
  1098. secretKey = '',
  1099. address = [''],
  1100. workers = 2,
  1101. domainStrategy = '',
  1102. reserved = '',
  1103. peers = [new Outbound.WireguardSettings.Peer()],
  1104. noKernelTun = false,
  1105. ) {
  1106. super();
  1107. this.mtu = mtu;
  1108. this.secretKey = secretKey;
  1109. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1110. this.address = Array.isArray(address) ? address.join(',') : address;
  1111. this.workers = workers;
  1112. this.domainStrategy = domainStrategy;
  1113. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1114. this.peers = peers;
  1115. this.noKernelTun = noKernelTun;
  1116. }
  1117. addPeer() {
  1118. this.peers.push(new Outbound.WireguardSettings.Peer());
  1119. }
  1120. delPeer(index) {
  1121. this.peers.splice(index, 1);
  1122. }
  1123. static fromJson(json = {}) {
  1124. return new Outbound.WireguardSettings(
  1125. json.mtu,
  1126. json.secretKey,
  1127. json.address,
  1128. json.workers,
  1129. json.domainStrategy,
  1130. json.reserved,
  1131. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1132. json.noKernelTun,
  1133. );
  1134. }
  1135. toJson() {
  1136. return {
  1137. mtu: this.mtu ?? undefined,
  1138. secretKey: this.secretKey,
  1139. address: this.address ? this.address.split(",") : [],
  1140. workers: this.workers ?? undefined,
  1141. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1142. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1143. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1144. noKernelTun: this.noKernelTun,
  1145. };
  1146. }
  1147. };
  1148. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1149. constructor(
  1150. publicKey = '',
  1151. psk = '',
  1152. allowedIPs = ['0.0.0.0/0', '::/0'],
  1153. endpoint = '',
  1154. keepAlive = 0
  1155. ) {
  1156. super();
  1157. this.publicKey = publicKey;
  1158. this.psk = psk;
  1159. this.allowedIPs = allowedIPs;
  1160. this.endpoint = endpoint;
  1161. this.keepAlive = keepAlive;
  1162. }
  1163. static fromJson(json = {}) {
  1164. return new Outbound.WireguardSettings.Peer(
  1165. json.publicKey,
  1166. json.preSharedKey,
  1167. json.allowedIPs,
  1168. json.endpoint,
  1169. json.keepAlive
  1170. );
  1171. }
  1172. toJson() {
  1173. return {
  1174. publicKey: this.publicKey,
  1175. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1176. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1177. endpoint: this.endpoint,
  1178. keepAlive: this.keepAlive ?? undefined,
  1179. };
  1180. }
  1181. };