outbound.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. ) {
  836. super();
  837. this.packets = packets;
  838. this.length = length;
  839. this.interval = interval;
  840. }
  841. static fromJson(json = {}) {
  842. return new Outbound.FreedomSettings.Fragment(
  843. json.packets,
  844. json.length,
  845. json.interval,
  846. );
  847. }
  848. };
  849. Outbound.FreedomSettings.Noise = class extends CommonClass {
  850. constructor(
  851. type = 'rand',
  852. packet = '10-20',
  853. delay = '10-16'
  854. ) {
  855. super();
  856. this.type = type;
  857. this.packet = packet;
  858. this.delay = delay;
  859. }
  860. static fromJson(json = {}) {
  861. return new Outbound.FreedomSettings.Noise(
  862. json.type,
  863. json.packet,
  864. json.delay,
  865. );
  866. }
  867. toJson() {
  868. return {
  869. type: this.type,
  870. packet: this.packet,
  871. delay: this.delay,
  872. };
  873. }
  874. };
  875. Outbound.BlackholeSettings = class extends CommonClass {
  876. constructor(type) {
  877. super();
  878. this.type = type;
  879. }
  880. static fromJson(json = {}) {
  881. return new Outbound.BlackholeSettings(
  882. json.response ? json.response.type : undefined,
  883. );
  884. }
  885. toJson() {
  886. return {
  887. response: ObjectUtil.isEmpty(this.type) ? undefined : { type: this.type },
  888. };
  889. }
  890. };
  891. Outbound.DNSSettings = class extends CommonClass {
  892. constructor(
  893. network = 'udp',
  894. address = '',
  895. port = 53,
  896. nonIPQuery = 'drop',
  897. blockTypes = []
  898. ) {
  899. super();
  900. this.network = network;
  901. this.address = address;
  902. this.port = port;
  903. this.nonIPQuery = nonIPQuery;
  904. this.blockTypes = blockTypes;
  905. }
  906. static fromJson(json = {}) {
  907. return new Outbound.DNSSettings(
  908. json.network,
  909. json.address,
  910. json.port,
  911. json.nonIPQuery,
  912. json.blockTypes,
  913. );
  914. }
  915. };
  916. Outbound.VmessSettings = class extends CommonClass {
  917. constructor(address, port, id, security) {
  918. super();
  919. this.address = address;
  920. this.port = port;
  921. this.id = id;
  922. this.security = security;
  923. }
  924. static fromJson(json = {}) {
  925. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  926. return new Outbound.VmessSettings(
  927. json.vnext[0].address,
  928. json.vnext[0].port,
  929. json.vnext[0].users[0].id,
  930. json.vnext[0].users[0].security,
  931. );
  932. }
  933. toJson() {
  934. return {
  935. vnext: [{
  936. address: this.address,
  937. port: this.port,
  938. users: [{ id: this.id, security: this.security }],
  939. }],
  940. };
  941. }
  942. };
  943. Outbound.VLESSSettings = class extends CommonClass {
  944. constructor(address, port, id, flow, encryption = 'none') {
  945. super();
  946. this.address = address;
  947. this.port = port;
  948. this.id = id;
  949. this.flow = flow;
  950. this.encryption = encryption
  951. }
  952. static fromJson(json = {}) {
  953. if (ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  954. return new Outbound.VLESSSettings(
  955. json.vnext[0].address,
  956. json.vnext[0].port,
  957. json.vnext[0].users[0].id,
  958. json.vnext[0].users[0].flow,
  959. json.vnext[0].users[0].encryption,
  960. );
  961. }
  962. toJson() {
  963. return {
  964. vnext: [{
  965. address: this.address,
  966. port: this.port,
  967. users: [{ id: this.id, flow: this.flow, encryption: 'none', }],
  968. }],
  969. };
  970. }
  971. };
  972. Outbound.TrojanSettings = class extends CommonClass {
  973. constructor(address, port, password) {
  974. super();
  975. this.address = address;
  976. this.port = port;
  977. this.password = password;
  978. }
  979. static fromJson(json = {}) {
  980. if (ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  981. return new Outbound.TrojanSettings(
  982. json.servers[0].address,
  983. json.servers[0].port,
  984. json.servers[0].password,
  985. );
  986. }
  987. toJson() {
  988. return {
  989. servers: [{
  990. address: this.address,
  991. port: this.port,
  992. password: this.password,
  993. }],
  994. };
  995. }
  996. };
  997. Outbound.ShadowsocksSettings = class extends CommonClass {
  998. constructor(address, port, password, method, uot, UoTVersion) {
  999. super();
  1000. this.address = address;
  1001. this.port = port;
  1002. this.password = password;
  1003. this.method = method;
  1004. this.uot = uot;
  1005. this.UoTVersion = UoTVersion;
  1006. }
  1007. static fromJson(json = {}) {
  1008. let servers = json.servers;
  1009. if (ObjectUtil.isArrEmpty(servers)) servers = [{}];
  1010. return new Outbound.ShadowsocksSettings(
  1011. servers[0].address,
  1012. servers[0].port,
  1013. servers[0].password,
  1014. servers[0].method,
  1015. servers[0].uot,
  1016. servers[0].UoTVersion,
  1017. );
  1018. }
  1019. toJson() {
  1020. return {
  1021. servers: [{
  1022. address: this.address,
  1023. port: this.port,
  1024. password: this.password,
  1025. method: this.method,
  1026. uot: this.uot,
  1027. UoTVersion: this.UoTVersion,
  1028. }],
  1029. };
  1030. }
  1031. };
  1032. Outbound.SocksSettings = class extends CommonClass {
  1033. constructor(address, port, user, pass) {
  1034. super();
  1035. this.address = address;
  1036. this.port = port;
  1037. this.user = user;
  1038. this.pass = pass;
  1039. }
  1040. static fromJson(json = {}) {
  1041. let servers = json.servers;
  1042. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1043. return new Outbound.SocksSettings(
  1044. servers[0].address,
  1045. servers[0].port,
  1046. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1047. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1048. );
  1049. }
  1050. toJson() {
  1051. return {
  1052. servers: [{
  1053. address: this.address,
  1054. port: this.port,
  1055. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1056. }],
  1057. };
  1058. }
  1059. };
  1060. Outbound.HttpSettings = class extends CommonClass {
  1061. constructor(address, port, user, pass) {
  1062. super();
  1063. this.address = address;
  1064. this.port = port;
  1065. this.user = user;
  1066. this.pass = pass;
  1067. }
  1068. static fromJson(json = {}) {
  1069. let servers = json.servers;
  1070. if (ObjectUtil.isArrEmpty(servers)) servers = [{ users: [{}] }];
  1071. return new Outbound.HttpSettings(
  1072. servers[0].address,
  1073. servers[0].port,
  1074. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  1075. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  1076. );
  1077. }
  1078. toJson() {
  1079. return {
  1080. servers: [{
  1081. address: this.address,
  1082. port: this.port,
  1083. users: ObjectUtil.isEmpty(this.user) ? [] : [{ user: this.user, pass: this.pass }],
  1084. }],
  1085. };
  1086. }
  1087. };
  1088. Outbound.WireguardSettings = class extends CommonClass {
  1089. constructor(
  1090. mtu = 1420,
  1091. secretKey = '',
  1092. address = [''],
  1093. workers = 2,
  1094. domainStrategy = '',
  1095. reserved = '',
  1096. peers = [new Outbound.WireguardSettings.Peer()],
  1097. noKernelTun = false,
  1098. ) {
  1099. super();
  1100. this.mtu = mtu;
  1101. this.secretKey = secretKey;
  1102. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  1103. this.address = Array.isArray(address) ? address.join(',') : address;
  1104. this.workers = workers;
  1105. this.domainStrategy = domainStrategy;
  1106. this.reserved = Array.isArray(reserved) ? reserved.join(',') : reserved;
  1107. this.peers = peers;
  1108. this.noKernelTun = noKernelTun;
  1109. }
  1110. addPeer() {
  1111. this.peers.push(new Outbound.WireguardSettings.Peer());
  1112. }
  1113. delPeer(index) {
  1114. this.peers.splice(index, 1);
  1115. }
  1116. static fromJson(json = {}) {
  1117. return new Outbound.WireguardSettings(
  1118. json.mtu,
  1119. json.secretKey,
  1120. json.address,
  1121. json.workers,
  1122. json.domainStrategy,
  1123. json.reserved,
  1124. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  1125. json.noKernelTun,
  1126. );
  1127. }
  1128. toJson() {
  1129. return {
  1130. mtu: this.mtu ?? undefined,
  1131. secretKey: this.secretKey,
  1132. address: this.address ? this.address.split(",") : [],
  1133. workers: this.workers ?? undefined,
  1134. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  1135. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  1136. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  1137. noKernelTun: this.noKernelTun,
  1138. };
  1139. }
  1140. };
  1141. Outbound.WireguardSettings.Peer = class extends CommonClass {
  1142. constructor(
  1143. publicKey = '',
  1144. psk = '',
  1145. allowedIPs = ['0.0.0.0/0', '::/0'],
  1146. endpoint = '',
  1147. keepAlive = 0
  1148. ) {
  1149. super();
  1150. this.publicKey = publicKey;
  1151. this.psk = psk;
  1152. this.allowedIPs = allowedIPs;
  1153. this.endpoint = endpoint;
  1154. this.keepAlive = keepAlive;
  1155. }
  1156. static fromJson(json = {}) {
  1157. return new Outbound.WireguardSettings.Peer(
  1158. json.publicKey,
  1159. json.preSharedKey,
  1160. json.allowedIPs,
  1161. json.endpoint,
  1162. json.keepAlive
  1163. );
  1164. }
  1165. toJson() {
  1166. return {
  1167. publicKey: this.publicKey,
  1168. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  1169. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1170. endpoint: this.endpoint,
  1171. keepAlive: this.keepAlive ?? undefined,
  1172. };
  1173. }
  1174. };