xray.js 64 KB

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