xray.js 53 KB

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