outbound.js 39 KB

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