xray.js 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  1. const Protocols = {
  2. VMESS: 'vmess',
  3. VLESS: 'vless',
  4. TROJAN: 'trojan',
  5. SHADOWSOCKS: 'shadowsocks',
  6. DOKODEMO: 'dokodemo-door',
  7. MTPROTO: 'mtproto',
  8. SOCKS: 'socks',
  9. HTTP: 'http',
  10. };
  11. const VmessMethods = {
  12. AES_128_GCM: 'aes-128-gcm',
  13. CHACHA20_POLY1305: 'chacha20-poly1305',
  14. AUTO: 'auto',
  15. NONE: 'none',
  16. };
  17. const SSMethods = {
  18. // AES_256_CFB: 'aes-256-cfb',
  19. // AES_128_CFB: 'aes-128-cfb',
  20. // CHACHA20: 'chacha20',
  21. // CHACHA20_IETF: 'chacha20-ietf',
  22. CHACHA20_POLY1305: 'chacha20-poly1305',
  23. AES_256_GCM: 'aes-256-gcm',
  24. AES_128_GCM: 'aes-128-gcm',
  25. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  26. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  27. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  28. };
  29. const RULE_IP = {
  30. PRIVATE: 'geoip:private',
  31. CN: 'geoip:cn',
  32. };
  33. const RULE_DOMAIN = {
  34. ADS: 'geosite:category-ads',
  35. ADS_ALL: 'geosite:category-ads-all',
  36. CN: 'geosite:cn',
  37. GOOGLE: 'geosite:google',
  38. FACEBOOK: 'geosite:facebook',
  39. SPEEDTEST: 'geosite:speedtest',
  40. };
  41. const XTLS_FLOW_CONTROL = {
  42. ORIGIN: "xtls-rprx-origin",
  43. DIRECT: "xtls-rprx-direct",
  44. };
  45. const TLS_FLOW_CONTROL = {
  46. VISION: "xtls-rprx-vision",
  47. };
  48. const TLS_VERSION_OPTION = {
  49. TLS10: "1.0",
  50. TLS11: "1.1",
  51. TLS12: "1.2",
  52. TLS13: "1.3",
  53. }
  54. const TLS_CIPHER_OPTION = {
  55. RSA_AES_128_CBC: "TLS_RSA_WITH_AES_128_CBC_SHA",
  56. RSA_AES_256_CBC: "TLS_RSA_WITH_AES_256_CBC_SHA",
  57. RSA_AES_128_GCM: "TLS_RSA_WITH_AES_128_GCM_SHA256",
  58. RSA_AES_256_GCM: "TLS_RSA_WITH_AES_256_GCM_SHA384",
  59. AES_128_GCM: "TLS_AES_128_GCM_SHA256",
  60. AES_256_GCM: "TLS_AES_256_GCM_SHA384",
  61. CHACHA20_POLY1305: "TLS_CHACHA20_POLY1305_SHA256",
  62. ECDHE_ECDSA_AES_128_CBC: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  63. ECDHE_ECDSA_AES_256_CBC: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  64. ECDHE_RSA_AES_128_CBC: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  65. ECDHE_RSA_AES_256_CBC: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  66. ECDHE_ECDSA_AES_128_GCM: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  67. ECDHE_ECDSA_AES_256_GCM: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  68. ECDHE_RSA_AES_128_GCM: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  69. ECDHE_RSA_AES_256_GCM: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  70. ECDHE_ECDSA_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
  71. ECDHE_RSA_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
  72. };
  73. const UTLS_FINGERPRINT = {
  74. UTLS_CHROME: "chrome",
  75. UTLS_FIREFOX: "firefox",
  76. UTLS_SAFARI: "safari",
  77. UTLS_IOS: "ios",
  78. UTLS_android: "android",
  79. UTLS_EDGE: "edge",
  80. UTLS_360: "360",
  81. UTLS_QQ: "qq",
  82. UTLS_RANDOM: "random",
  83. UTLS_RANDOMIZED: "randomized",
  84. };
  85. Object.freeze(Protocols);
  86. Object.freeze(VmessMethods);
  87. Object.freeze(SSMethods);
  88. Object.freeze(RULE_IP);
  89. Object.freeze(RULE_DOMAIN);
  90. Object.freeze(XTLS_FLOW_CONTROL);
  91. Object.freeze(TLS_FLOW_CONTROL);
  92. Object.freeze(TLS_VERSION_OPTION);
  93. Object.freeze(TLS_CIPHER_OPTION);
  94. Object.freeze(UTLS_FINGERPRINT);
  95. class XrayCommonClass {
  96. static toJsonArray(arr) {
  97. return arr.map(obj => obj.toJson());
  98. }
  99. static fromJson() {
  100. return new XrayCommonClass();
  101. }
  102. toJson() {
  103. return this;
  104. }
  105. toString(format=true) {
  106. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  107. }
  108. static toHeaders(v2Headers) {
  109. let newHeaders = [];
  110. if (v2Headers) {
  111. Object.keys(v2Headers).forEach(key => {
  112. let values = v2Headers[key];
  113. if (typeof(values) === 'string') {
  114. newHeaders.push({ name: key, value: values });
  115. } else {
  116. for (let i = 0; i < values.length; ++i) {
  117. newHeaders.push({ name: key, value: values[i] });
  118. }
  119. }
  120. });
  121. }
  122. return newHeaders;
  123. }
  124. static toV2Headers(headers, arr=true) {
  125. let v2Headers = {};
  126. for (let i = 0; i < headers.length; ++i) {
  127. let name = headers[i].name;
  128. let value = headers[i].value;
  129. if (ObjectUtil.isEmpty(name) || ObjectUtil.isEmpty(value)) {
  130. continue;
  131. }
  132. if (!(name in v2Headers)) {
  133. v2Headers[name] = arr ? [value] : value;
  134. } else {
  135. if (arr) {
  136. v2Headers[name].push(value);
  137. } else {
  138. v2Headers[name] = value;
  139. }
  140. }
  141. }
  142. return v2Headers;
  143. }
  144. }
  145. class TcpStreamSettings extends XrayCommonClass {
  146. constructor(
  147. type = 'none',
  148. acceptProxyProtocol = false,
  149. request = new TcpStreamSettings.TcpRequest(),
  150. response = new TcpStreamSettings.TcpResponse(),
  151. ) {
  152. super();
  153. this.type = type;
  154. this.request = request;
  155. this.response = response;
  156. this.acceptProxyProtocol = acceptProxyProtocol;
  157. }
  158. static fromJson(json = {}) {
  159. let header = json.header;
  160. if (!header) {
  161. header = {};
  162. }
  163. return new TcpStreamSettings(
  164. header.type,
  165. json.acceptProxyProtocol,
  166. TcpStreamSettings.TcpRequest.fromJson(header.request),
  167. TcpStreamSettings.TcpResponse.fromJson(header.response),
  168. );
  169. }
  170. toJson() {
  171. return {
  172. header: {
  173. type: this.type,
  174. request: this.type === 'http' ? this.request.toJson() : undefined,
  175. response: this.type === 'http' ? this.response.toJson() : undefined,
  176. },
  177. acceptProxyProtocol: this.acceptProxyProtocol,
  178. };
  179. }
  180. }
  181. TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
  182. constructor(version = '1.1',
  183. method = 'GET',
  184. path = ['/'],
  185. headers = [],
  186. ) {
  187. super();
  188. this.version = version;
  189. this.method = method;
  190. this.path = path.length === 0 ? ['/'] : path;
  191. this.headers = headers;
  192. }
  193. addPath(path) {
  194. this.path.push(path);
  195. }
  196. removePath(index) {
  197. this.path.splice(index, 1);
  198. }
  199. addHeader(name, value) {
  200. this.headers.push({ name: name, value: value });
  201. }
  202. getHeader(name) {
  203. for (const header of this.headers) {
  204. if (header.name.toLowerCase() === name.toLowerCase()) {
  205. return header.value;
  206. }
  207. }
  208. return null;
  209. }
  210. removeHeader(index) {
  211. this.headers.splice(index, 1);
  212. }
  213. static fromJson(json = {}) {
  214. return new TcpStreamSettings.TcpRequest(
  215. json.version,
  216. json.method,
  217. json.path,
  218. XrayCommonClass.toHeaders(json.headers),
  219. );
  220. }
  221. toJson() {
  222. return {
  223. method: this.method,
  224. path: ObjectUtil.clone(this.path),
  225. headers: XrayCommonClass.toV2Headers(this.headers),
  226. };
  227. }
  228. };
  229. TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
  230. constructor(version = '1.1',
  231. status = '200',
  232. reason = 'OK',
  233. headers = [],
  234. ) {
  235. super();
  236. this.version = version;
  237. this.status = status;
  238. this.reason = reason;
  239. this.headers = headers;
  240. }
  241. addHeader(name, value) {
  242. this.headers.push({ name: name, value: value });
  243. }
  244. removeHeader(index) {
  245. this.headers.splice(index, 1);
  246. }
  247. static fromJson(json = {}) {
  248. return new TcpStreamSettings.TcpResponse(
  249. json.version,
  250. json.status,
  251. json.reason,
  252. XrayCommonClass.toHeaders(json.headers),
  253. );
  254. }
  255. toJson() {
  256. return {
  257. version: this.version,
  258. status: this.status,
  259. reason: this.reason,
  260. headers: XrayCommonClass.toV2Headers(this.headers),
  261. };
  262. }
  263. };
  264. class KcpStreamSettings extends XrayCommonClass {
  265. constructor(mtu=1350, tti=20,
  266. uplinkCapacity=5,
  267. downlinkCapacity=20,
  268. congestion=false,
  269. readBufferSize=2,
  270. writeBufferSize=2,
  271. type='none',
  272. seed=RandomUtil.randomSeq(10),
  273. ) {
  274. super();
  275. this.mtu = mtu;
  276. this.tti = tti;
  277. this.upCap = uplinkCapacity;
  278. this.downCap = downlinkCapacity;
  279. this.congestion = congestion;
  280. this.readBuffer = readBufferSize;
  281. this.writeBuffer = writeBufferSize;
  282. this.type = type;
  283. this.seed = seed;
  284. }
  285. static fromJson(json={}) {
  286. return new KcpStreamSettings(
  287. json.mtu,
  288. json.tti,
  289. json.uplinkCapacity,
  290. json.downlinkCapacity,
  291. json.congestion,
  292. json.readBufferSize,
  293. json.writeBufferSize,
  294. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  295. json.seed,
  296. );
  297. }
  298. toJson() {
  299. return {
  300. mtu: this.mtu,
  301. tti: this.tti,
  302. uplinkCapacity: this.upCap,
  303. downlinkCapacity: this.downCap,
  304. congestion: this.congestion,
  305. readBufferSize: this.readBuffer,
  306. writeBufferSize: this.writeBuffer,
  307. header: {
  308. type: this.type,
  309. },
  310. seed: this.seed,
  311. };
  312. }
  313. }
  314. class WsStreamSettings extends XrayCommonClass {
  315. constructor(acceptProxyProtocol=false, path='/', headers=[]) {
  316. super();
  317. this.acceptProxyProtocol = acceptProxyProtocol;
  318. this.path = path;
  319. this.headers = headers;
  320. }
  321. addHeader(name, value) {
  322. this.headers.push({ name: name, value: value });
  323. }
  324. getHeader(name) {
  325. for (const header of this.headers) {
  326. if (header.name.toLowerCase() === name.toLowerCase()) {
  327. return header.value;
  328. }
  329. }
  330. return null;
  331. }
  332. removeHeader(index) {
  333. this.headers.splice(index, 1);
  334. }
  335. static fromJson(json={}) {
  336. return new WsStreamSettings(
  337. json.acceptProxyProtocol,
  338. json.path,
  339. XrayCommonClass.toHeaders(json.headers),
  340. );
  341. }
  342. toJson() {
  343. return {
  344. acceptProxyProtocol: this.acceptProxyProtocol,
  345. path: this.path,
  346. headers: XrayCommonClass.toV2Headers(this.headers, false),
  347. };
  348. }
  349. }
  350. class HttpStreamSettings extends XrayCommonClass {
  351. constructor(path='/', host=['']) {
  352. super();
  353. this.path = path;
  354. this.host = host.length === 0 ? [''] : host;
  355. }
  356. addHost(host) {
  357. this.host.push(host);
  358. }
  359. removeHost(index) {
  360. this.host.splice(index, 1);
  361. }
  362. static fromJson(json={}) {
  363. return new HttpStreamSettings(json.path, json.host);
  364. }
  365. toJson() {
  366. let host = [];
  367. for (let i = 0; i < this.host.length; ++i) {
  368. if (!ObjectUtil.isEmpty(this.host[i])) {
  369. host.push(this.host[i]);
  370. }
  371. }
  372. return {
  373. path: this.path,
  374. host: host,
  375. }
  376. }
  377. }
  378. class QuicStreamSettings extends XrayCommonClass {
  379. constructor(security=VmessMethods.NONE,
  380. key='', type='none') {
  381. super();
  382. this.security = security;
  383. this.key = key;
  384. this.type = type;
  385. }
  386. static fromJson(json={}) {
  387. return new QuicStreamSettings(
  388. json.security,
  389. json.key,
  390. json.header ? json.header.type : 'none',
  391. );
  392. }
  393. toJson() {
  394. return {
  395. security: this.security,
  396. key: this.key,
  397. header: {
  398. type: this.type,
  399. }
  400. }
  401. }
  402. }
  403. class GrpcStreamSettings extends XrayCommonClass {
  404. constructor(serviceName="") {
  405. super();
  406. this.serviceName = serviceName;
  407. }
  408. static fromJson(json={}) {
  409. return new GrpcStreamSettings(json.serviceName);
  410. }
  411. toJson() {
  412. return {
  413. serviceName: this.serviceName,
  414. }
  415. }
  416. }
  417. class TlsStreamSettings extends XrayCommonClass {
  418. constructor(serverName = '', minVersion = TLS_VERSION_OPTION.TLS12, maxVersion = TLS_VERSION_OPTION.TLS13,
  419. cipherSuites = '',
  420. certificates = [new TlsStreamSettings.Cert()], alpn = ["h2", "http/1.1"]) {
  421. super();
  422. this.server = serverName;
  423. this.minVersion = minVersion;
  424. this.maxVersion = maxVersion;
  425. this.cipherSuites = cipherSuites;
  426. this.certs = certificates;
  427. this.alpn = alpn;
  428. }
  429. addCert(cert) {
  430. this.certs.push(cert);
  431. }
  432. removeCert(index) {
  433. this.certs.splice(index, 1);
  434. }
  435. static fromJson(json={}) {
  436. let certs;
  437. if (!ObjectUtil.isEmpty(json.certificates)) {
  438. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  439. }
  440. return new TlsStreamSettings(
  441. json.serverName,
  442. json.minVersion,
  443. json.maxVersion,
  444. json.cipherSuites,
  445. certs,
  446. json.alpn,
  447. );
  448. }
  449. toJson() {
  450. return {
  451. serverName: this.server,
  452. minVersion: this.minVersion,
  453. maxVersion: this.maxVersion,
  454. cipherSuites: this.cipherSuites,
  455. certificates: TlsStreamSettings.toJsonArray(this.certs),
  456. alpn: this.alpn
  457. };
  458. }
  459. }
  460. TlsStreamSettings.Cert = class extends XrayCommonClass {
  461. constructor(useFile=true, certificateFile='', keyFile='', certificate='', key='') {
  462. super();
  463. this.useFile = useFile;
  464. this.certFile = certificateFile;
  465. this.keyFile = keyFile;
  466. this.cert = certificate instanceof Array ? certificate.join('\n') : certificate;
  467. this.key = key instanceof Array ? key.join('\n') : key;
  468. }
  469. static fromJson(json={}) {
  470. if ('certificateFile' in json && 'keyFile' in json) {
  471. return new TlsStreamSettings.Cert(
  472. true,
  473. json.certificateFile,
  474. json.keyFile,
  475. );
  476. } else {
  477. return new TlsStreamSettings.Cert(
  478. false, '', '',
  479. json.certificate.join('\n'),
  480. json.key.join('\n'),
  481. );
  482. }
  483. }
  484. toJson() {
  485. if (this.useFile) {
  486. return {
  487. certificateFile: this.certFile,
  488. keyFile: this.keyFile,
  489. };
  490. } else {
  491. return {
  492. certificate: this.cert.split('\n'),
  493. key: this.key.split('\n'),
  494. };
  495. }
  496. }
  497. };
  498. class StreamSettings extends XrayCommonClass {
  499. constructor(network='tcp',
  500. security='none',
  501. tlsSettings=new TlsStreamSettings(),
  502. tcpSettings=new TcpStreamSettings(),
  503. kcpSettings=new KcpStreamSettings(),
  504. wsSettings=new WsStreamSettings(),
  505. httpSettings=new HttpStreamSettings(),
  506. quicSettings=new QuicStreamSettings(),
  507. grpcSettings=new GrpcStreamSettings(),
  508. ) {
  509. super();
  510. this.network = network;
  511. this.security = security;
  512. this.tls = tlsSettings;
  513. this.tcp = tcpSettings;
  514. this.kcp = kcpSettings;
  515. this.ws = wsSettings;
  516. this.http = httpSettings;
  517. this.quic = quicSettings;
  518. this.grpc = grpcSettings;
  519. }
  520. get isTls() {
  521. return this.security === 'tls';
  522. }
  523. set isTls(isTls) {
  524. if (isTls) {
  525. this.security = 'tls';
  526. } else {
  527. this.security = 'none';
  528. }
  529. }
  530. get isXTls() {
  531. return this.security === "xtls";
  532. }
  533. set isXTls(isXTls) {
  534. if (isXTls) {
  535. this.security = 'xtls';
  536. } else {
  537. this.security = 'none';
  538. }
  539. }
  540. static fromJson(json={}) {
  541. let tls;
  542. if (json.security === "xtls") {
  543. tls = TlsStreamSettings.fromJson(json.xtlsSettings);
  544. } else {
  545. tls = TlsStreamSettings.fromJson(json.tlsSettings);
  546. }
  547. return new StreamSettings(
  548. json.network,
  549. json.security,
  550. tls,
  551. TcpStreamSettings.fromJson(json.tcpSettings),
  552. KcpStreamSettings.fromJson(json.kcpSettings),
  553. WsStreamSettings.fromJson(json.wsSettings),
  554. HttpStreamSettings.fromJson(json.httpSettings),
  555. QuicStreamSettings.fromJson(json.quicSettings),
  556. GrpcStreamSettings.fromJson(json.grpcSettings),
  557. );
  558. }
  559. toJson() {
  560. const network = this.network;
  561. return {
  562. network: network,
  563. security: this.security,
  564. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  565. xtlsSettings: this.isXTls ? this.tls.toJson() : undefined,
  566. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  567. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  568. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  569. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  570. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  571. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  572. };
  573. }
  574. }
  575. class Sniffing extends XrayCommonClass {
  576. constructor(enabled=true, destOverride=['http', 'tls']) {
  577. super();
  578. this.enabled = enabled;
  579. this.destOverride = destOverride;
  580. }
  581. static fromJson(json={}) {
  582. let destOverride = ObjectUtil.clone(json.destOverride);
  583. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  584. if (ObjectUtil.isEmpty(destOverride[0])) {
  585. destOverride = ['http', 'tls'];
  586. }
  587. }
  588. return new Sniffing(
  589. !!json.enabled,
  590. destOverride,
  591. );
  592. }
  593. }
  594. class Inbound extends XrayCommonClass {
  595. constructor(port=RandomUtil.randomIntRange(10000, 60000),
  596. listen='',
  597. protocol=Protocols.VMESS,
  598. settings=null,
  599. streamSettings=new StreamSettings(),
  600. tag='',
  601. sniffing=new Sniffing(),
  602. clientStats='',
  603. ) {
  604. super();
  605. this.port = port;
  606. this.listen = listen;
  607. this._protocol = protocol;
  608. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  609. this.stream = streamSettings;
  610. this.tag = tag;
  611. this.sniffing = sniffing;
  612. this.clientStats = clientStats;
  613. }
  614. getClientStats() {
  615. return this.clientStats;
  616. }
  617. get protocol() {
  618. return this._protocol;
  619. }
  620. set protocol(protocol) {
  621. this._protocol = protocol;
  622. this.settings = Inbound.Settings.getSettings(protocol);
  623. if (protocol === Protocols.TROJAN) {
  624. this.tls = false;
  625. }
  626. }
  627. get tls() {
  628. return this.stream.security === 'tls';
  629. }
  630. set tls(isTls) {
  631. if (isTls) {
  632. this.stream.security = 'tls';
  633. } else {
  634. this.stream.security = 'none';
  635. }
  636. }
  637. get xtls() {
  638. return this.stream.security === 'xtls';
  639. }
  640. set xtls(isXTls) {
  641. if (isXTls) {
  642. this.stream.security = 'xtls';
  643. } else {
  644. this.stream.security = 'none';
  645. }
  646. }
  647. get network() {
  648. return this.stream.network;
  649. }
  650. set network(network) {
  651. this.stream.network = network;
  652. }
  653. get isTcp() {
  654. return this.network === "tcp";
  655. }
  656. get isWs() {
  657. return this.network === "ws";
  658. }
  659. get isKcp() {
  660. return this.network === "kcp";
  661. }
  662. get isQuic() {
  663. return this.network === "quic"
  664. }
  665. get isGrpc() {
  666. return this.network === "grpc";
  667. }
  668. get isH2() {
  669. return this.network === "http";
  670. }
  671. isInboundEmpty() {
  672. if (this.protocol == Protocols.VMESS && this.settings.vmesses.length == 0) {
  673. return true;
  674. } else if (this.protocol == Protocols.VLESS && this.settings.vlesses.length == 0) {
  675. return true;
  676. } else if (this.protocol == Protocols.TROJAN && this.settings.trojans.length == 0) {
  677. return true;
  678. } else {
  679. return false;
  680. }
  681. }
  682. // VMess & VLess
  683. get uuid() {
  684. switch (this.protocol) {
  685. case Protocols.VMESS:
  686. return this.settings.vmesses[0].id;
  687. case Protocols.VLESS:
  688. return this.settings.vlesses[0].id;
  689. default:
  690. return "";
  691. }
  692. }
  693. // VLess & Trojan
  694. get flow() {
  695. switch (this.protocol) {
  696. case Protocols.VLESS:
  697. return this.settings.vlesses[0].flow;
  698. case Protocols.TROJAN:
  699. return this.settings.trojans[0].flow;
  700. default:
  701. return "";
  702. }
  703. }
  704. // VMess
  705. get alterId() {
  706. switch (this.protocol) {
  707. case Protocols.VMESS:
  708. return this.settings.vmesses[0].alterId;
  709. default:
  710. return "";
  711. }
  712. }
  713. // Socks & HTTP
  714. get username() {
  715. switch (this.protocol) {
  716. case Protocols.SOCKS:
  717. case Protocols.HTTP:
  718. return this.settings.accounts[0].user;
  719. default:
  720. return "";
  721. }
  722. }
  723. // Trojan & Shadowsocks & Socks & HTTP
  724. get password() {
  725. switch (this.protocol) {
  726. case Protocols.TROJAN:
  727. return this.settings.trojans[0].password;
  728. case Protocols.SHADOWSOCKS:
  729. return this.settings.password;
  730. case Protocols.SOCKS:
  731. case Protocols.HTTP:
  732. return this.settings.accounts[0].pass;
  733. default:
  734. return "";
  735. }
  736. }
  737. // Shadowsocks
  738. get method() {
  739. switch (this.protocol) {
  740. case Protocols.SHADOWSOCKS:
  741. return this.settings.method;
  742. default:
  743. return "";
  744. }
  745. }
  746. get serverName() {
  747. if (this.stream.isTls || this.stream.isXTls) {
  748. return this.stream.tls.server;
  749. }
  750. return "";
  751. }
  752. get host() {
  753. if (this.isTcp) {
  754. return this.stream.tcp.request.getHeader("Host");
  755. } else if (this.isWs) {
  756. return this.stream.ws.getHeader("Host");
  757. } else if (this.isH2) {
  758. return this.stream.http.host[0];
  759. }
  760. return null;
  761. }
  762. get path() {
  763. if (this.isTcp) {
  764. return this.stream.tcp.request.path[0];
  765. } else if (this.isWs) {
  766. return this.stream.ws.path;
  767. } else if (this.isH2) {
  768. return this.stream.http.path[0];
  769. }
  770. return null;
  771. }
  772. get quicSecurity() {
  773. return this.stream.quic.security;
  774. }
  775. get quicKey() {
  776. return this.stream.quic.key;
  777. }
  778. get quicType() {
  779. return this.stream.quic.type;
  780. }
  781. get kcpType() {
  782. return this.stream.kcp.type;
  783. }
  784. get kcpSeed() {
  785. return this.stream.kcp.seed;
  786. }
  787. get serviceName() {
  788. return this.stream.grpc.serviceName;
  789. }
  790. isExpiry(index) {
  791. switch (this.protocol) {
  792. case Protocols.VMESS:
  793. if(this.settings.vmesses[index]._expiryTime != null)
  794. return this.settings.vmesses[index]._expiryTime < new Date().getTime();
  795. return false
  796. case Protocols.VLESS:
  797. if(this.settings.vlesses[index]._expiryTime != null)
  798. return this.settings.vlesses[index]._expiryTime < new Date().getTime();
  799. return false
  800. case Protocols.TROJAN:
  801. if(this.settings.trojans[index]._expiryTime != null)
  802. return this.settings.trojans[index]._expiryTime < new Date().getTime();
  803. return false
  804. default:
  805. return false;
  806. }
  807. }
  808. canEnableTls() {
  809. switch (this.protocol) {
  810. case Protocols.VMESS:
  811. case Protocols.VLESS:
  812. case Protocols.TROJAN:
  813. case Protocols.SHADOWSOCKS:
  814. break;
  815. default:
  816. return false;
  817. }
  818. switch (this.network) {
  819. case "tcp":
  820. case "ws":
  821. case "http":
  822. case "quic":
  823. case "grpc":
  824. return true;
  825. default:
  826. return false;
  827. }
  828. }
  829. //this is used for xtls-rprx-vison
  830. canEnableTlsFlow() {
  831. if ((this.stream.security === 'tls') && (this.network === "tcp")) {
  832. switch (this.protocol) {
  833. case Protocols.VLESS:
  834. return true;
  835. default:
  836. return false;
  837. }
  838. }
  839. return false;
  840. }
  841. canSetTls() {
  842. return this.canEnableTls();
  843. }
  844. canEnableXTls() {
  845. switch (this.protocol) {
  846. case Protocols.VLESS:
  847. case Protocols.TROJAN:
  848. break;
  849. default:
  850. return false;
  851. }
  852. return this.network === "tcp";
  853. }
  854. canEnableStream() {
  855. switch (this.protocol) {
  856. case Protocols.VMESS:
  857. case Protocols.VLESS:
  858. case Protocols.TROJAN:
  859. case Protocols.SHADOWSOCKS:
  860. return true;
  861. default:
  862. return false;
  863. }
  864. }
  865. canSniffing() {
  866. switch (this.protocol) {
  867. case Protocols.VMESS:
  868. case Protocols.VLESS:
  869. case Protocols.TROJAN:
  870. case Protocols.SHADOWSOCKS:
  871. return true;
  872. default:
  873. return false;
  874. }
  875. }
  876. reset() {
  877. this.port = RandomUtil.randomIntRange(10000, 60000);
  878. this.listen = '';
  879. this.protocol = Protocols.VMESS;
  880. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  881. this.stream = new StreamSettings();
  882. this.tag = '';
  883. this.sniffing = new Sniffing();
  884. }
  885. genVmessLink(address='', remark='', clientIndex=0) {
  886. if (this.protocol !== Protocols.VMESS) {
  887. return '';
  888. }
  889. let network = this.stream.network;
  890. let type = 'none';
  891. let host = '';
  892. let path = '';
  893. if (network === 'tcp') {
  894. let tcp = this.stream.tcp;
  895. type = tcp.type;
  896. if (type === 'http') {
  897. let request = tcp.request;
  898. path = request.path.join(',');
  899. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  900. if (index >= 0) {
  901. host = request.headers[index].value;
  902. }
  903. }
  904. } else if (network === 'kcp') {
  905. let kcp = this.stream.kcp;
  906. type = kcp.type;
  907. path = kcp.seed;
  908. } else if (network === 'ws') {
  909. let ws = this.stream.ws;
  910. path = ws.path;
  911. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  912. if (index >= 0) {
  913. host = ws.headers[index].value;
  914. }
  915. } else if (network === 'http') {
  916. network = 'h2';
  917. path = this.stream.http.path;
  918. host = this.stream.http.host.join(',');
  919. } else if (network === 'quic') {
  920. type = this.stream.quic.type;
  921. host = this.stream.quic.security;
  922. path = this.stream.quic.key;
  923. } else if (network === 'grpc') {
  924. path = this.stream.grpc.serviceName;
  925. }
  926. if (this.stream.security === 'tls') {
  927. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  928. address = this.stream.tls.server;
  929. }
  930. }
  931. let obj = {
  932. v: '2',
  933. ps: remark,
  934. add: address,
  935. port: this.port,
  936. id: this.settings.vmesses[clientIndex].id,
  937. aid: this.settings.vmesses[clientIndex].alterId,
  938. net: network,
  939. type: type,
  940. host: host,
  941. path: path,
  942. tls: this.stream.security,
  943. };
  944. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  945. }
  946. genVLESSLink(address = '', remark='', clientIndex=0) {
  947. const settings = this.settings;
  948. const uuid = settings.vlesses[clientIndex].id;
  949. const port = this.port;
  950. const type = this.stream.network;
  951. const params = new Map();
  952. params.set("type", this.stream.network);
  953. if (this.xtls) {
  954. params.set("security", "xtls");
  955. } else {
  956. params.set("security", this.stream.security);
  957. }
  958. switch (type) {
  959. case "tcp":
  960. const tcp = this.stream.tcp;
  961. if (tcp.type === 'http') {
  962. const request = tcp.request;
  963. params.set("path", request.path.join(','));
  964. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  965. if (index >= 0) {
  966. const host = request.headers[index].value;
  967. params.set("host", host);
  968. }
  969. params.set("headerType", 'http');
  970. }
  971. break;
  972. case "kcp":
  973. const kcp = this.stream.kcp;
  974. params.set("headerType", kcp.type);
  975. params.set("seed", kcp.seed);
  976. break;
  977. case "ws":
  978. const ws = this.stream.ws;
  979. params.set("path", ws.path);
  980. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  981. if (index >= 0) {
  982. const host = ws.headers[index].value;
  983. params.set("host", host);
  984. }
  985. break;
  986. case "http":
  987. const http = this.stream.http;
  988. params.set("path", http.path);
  989. params.set("host", http.host);
  990. break;
  991. case "quic":
  992. const quic = this.stream.quic;
  993. params.set("quicSecurity", quic.security);
  994. params.set("key", quic.key);
  995. params.set("headerType", quic.type);
  996. break;
  997. case "grpc":
  998. const grpc = this.stream.grpc;
  999. params.set("serviceName", grpc.serviceName);
  1000. break;
  1001. }
  1002. if (this.stream.security === 'tls') {
  1003. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1004. address = this.stream.tls.server;
  1005. params.set("sni", address);
  1006. }
  1007. if (this.settings.vlesses[clientIndex].flow === "xtls-rprx-vision") {
  1008. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1009. }
  1010. params.set("fp", this.settings.vlesses[clientIndex].fingerprint);
  1011. }
  1012. if (this.xtls) {
  1013. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1014. }
  1015. const link = `vless://${uuid}@${address}:${port}`;
  1016. const url = new URL(link);
  1017. for (const [key, value] of params) {
  1018. url.searchParams.set(key, value)
  1019. }
  1020. url.hash = encodeURIComponent(remark);
  1021. return url.toString();
  1022. }
  1023. genSSLink(address = '', remark = '',clientIndex) {
  1024. let settings = this.settings;
  1025. const server = this.stream.tls.server;
  1026. if (!ObjectUtil.isEmpty(server)) {
  1027. address = server;
  1028. }
  1029. if (settings.method == SSMethods.BLAKE3_AES_128_GCM || settings.method == SSMethods.BLAKE3_AES_256_GCM || settings.method == SSMethods.BLAKE3_CHACHA20_POLY1305) {
  1030. return `ss://${settings.method}:${settings.password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1031. } else {
  1032. return 'ss://' + safeBase64(settings.method + ':' + settings.password + '@' + address + ':' + this.port)
  1033. + '#' + encodeURIComponent(remark);
  1034. }
  1035. }
  1036. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1037. let settings = this.settings;
  1038. const port = this.port;
  1039. const type = this.stream.network;
  1040. const params = new Map();
  1041. params.set("type", this.stream.network);
  1042. if (this.xtls) {
  1043. params.set("security", "xtls");
  1044. } else {
  1045. params.set("security", this.stream.security);
  1046. }
  1047. switch (type) {
  1048. case "tcp":
  1049. const tcp = this.stream.tcp;
  1050. if (tcp.type === 'http') {
  1051. const request = tcp.request;
  1052. params.set("path", request.path.join(','));
  1053. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1054. if (index >= 0) {
  1055. const host = request.headers[index].value;
  1056. params.set("host", host);
  1057. }
  1058. params.set("headerType", 'http');
  1059. }
  1060. break;
  1061. case "kcp":
  1062. const kcp = this.stream.kcp;
  1063. params.set("headerType", kcp.type);
  1064. params.set("seed", kcp.seed);
  1065. break;
  1066. case "ws":
  1067. const ws = this.stream.ws;
  1068. params.set("path", ws.path);
  1069. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1070. if (index >= 0) {
  1071. const host = ws.headers[index].value;
  1072. params.set("host", host);
  1073. }
  1074. break;
  1075. case "http":
  1076. const http = this.stream.http;
  1077. params.set("path", http.path);
  1078. params.set("host", http.host);
  1079. break;
  1080. case "quic":
  1081. const quic = this.stream.quic;
  1082. params.set("quicSecurity", quic.security);
  1083. params.set("key", quic.key);
  1084. params.set("headerType", quic.type);
  1085. break;
  1086. case "grpc":
  1087. const grpc = this.stream.grpc;
  1088. params.set("serviceName", grpc.serviceName);
  1089. break;
  1090. }
  1091. if (this.stream.security === 'tls') {
  1092. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1093. address = this.stream.tls.server;
  1094. params.set("sni", address);
  1095. }
  1096. params.set("flow", this.settings.trojans[clientIndex].flow);
  1097. }
  1098. if (this.xtls) {
  1099. params.set("flow", this.settings.trojans[clientIndex].flow);
  1100. }
  1101. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1102. const url = new URL(link);
  1103. for (const [key, value] of params) {
  1104. url.searchParams.set(key, value)
  1105. }
  1106. url.hash = encodeURIComponent(remark);
  1107. return url.toString();
  1108. }
  1109. genLink(address='', remark='') {
  1110. switch (this.protocol) {
  1111. case Protocols.VMESS:
  1112. if (this.settings.vmesses[clientIndex].email != ""){
  1113. remark += '-' + this.settings.vmesses[clientIndex].email
  1114. }
  1115. return this.genVmessLink(address, remark, clientIndex);
  1116. case Protocols.VLESS:
  1117. if (this.settings.vlesses[clientIndex].email != ""){
  1118. remark += '-' + this.settings.vlesses[clientIndex].email
  1119. }
  1120. return this.genVLESSLink(address, remark, clientIndex);
  1121. case Protocols.SHADOWSOCKS: return this.genSSLink(address, remark);
  1122. case Protocols.TROJAN:
  1123. if (this.settings.trojans[clientIndex].email != ""){
  1124. remark += '-' + this.settings.trojans[clientIndex].email
  1125. }
  1126. return this.genTrojanLink(address, remark, clientIndex);
  1127. default: return '';
  1128. }
  1129. }
  1130. genInboundLinks(address = '', remark = '') {
  1131. let link = '';
  1132. switch (this.protocol) {
  1133. case Protocols.VMESS:
  1134. case Protocols.VLESS:
  1135. case Protocols.TROJAN:
  1136. JSON.parse(this.settings).clients.forEach((_,index) => {
  1137. link += this.genLink(address, remark, index) + '\r\n';
  1138. });
  1139. return link;
  1140. case Protocols.SHADOWSOCKS:
  1141. return (this.genSSLink(address, remark) + '\r\n');
  1142. default: return '';
  1143. }
  1144. }
  1145. static fromJson(json={}) {
  1146. return new Inbound(
  1147. json.port,
  1148. json.listen,
  1149. json.protocol,
  1150. Inbound.Settings.fromJson(json.protocol, json.settings),
  1151. StreamSettings.fromJson(json.streamSettings),
  1152. json.tag,
  1153. Sniffing.fromJson(json.sniffing),
  1154. json.clientStats
  1155. )
  1156. }
  1157. toJson() {
  1158. let streamSettings;
  1159. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1160. streamSettings = this.stream.toJson();
  1161. }
  1162. return {
  1163. port: this.port,
  1164. listen: this.listen,
  1165. protocol: this.protocol,
  1166. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1167. streamSettings: streamSettings,
  1168. tag: this.tag,
  1169. sniffing: this.sniffing.toJson(),
  1170. clientStats: this.clientStats
  1171. };
  1172. }
  1173. }
  1174. Inbound.Settings = class extends XrayCommonClass {
  1175. constructor(protocol) {
  1176. super();
  1177. this.protocol = protocol;
  1178. }
  1179. static getSettings(protocol) {
  1180. switch (protocol) {
  1181. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1182. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1183. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1184. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1185. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1186. case Protocols.MTPROTO: return new Inbound.MtprotoSettings(protocol);
  1187. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1188. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1189. default: return null;
  1190. }
  1191. }
  1192. static fromJson(protocol, json) {
  1193. switch (protocol) {
  1194. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1195. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1196. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1197. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1198. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1199. case Protocols.MTPROTO: return Inbound.MtprotoSettings.fromJson(json);
  1200. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1201. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1202. default: return null;
  1203. }
  1204. }
  1205. toJson() {
  1206. return {};
  1207. }
  1208. };
  1209. Inbound.VmessSettings = class extends Inbound.Settings {
  1210. constructor(protocol,
  1211. vmesses=[new Inbound.VmessSettings.Vmess()],
  1212. disableInsecureEncryption=false) {
  1213. super(protocol);
  1214. this.vmesses = vmesses;
  1215. this.disableInsecure = disableInsecureEncryption;
  1216. }
  1217. indexOfVmessById(id) {
  1218. return this.vmesses.findIndex(vmess => vmess.id === id);
  1219. }
  1220. addVmess(vmess) {
  1221. if (this.indexOfVmessById(vmess.id) >= 0) {
  1222. return false;
  1223. }
  1224. this.vmesses.push(vmess);
  1225. }
  1226. delVmess(vmess) {
  1227. const i = this.indexOfVmessById(vmess.id);
  1228. if (i >= 0) {
  1229. this.vmesses.splice(i, 1);
  1230. }
  1231. }
  1232. static fromJson(json={}) {
  1233. return new Inbound.VmessSettings(
  1234. Protocols.VMESS,
  1235. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1236. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1237. );
  1238. }
  1239. toJson() {
  1240. return {
  1241. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1242. disableInsecureEncryption: this.disableInsecure,
  1243. };
  1244. }
  1245. };
  1246. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1247. constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1248. super();
  1249. this.id = id;
  1250. this.alterId = alterId;
  1251. this.email = email;
  1252. this.limitIp = limitIp;
  1253. this.totalGB = totalGB;
  1254. this.expiryTime = expiryTime;
  1255. }
  1256. static fromJson(json={}) {
  1257. return new Inbound.VmessSettings.Vmess(
  1258. json.id,
  1259. json.alterId,
  1260. json.email,
  1261. json.limitIp,
  1262. json.totalGB,
  1263. json.expiryTime,
  1264. );
  1265. }
  1266. get _expiryTime() {
  1267. if (this.expiryTime === 0 || this.expiryTime === "") {
  1268. return null;
  1269. }
  1270. return moment(this.expiryTime);
  1271. }
  1272. set _expiryTime(t) {
  1273. if (t == null || t === "") {
  1274. this.expiryTime = 0;
  1275. } else {
  1276. this.expiryTime = t.valueOf();
  1277. }
  1278. }
  1279. get _totalGB() {
  1280. return toFixed(this.totalGB / ONE_GB, 2);
  1281. }
  1282. set _totalGB(gb) {
  1283. this.totalGB = toFixed(gb * ONE_GB, 0);
  1284. }
  1285. };
  1286. Inbound.VLESSSettings = class extends Inbound.Settings {
  1287. constructor(protocol,
  1288. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1289. decryption='none',
  1290. fallbacks=[],) {
  1291. super(protocol);
  1292. this.vlesses = vlesses;
  1293. this.decryption = decryption;
  1294. this.fallbacks = fallbacks;
  1295. }
  1296. addFallback() {
  1297. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1298. }
  1299. delFallback(index) {
  1300. this.fallbacks.splice(index, 1);
  1301. }
  1302. static fromJson(json={}) {
  1303. return new Inbound.VLESSSettings(
  1304. Protocols.VLESS,
  1305. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1306. json.decryption,
  1307. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1308. );
  1309. }
  1310. toJson() {
  1311. return {
  1312. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1313. decryption: this.decryption,
  1314. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1315. };
  1316. }
  1317. };
  1318. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1319. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, fingerprint = UTLS_FINGERPRINT.UTLS_CHROME, expiryTime='') {
  1320. super();
  1321. this.id = id;
  1322. this.flow = flow;
  1323. this.email = email;
  1324. this.limitIp = limitIp;
  1325. this.totalGB = totalGB;
  1326. this.fingerprint = fingerprint;
  1327. this.expiryTime = expiryTime;
  1328. }
  1329. static fromJson(json={}) {
  1330. return new Inbound.VLESSSettings.VLESS(
  1331. json.id,
  1332. json.flow,
  1333. json.email,
  1334. json.limitIp,
  1335. json.totalGB,
  1336. json.fingerprint,
  1337. json.expiryTime,
  1338. );
  1339. }
  1340. get _expiryTime() {
  1341. if (this.expiryTime === 0 || this.expiryTime === "") {
  1342. return null;
  1343. }
  1344. return moment(this.expiryTime);
  1345. }
  1346. set _expiryTime(t) {
  1347. if (t == null || t === "") {
  1348. this.expiryTime = 0;
  1349. } else {
  1350. this.expiryTime = t.valueOf();
  1351. }
  1352. }
  1353. get _totalGB() {
  1354. return toFixed(this.totalGB / ONE_GB, 2);
  1355. }
  1356. set _totalGB(gb) {
  1357. this.totalGB = toFixed(gb * ONE_GB, 0);
  1358. }
  1359. };
  1360. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1361. constructor(name="", alpn='', path='', dest='', xver=0) {
  1362. super();
  1363. this.name = name;
  1364. this.alpn = alpn;
  1365. this.path = path;
  1366. this.dest = dest;
  1367. this.xver = xver;
  1368. }
  1369. toJson() {
  1370. let xver = this.xver;
  1371. if (!Number.isInteger(xver)) {
  1372. xver = 0;
  1373. }
  1374. return {
  1375. name: this.name,
  1376. alpn: this.alpn,
  1377. path: this.path,
  1378. dest: this.dest,
  1379. xver: xver,
  1380. }
  1381. }
  1382. static fromJson(json=[]) {
  1383. const fallbacks = [];
  1384. for (let fallback of json) {
  1385. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1386. fallback.name,
  1387. fallback.alpn,
  1388. fallback.path,
  1389. fallback.dest,
  1390. fallback.xver,
  1391. ))
  1392. }
  1393. return fallbacks;
  1394. }
  1395. };
  1396. Inbound.TrojanSettings = class extends Inbound.Settings {
  1397. constructor(protocol,
  1398. trojans=[new Inbound.TrojanSettings.Trojan()],
  1399. fallbacks=[],) {
  1400. super(protocol);
  1401. this.trojans = trojans;
  1402. this.fallbacks = fallbacks;
  1403. }
  1404. addTrojanFallback() {
  1405. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1406. }
  1407. delTrojanFallback(index) {
  1408. this.fallbacks.splice(index, 1);
  1409. }
  1410. static fromJson(json={}) {
  1411. return new Inbound.TrojanSettings(
  1412. Protocols.TROJAN,
  1413. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1414. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1415. }
  1416. toJson() {
  1417. return {
  1418. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1419. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1420. };
  1421. }
  1422. };
  1423. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1424. constructor(password=RandomUtil.randomSeq(10), flow ='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime='') {
  1425. super();
  1426. this.password = password;
  1427. this.flow = flow;
  1428. this.email = email;
  1429. this.limitIp = limitIp;
  1430. this.totalGB = totalGB;
  1431. this.expiryTime = expiryTime;
  1432. }
  1433. toJson() {
  1434. return {
  1435. password: this.password,
  1436. flow: this.flow,
  1437. email: this.email,
  1438. limitIp: this.limitIp,
  1439. totalGB: this.totalGB,
  1440. expiryTime: this.expiryTime,
  1441. };
  1442. }
  1443. static fromJson(json={}) {
  1444. return new Inbound.TrojanSettings.Trojan(
  1445. json.password,
  1446. json.flow,
  1447. json.email,
  1448. json.limitIp,
  1449. json.totalGB,
  1450. json.expiryTime,
  1451. );
  1452. }
  1453. get _expiryTime() {
  1454. if (this.expiryTime === 0 || this.expiryTime === "") {
  1455. return null;
  1456. }
  1457. return moment(this.expiryTime);
  1458. }
  1459. set _expiryTime(t) {
  1460. if (t == null || t === "") {
  1461. this.expiryTime = 0;
  1462. } else {
  1463. this.expiryTime = t.valueOf();
  1464. }
  1465. }
  1466. get _totalGB() {
  1467. return toFixed(this.totalGB / ONE_GB, 2);
  1468. }
  1469. set _totalGB(gb) {
  1470. this.totalGB = toFixed(gb * ONE_GB, 0);
  1471. }
  1472. };
  1473. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1474. constructor(name="", alpn='', path='', dest='', xver=0) {
  1475. super();
  1476. this.name = name;
  1477. this.alpn = alpn;
  1478. this.path = path;
  1479. this.dest = dest;
  1480. this.xver = xver;
  1481. }
  1482. toJson() {
  1483. let xver = this.xver;
  1484. if (!Number.isInteger(xver)) {
  1485. xver = 0;
  1486. }
  1487. return {
  1488. name: this.name,
  1489. alpn: this.alpn,
  1490. path: this.path,
  1491. dest: this.dest,
  1492. xver: xver,
  1493. }
  1494. }
  1495. static fromJson(json=[]) {
  1496. const fallbacks = [];
  1497. for (let fallback of json) {
  1498. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1499. fallback.name,
  1500. fallback.alpn,
  1501. fallback.path,
  1502. fallback.dest,
  1503. fallback.xver,
  1504. ))
  1505. }
  1506. return fallbacks;
  1507. }
  1508. };
  1509. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1510. constructor(protocol,
  1511. method = SSMethods.BLAKE3_AES_256_GCM,
  1512. password = RandomUtil.randomSeq(44),
  1513. network = 'tcp,udp'
  1514. ) {
  1515. super(protocol);
  1516. this.method = method;
  1517. this.password = password;
  1518. this.network = network;
  1519. }
  1520. static fromJson(json = {}) {
  1521. return new Inbound.ShadowsocksSettings(
  1522. Protocols.SHADOWSOCKS,
  1523. json.method,
  1524. json.password,
  1525. json.network,
  1526. );
  1527. }
  1528. toJson() {
  1529. return {
  1530. method: this.method,
  1531. password: this.password,
  1532. network: this.network,
  1533. };
  1534. }
  1535. };
  1536. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1537. constructor(protocol, address, port, network='tcp,udp') {
  1538. super(protocol);
  1539. this.address = address;
  1540. this.port = port;
  1541. this.network = network;
  1542. }
  1543. static fromJson(json={}) {
  1544. return new Inbound.DokodemoSettings(
  1545. Protocols.DOKODEMO,
  1546. json.address,
  1547. json.port,
  1548. json.network,
  1549. );
  1550. }
  1551. toJson() {
  1552. return {
  1553. address: this.address,
  1554. port: this.port,
  1555. network: this.network,
  1556. };
  1557. }
  1558. };
  1559. Inbound.MtprotoSettings = class extends Inbound.Settings {
  1560. constructor(protocol, users=[new Inbound.MtprotoSettings.MtUser()]) {
  1561. super(protocol);
  1562. this.users = users;
  1563. }
  1564. static fromJson(json={}) {
  1565. return new Inbound.MtprotoSettings(
  1566. Protocols.MTPROTO,
  1567. json.users.map(user => Inbound.MtprotoSettings.MtUser.fromJson(user)),
  1568. );
  1569. }
  1570. toJson() {
  1571. return {
  1572. users: XrayCommonClass.toJsonArray(this.users),
  1573. };
  1574. }
  1575. };
  1576. Inbound.MtprotoSettings.MtUser = class extends XrayCommonClass {
  1577. constructor(secret=RandomUtil.randomMTSecret()) {
  1578. super();
  1579. this.secret = secret;
  1580. }
  1581. static fromJson(json={}) {
  1582. return new Inbound.MtprotoSettings.MtUser(json.secret);
  1583. }
  1584. };
  1585. Inbound.SocksSettings = class extends Inbound.Settings {
  1586. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1587. super(protocol);
  1588. this.auth = auth;
  1589. this.accounts = accounts;
  1590. this.udp = udp;
  1591. this.ip = ip;
  1592. }
  1593. addAccount(account) {
  1594. this.accounts.push(account);
  1595. }
  1596. delAccount(index) {
  1597. this.accounts.splice(index, 1);
  1598. }
  1599. static fromJson(json={}) {
  1600. let accounts;
  1601. if (json.auth === 'password') {
  1602. accounts = json.accounts.map(
  1603. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1604. )
  1605. }
  1606. return new Inbound.SocksSettings(
  1607. Protocols.SOCKS,
  1608. json.auth,
  1609. accounts,
  1610. json.udp,
  1611. json.ip,
  1612. );
  1613. }
  1614. toJson() {
  1615. return {
  1616. auth: this.auth,
  1617. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1618. udp: this.udp,
  1619. ip: this.ip,
  1620. };
  1621. }
  1622. };
  1623. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1624. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1625. super();
  1626. this.user = user;
  1627. this.pass = pass;
  1628. }
  1629. static fromJson(json={}) {
  1630. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1631. }
  1632. };
  1633. Inbound.HttpSettings = class extends Inbound.Settings {
  1634. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1635. super(protocol);
  1636. this.accounts = accounts;
  1637. }
  1638. addAccount(account) {
  1639. this.accounts.push(account);
  1640. }
  1641. delAccount(index) {
  1642. this.accounts.splice(index, 1);
  1643. }
  1644. static fromJson(json={}) {
  1645. return new Inbound.HttpSettings(
  1646. Protocols.HTTP,
  1647. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1648. );
  1649. }
  1650. toJson() {
  1651. return {
  1652. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  1653. };
  1654. }
  1655. };
  1656. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  1657. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1658. super();
  1659. this.user = user;
  1660. this.pass = pass;
  1661. }
  1662. static fromJson(json={}) {
  1663. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  1664. }
  1665. };