xray.js 53 KB

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