xray.js 64 KB

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