inbound.js 75 KB

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