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. 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.H2, ALPN_OPTION.HTTP1],
  506. echServerKeys = '',
  507. echForceQuery = 'none',
  508. settings = new TlsStreamSettings.Settings()
  509. ) {
  510. super();
  511. this.sni = serverName;
  512. this.minVersion = minVersion;
  513. this.maxVersion = maxVersion;
  514. this.cipherSuites = cipherSuites;
  515. this.rejectUnknownSni = rejectUnknownSni;
  516. this.verifyPeerCertInNames = Array.isArray(verifyPeerCertInNames) ? verifyPeerCertInNames.join(",") : verifyPeerCertInNames;
  517. this.disableSystemRoot = disableSystemRoot;
  518. this.enableSessionResumption = enableSessionResumption;
  519. this.certs = certificates;
  520. this.alpn = alpn;
  521. this.echServerKeys = echServerKeys;
  522. this.echForceQuery = echForceQuery;
  523. this.settings = settings;
  524. }
  525. addCert() {
  526. this.certs.push(new TlsStreamSettings.Cert());
  527. }
  528. removeCert(index) {
  529. this.certs.splice(index, 1);
  530. }
  531. static fromJson(json = {}) {
  532. let certs;
  533. let settings;
  534. if (!ObjectUtil.isEmpty(json.certificates)) {
  535. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  536. }
  537. if (!ObjectUtil.isEmpty(json.settings)) {
  538. settings = new TlsStreamSettings.Settings(json.settings.allowInsecure, json.settings.fingerprint, json.settings.echConfigList);
  539. }
  540. return new TlsStreamSettings(
  541. json.serverName,
  542. json.minVersion,
  543. json.maxVersion,
  544. json.cipherSuites,
  545. json.rejectUnknownSni,
  546. json.verifyPeerCertInNames,
  547. json.disableSystemRoot,
  548. json.enableSessionResumption,
  549. certs,
  550. json.alpn,
  551. json.echServerKeys,
  552. json.echForceQuery,
  553. settings,
  554. );
  555. }
  556. toJson() {
  557. return {
  558. serverName: this.sni,
  559. minVersion: this.minVersion,
  560. maxVersion: this.maxVersion,
  561. cipherSuites: this.cipherSuites,
  562. rejectUnknownSni: this.rejectUnknownSni,
  563. verifyPeerCertInNames: this.verifyPeerCertInNames.split(","),
  564. disableSystemRoot: this.disableSystemRoot,
  565. enableSessionResumption: this.enableSessionResumption,
  566. certificates: TlsStreamSettings.toJsonArray(this.certs),
  567. alpn: this.alpn,
  568. echServerKeys: this.echServerKeys,
  569. echForceQuery: this.echForceQuery,
  570. settings: this.settings,
  571. };
  572. }
  573. }
  574. TlsStreamSettings.Cert = class extends XrayCommonClass {
  575. constructor(
  576. useFile = true,
  577. certificateFile = '',
  578. keyFile = '',
  579. certificate = '',
  580. key = '',
  581. oneTimeLoading = false,
  582. usage = USAGE_OPTION.ENCIPHERMENT,
  583. buildChain = false,
  584. ) {
  585. super();
  586. this.useFile = useFile;
  587. this.certFile = certificateFile;
  588. this.keyFile = keyFile;
  589. this.cert = Array.isArray(certificate) ? certificate.join('\n') : certificate;
  590. this.key = Array.isArray(key) ? key.join('\n') : key;
  591. this.oneTimeLoading = oneTimeLoading;
  592. this.usage = usage;
  593. this.buildChain = buildChain
  594. }
  595. static fromJson(json = {}) {
  596. if ('certificateFile' in json && 'keyFile' in json) {
  597. return new TlsStreamSettings.Cert(
  598. true,
  599. json.certificateFile,
  600. json.keyFile, '', '',
  601. json.oneTimeLoading,
  602. json.usage,
  603. json.buildChain,
  604. );
  605. } else {
  606. return new TlsStreamSettings.Cert(
  607. false, '', '',
  608. json.certificate.join('\n'),
  609. json.key.join('\n'),
  610. json.oneTimeLoading,
  611. json.usage,
  612. json.buildChain,
  613. );
  614. }
  615. }
  616. toJson() {
  617. if (this.useFile) {
  618. return {
  619. certificateFile: this.certFile,
  620. keyFile: this.keyFile,
  621. oneTimeLoading: this.oneTimeLoading,
  622. usage: this.usage,
  623. buildChain: this.buildChain,
  624. };
  625. } else {
  626. return {
  627. certificate: this.cert.split('\n'),
  628. key: this.key.split('\n'),
  629. oneTimeLoading: this.oneTimeLoading,
  630. usage: this.usage,
  631. buildChain: this.buildChain,
  632. };
  633. }
  634. }
  635. };
  636. TlsStreamSettings.Settings = class extends XrayCommonClass {
  637. constructor(
  638. allowInsecure = false,
  639. fingerprint = UTLS_FINGERPRINT.UTLS_CHROME,
  640. echConfigList = '',
  641. ) {
  642. super();
  643. this.allowInsecure = allowInsecure;
  644. this.fingerprint = fingerprint;
  645. this.echConfigList = echConfigList;
  646. }
  647. static fromJson(json = {}) {
  648. return new TlsStreamSettings.Settings(
  649. json.allowInsecure,
  650. json.fingerprint,
  651. json.echConfigList,
  652. );
  653. }
  654. toJson() {
  655. return {
  656. allowInsecure: this.allowInsecure,
  657. fingerprint: this.fingerprint,
  658. echConfigList: this.echConfigList
  659. };
  660. }
  661. };
  662. class RealityStreamSettings extends XrayCommonClass {
  663. constructor(
  664. show = false,
  665. xver = 0,
  666. dest = 'google.com:443',
  667. serverNames = 'google.com,www.google.com',
  668. privateKey = '',
  669. minClientVer = '',
  670. maxClientVer = '',
  671. maxTimediff = 0,
  672. shortIds = RandomUtil.randomShortIds(),
  673. mldsa65Seed = '',
  674. settings = new RealityStreamSettings.Settings()
  675. ) {
  676. super();
  677. this.show = show;
  678. this.xver = xver;
  679. this.dest = dest;
  680. this.serverNames = Array.isArray(serverNames) ? serverNames.join(",") : serverNames;
  681. this.privateKey = privateKey;
  682. this.minClientVer = minClientVer;
  683. this.maxClientVer = maxClientVer;
  684. this.maxTimediff = maxTimediff;
  685. this.shortIds = Array.isArray(shortIds) ? shortIds.join(",") : shortIds;
  686. this.mldsa65Seed = mldsa65Seed;
  687. this.settings = settings;
  688. }
  689. static fromJson(json = {}) {
  690. let settings;
  691. if (!ObjectUtil.isEmpty(json.settings)) {
  692. settings = new RealityStreamSettings.Settings(
  693. json.settings.publicKey,
  694. json.settings.fingerprint,
  695. json.settings.serverName,
  696. json.settings.spiderX,
  697. json.settings.mldsa65Verify,
  698. );
  699. }
  700. return new RealityStreamSettings(
  701. json.show,
  702. json.xver,
  703. json.dest,
  704. json.serverNames,
  705. json.privateKey,
  706. json.minClientVer,
  707. json.maxClientVer,
  708. json.maxTimediff,
  709. json.shortIds,
  710. json.mldsa65Seed,
  711. settings,
  712. );
  713. }
  714. toJson() {
  715. return {
  716. show: this.show,
  717. xver: this.xver,
  718. dest: this.dest,
  719. serverNames: this.serverNames.split(","),
  720. privateKey: this.privateKey,
  721. minClientVer: this.minClientVer,
  722. maxClientVer: this.maxClientVer,
  723. maxTimediff: this.maxTimediff,
  724. shortIds: this.shortIds.split(","),
  725. mldsa65Seed: this.mldsa65Seed,
  726. settings: this.settings,
  727. };
  728. }
  729. }
  730. RealityStreamSettings.Settings = class extends XrayCommonClass {
  731. constructor(
  732. publicKey = '',
  733. fingerprint = UTLS_FINGERPRINT.UTLS_CHROME,
  734. serverName = '',
  735. spiderX = '/',
  736. mldsa65Verify = ''
  737. ) {
  738. super();
  739. this.publicKey = publicKey;
  740. this.fingerprint = fingerprint;
  741. this.serverName = serverName;
  742. this.spiderX = spiderX;
  743. this.mldsa65Verify = mldsa65Verify;
  744. }
  745. static fromJson(json = {}) {
  746. return new RealityStreamSettings.Settings(
  747. json.publicKey,
  748. json.fingerprint,
  749. json.serverName,
  750. json.spiderX,
  751. json.mldsa65Verify
  752. );
  753. }
  754. toJson() {
  755. return {
  756. publicKey: this.publicKey,
  757. fingerprint: this.fingerprint,
  758. serverName: this.serverName,
  759. spiderX: this.spiderX,
  760. mldsa65Verify: this.mldsa65Verify
  761. };
  762. }
  763. };
  764. class SockoptStreamSettings extends XrayCommonClass {
  765. constructor(
  766. acceptProxyProtocol = false,
  767. tcpFastOpen = false,
  768. mark = 0,
  769. tproxy = "off",
  770. tcpMptcp = false,
  771. penetrate = false,
  772. domainStrategy = DOMAIN_STRATEGY_OPTION.USE_IP,
  773. tcpMaxSeg = 1440,
  774. dialerProxy = "",
  775. tcpKeepAliveInterval = 0,
  776. tcpKeepAliveIdle = 300,
  777. tcpUserTimeout = 10000,
  778. tcpcongestion = TCP_CONGESTION_OPTION.BBR,
  779. V6Only = false,
  780. tcpWindowClamp = 600,
  781. interfaceName = "",
  782. ) {
  783. super();
  784. this.acceptProxyProtocol = acceptProxyProtocol;
  785. this.tcpFastOpen = tcpFastOpen;
  786. this.mark = mark;
  787. this.tproxy = tproxy;
  788. this.tcpMptcp = tcpMptcp;
  789. this.penetrate = penetrate;
  790. this.domainStrategy = domainStrategy;
  791. this.tcpMaxSeg = tcpMaxSeg;
  792. this.dialerProxy = dialerProxy;
  793. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  794. this.tcpKeepAliveIdle = tcpKeepAliveIdle;
  795. this.tcpUserTimeout = tcpUserTimeout;
  796. this.tcpcongestion = tcpcongestion;
  797. this.V6Only = V6Only;
  798. this.tcpWindowClamp = tcpWindowClamp;
  799. this.interfaceName = interfaceName;
  800. }
  801. static fromJson(json = {}) {
  802. if (Object.keys(json).length === 0) return undefined;
  803. return new SockoptStreamSettings(
  804. json.acceptProxyProtocol,
  805. json.tcpFastOpen,
  806. json.mark,
  807. json.tproxy,
  808. json.tcpMptcp,
  809. json.penetrate,
  810. json.domainStrategy,
  811. json.tcpMaxSeg,
  812. json.dialerProxy,
  813. json.tcpKeepAliveInterval,
  814. json.tcpKeepAliveIdle,
  815. json.tcpUserTimeout,
  816. json.tcpcongestion,
  817. json.V6Only,
  818. json.tcpWindowClamp,
  819. json.interface,
  820. );
  821. }
  822. toJson() {
  823. return {
  824. acceptProxyProtocol: this.acceptProxyProtocol,
  825. tcpFastOpen: this.tcpFastOpen,
  826. mark: this.mark,
  827. tproxy: this.tproxy,
  828. tcpMptcp: this.tcpMptcp,
  829. penetrate: this.penetrate,
  830. domainStrategy: this.domainStrategy,
  831. tcpMaxSeg: this.tcpMaxSeg,
  832. dialerProxy: this.dialerProxy,
  833. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  834. tcpKeepAliveIdle: this.tcpKeepAliveIdle,
  835. tcpUserTimeout: this.tcpUserTimeout,
  836. tcpcongestion: this.tcpcongestion,
  837. V6Only: this.V6Only,
  838. tcpWindowClamp: this.tcpWindowClamp,
  839. interface: this.interfaceName,
  840. };
  841. }
  842. }
  843. class StreamSettings extends XrayCommonClass {
  844. constructor(network = 'tcp',
  845. security = 'none',
  846. externalProxy = [],
  847. tlsSettings = new TlsStreamSettings(),
  848. realitySettings = new RealityStreamSettings(),
  849. tcpSettings = new TcpStreamSettings(),
  850. kcpSettings = new KcpStreamSettings(),
  851. wsSettings = new WsStreamSettings(),
  852. grpcSettings = new GrpcStreamSettings(),
  853. httpupgradeSettings = new HTTPUpgradeStreamSettings(),
  854. xhttpSettings = new xHTTPStreamSettings(),
  855. sockopt = undefined,
  856. ) {
  857. super();
  858. this.network = network;
  859. this.security = security;
  860. this.externalProxy = externalProxy;
  861. this.tls = tlsSettings;
  862. this.reality = realitySettings;
  863. this.tcp = tcpSettings;
  864. this.kcp = kcpSettings;
  865. this.ws = wsSettings;
  866. this.grpc = grpcSettings;
  867. this.httpupgrade = httpupgradeSettings;
  868. this.xhttp = xhttpSettings;
  869. this.sockopt = sockopt;
  870. }
  871. get isTls() {
  872. return this.security === "tls";
  873. }
  874. set isTls(isTls) {
  875. if (isTls) {
  876. this.security = 'tls';
  877. } else {
  878. this.security = 'none';
  879. }
  880. }
  881. //for Reality
  882. get isReality() {
  883. return this.security === "reality";
  884. }
  885. set isReality(isReality) {
  886. if (isReality) {
  887. this.security = 'reality';
  888. } else {
  889. this.security = 'none';
  890. }
  891. }
  892. get sockoptSwitch() {
  893. return this.sockopt != undefined;
  894. }
  895. set sockoptSwitch(value) {
  896. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  897. }
  898. static fromJson(json = {}) {
  899. return new StreamSettings(
  900. json.network,
  901. json.security,
  902. json.externalProxy,
  903. TlsStreamSettings.fromJson(json.tlsSettings),
  904. RealityStreamSettings.fromJson(json.realitySettings),
  905. TcpStreamSettings.fromJson(json.tcpSettings),
  906. KcpStreamSettings.fromJson(json.kcpSettings),
  907. WsStreamSettings.fromJson(json.wsSettings),
  908. GrpcStreamSettings.fromJson(json.grpcSettings),
  909. HTTPUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  910. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  911. SockoptStreamSettings.fromJson(json.sockopt),
  912. );
  913. }
  914. toJson() {
  915. const network = this.network;
  916. return {
  917. network: network,
  918. security: this.security,
  919. externalProxy: this.externalProxy,
  920. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  921. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  922. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  923. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  924. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  925. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  926. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  927. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  928. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  929. };
  930. }
  931. }
  932. class Sniffing extends XrayCommonClass {
  933. constructor(
  934. enabled = false,
  935. destOverride = ['http', 'tls', 'quic', 'fakedns'],
  936. metadataOnly = false,
  937. routeOnly = false) {
  938. super();
  939. this.enabled = enabled;
  940. this.destOverride = destOverride;
  941. this.metadataOnly = metadataOnly;
  942. this.routeOnly = routeOnly;
  943. }
  944. static fromJson(json = {}) {
  945. let destOverride = ObjectUtil.clone(json.destOverride);
  946. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  947. if (ObjectUtil.isEmpty(destOverride[0])) {
  948. destOverride = ['http', 'tls', 'quic', 'fakedns'];
  949. }
  950. }
  951. return new Sniffing(
  952. !!json.enabled,
  953. destOverride,
  954. json.metadataOnly,
  955. json.routeOnly,
  956. );
  957. }
  958. }
  959. class Inbound extends XrayCommonClass {
  960. constructor(
  961. port = RandomUtil.randomInteger(10000, 60000),
  962. listen = '',
  963. protocol = Protocols.VLESS,
  964. settings = null,
  965. streamSettings = new StreamSettings(),
  966. tag = '',
  967. sniffing = new Sniffing(),
  968. clientStats = '',
  969. ) {
  970. super();
  971. this.port = port;
  972. this.listen = listen;
  973. this._protocol = protocol;
  974. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  975. this.stream = streamSettings;
  976. this.tag = tag;
  977. this.sniffing = sniffing;
  978. this.clientStats = clientStats;
  979. }
  980. getClientStats() {
  981. return this.clientStats;
  982. }
  983. get clients() {
  984. switch (this.protocol) {
  985. case Protocols.VMESS: return this.settings.vmesses;
  986. case Protocols.VLESS: return this.settings.vlesses;
  987. case Protocols.TROJAN: return this.settings.trojans;
  988. case Protocols.SHADOWSOCKS: return this.isSSMultiUser ? this.settings.shadowsockses : null;
  989. default: return null;
  990. }
  991. }
  992. get protocol() {
  993. return this._protocol;
  994. }
  995. set protocol(protocol) {
  996. this._protocol = protocol;
  997. this.settings = Inbound.Settings.getSettings(protocol);
  998. if (protocol === Protocols.TROJAN) {
  999. this.tls = false;
  1000. }
  1001. }
  1002. get network() {
  1003. return this.stream.network;
  1004. }
  1005. set network(network) {
  1006. this.stream.network = network;
  1007. }
  1008. get isTcp() {
  1009. return this.network === "tcp";
  1010. }
  1011. get isWs() {
  1012. return this.network === "ws";
  1013. }
  1014. get isKcp() {
  1015. return this.network === "kcp";
  1016. }
  1017. get isGrpc() {
  1018. return this.network === "grpc";
  1019. }
  1020. get isHttpupgrade() {
  1021. return this.network === "httpupgrade";
  1022. }
  1023. get isXHTTP() {
  1024. return this.network === "xhttp";
  1025. }
  1026. // Shadowsocks
  1027. get method() {
  1028. switch (this.protocol) {
  1029. case Protocols.SHADOWSOCKS:
  1030. return this.settings.method;
  1031. default:
  1032. return "";
  1033. }
  1034. }
  1035. get isSSMultiUser() {
  1036. return this.method != SSMethods.BLAKE3_CHACHA20_POLY1305;
  1037. }
  1038. get isSS2022() {
  1039. return this.method.substring(0, 4) === "2022";
  1040. }
  1041. get serverName() {
  1042. if (this.stream.isTls) return this.stream.tls.sni;
  1043. if (this.stream.isReality) return this.stream.reality.serverNames;
  1044. return "";
  1045. }
  1046. getHeader(obj, name) {
  1047. for (const header of obj.headers) {
  1048. if (header.name.toLowerCase() === name.toLowerCase()) {
  1049. return header.value;
  1050. }
  1051. }
  1052. return "";
  1053. }
  1054. get host() {
  1055. if (this.isTcp) {
  1056. return this.getHeader(this.stream.tcp.request, 'host');
  1057. } else if (this.isWs) {
  1058. return this.stream.ws.host?.length > 0 ? this.stream.ws.host : this.getHeader(this.stream.ws, 'host');
  1059. } else if (this.isHttpupgrade) {
  1060. return this.stream.httpupgrade.host?.length > 0 ? this.stream.httpupgrade.host : this.getHeader(this.stream.httpupgrade, 'host');
  1061. } else if (this.isXHTTP) {
  1062. return this.stream.xhttp.host?.length > 0 ? this.stream.xhttp.host : this.getHeader(this.stream.xhttp, 'host');
  1063. }
  1064. return null;
  1065. }
  1066. get path() {
  1067. if (this.isTcp) {
  1068. return this.stream.tcp.request.path[0];
  1069. } else if (this.isWs) {
  1070. return this.stream.ws.path;
  1071. } else if (this.isHttpupgrade) {
  1072. return this.stream.httpupgrade.path;
  1073. } else if (this.isXHTTP) {
  1074. return this.stream.xhttp.path;
  1075. }
  1076. return null;
  1077. }
  1078. get kcpType() {
  1079. return this.stream.kcp.type;
  1080. }
  1081. get kcpSeed() {
  1082. return this.stream.kcp.seed;
  1083. }
  1084. get serviceName() {
  1085. return this.stream.grpc.serviceName;
  1086. }
  1087. isExpiry(index) {
  1088. let exp = this.clients[index].expiryTime;
  1089. return exp > 0 ? exp < new Date().getTime() : false;
  1090. }
  1091. canEnableTls() {
  1092. if (![Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol)) return false;
  1093. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.network);
  1094. }
  1095. //this is used for xtls-rprx-vision
  1096. canEnableTlsFlow() {
  1097. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1098. return this.protocol === Protocols.VLESS;
  1099. }
  1100. return false;
  1101. }
  1102. canEnableReality() {
  1103. if (![Protocols.VLESS, Protocols.TROJAN].includes(this.protocol)) return false;
  1104. return ["tcp", "http", "grpc", "xhttp"].includes(this.network);
  1105. }
  1106. canEnableStream() {
  1107. return [Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol);
  1108. }
  1109. reset() {
  1110. this.port = RandomUtil.randomInteger(10000, 60000);
  1111. this.listen = '';
  1112. this.protocol = Protocols.VMESS;
  1113. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1114. this.stream = new StreamSettings();
  1115. this.tag = '';
  1116. this.sniffing = new Sniffing();
  1117. }
  1118. genVmessLink(address = '', port = this.port, forceTls, remark = '', clientId, security) {
  1119. if (this.protocol !== Protocols.VMESS) {
  1120. return '';
  1121. }
  1122. const tls = forceTls == 'same' ? this.stream.security : forceTls;
  1123. let obj = {
  1124. v: '2',
  1125. ps: remark,
  1126. add: address,
  1127. port: port,
  1128. id: clientId,
  1129. scy: security,
  1130. net: this.stream.network,
  1131. tls: tls,
  1132. };
  1133. const network = this.stream.network;
  1134. if (network === 'tcp') {
  1135. const tcp = this.stream.tcp;
  1136. obj.type = tcp.type;
  1137. if (tcp.type === 'http') {
  1138. const request = tcp.request;
  1139. obj.path = request.path.join(',');
  1140. const host = this.getHeader(request, 'host');
  1141. if (host) obj.host = host;
  1142. }
  1143. } else if (network === 'kcp') {
  1144. const kcp = this.stream.kcp;
  1145. obj.type = kcp.type;
  1146. obj.path = kcp.seed;
  1147. } else if (network === 'ws') {
  1148. const ws = this.stream.ws;
  1149. obj.path = ws.path;
  1150. obj.host = ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host');
  1151. } else if (network === 'grpc') {
  1152. obj.path = this.stream.grpc.serviceName;
  1153. obj.authority = this.stream.grpc.authority;
  1154. if (this.stream.grpc.multiMode) {
  1155. obj.type = 'multi'
  1156. }
  1157. } else if (network === 'httpupgrade') {
  1158. const httpupgrade = this.stream.httpupgrade;
  1159. obj.path = httpupgrade.path;
  1160. obj.host = httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host');
  1161. } else if (network === 'xhttp') {
  1162. const xhttp = this.stream.xhttp;
  1163. obj.path = xhttp.path;
  1164. obj.host = xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host');
  1165. obj.type = xhttp.mode;
  1166. }
  1167. if (tls === 'tls') {
  1168. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1169. obj.sni = this.stream.tls.sni;
  1170. }
  1171. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)) {
  1172. obj.fp = this.stream.tls.settings.fingerprint;
  1173. }
  1174. if (this.stream.tls.alpn.length > 0) {
  1175. obj.alpn = this.stream.tls.alpn.join(',');
  1176. }
  1177. if (this.stream.tls.settings.allowInsecure) {
  1178. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1179. }
  1180. }
  1181. return 'vmess://' + Base64.encode(JSON.stringify(obj, null, 2));
  1182. }
  1183. genVLESSLink(address = '', port = this.port, forceTls, remark = '', clientId, flow) {
  1184. const uuid = clientId;
  1185. const type = this.stream.network;
  1186. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1187. const params = new Map();
  1188. params.set("type", this.stream.network);
  1189. params.set("encryption", this.settings.encryption);
  1190. switch (type) {
  1191. case "tcp":
  1192. const tcp = this.stream.tcp;
  1193. if (tcp.type === 'http') {
  1194. const request = tcp.request;
  1195. params.set("path", request.path.join(','));
  1196. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1197. if (index >= 0) {
  1198. const host = request.headers[index].value;
  1199. params.set("host", host);
  1200. }
  1201. params.set("headerType", 'http');
  1202. }
  1203. break;
  1204. case "kcp":
  1205. const kcp = this.stream.kcp;
  1206. params.set("headerType", kcp.type);
  1207. params.set("seed", kcp.seed);
  1208. break;
  1209. case "ws":
  1210. const ws = this.stream.ws;
  1211. params.set("path", ws.path);
  1212. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1213. break;
  1214. case "grpc":
  1215. const grpc = this.stream.grpc;
  1216. params.set("serviceName", grpc.serviceName);
  1217. params.set("authority", grpc.authority);
  1218. if (grpc.multiMode) {
  1219. params.set("mode", "multi");
  1220. }
  1221. break;
  1222. case "httpupgrade":
  1223. const httpupgrade = this.stream.httpupgrade;
  1224. params.set("path", httpupgrade.path);
  1225. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1226. break;
  1227. case "xhttp":
  1228. const xhttp = this.stream.xhttp;
  1229. params.set("path", xhttp.path);
  1230. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1231. params.set("mode", xhttp.mode);
  1232. break;
  1233. }
  1234. if (security === 'tls') {
  1235. params.set("security", "tls");
  1236. if (this.stream.isTls) {
  1237. params.set("fp", this.stream.tls.settings.fingerprint);
  1238. params.set("alpn", this.stream.tls.alpn);
  1239. if (this.stream.tls.settings.allowInsecure) {
  1240. params.set("allowInsecure", "1");
  1241. }
  1242. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1243. params.set("sni", this.stream.tls.sni);
  1244. }
  1245. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1246. params.set("ech", this.stream.tls.settings.echConfigList);
  1247. }
  1248. if (type == "tcp" && !ObjectUtil.isEmpty(flow)) {
  1249. params.set("flow", flow);
  1250. }
  1251. }
  1252. }
  1253. else if (security === 'reality') {
  1254. params.set("security", "reality");
  1255. params.set("pbk", this.stream.reality.settings.publicKey);
  1256. params.set("fp", this.stream.reality.settings.fingerprint);
  1257. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1258. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1259. }
  1260. if (this.stream.reality.shortIds.length > 0) {
  1261. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1262. }
  1263. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1264. params.set("spx", this.stream.reality.settings.spiderX);
  1265. }
  1266. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1267. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1268. }
  1269. if (type == 'tcp' && !ObjectUtil.isEmpty(flow)) {
  1270. params.set("flow", flow);
  1271. }
  1272. }
  1273. else {
  1274. params.set("security", "none");
  1275. }
  1276. const link = `vless://${uuid}@${address}:${port}`;
  1277. const url = new URL(link);
  1278. for (const [key, value] of params) {
  1279. url.searchParams.set(key, value)
  1280. }
  1281. url.hash = encodeURIComponent(remark);
  1282. return url.toString();
  1283. }
  1284. genSSLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1285. let settings = this.settings;
  1286. const type = this.stream.network;
  1287. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1288. const params = new Map();
  1289. params.set("type", this.stream.network);
  1290. switch (type) {
  1291. case "tcp":
  1292. const tcp = this.stream.tcp;
  1293. if (tcp.type === 'http') {
  1294. const request = tcp.request;
  1295. params.set("path", request.path.join(','));
  1296. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1297. if (index >= 0) {
  1298. const host = request.headers[index].value;
  1299. params.set("host", host);
  1300. }
  1301. params.set("headerType", 'http');
  1302. }
  1303. break;
  1304. case "kcp":
  1305. const kcp = this.stream.kcp;
  1306. params.set("headerType", kcp.type);
  1307. params.set("seed", kcp.seed);
  1308. break;
  1309. case "ws":
  1310. const ws = this.stream.ws;
  1311. params.set("path", ws.path);
  1312. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1313. break;
  1314. case "grpc":
  1315. const grpc = this.stream.grpc;
  1316. params.set("serviceName", grpc.serviceName);
  1317. params.set("authority", grpc.authority);
  1318. if (grpc.multiMode) {
  1319. params.set("mode", "multi");
  1320. }
  1321. break;
  1322. case "httpupgrade":
  1323. const httpupgrade = this.stream.httpupgrade;
  1324. params.set("path", httpupgrade.path);
  1325. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1326. break;
  1327. case "xhttp":
  1328. const xhttp = this.stream.xhttp;
  1329. params.set("path", xhttp.path);
  1330. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1331. params.set("mode", xhttp.mode);
  1332. break;
  1333. }
  1334. if (security === 'tls') {
  1335. params.set("security", "tls");
  1336. if (this.stream.isTls) {
  1337. params.set("fp", this.stream.tls.settings.fingerprint);
  1338. params.set("alpn", this.stream.tls.alpn);
  1339. if (this.stream.tls.settings.allowInsecure) {
  1340. params.set("allowInsecure", "1");
  1341. }
  1342. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1343. params.set("ech", this.stream.tls.settings.echConfigList);
  1344. }
  1345. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1346. params.set("sni", this.stream.tls.sni);
  1347. }
  1348. }
  1349. }
  1350. let password = new Array();
  1351. if (this.isSS2022) password.push(settings.password);
  1352. if (this.isSSMultiUser) password.push(clientPassword);
  1353. let link = `ss://${Base64.encode(`${settings.method}:${password.join(':')}`, true)}@${address}:${port}`;
  1354. const url = new URL(link);
  1355. for (const [key, value] of params) {
  1356. url.searchParams.set(key, value)
  1357. }
  1358. url.hash = encodeURIComponent(remark);
  1359. return url.toString();
  1360. }
  1361. genTrojanLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1362. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1363. const type = this.stream.network;
  1364. const params = new Map();
  1365. params.set("type", this.stream.network);
  1366. switch (type) {
  1367. case "tcp":
  1368. const tcp = this.stream.tcp;
  1369. if (tcp.type === 'http') {
  1370. const request = tcp.request;
  1371. params.set("path", request.path.join(','));
  1372. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1373. if (index >= 0) {
  1374. const host = request.headers[index].value;
  1375. params.set("host", host);
  1376. }
  1377. params.set("headerType", 'http');
  1378. }
  1379. break;
  1380. case "kcp":
  1381. const kcp = this.stream.kcp;
  1382. params.set("headerType", kcp.type);
  1383. params.set("seed", kcp.seed);
  1384. break;
  1385. case "ws":
  1386. const ws = this.stream.ws;
  1387. params.set("path", ws.path);
  1388. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1389. break;
  1390. case "grpc":
  1391. const grpc = this.stream.grpc;
  1392. params.set("serviceName", grpc.serviceName);
  1393. params.set("authority", grpc.authority);
  1394. if (grpc.multiMode) {
  1395. params.set("mode", "multi");
  1396. }
  1397. break;
  1398. case "httpupgrade":
  1399. const httpupgrade = this.stream.httpupgrade;
  1400. params.set("path", httpupgrade.path);
  1401. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1402. break;
  1403. case "xhttp":
  1404. const xhttp = this.stream.xhttp;
  1405. params.set("path", xhttp.path);
  1406. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1407. params.set("mode", xhttp.mode);
  1408. break;
  1409. }
  1410. if (security === 'tls') {
  1411. params.set("security", "tls");
  1412. if (this.stream.isTls) {
  1413. params.set("fp", this.stream.tls.settings.fingerprint);
  1414. params.set("alpn", this.stream.tls.alpn);
  1415. if (this.stream.tls.settings.allowInsecure) {
  1416. params.set("allowInsecure", "1");
  1417. }
  1418. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1419. params.set("ech", this.stream.tls.settings.echConfigList);
  1420. }
  1421. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1422. params.set("sni", this.stream.tls.sni);
  1423. }
  1424. }
  1425. }
  1426. else if (security === 'reality') {
  1427. params.set("security", "reality");
  1428. params.set("pbk", this.stream.reality.settings.publicKey);
  1429. params.set("fp", this.stream.reality.settings.fingerprint);
  1430. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1431. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1432. }
  1433. if (this.stream.reality.shortIds.length > 0) {
  1434. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1435. }
  1436. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1437. params.set("spx", this.stream.reality.settings.spiderX);
  1438. }
  1439. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1440. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1441. }
  1442. }
  1443. else {
  1444. params.set("security", "none");
  1445. }
  1446. const link = `trojan://${clientPassword}@${address}:${port}`;
  1447. const url = new URL(link);
  1448. for (const [key, value] of params) {
  1449. url.searchParams.set(key, value)
  1450. }
  1451. url.hash = encodeURIComponent(remark);
  1452. return url.toString();
  1453. }
  1454. getWireguardLink(address, port, remark, peerId) {
  1455. let txt = `[Interface]\n`
  1456. txt += `PrivateKey = ${this.settings.peers[peerId].privateKey}\n`
  1457. txt += `Address = ${this.settings.peers[peerId].allowedIPs[0]}\n`
  1458. txt += `DNS = 1.1.1.1, 1.0.0.1\n`
  1459. if (this.settings.mtu) {
  1460. txt += `MTU = ${this.settings.mtu}\n`
  1461. }
  1462. txt += `\n# ${remark}\n`
  1463. txt += `[Peer]\n`
  1464. txt += `PublicKey = ${this.settings.pubKey}\n`
  1465. txt += `AllowedIPs = 0.0.0.0/0, ::/0\n`
  1466. txt += `Endpoint = ${address}:${port}`
  1467. if (this.settings.peers[peerId].psk) {
  1468. txt += `\nPresharedKey = ${this.settings.peers[peerId].psk}`
  1469. }
  1470. if (this.settings.peers[peerId].keepAlive) {
  1471. txt += `\nPersistentKeepalive = ${this.settings.peers[peerId].keepAlive}\n`
  1472. }
  1473. return txt;
  1474. }
  1475. genLink(address = '', port = this.port, forceTls = 'same', remark = '', client) {
  1476. switch (this.protocol) {
  1477. case Protocols.VMESS:
  1478. return this.genVmessLink(address, port, forceTls, remark, client.id, client.security);
  1479. case Protocols.VLESS:
  1480. return this.genVLESSLink(address, port, forceTls, remark, client.id, client.flow);
  1481. case Protocols.SHADOWSOCKS:
  1482. return this.genSSLink(address, port, forceTls, remark, this.isSSMultiUser ? client.password : '');
  1483. case Protocols.TROJAN:
  1484. return this.genTrojanLink(address, port, forceTls, remark, client.password);
  1485. default: return '';
  1486. }
  1487. }
  1488. genAllLinks(remark = '', remarkModel = '-ieo', client) {
  1489. let result = [];
  1490. let email = client ? client.email : '';
  1491. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1492. let port = this.port;
  1493. const separationChar = remarkModel.charAt(0);
  1494. const orderChars = remarkModel.slice(1);
  1495. let orders = {
  1496. 'i': remark,
  1497. 'e': email,
  1498. 'o': '',
  1499. };
  1500. if (ObjectUtil.isArrEmpty(this.stream.externalProxy)) {
  1501. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1502. result.push({
  1503. remark: r,
  1504. link: this.genLink(addr, port, 'same', r, client)
  1505. });
  1506. } else {
  1507. this.stream.externalProxy.forEach((ep) => {
  1508. orders['o'] = ep.remark;
  1509. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1510. result.push({
  1511. remark: r,
  1512. link: this.genLink(ep.dest, ep.port, ep.forceTls, r, client)
  1513. });
  1514. });
  1515. }
  1516. return result;
  1517. }
  1518. genInboundLinks(remark = '', remarkModel = '-ieo') {
  1519. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1520. if (this.clients) {
  1521. let links = [];
  1522. this.clients.forEach((client) => {
  1523. this.genAllLinks(remark, remarkModel, client).forEach(l => {
  1524. links.push(l.link);
  1525. })
  1526. });
  1527. return links.join('\r\n');
  1528. } else {
  1529. if (this.protocol == Protocols.SHADOWSOCKS && !this.isSSMultiUser) return this.genSSLink(addr, this.port, 'same', remark);
  1530. if (this.protocol == Protocols.WIREGUARD) {
  1531. let links = [];
  1532. this.settings.peers.forEach((p, index) => {
  1533. links.push(this.getWireguardLink(addr, this.port, remark + remarkModel.charAt(0) + (index + 1), index));
  1534. });
  1535. return links.join('\r\n');
  1536. }
  1537. return '';
  1538. }
  1539. }
  1540. static fromJson(json = {}) {
  1541. return new Inbound(
  1542. json.port,
  1543. json.listen,
  1544. json.protocol,
  1545. Inbound.Settings.fromJson(json.protocol, json.settings),
  1546. StreamSettings.fromJson(json.streamSettings),
  1547. json.tag,
  1548. Sniffing.fromJson(json.sniffing),
  1549. json.clientStats
  1550. )
  1551. }
  1552. toJson() {
  1553. let streamSettings;
  1554. if (this.canEnableStream() || this.stream?.sockopt) {
  1555. streamSettings = this.stream.toJson();
  1556. }
  1557. return {
  1558. port: this.port,
  1559. listen: this.listen,
  1560. protocol: this.protocol,
  1561. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1562. streamSettings: streamSettings,
  1563. tag: this.tag,
  1564. sniffing: this.sniffing.toJson(),
  1565. clientStats: this.clientStats
  1566. };
  1567. }
  1568. }
  1569. Inbound.Settings = class extends XrayCommonClass {
  1570. constructor(protocol) {
  1571. super();
  1572. this.protocol = protocol;
  1573. }
  1574. static getSettings(protocol) {
  1575. switch (protocol) {
  1576. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1577. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1578. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1579. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1580. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1581. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1582. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1583. case Protocols.WIREGUARD: return new Inbound.WireguardSettings(protocol);
  1584. default: return null;
  1585. }
  1586. }
  1587. static fromJson(protocol, json) {
  1588. switch (protocol) {
  1589. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1590. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1591. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1592. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1593. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1594. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1595. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1596. case Protocols.WIREGUARD: return Inbound.WireguardSettings.fromJson(json);
  1597. default: return null;
  1598. }
  1599. }
  1600. toJson() {
  1601. return {};
  1602. }
  1603. };
  1604. Inbound.VmessSettings = class extends Inbound.Settings {
  1605. constructor(protocol,
  1606. vmesses = [new Inbound.VmessSettings.VMESS()]) {
  1607. super(protocol);
  1608. this.vmesses = vmesses;
  1609. }
  1610. indexOfVmessById(id) {
  1611. return this.vmesses.findIndex(VMESS => VMESS.id === id);
  1612. }
  1613. addVmess(VMESS) {
  1614. if (this.indexOfVmessById(VMESS.id) >= 0) {
  1615. return false;
  1616. }
  1617. this.vmesses.push(VMESS);
  1618. }
  1619. delVmess(VMESS) {
  1620. const i = this.indexOfVmessById(VMESS.id);
  1621. if (i >= 0) {
  1622. this.vmesses.splice(i, 1);
  1623. }
  1624. }
  1625. static fromJson(json = {}) {
  1626. return new Inbound.VmessSettings(
  1627. Protocols.VMESS,
  1628. json.clients.map(client => Inbound.VmessSettings.VMESS.fromJson(client)),
  1629. );
  1630. }
  1631. toJson() {
  1632. return {
  1633. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1634. };
  1635. }
  1636. };
  1637. Inbound.VmessSettings.VMESS = class extends XrayCommonClass {
  1638. constructor(
  1639. id = RandomUtil.randomUUID(),
  1640. security = USERS_SECURITY.AUTO,
  1641. email = RandomUtil.randomLowerAndNum(8),
  1642. limitIp = 0,
  1643. totalGB = 0,
  1644. expiryTime = 0,
  1645. enable = true,
  1646. tgId = '',
  1647. subId = RandomUtil.randomLowerAndNum(16),
  1648. comment = '',
  1649. reset = 0,
  1650. created_at = undefined,
  1651. updated_at = undefined
  1652. ) {
  1653. super();
  1654. this.id = id;
  1655. this.security = security;
  1656. this.email = email;
  1657. this.limitIp = limitIp;
  1658. this.totalGB = totalGB;
  1659. this.expiryTime = expiryTime;
  1660. this.enable = enable;
  1661. this.tgId = tgId;
  1662. this.subId = subId;
  1663. this.comment = comment;
  1664. this.reset = reset;
  1665. this.created_at = created_at;
  1666. this.updated_at = updated_at;
  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. json.created_at,
  1682. json.updated_at,
  1683. );
  1684. }
  1685. get _expiryTime() {
  1686. if (this.expiryTime === 0 || this.expiryTime === "") {
  1687. return null;
  1688. }
  1689. if (this.expiryTime < 0) {
  1690. return this.expiryTime / -86400000;
  1691. }
  1692. return moment(this.expiryTime);
  1693. }
  1694. set _expiryTime(t) {
  1695. if (t == null || t === "") {
  1696. this.expiryTime = 0;
  1697. } else {
  1698. this.expiryTime = t.valueOf();
  1699. }
  1700. }
  1701. get _totalGB() {
  1702. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1703. }
  1704. set _totalGB(gb) {
  1705. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1706. }
  1707. };
  1708. Inbound.VLESSSettings = class extends Inbound.Settings {
  1709. constructor(
  1710. protocol,
  1711. vlesses = [new Inbound.VLESSSettings.VLESS()],
  1712. decryption = "none",
  1713. fallbacks = []
  1714. ) {
  1715. super(protocol);
  1716. this.vlesses = vlesses;
  1717. this.decryption = decryption;
  1718. this.fallbacks = fallbacks;
  1719. this.selectedAuth = "X25519, not Post-Quantum";
  1720. this.encryption = "";
  1721. }
  1722. addFallback() {
  1723. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1724. }
  1725. delFallback(index) {
  1726. this.fallbacks.splice(index, 1);
  1727. }
  1728. static fromJson(json = {}) {
  1729. const obj = new Inbound.VLESSSettings(
  1730. Protocols.VLESS,
  1731. (json.clients || []).map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1732. json.decryption || "none",
  1733. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks || [])
  1734. );
  1735. obj.encryption = json.encryption || "";
  1736. obj.selectedAuth = json.selectedAuth || "X25519, not Post-Quantum";
  1737. return obj;
  1738. }
  1739. toJson() {
  1740. const json = {
  1741. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1742. };
  1743. if (this.decryption) {
  1744. json.decryption = this.decryption;
  1745. }
  1746. if (this.encryption) {
  1747. json.encryption = this.encryption;
  1748. }
  1749. if (this.fallbacks && this.fallbacks.length > 0) {
  1750. json.fallbacks = Inbound.VLESSSettings.toJsonArray(this.fallbacks);
  1751. }
  1752. return json;
  1753. }
  1754. };
  1755. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1756. constructor(
  1757. id = RandomUtil.randomUUID(),
  1758. flow = '',
  1759. email = RandomUtil.randomLowerAndNum(8),
  1760. limitIp = 0,
  1761. totalGB = 0,
  1762. expiryTime = 0,
  1763. enable = true,
  1764. tgId = '',
  1765. subId = RandomUtil.randomLowerAndNum(16),
  1766. comment = '',
  1767. reset = 0,
  1768. created_at = undefined,
  1769. updated_at = undefined
  1770. ) {
  1771. super();
  1772. this.id = id;
  1773. this.flow = flow;
  1774. this.email = email;
  1775. this.limitIp = limitIp;
  1776. this.totalGB = totalGB;
  1777. this.expiryTime = expiryTime;
  1778. this.enable = enable;
  1779. this.tgId = tgId;
  1780. this.subId = subId;
  1781. this.comment = comment;
  1782. this.reset = reset;
  1783. this.created_at = created_at;
  1784. this.updated_at = updated_at;
  1785. }
  1786. static fromJson(json = {}) {
  1787. return new Inbound.VLESSSettings.VLESS(
  1788. json.id,
  1789. json.flow,
  1790. json.email,
  1791. json.limitIp,
  1792. json.totalGB,
  1793. json.expiryTime,
  1794. json.enable,
  1795. json.tgId,
  1796. json.subId,
  1797. json.comment,
  1798. json.reset,
  1799. json.created_at,
  1800. json.updated_at,
  1801. );
  1802. }
  1803. get _expiryTime() {
  1804. if (this.expiryTime === 0 || this.expiryTime === "") {
  1805. return null;
  1806. }
  1807. if (this.expiryTime < 0) {
  1808. return this.expiryTime / -86400000;
  1809. }
  1810. return moment(this.expiryTime);
  1811. }
  1812. set _expiryTime(t) {
  1813. if (t == null || t === "") {
  1814. this.expiryTime = 0;
  1815. } else {
  1816. this.expiryTime = t.valueOf();
  1817. }
  1818. }
  1819. get _totalGB() {
  1820. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1821. }
  1822. set _totalGB(gb) {
  1823. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1824. }
  1825. };
  1826. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1827. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1828. super();
  1829. this.name = name;
  1830. this.alpn = alpn;
  1831. this.path = path;
  1832. this.dest = dest;
  1833. this.xver = xver;
  1834. }
  1835. toJson() {
  1836. let xver = this.xver;
  1837. if (!Number.isInteger(xver)) {
  1838. xver = 0;
  1839. }
  1840. return {
  1841. name: this.name,
  1842. alpn: this.alpn,
  1843. path: this.path,
  1844. dest: this.dest,
  1845. xver: xver,
  1846. }
  1847. }
  1848. static fromJson(json = []) {
  1849. const fallbacks = [];
  1850. for (let fallback of json) {
  1851. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1852. fallback.name,
  1853. fallback.alpn,
  1854. fallback.path,
  1855. fallback.dest,
  1856. fallback.xver,
  1857. ))
  1858. }
  1859. return fallbacks;
  1860. }
  1861. };
  1862. Inbound.TrojanSettings = class extends Inbound.Settings {
  1863. constructor(protocol,
  1864. trojans = [new Inbound.TrojanSettings.Trojan()],
  1865. fallbacks = [],) {
  1866. super(protocol);
  1867. this.trojans = trojans;
  1868. this.fallbacks = fallbacks;
  1869. }
  1870. addFallback() {
  1871. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1872. }
  1873. delFallback(index) {
  1874. this.fallbacks.splice(index, 1);
  1875. }
  1876. static fromJson(json = {}) {
  1877. return new Inbound.TrojanSettings(
  1878. Protocols.TROJAN,
  1879. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1880. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1881. }
  1882. toJson() {
  1883. return {
  1884. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1885. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks)
  1886. };
  1887. }
  1888. };
  1889. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1890. constructor(
  1891. password = RandomUtil.randomSeq(10),
  1892. email = RandomUtil.randomLowerAndNum(8),
  1893. limitIp = 0,
  1894. totalGB = 0,
  1895. expiryTime = 0,
  1896. enable = true,
  1897. tgId = '',
  1898. subId = RandomUtil.randomLowerAndNum(16),
  1899. comment = '',
  1900. reset = 0,
  1901. created_at = undefined,
  1902. updated_at = undefined
  1903. ) {
  1904. super();
  1905. this.password = password;
  1906. this.email = email;
  1907. this.limitIp = limitIp;
  1908. this.totalGB = totalGB;
  1909. this.expiryTime = expiryTime;
  1910. this.enable = enable;
  1911. this.tgId = tgId;
  1912. this.subId = subId;
  1913. this.comment = comment;
  1914. this.reset = reset;
  1915. this.created_at = created_at;
  1916. this.updated_at = updated_at;
  1917. }
  1918. toJson() {
  1919. return {
  1920. password: this.password,
  1921. email: this.email,
  1922. limitIp: this.limitIp,
  1923. totalGB: this.totalGB,
  1924. expiryTime: this.expiryTime,
  1925. enable: this.enable,
  1926. tgId: this.tgId,
  1927. subId: this.subId,
  1928. comment: this.comment,
  1929. reset: this.reset,
  1930. created_at: this.created_at,
  1931. updated_at: this.updated_at,
  1932. };
  1933. }
  1934. static fromJson(json = {}) {
  1935. return new Inbound.TrojanSettings.Trojan(
  1936. json.password,
  1937. json.email,
  1938. json.limitIp,
  1939. json.totalGB,
  1940. json.expiryTime,
  1941. json.enable,
  1942. json.tgId,
  1943. json.subId,
  1944. json.comment,
  1945. json.reset,
  1946. json.created_at,
  1947. json.updated_at,
  1948. );
  1949. }
  1950. get _expiryTime() {
  1951. if (this.expiryTime === 0 || this.expiryTime === "") {
  1952. return null;
  1953. }
  1954. if (this.expiryTime < 0) {
  1955. return this.expiryTime / -86400000;
  1956. }
  1957. return moment(this.expiryTime);
  1958. }
  1959. set _expiryTime(t) {
  1960. if (t == null || t === "") {
  1961. this.expiryTime = 0;
  1962. } else {
  1963. this.expiryTime = t.valueOf();
  1964. }
  1965. }
  1966. get _totalGB() {
  1967. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1968. }
  1969. set _totalGB(gb) {
  1970. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1971. }
  1972. };
  1973. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1974. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1975. super();
  1976. this.name = name;
  1977. this.alpn = alpn;
  1978. this.path = path;
  1979. this.dest = dest;
  1980. this.xver = xver;
  1981. }
  1982. toJson() {
  1983. let xver = this.xver;
  1984. if (!Number.isInteger(xver)) {
  1985. xver = 0;
  1986. }
  1987. return {
  1988. name: this.name,
  1989. alpn: this.alpn,
  1990. path: this.path,
  1991. dest: this.dest,
  1992. xver: xver,
  1993. }
  1994. }
  1995. static fromJson(json = []) {
  1996. const fallbacks = [];
  1997. for (let fallback of json) {
  1998. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1999. fallback.name,
  2000. fallback.alpn,
  2001. fallback.path,
  2002. fallback.dest,
  2003. fallback.xver,
  2004. ))
  2005. }
  2006. return fallbacks;
  2007. }
  2008. };
  2009. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  2010. constructor(protocol,
  2011. method = SSMethods.BLAKE3_AES_256_GCM,
  2012. password = RandomUtil.randomShadowsocksPassword(),
  2013. network = 'tcp,udp',
  2014. shadowsockses = [new Inbound.ShadowsocksSettings.Shadowsocks()],
  2015. ivCheck = false,
  2016. ) {
  2017. super(protocol);
  2018. this.method = method;
  2019. this.password = password;
  2020. this.network = network;
  2021. this.shadowsockses = shadowsockses;
  2022. this.ivCheck = ivCheck;
  2023. }
  2024. static fromJson(json = {}) {
  2025. return new Inbound.ShadowsocksSettings(
  2026. Protocols.SHADOWSOCKS,
  2027. json.method,
  2028. json.password,
  2029. json.network,
  2030. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  2031. json.ivCheck,
  2032. );
  2033. }
  2034. toJson() {
  2035. return {
  2036. method: this.method,
  2037. password: this.password,
  2038. network: this.network,
  2039. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses),
  2040. ivCheck: this.ivCheck,
  2041. };
  2042. }
  2043. };
  2044. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  2045. constructor(
  2046. method = '',
  2047. password = RandomUtil.randomShadowsocksPassword(),
  2048. email = RandomUtil.randomLowerAndNum(8),
  2049. limitIp = 0,
  2050. totalGB = 0,
  2051. expiryTime = 0,
  2052. enable = true,
  2053. tgId = '',
  2054. subId = RandomUtil.randomLowerAndNum(16),
  2055. comment = '',
  2056. reset = 0,
  2057. created_at = undefined,
  2058. updated_at = undefined
  2059. ) {
  2060. super();
  2061. this.method = method;
  2062. this.password = password;
  2063. this.email = email;
  2064. this.limitIp = limitIp;
  2065. this.totalGB = totalGB;
  2066. this.expiryTime = expiryTime;
  2067. this.enable = enable;
  2068. this.tgId = tgId;
  2069. this.subId = subId;
  2070. this.comment = comment;
  2071. this.reset = reset;
  2072. this.created_at = created_at;
  2073. this.updated_at = updated_at;
  2074. }
  2075. toJson() {
  2076. return {
  2077. method: this.method,
  2078. password: this.password,
  2079. email: this.email,
  2080. limitIp: this.limitIp,
  2081. totalGB: this.totalGB,
  2082. expiryTime: this.expiryTime,
  2083. enable: this.enable,
  2084. tgId: this.tgId,
  2085. subId: this.subId,
  2086. comment: this.comment,
  2087. reset: this.reset,
  2088. created_at: this.created_at,
  2089. updated_at: this.updated_at,
  2090. };
  2091. }
  2092. static fromJson(json = {}) {
  2093. return new Inbound.ShadowsocksSettings.Shadowsocks(
  2094. json.method,
  2095. json.password,
  2096. json.email,
  2097. json.limitIp,
  2098. json.totalGB,
  2099. json.expiryTime,
  2100. json.enable,
  2101. json.tgId,
  2102. json.subId,
  2103. json.comment,
  2104. json.reset,
  2105. json.created_at,
  2106. json.updated_at,
  2107. );
  2108. }
  2109. get _expiryTime() {
  2110. if (this.expiryTime === 0 || this.expiryTime === "") {
  2111. return null;
  2112. }
  2113. if (this.expiryTime < 0) {
  2114. return this.expiryTime / -86400000;
  2115. }
  2116. return moment(this.expiryTime);
  2117. }
  2118. set _expiryTime(t) {
  2119. if (t == null || t === "") {
  2120. this.expiryTime = 0;
  2121. } else {
  2122. this.expiryTime = t.valueOf();
  2123. }
  2124. }
  2125. get _totalGB() {
  2126. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  2127. }
  2128. set _totalGB(gb) {
  2129. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  2130. }
  2131. };
  2132. Inbound.DokodemoSettings = class extends Inbound.Settings {
  2133. constructor(
  2134. protocol,
  2135. address,
  2136. port,
  2137. portMap = [],
  2138. network = 'tcp,udp',
  2139. followRedirect = false
  2140. ) {
  2141. super(protocol);
  2142. this.address = address;
  2143. this.port = port;
  2144. this.portMap = portMap;
  2145. this.network = network;
  2146. this.followRedirect = followRedirect;
  2147. }
  2148. static fromJson(json = {}) {
  2149. return new Inbound.DokodemoSettings(
  2150. Protocols.DOKODEMO,
  2151. json.address,
  2152. json.port,
  2153. XrayCommonClass.toHeaders(json.portMap),
  2154. json.network,
  2155. json.followRedirect,
  2156. );
  2157. }
  2158. toJson() {
  2159. return {
  2160. address: this.address,
  2161. port: this.port,
  2162. portMap: XrayCommonClass.toV2Headers(this.portMap, false),
  2163. network: this.network,
  2164. followRedirect: this.followRedirect,
  2165. };
  2166. }
  2167. };
  2168. Inbound.SocksSettings = class extends Inbound.Settings {
  2169. constructor(protocol, auth = 'password', accounts = [new Inbound.SocksSettings.SocksAccount()], udp = false, ip = '127.0.0.1') {
  2170. super(protocol);
  2171. this.auth = auth;
  2172. this.accounts = accounts;
  2173. this.udp = udp;
  2174. this.ip = ip;
  2175. }
  2176. addAccount(account) {
  2177. this.accounts.push(account);
  2178. }
  2179. delAccount(index) {
  2180. this.accounts.splice(index, 1);
  2181. }
  2182. static fromJson(json = {}) {
  2183. let accounts;
  2184. if (json.auth === 'password') {
  2185. accounts = json.accounts.map(
  2186. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  2187. )
  2188. }
  2189. return new Inbound.SocksSettings(
  2190. Protocols.SOCKS,
  2191. json.auth,
  2192. accounts,
  2193. json.udp,
  2194. json.ip,
  2195. );
  2196. }
  2197. toJson() {
  2198. return {
  2199. auth: this.auth,
  2200. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  2201. udp: this.udp,
  2202. ip: this.ip,
  2203. };
  2204. }
  2205. };
  2206. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  2207. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2208. super();
  2209. this.user = user;
  2210. this.pass = pass;
  2211. }
  2212. static fromJson(json = {}) {
  2213. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  2214. }
  2215. };
  2216. Inbound.HttpSettings = class extends Inbound.Settings {
  2217. constructor(
  2218. protocol,
  2219. accounts = [new Inbound.HttpSettings.HttpAccount()],
  2220. allowTransparent = false,
  2221. ) {
  2222. super(protocol);
  2223. this.accounts = accounts;
  2224. this.allowTransparent = allowTransparent;
  2225. }
  2226. addAccount(account) {
  2227. this.accounts.push(account);
  2228. }
  2229. delAccount(index) {
  2230. this.accounts.splice(index, 1);
  2231. }
  2232. static fromJson(json = {}) {
  2233. return new Inbound.HttpSettings(
  2234. Protocols.HTTP,
  2235. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  2236. json.allowTransparent,
  2237. );
  2238. }
  2239. toJson() {
  2240. return {
  2241. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2242. allowTransparent: this.allowTransparent,
  2243. };
  2244. }
  2245. };
  2246. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2247. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2248. super();
  2249. this.user = user;
  2250. this.pass = pass;
  2251. }
  2252. static fromJson(json = {}) {
  2253. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2254. }
  2255. };
  2256. Inbound.WireguardSettings = class extends XrayCommonClass {
  2257. constructor(
  2258. protocol,
  2259. mtu = 1420,
  2260. secretKey = Wireguard.generateKeypair().privateKey,
  2261. peers = [new Inbound.WireguardSettings.Peer()],
  2262. noKernelTun = false
  2263. ) {
  2264. super(protocol);
  2265. this.mtu = mtu;
  2266. this.secretKey = secretKey;
  2267. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  2268. this.peers = peers;
  2269. this.noKernelTun = noKernelTun;
  2270. }
  2271. addPeer() {
  2272. this.peers.push(new Inbound.WireguardSettings.Peer(null, null, '', ['10.0.0.' + (this.peers.length + 2)]));
  2273. }
  2274. delPeer(index) {
  2275. this.peers.splice(index, 1);
  2276. }
  2277. static fromJson(json = {}) {
  2278. return new Inbound.WireguardSettings(
  2279. Protocols.WIREGUARD,
  2280. json.mtu,
  2281. json.secretKey,
  2282. json.peers.map(peer => Inbound.WireguardSettings.Peer.fromJson(peer)),
  2283. json.noKernelTun,
  2284. );
  2285. }
  2286. toJson() {
  2287. return {
  2288. mtu: this.mtu ?? undefined,
  2289. secretKey: this.secretKey,
  2290. peers: Inbound.WireguardSettings.Peer.toJsonArray(this.peers),
  2291. noKernelTun: this.noKernelTun,
  2292. };
  2293. }
  2294. };
  2295. Inbound.WireguardSettings.Peer = class extends XrayCommonClass {
  2296. constructor(privateKey, publicKey, psk = '', allowedIPs = ['10.0.0.2/32'], keepAlive = 0) {
  2297. super();
  2298. this.privateKey = privateKey
  2299. this.publicKey = publicKey;
  2300. if (!this.publicKey) {
  2301. [this.publicKey, this.privateKey] = Object.values(Wireguard.generateKeypair())
  2302. }
  2303. this.psk = psk;
  2304. allowedIPs.forEach((a, index) => {
  2305. if (a.length > 0 && !a.includes('/')) allowedIPs[index] += '/32';
  2306. })
  2307. this.allowedIPs = allowedIPs;
  2308. this.keepAlive = keepAlive;
  2309. }
  2310. static fromJson(json = {}) {
  2311. return new Inbound.WireguardSettings.Peer(
  2312. json.privateKey,
  2313. json.publicKey,
  2314. json.preSharedKey,
  2315. json.allowedIPs,
  2316. json.keepAlive
  2317. );
  2318. }
  2319. toJson() {
  2320. this.allowedIPs.forEach((a, index) => {
  2321. if (a.length > 0 && !a.includes('/')) this.allowedIPs[index] += '/32';
  2322. });
  2323. return {
  2324. privateKey: this.privateKey,
  2325. publicKey: this.publicKey,
  2326. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  2327. allowedIPs: this.allowedIPs,
  2328. keepAlive: this.keepAlive ?? undefined,
  2329. };
  2330. }
  2331. };