outbound.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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.headers && !ObjectUtil.isEmpty(json.headers.Host) ? json.headers.Host : '',
  178. );
  179. }
  180. toJson() {
  181. return {
  182. path: this.path,
  183. headers: ObjectUtil.isEmpty(this.host) ? undefined : {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="", multiMode=false) {
  233. super();
  234. this.serviceName = serviceName;
  235. this.multiMode = multiMode;
  236. }
  237. static fromJson(json={}) {
  238. return new GrpcStreamSettings(json.serviceName, json.multiMode);
  239. }
  240. toJson() {
  241. return {
  242. serviceName: this.serviceName,
  243. multiMode: this.multiMode,
  244. }
  245. }
  246. }
  247. class HttpUpgradeStreamSettings extends CommonClass {
  248. constructor(path='/', host='') {
  249. super();
  250. this.path = path;
  251. this.host = host;
  252. }
  253. static fromJson(json={}) {
  254. return new HttpUpgradeStreamSettings(
  255. json.path,
  256. json.Host,
  257. );
  258. }
  259. toJson() {
  260. return {
  261. path: this.path,
  262. host: this.host,
  263. };
  264. }
  265. }
  266. class TlsStreamSettings extends CommonClass {
  267. constructor(serverName='',
  268. alpn=[],
  269. fingerprint = '',
  270. allowInsecure = false) {
  271. super();
  272. this.serverName = serverName;
  273. this.alpn = alpn;
  274. this.fingerprint = fingerprint;
  275. this.allowInsecure = allowInsecure;
  276. }
  277. static fromJson(json={}) {
  278. return new TlsStreamSettings(
  279. json.serverName,
  280. json.alpn,
  281. json.fingerprint,
  282. json.allowInsecure,
  283. );
  284. }
  285. toJson() {
  286. return {
  287. serverName: this.serverName,
  288. alpn: this.alpn,
  289. fingerprint: this.fingerprint,
  290. allowInsecure: this.allowInsecure,
  291. };
  292. }
  293. }
  294. class RealityStreamSettings extends CommonClass {
  295. constructor(publicKey = '', fingerprint = '', serverName = '', shortId = '', spiderX = '/') {
  296. super();
  297. this.publicKey = publicKey;
  298. this.fingerprint = fingerprint;
  299. this.serverName = serverName;
  300. this.shortId = shortId
  301. this.spiderX = spiderX;
  302. }
  303. static fromJson(json = {}) {
  304. return new RealityStreamSettings(
  305. json.publicKey,
  306. json.fingerprint,
  307. json.serverName,
  308. json.shortId,
  309. json.spiderX,
  310. );
  311. }
  312. toJson() {
  313. return {
  314. publicKey: this.publicKey,
  315. fingerprint: this.fingerprint,
  316. serverName: this.serverName,
  317. shortId: this.shortId,
  318. spiderX: this.spiderX,
  319. };
  320. }
  321. };
  322. class SockoptStreamSettings extends CommonClass {
  323. constructor(dialerProxy = "", tcpFastOpen = false, tcpKeepAliveInterval = 0, tcpNoDelay = false) {
  324. super();
  325. this.dialerProxy = dialerProxy;
  326. this.tcpFastOpen = tcpFastOpen;
  327. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  328. this.tcpNoDelay = tcpNoDelay;
  329. }
  330. static fromJson(json = {}) {
  331. if (Object.keys(json).length === 0) return undefined;
  332. return new SockoptStreamSettings(
  333. json.dialerProxy,
  334. json.tcpFastOpen,
  335. json.tcpKeepAliveInterval,
  336. json.tcpNoDelay,
  337. );
  338. }
  339. toJson() {
  340. return {
  341. dialerProxy: this.dialerProxy,
  342. tcpFastOpen: this.tcpFastOpen,
  343. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  344. tcpNoDelay: this.tcpNoDelay,
  345. };
  346. }
  347. }
  348. class StreamSettings extends CommonClass {
  349. constructor(network='tcp',
  350. security='none',
  351. tlsSettings=new TlsStreamSettings(),
  352. realitySettings = new RealityStreamSettings(),
  353. tcpSettings=new TcpStreamSettings(),
  354. kcpSettings=new KcpStreamSettings(),
  355. wsSettings=new WsStreamSettings(),
  356. httpSettings=new HttpStreamSettings(),
  357. quicSettings=new QuicStreamSettings(),
  358. grpcSettings=new GrpcStreamSettings(),
  359. httpupgradeSettings=new HttpUpgradeStreamSettings(),
  360. sockopt = undefined,
  361. ) {
  362. super();
  363. this.network = network;
  364. this.security = security;
  365. this.tls = tlsSettings;
  366. this.reality = realitySettings;
  367. this.tcp = tcpSettings;
  368. this.kcp = kcpSettings;
  369. this.ws = wsSettings;
  370. this.http = httpSettings;
  371. this.quic = quicSettings;
  372. this.grpc = grpcSettings;
  373. this.httpupgrade = httpupgradeSettings;
  374. this.sockopt = sockopt;
  375. }
  376. get isTls() {
  377. return this.security === 'tls';
  378. }
  379. get isReality() {
  380. return this.security === "reality";
  381. }
  382. get sockoptSwitch() {
  383. return this.sockopt != undefined;
  384. }
  385. set sockoptSwitch(value) {
  386. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  387. }
  388. static fromJson(json={}) {
  389. return new StreamSettings(
  390. json.network,
  391. json.security,
  392. TlsStreamSettings.fromJson(json.tlsSettings),
  393. RealityStreamSettings.fromJson(json.realitySettings),
  394. TcpStreamSettings.fromJson(json.tcpSettings),
  395. KcpStreamSettings.fromJson(json.kcpSettings),
  396. WsStreamSettings.fromJson(json.wsSettings),
  397. HttpStreamSettings.fromJson(json.httpSettings),
  398. QuicStreamSettings.fromJson(json.quicSettings),
  399. GrpcStreamSettings.fromJson(json.grpcSettings),
  400. HttpUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  401. SockoptStreamSettings.fromJson(json.sockopt),
  402. );
  403. }
  404. toJson() {
  405. const network = this.network;
  406. return {
  407. network: network,
  408. security: this.security,
  409. tlsSettings: this.security == 'tls' ? this.tls.toJson() : undefined,
  410. realitySettings: this.security == 'reality' ? this.reality.toJson() : undefined,
  411. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  412. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  413. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  414. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  415. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  416. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  417. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  418. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  419. };
  420. }
  421. }
  422. class Mux extends CommonClass {
  423. constructor(enabled = false, concurrency = 8, xudpConcurrency = 16, xudpProxyUDP443 = "reject") {
  424. super();
  425. this.enabled = enabled;
  426. this.concurrency = concurrency;
  427. this.xudpConcurrency = xudpConcurrency;
  428. this.xudpProxyUDP443 = xudpProxyUDP443;
  429. }
  430. static fromJson(json = {}) {
  431. if (Object.keys(json).length === 0) return undefined;
  432. return new Mux(
  433. json.enabled,
  434. json.concurrency,
  435. json.xudpConcurrency,
  436. json.xudpProxyUDP443,
  437. );
  438. }
  439. toJson() {
  440. return {
  441. enabled: this.enabled,
  442. concurrency: this.concurrency,
  443. xudpConcurrency: this.xudpConcurrency,
  444. xudpProxyUDP443: this.xudpProxyUDP443,
  445. };
  446. }
  447. }
  448. class Outbound extends CommonClass {
  449. constructor(
  450. tag='',
  451. protocol=Protocols.VMess,
  452. settings=null,
  453. streamSettings = new StreamSettings(),
  454. sendThrough,
  455. mux = new Mux(),
  456. ) {
  457. super();
  458. this.tag = tag;
  459. this._protocol = protocol;
  460. this.settings = settings == null ? Outbound.Settings.getSettings(protocol) : settings;
  461. this.stream = streamSettings;
  462. this.sendThrough = sendThrough;
  463. this.mux = mux;
  464. }
  465. get protocol() {
  466. return this._protocol;
  467. }
  468. set protocol(protocol) {
  469. this._protocol = protocol;
  470. this.settings = Outbound.Settings.getSettings(protocol);
  471. this.stream = new StreamSettings();
  472. }
  473. canEnableTls() {
  474. if (![Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol)) return false;
  475. return ["tcp", "ws", "http", "quic", "grpc", "httpupgrade"].includes(this.stream.network);
  476. }
  477. //this is used for xtls-rprx-vision
  478. canEnableTlsFlow() {
  479. if ((this.stream.security != 'none') && (this.stream.network === "tcp")) {
  480. return this.protocol === Protocols.VLESS;
  481. }
  482. return false;
  483. }
  484. canEnableReality() {
  485. if (![Protocols.VLESS, Protocols.Trojan].includes(this.protocol)) return false;
  486. return ["tcp", "http", "grpc"].includes(this.stream.network);
  487. }
  488. canEnableStream() {
  489. return [Protocols.VMess, Protocols.VLESS, Protocols.Trojan, Protocols.Shadowsocks].includes(this.protocol);
  490. }
  491. hasVnext() {
  492. return [Protocols.VMess, Protocols.VLESS].includes(this.protocol);
  493. }
  494. hasServers() {
  495. return [Protocols.Trojan, Protocols.Shadowsocks, Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  496. }
  497. hasAddressPort() {
  498. return [
  499. Protocols.DNS,
  500. Protocols.VMess,
  501. Protocols.VLESS,
  502. Protocols.Trojan,
  503. Protocols.Shadowsocks,
  504. Protocols.Socks,
  505. Protocols.HTTP
  506. ].includes(this.protocol);
  507. }
  508. hasUsername() {
  509. return [Protocols.Socks, Protocols.HTTP].includes(this.protocol);
  510. }
  511. static fromJson(json={}) {
  512. return new Outbound(
  513. json.tag,
  514. json.protocol,
  515. Outbound.Settings.fromJson(json.protocol, json.settings),
  516. StreamSettings.fromJson(json.streamSettings),
  517. json.sendThrough,
  518. Mux.fromJson(json.mux),
  519. )
  520. }
  521. toJson() {
  522. var stream;
  523. if (this.canEnableStream()) {
  524. stream = this.stream.toJson();
  525. } else {
  526. if (this.stream?.sockopt)
  527. stream = { sockopt: this.stream.sockopt.toJson() };
  528. }
  529. return {
  530. tag: this.tag == '' ? undefined : this.tag,
  531. protocol: this.protocol,
  532. settings: this.settings instanceof CommonClass ? this.settings.toJson() : this.settings,
  533. streamSettings: stream,
  534. sendThrough: this.sendThrough != "" ? this.sendThrough : undefined,
  535. mux: this.mux?.enabled ? this.mux : undefined,
  536. };
  537. }
  538. static fromLink(link) {
  539. data = link.split('://');
  540. if(data.length !=2) return null;
  541. switch(data[0].toLowerCase()){
  542. case Protocols.VMess:
  543. return this.fromVmessLink(JSON.parse(Base64.decode(data[1])));
  544. case Protocols.VLESS:
  545. case Protocols.Trojan:
  546. case 'ss':
  547. return this.fromParamLink(link);
  548. default:
  549. return null;
  550. }
  551. }
  552. static fromVmessLink(json={}){
  553. let stream = new StreamSettings(json.net, json.tls);
  554. let network = json.net;
  555. if (network === 'tcp') {
  556. stream.tcp = new TcpStreamSettings(
  557. json.type,
  558. json.host ?? '',
  559. json.path ?? '');
  560. } else if (network === 'kcp') {
  561. stream.kcp = new KcpStreamSettings();
  562. stream.type = json.type;
  563. stream.seed = json.path;
  564. } else if (network === 'ws') {
  565. stream.ws = new WsStreamSettings(json.path,json.host);
  566. } else if (network === 'http' || network == 'h2') {
  567. stream.network = 'http'
  568. stream.http = new HttpStreamSettings(
  569. json.path,
  570. json.host);
  571. } else if (network === 'quic') {
  572. stream.quic = new QuicStreamSettings(
  573. json.host ? json.host : 'none',
  574. json.path,
  575. json.type ? json.type : 'none');
  576. } else if (network === 'grpc') {
  577. stream.grpc = new GrpcStreamSettings(json.path, json.type == 'multi');
  578. } else if (network === 'httpupgrade') {
  579. stream.httpupgrade = new HttpUpgradeStreamSettings(json.path,json.host);
  580. }
  581. if(json.tls && json.tls == 'tls'){
  582. stream.tls = new TlsStreamSettings(
  583. json.sni,
  584. json.alpn ? json.alpn.split(',') : [],
  585. json.fp,
  586. json.allowInsecure);
  587. }
  588. return new Outbound(json.ps, Protocols.VMess, new Outbound.VmessSettings(json.add, json.port, json.id), stream);
  589. }
  590. static fromParamLink(link){
  591. const url = new URL(link);
  592. let type = url.searchParams.get('type');
  593. let security = url.searchParams.get('security') ?? 'none';
  594. let stream = new StreamSettings(type, security);
  595. let headerType = url.searchParams.get('headerType');
  596. let host = url.searchParams.get('host');
  597. let path = url.searchParams.get('path');
  598. if (type === 'tcp') {
  599. stream.tcp = new TcpStreamSettings(headerType ?? 'none', host, path);
  600. } else if (type === 'kcp') {
  601. stream.kcp = new KcpStreamSettings();
  602. stream.kcp.type = headerType ?? 'none';
  603. stream.kcp.seed = path;
  604. } else if (type === 'ws') {
  605. stream.ws = new WsStreamSettings(path,host);
  606. } else if (type === 'http' || type == 'h2') {
  607. stream.http = new HttpStreamSettings(path,host);
  608. } else if (type === 'quic') {
  609. stream.quic = new QuicStreamSettings(
  610. url.searchParams.get('quicSecurity') ?? 'none',
  611. url.searchParams.get('key') ?? '',
  612. headerType ?? 'none');
  613. } else if (type === 'grpc') {
  614. stream.grpc = new GrpcStreamSettings(url.searchParams.get('serviceName') ?? '', url.searchParams.get('mode') == 'multi');
  615. } else if (type === 'httpupgrade') {
  616. stream.httpupgrade = new HttpUpgradeStreamSettings(path,host);
  617. }
  618. if(security == 'tls'){
  619. let fp=url.searchParams.get('fp') ?? 'none';
  620. let alpn=url.searchParams.get('alpn');
  621. let allowInsecure=url.searchParams.get('allowInsecure');
  622. let sni=url.searchParams.get('sni') ?? '';
  623. stream.tls = new TlsStreamSettings(sni, alpn ? alpn.split(',') : [], fp, allowInsecure == 1);
  624. }
  625. if(security == 'reality'){
  626. let pbk=url.searchParams.get('pbk');
  627. let fp=url.searchParams.get('fp');
  628. let sni=url.searchParams.get('sni') ?? '';
  629. let sid=url.searchParams.get('sid') ?? '';
  630. let spx=url.searchParams.get('spx') ?? '';
  631. stream.reality = new RealityStreamSettings(pbk, fp, sni, sid, spx);
  632. }
  633. let data = link.split('?');
  634. if(data.length != 2) return null;
  635. const regex = /([^@]+):\/\/([^@]+)@([^:]+):(\d+)\?(.*)$/;
  636. const match = link.match(regex);
  637. if (!match) return null;
  638. let [, protocol, userData, address, port, ] = match;
  639. port *= 1;
  640. if(protocol == 'ss') {
  641. protocol = 'shadowsocks';
  642. userData = atob(userData).split(':');
  643. }
  644. var settings;
  645. switch(protocol){
  646. case Protocols.VLESS:
  647. settings = new Outbound.VLESSSettings(address, port, userData, url.searchParams.get('flow') ?? '');
  648. break;
  649. case Protocols.Trojan:
  650. settings = new Outbound.TrojanSettings(address, port, userData);
  651. break;
  652. case Protocols.Shadowsocks:
  653. let method = userData.splice(0,1)[0];
  654. settings = new Outbound.ShadowsocksSettings(address, port, userData.join(":"), method, true);
  655. break;
  656. default:
  657. return null;
  658. }
  659. let remark = decodeURIComponent(url.hash);
  660. // Remove '#' from url.hash
  661. remark = remark.length > 0 ? remark.substring(1) : 'out-' + protocol + '-' + port;
  662. return new Outbound(remark, protocol, settings, stream);
  663. }
  664. }
  665. Outbound.Settings = class extends CommonClass {
  666. constructor(protocol) {
  667. super();
  668. this.protocol = protocol;
  669. }
  670. static getSettings(protocol) {
  671. switch (protocol) {
  672. case Protocols.Freedom: return new Outbound.FreedomSettings();
  673. case Protocols.Blackhole: return new Outbound.BlackholeSettings();
  674. case Protocols.DNS: return new Outbound.DNSSettings();
  675. case Protocols.VMess: return new Outbound.VmessSettings();
  676. case Protocols.VLESS: return new Outbound.VLESSSettings();
  677. case Protocols.Trojan: return new Outbound.TrojanSettings();
  678. case Protocols.Shadowsocks: return new Outbound.ShadowsocksSettings();
  679. case Protocols.Socks: return new Outbound.SocksSettings();
  680. case Protocols.HTTP: return new Outbound.HttpSettings();
  681. case Protocols.Wireguard: return new Outbound.WireguardSettings();
  682. default: return null;
  683. }
  684. }
  685. static fromJson(protocol, json) {
  686. switch (protocol) {
  687. case Protocols.Freedom: return Outbound.FreedomSettings.fromJson(json);
  688. case Protocols.Blackhole: return Outbound.BlackholeSettings.fromJson(json);
  689. case Protocols.DNS: return Outbound.DNSSettings.fromJson(json);
  690. case Protocols.VMess: return Outbound.VmessSettings.fromJson(json);
  691. case Protocols.VLESS: return Outbound.VLESSSettings.fromJson(json);
  692. case Protocols.Trojan: return Outbound.TrojanSettings.fromJson(json);
  693. case Protocols.Shadowsocks: return Outbound.ShadowsocksSettings.fromJson(json);
  694. case Protocols.Socks: return Outbound.SocksSettings.fromJson(json);
  695. case Protocols.HTTP: return Outbound.HttpSettings.fromJson(json);
  696. case Protocols.Wireguard: return Outbound.WireguardSettings.fromJson(json);
  697. default: return null;
  698. }
  699. }
  700. toJson() {
  701. return {};
  702. }
  703. };
  704. Outbound.FreedomSettings = class extends CommonClass {
  705. constructor(domainStrategy='', fragment={}) {
  706. super();
  707. this.domainStrategy = domainStrategy;
  708. this.fragment = fragment;
  709. }
  710. static fromJson(json={}) {
  711. return new Outbound.FreedomSettings(
  712. json.domainStrategy,
  713. json.fragment ? Outbound.FreedomSettings.Fragment.fromJson(json.fragment) : undefined,
  714. );
  715. }
  716. toJson() {
  717. return {
  718. domainStrategy: ObjectUtil.isEmpty(this.domainStrategy) ? undefined : this.domainStrategy,
  719. fragment: Object.keys(this.fragment).length === 0 ? undefined : this.fragment,
  720. };
  721. }
  722. };
  723. Outbound.FreedomSettings.Fragment = class extends CommonClass {
  724. constructor(packets='1-3',length='',interval=''){
  725. super();
  726. this.packets = packets;
  727. this.length = length;
  728. this.interval = interval;
  729. }
  730. static fromJson(json={}) {
  731. return new Outbound.FreedomSettings.Fragment(
  732. json.packets,
  733. json.length,
  734. json.interval,
  735. );
  736. }
  737. };
  738. Outbound.BlackholeSettings = class extends CommonClass {
  739. constructor(type) {
  740. super();
  741. this.type;
  742. }
  743. static fromJson(json={}) {
  744. return new Outbound.BlackholeSettings(
  745. json.response ? json.response.type : undefined,
  746. );
  747. }
  748. toJson() {
  749. return {
  750. response: ObjectUtil.isEmpty(this.type) ? undefined : {type: this.type},
  751. };
  752. }
  753. };
  754. Outbound.DNSSettings = class extends CommonClass {
  755. constructor(network='udp', address='1.1.1.1', port=53) {
  756. super();
  757. this.network = network;
  758. this.address = address;
  759. this.port = port;
  760. }
  761. static fromJson(json={}){
  762. return new Outbound.DNSSettings(
  763. json.network,
  764. json.address,
  765. json.port,
  766. );
  767. }
  768. };
  769. Outbound.VmessSettings = class extends CommonClass {
  770. constructor(address, port, id) {
  771. super();
  772. this.address = address;
  773. this.port = port;
  774. this.id = id;
  775. }
  776. static fromJson(json={}) {
  777. if(ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VmessSettings();
  778. return new Outbound.VmessSettings(
  779. json.vnext[0].address,
  780. json.vnext[0].port,
  781. json.vnext[0].users[0].id,
  782. );
  783. }
  784. toJson() {
  785. return {
  786. vnext: [{
  787. address: this.address,
  788. port: this.port,
  789. users: [{id: this.id}],
  790. }],
  791. };
  792. }
  793. };
  794. Outbound.VLESSSettings = class extends CommonClass {
  795. constructor(address, port, id, flow, encryption='none') {
  796. super();
  797. this.address = address;
  798. this.port = port;
  799. this.id = id;
  800. this.flow = flow;
  801. this.encryption = encryption
  802. }
  803. static fromJson(json={}) {
  804. if(ObjectUtil.isArrEmpty(json.vnext)) return new Outbound.VLESSSettings();
  805. return new Outbound.VLESSSettings(
  806. json.vnext[0].address,
  807. json.vnext[0].port,
  808. json.vnext[0].users[0].id,
  809. json.vnext[0].users[0].flow,
  810. json.vnext[0].users[0].encryption,
  811. );
  812. }
  813. toJson() {
  814. return {
  815. vnext: [{
  816. address: this.address,
  817. port: this.port,
  818. users: [{id: this.id, flow: this.flow, encryption: 'none',}],
  819. }],
  820. };
  821. }
  822. };
  823. Outbound.TrojanSettings = class extends CommonClass {
  824. constructor(address, port, password) {
  825. super();
  826. this.address = address;
  827. this.port = port;
  828. this.password = password;
  829. }
  830. static fromJson(json={}) {
  831. if(ObjectUtil.isArrEmpty(json.servers)) return new Outbound.TrojanSettings();
  832. return new Outbound.TrojanSettings(
  833. json.servers[0].address,
  834. json.servers[0].port,
  835. json.servers[0].password,
  836. );
  837. }
  838. toJson() {
  839. return {
  840. servers: [{
  841. address: this.address,
  842. port: this.port,
  843. password: this.password,
  844. }],
  845. };
  846. }
  847. };
  848. Outbound.ShadowsocksSettings = class extends CommonClass {
  849. constructor(address, port, password, method, uot) {
  850. super();
  851. this.address = address;
  852. this.port = port;
  853. this.password = password;
  854. this.method = method;
  855. this.uot = uot;
  856. }
  857. static fromJson(json={}) {
  858. let servers = json.servers;
  859. if(ObjectUtil.isArrEmpty(servers)) servers=[{}];
  860. return new Outbound.ShadowsocksSettings(
  861. servers[0].address,
  862. servers[0].port,
  863. servers[0].password,
  864. servers[0].method,
  865. servers[0].uot,
  866. );
  867. }
  868. toJson() {
  869. return {
  870. servers: [{
  871. address: this.address,
  872. port: this.port,
  873. password: this.password,
  874. method: this.method,
  875. uot: this.uot,
  876. }],
  877. };
  878. }
  879. };
  880. Outbound.SocksSettings = class extends CommonClass {
  881. constructor(address, port, user, pass) {
  882. super();
  883. this.address = address;
  884. this.port = port;
  885. this.user = user;
  886. this.pass = pass;
  887. }
  888. static fromJson(json={}) {
  889. let servers = json.servers;
  890. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  891. return new Outbound.SocksSettings(
  892. servers[0].address,
  893. servers[0].port,
  894. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  895. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  896. );
  897. }
  898. toJson() {
  899. return {
  900. servers: [{
  901. address: this.address,
  902. port: this.port,
  903. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  904. }],
  905. };
  906. }
  907. };
  908. Outbound.HttpSettings = class extends CommonClass {
  909. constructor(address, port, user, pass) {
  910. super();
  911. this.address = address;
  912. this.port = port;
  913. this.user = user;
  914. this.pass = pass;
  915. }
  916. static fromJson(json={}) {
  917. let servers = json.servers;
  918. if(ObjectUtil.isArrEmpty(servers)) servers=[{users: [{}]}];
  919. return new Outbound.HttpSettings(
  920. servers[0].address,
  921. servers[0].port,
  922. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].user,
  923. ObjectUtil.isArrEmpty(servers[0].users) ? '' : servers[0].users[0].pass,
  924. );
  925. }
  926. toJson() {
  927. return {
  928. servers: [{
  929. address: this.address,
  930. port: this.port,
  931. users: ObjectUtil.isEmpty(this.user) ? [] : [{user: this.user, pass: this.pass}],
  932. }],
  933. };
  934. }
  935. };
  936. Outbound.WireguardSettings = class extends CommonClass {
  937. constructor(
  938. mtu=1420, secretKey='',
  939. address=[''], workers=2, domainStrategy='', reserved='',
  940. peers=[new Outbound.WireguardSettings.Peer()], kernelMode=false) {
  941. super();
  942. this.mtu = mtu;
  943. this.secretKey = secretKey;
  944. this.pubKey = secretKey.length>0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  945. this.address = address instanceof Array ? address.join(',') : address;
  946. this.workers = workers;
  947. this.domainStrategy = domainStrategy;
  948. this.reserved = reserved instanceof Array ? reserved.join(',') : reserved;
  949. this.peers = peers;
  950. this.kernelMode = kernelMode;
  951. }
  952. addPeer() {
  953. this.peers.push(new Outbound.WireguardSettings.Peer());
  954. }
  955. delPeer(index) {
  956. this.peers.splice(index, 1);
  957. }
  958. static fromJson(json={}){
  959. return new Outbound.WireguardSettings(
  960. json.mtu,
  961. json.secretKey,
  962. json.address,
  963. json.workers,
  964. json.domainStrategy,
  965. json.reserved,
  966. json.peers.map(peer => Outbound.WireguardSettings.Peer.fromJson(peer)),
  967. json.kernelMode,
  968. );
  969. }
  970. toJson() {
  971. return {
  972. mtu: this.mtu?? undefined,
  973. secretKey: this.secretKey,
  974. address: this.address ? this.address.split(",") : [],
  975. workers: this.workers?? undefined,
  976. domainStrategy: WireguardDomainStrategy.includes(this.domainStrategy) ? this.domainStrategy : undefined,
  977. reserved: this.reserved ? this.reserved.split(",").map(Number) : undefined,
  978. peers: Outbound.WireguardSettings.Peer.toJsonArray(this.peers),
  979. kernelMode: this.kernelMode,
  980. };
  981. }
  982. };
  983. Outbound.WireguardSettings.Peer = class extends CommonClass {
  984. constructor(publicKey='', psk='', allowedIPs=['0.0.0.0/0','::/0'], endpoint='', keepAlive=0) {
  985. super();
  986. this.publicKey = publicKey;
  987. this.psk = psk;
  988. this.allowedIPs = allowedIPs;
  989. this.endpoint = endpoint;
  990. this.keepAlive = keepAlive;
  991. }
  992. static fromJson(json={}){
  993. return new Outbound.WireguardSettings.Peer(
  994. json.publicKey,
  995. json.preSharedKey,
  996. json.allowedIPs,
  997. json.endpoint,
  998. json.keepAlive
  999. );
  1000. }
  1001. toJson() {
  1002. return {
  1003. publicKey: this.publicKey,
  1004. preSharedKey: this.psk.length>0 ? this.psk : undefined,
  1005. allowedIPs: this.allowedIPs ? this.allowedIPs : undefined,
  1006. endpoint: this.endpoint,
  1007. keepAlive: this.keepAlive?? undefined,
  1008. };
  1009. }
  1010. };