outbound.js 34 KB

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