xray.js 50 KB

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