outbound.js 37 KB

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