xray.js 64 KB

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