outbound.js 32 KB

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