xray.js 65 KB

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