xray.js 63 KB

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