xray.js 63 KB

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