outbound.js 32 KB

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