inbound.js 77 KB

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