1
0

outbound.js 35 KB

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