inbound.js 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  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 Allocate extends XrayCommonClass {
  960. constructor(
  961. strategy = "always",
  962. refresh = 5,
  963. concurrency = 3,
  964. ) {
  965. super();
  966. this.strategy = strategy;
  967. this.refresh = refresh;
  968. this.concurrency = concurrency;
  969. }
  970. static fromJson(json = {}) {
  971. return new Allocate(
  972. json.strategy,
  973. json.refresh,
  974. json.concurrency,
  975. );
  976. }
  977. }
  978. class Inbound extends XrayCommonClass {
  979. constructor(
  980. port = RandomUtil.randomInteger(10000, 60000),
  981. listen = '',
  982. protocol = Protocols.VLESS,
  983. settings = null,
  984. streamSettings = new StreamSettings(),
  985. tag = '',
  986. sniffing = new Sniffing(),
  987. allocate = new Allocate(),
  988. clientStats = '',
  989. ) {
  990. super();
  991. this.port = port;
  992. this.listen = listen;
  993. this._protocol = protocol;
  994. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  995. this.stream = streamSettings;
  996. this.tag = tag;
  997. this.sniffing = sniffing;
  998. this.allocate = allocate;
  999. this.clientStats = clientStats;
  1000. }
  1001. getClientStats() {
  1002. return this.clientStats;
  1003. }
  1004. get clients() {
  1005. switch (this.protocol) {
  1006. case Protocols.VMESS: return this.settings.vmesses;
  1007. case Protocols.VLESS: return this.settings.vlesses;
  1008. case Protocols.TROJAN: return this.settings.trojans;
  1009. case Protocols.SHADOWSOCKS: return this.isSSMultiUser ? this.settings.shadowsockses : null;
  1010. default: return null;
  1011. }
  1012. }
  1013. get protocol() {
  1014. return this._protocol;
  1015. }
  1016. set protocol(protocol) {
  1017. this._protocol = protocol;
  1018. this.settings = Inbound.Settings.getSettings(protocol);
  1019. if (protocol === Protocols.TROJAN) {
  1020. this.tls = false;
  1021. }
  1022. }
  1023. get network() {
  1024. return this.stream.network;
  1025. }
  1026. set network(network) {
  1027. this.stream.network = network;
  1028. }
  1029. get isTcp() {
  1030. return this.network === "tcp";
  1031. }
  1032. get isWs() {
  1033. return this.network === "ws";
  1034. }
  1035. get isKcp() {
  1036. return this.network === "kcp";
  1037. }
  1038. get isGrpc() {
  1039. return this.network === "grpc";
  1040. }
  1041. get isHttpupgrade() {
  1042. return this.network === "httpupgrade";
  1043. }
  1044. get isXHTTP() {
  1045. return this.network === "xhttp";
  1046. }
  1047. // Shadowsocks
  1048. get method() {
  1049. switch (this.protocol) {
  1050. case Protocols.SHADOWSOCKS:
  1051. return this.settings.method;
  1052. default:
  1053. return "";
  1054. }
  1055. }
  1056. get isSSMultiUser() {
  1057. return this.method != SSMethods.BLAKE3_CHACHA20_POLY1305;
  1058. }
  1059. get isSS2022() {
  1060. return this.method.substring(0, 4) === "2022";
  1061. }
  1062. get serverName() {
  1063. if (this.stream.isTls) return this.stream.tls.sni;
  1064. if (this.stream.isReality) return this.stream.reality.serverNames;
  1065. return "";
  1066. }
  1067. getHeader(obj, name) {
  1068. for (const header of obj.headers) {
  1069. if (header.name.toLowerCase() === name.toLowerCase()) {
  1070. return header.value;
  1071. }
  1072. }
  1073. return "";
  1074. }
  1075. get host() {
  1076. if (this.isTcp) {
  1077. return this.getHeader(this.stream.tcp.request, 'host');
  1078. } else if (this.isWs) {
  1079. return this.stream.ws.host?.length > 0 ? this.stream.ws.host : this.getHeader(this.stream.ws, 'host');
  1080. } else if (this.isHttpupgrade) {
  1081. return this.stream.httpupgrade.host?.length > 0 ? this.stream.httpupgrade.host : this.getHeader(this.stream.httpupgrade, 'host');
  1082. } else if (this.isXHTTP) {
  1083. return this.stream.xhttp.host?.length > 0 ? this.stream.xhttp.host : this.getHeader(this.stream.xhttp, 'host');
  1084. }
  1085. return null;
  1086. }
  1087. get path() {
  1088. if (this.isTcp) {
  1089. return this.stream.tcp.request.path[0];
  1090. } else if (this.isWs) {
  1091. return this.stream.ws.path;
  1092. } else if (this.isHttpupgrade) {
  1093. return this.stream.httpupgrade.path;
  1094. } else if (this.isXHTTP) {
  1095. return this.stream.xhttp.path;
  1096. }
  1097. return null;
  1098. }
  1099. get kcpType() {
  1100. return this.stream.kcp.type;
  1101. }
  1102. get kcpSeed() {
  1103. return this.stream.kcp.seed;
  1104. }
  1105. get serviceName() {
  1106. return this.stream.grpc.serviceName;
  1107. }
  1108. isExpiry(index) {
  1109. let exp = this.clients[index].expiryTime;
  1110. return exp > 0 ? exp < new Date().getTime() : false;
  1111. }
  1112. canEnableTls() {
  1113. if (![Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol)) return false;
  1114. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.network);
  1115. }
  1116. //this is used for xtls-rprx-vision
  1117. canEnableTlsFlow() {
  1118. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1119. return this.protocol === Protocols.VLESS;
  1120. }
  1121. return false;
  1122. }
  1123. canEnableReality() {
  1124. if (![Protocols.VLESS, Protocols.TROJAN].includes(this.protocol)) return false;
  1125. return ["tcp", "http", "grpc", "xhttp"].includes(this.network);
  1126. }
  1127. canEnableStream() {
  1128. return [Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol);
  1129. }
  1130. reset() {
  1131. this.port = RandomUtil.randomInteger(10000, 60000);
  1132. this.listen = '';
  1133. this.protocol = Protocols.VMESS;
  1134. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1135. this.stream = new StreamSettings();
  1136. this.tag = '';
  1137. this.sniffing = new Sniffing();
  1138. this.allocate = new Allocate();
  1139. }
  1140. genVmessLink(address = '', port = this.port, forceTls, remark = '', clientId, security) {
  1141. if (this.protocol !== Protocols.VMESS) {
  1142. return '';
  1143. }
  1144. const tls = forceTls == 'same' ? this.stream.security : forceTls;
  1145. let obj = {
  1146. v: '2',
  1147. ps: remark,
  1148. add: address,
  1149. port: port,
  1150. id: clientId,
  1151. scy: security,
  1152. net: this.stream.network,
  1153. tls: tls,
  1154. };
  1155. const network = this.stream.network;
  1156. if (network === 'tcp') {
  1157. const tcp = this.stream.tcp;
  1158. obj.type = tcp.type;
  1159. if (tcp.type === 'http') {
  1160. const request = tcp.request;
  1161. obj.path = request.path.join(',');
  1162. const host = this.getHeader(request, 'host');
  1163. if (host) obj.host = host;
  1164. }
  1165. } else if (network === 'kcp') {
  1166. const kcp = this.stream.kcp;
  1167. obj.type = kcp.type;
  1168. obj.path = kcp.seed;
  1169. } else if (network === 'ws') {
  1170. const ws = this.stream.ws;
  1171. obj.path = ws.path;
  1172. obj.host = ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host');
  1173. } else if (network === 'grpc') {
  1174. obj.path = this.stream.grpc.serviceName;
  1175. obj.authority = this.stream.grpc.authority;
  1176. if (this.stream.grpc.multiMode) {
  1177. obj.type = 'multi'
  1178. }
  1179. } else if (network === 'httpupgrade') {
  1180. const httpupgrade = this.stream.httpupgrade;
  1181. obj.path = httpupgrade.path;
  1182. obj.host = httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host');
  1183. } else if (network === 'xhttp') {
  1184. const xhttp = this.stream.xhttp;
  1185. obj.path = xhttp.path;
  1186. obj.host = xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host');
  1187. obj.type = xhttp.mode;
  1188. }
  1189. if (tls === 'tls') {
  1190. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1191. obj.sni = this.stream.tls.sni;
  1192. }
  1193. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)) {
  1194. obj.fp = this.stream.tls.settings.fingerprint;
  1195. }
  1196. if (this.stream.tls.alpn.length > 0) {
  1197. obj.alpn = this.stream.tls.alpn.join(',');
  1198. }
  1199. if (this.stream.tls.settings.allowInsecure) {
  1200. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1201. }
  1202. }
  1203. return 'vmess://' + Base64.encode(JSON.stringify(obj, null, 2));
  1204. }
  1205. genVLESSLink(address = '', port = this.port, forceTls, remark = '', clientId, flow) {
  1206. const uuid = clientId;
  1207. const type = this.stream.network;
  1208. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1209. const params = new Map();
  1210. params.set("type", this.stream.network);
  1211. switch (type) {
  1212. case "tcp":
  1213. const tcp = this.stream.tcp;
  1214. if (tcp.type === 'http') {
  1215. const request = tcp.request;
  1216. params.set("path", request.path.join(','));
  1217. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1218. if (index >= 0) {
  1219. const host = request.headers[index].value;
  1220. params.set("host", host);
  1221. }
  1222. params.set("headerType", 'http');
  1223. }
  1224. break;
  1225. case "kcp":
  1226. const kcp = this.stream.kcp;
  1227. params.set("headerType", kcp.type);
  1228. params.set("seed", kcp.seed);
  1229. break;
  1230. case "ws":
  1231. const ws = this.stream.ws;
  1232. params.set("path", ws.path);
  1233. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1234. break;
  1235. case "grpc":
  1236. const grpc = this.stream.grpc;
  1237. params.set("serviceName", grpc.serviceName);
  1238. params.set("authority", grpc.authority);
  1239. if (grpc.multiMode) {
  1240. params.set("mode", "multi");
  1241. }
  1242. break;
  1243. case "httpupgrade":
  1244. const httpupgrade = this.stream.httpupgrade;
  1245. params.set("path", httpupgrade.path);
  1246. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1247. break;
  1248. case "xhttp":
  1249. const xhttp = this.stream.xhttp;
  1250. params.set("path", xhttp.path);
  1251. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1252. params.set("mode", xhttp.mode);
  1253. break;
  1254. }
  1255. if (security === 'tls') {
  1256. params.set("security", "tls");
  1257. if (this.stream.isTls) {
  1258. params.set("fp", this.stream.tls.settings.fingerprint);
  1259. params.set("alpn", this.stream.tls.alpn);
  1260. if (this.stream.tls.settings.allowInsecure) {
  1261. params.set("allowInsecure", "1");
  1262. }
  1263. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1264. params.set("sni", this.stream.tls.sni);
  1265. }
  1266. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1267. params.set("ech", this.stream.tls.settings.echConfigList);
  1268. }
  1269. if (type == "tcp" && !ObjectUtil.isEmpty(flow)) {
  1270. params.set("flow", flow);
  1271. }
  1272. }
  1273. }
  1274. else if (security === 'reality') {
  1275. params.set("security", "reality");
  1276. params.set("pbk", this.stream.reality.settings.publicKey);
  1277. params.set("fp", this.stream.reality.settings.fingerprint);
  1278. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1279. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1280. }
  1281. if (this.stream.reality.shortIds.length > 0) {
  1282. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1283. }
  1284. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1285. params.set("spx", this.stream.reality.settings.spiderX);
  1286. }
  1287. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1288. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1289. }
  1290. if (type == 'tcp' && !ObjectUtil.isEmpty(flow)) {
  1291. params.set("flow", flow);
  1292. }
  1293. }
  1294. else {
  1295. params.set("security", "none");
  1296. }
  1297. const link = `vless://${uuid}@${address}:${port}`;
  1298. const url = new URL(link);
  1299. for (const [key, value] of params) {
  1300. url.searchParams.set(key, value)
  1301. }
  1302. url.hash = encodeURIComponent(remark);
  1303. return url.toString();
  1304. }
  1305. genSSLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1306. let settings = this.settings;
  1307. const type = this.stream.network;
  1308. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1309. const params = new Map();
  1310. params.set("type", this.stream.network);
  1311. switch (type) {
  1312. case "tcp":
  1313. const tcp = this.stream.tcp;
  1314. if (tcp.type === 'http') {
  1315. const request = tcp.request;
  1316. params.set("path", request.path.join(','));
  1317. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1318. if (index >= 0) {
  1319. const host = request.headers[index].value;
  1320. params.set("host", host);
  1321. }
  1322. params.set("headerType", 'http');
  1323. }
  1324. break;
  1325. case "kcp":
  1326. const kcp = this.stream.kcp;
  1327. params.set("headerType", kcp.type);
  1328. params.set("seed", kcp.seed);
  1329. break;
  1330. case "ws":
  1331. const ws = this.stream.ws;
  1332. params.set("path", ws.path);
  1333. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1334. break;
  1335. case "grpc":
  1336. const grpc = this.stream.grpc;
  1337. params.set("serviceName", grpc.serviceName);
  1338. params.set("authority", grpc.authority);
  1339. if (grpc.multiMode) {
  1340. params.set("mode", "multi");
  1341. }
  1342. break;
  1343. case "httpupgrade":
  1344. const httpupgrade = this.stream.httpupgrade;
  1345. params.set("path", httpupgrade.path);
  1346. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1347. break;
  1348. case "xhttp":
  1349. const xhttp = this.stream.xhttp;
  1350. params.set("path", xhttp.path);
  1351. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1352. params.set("mode", xhttp.mode);
  1353. break;
  1354. }
  1355. if (security === 'tls') {
  1356. params.set("security", "tls");
  1357. if (this.stream.isTls) {
  1358. params.set("fp", this.stream.tls.settings.fingerprint);
  1359. params.set("alpn", this.stream.tls.alpn);
  1360. if (this.stream.tls.settings.allowInsecure) {
  1361. params.set("allowInsecure", "1");
  1362. }
  1363. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1364. params.set("ech", this.stream.tls.settings.echConfigList);
  1365. }
  1366. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1367. params.set("sni", this.stream.tls.sni);
  1368. }
  1369. }
  1370. }
  1371. let password = new Array();
  1372. if (this.isSS2022) password.push(settings.password);
  1373. if (this.isSSMultiUser) password.push(clientPassword);
  1374. let link = `ss://${Base64.encode(`${settings.method}:${password.join(':')}`, true)}@${address}:${port}`;
  1375. const url = new URL(link);
  1376. for (const [key, value] of params) {
  1377. url.searchParams.set(key, value)
  1378. }
  1379. url.hash = encodeURIComponent(remark);
  1380. return url.toString();
  1381. }
  1382. genTrojanLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1383. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1384. const type = this.stream.network;
  1385. const params = new Map();
  1386. params.set("type", this.stream.network);
  1387. switch (type) {
  1388. case "tcp":
  1389. const tcp = this.stream.tcp;
  1390. if (tcp.type === 'http') {
  1391. const request = tcp.request;
  1392. params.set("path", request.path.join(','));
  1393. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1394. if (index >= 0) {
  1395. const host = request.headers[index].value;
  1396. params.set("host", host);
  1397. }
  1398. params.set("headerType", 'http');
  1399. }
  1400. break;
  1401. case "kcp":
  1402. const kcp = this.stream.kcp;
  1403. params.set("headerType", kcp.type);
  1404. params.set("seed", kcp.seed);
  1405. break;
  1406. case "ws":
  1407. const ws = this.stream.ws;
  1408. params.set("path", ws.path);
  1409. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1410. break;
  1411. case "grpc":
  1412. const grpc = this.stream.grpc;
  1413. params.set("serviceName", grpc.serviceName);
  1414. params.set("authority", grpc.authority);
  1415. if (grpc.multiMode) {
  1416. params.set("mode", "multi");
  1417. }
  1418. break;
  1419. case "httpupgrade":
  1420. const httpupgrade = this.stream.httpupgrade;
  1421. params.set("path", httpupgrade.path);
  1422. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1423. break;
  1424. case "xhttp":
  1425. const xhttp = this.stream.xhttp;
  1426. params.set("path", xhttp.path);
  1427. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1428. params.set("mode", xhttp.mode);
  1429. break;
  1430. }
  1431. if (security === 'tls') {
  1432. params.set("security", "tls");
  1433. if (this.stream.isTls) {
  1434. params.set("fp", this.stream.tls.settings.fingerprint);
  1435. params.set("alpn", this.stream.tls.alpn);
  1436. if (this.stream.tls.settings.allowInsecure) {
  1437. params.set("allowInsecure", "1");
  1438. }
  1439. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1440. params.set("ech", this.stream.tls.settings.echConfigList);
  1441. }
  1442. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1443. params.set("sni", this.stream.tls.sni);
  1444. }
  1445. }
  1446. }
  1447. else if (security === 'reality') {
  1448. params.set("security", "reality");
  1449. params.set("pbk", this.stream.reality.settings.publicKey);
  1450. params.set("fp", this.stream.reality.settings.fingerprint);
  1451. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1452. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1453. }
  1454. if (this.stream.reality.shortIds.length > 0) {
  1455. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1456. }
  1457. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1458. params.set("spx", this.stream.reality.settings.spiderX);
  1459. }
  1460. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1461. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1462. }
  1463. }
  1464. else {
  1465. params.set("security", "none");
  1466. }
  1467. const link = `trojan://${clientPassword}@${address}:${port}`;
  1468. const url = new URL(link);
  1469. for (const [key, value] of params) {
  1470. url.searchParams.set(key, value)
  1471. }
  1472. url.hash = encodeURIComponent(remark);
  1473. return url.toString();
  1474. }
  1475. getWireguardLink(address, port, remark, peerId) {
  1476. let txt = `[Interface]\n`
  1477. txt += `PrivateKey = ${this.settings.peers[peerId].privateKey}\n`
  1478. txt += `Address = ${this.settings.peers[peerId].allowedIPs[0]}\n`
  1479. txt += `DNS = 1.1.1.1, 1.0.0.1\n`
  1480. if (this.settings.mtu) {
  1481. txt += `MTU = ${this.settings.mtu}\n`
  1482. }
  1483. txt += `\n# ${remark}\n`
  1484. txt += `[Peer]\n`
  1485. txt += `PublicKey = ${this.settings.pubKey}\n`
  1486. txt += `AllowedIPs = 0.0.0.0/0, ::/0\n`
  1487. txt += `Endpoint = ${address}:${port}`
  1488. if (this.settings.peers[peerId].psk) {
  1489. txt += `\nPresharedKey = ${this.settings.peers[peerId].psk}`
  1490. }
  1491. if (this.settings.peers[peerId].keepAlive) {
  1492. txt += `\nPersistentKeepalive = ${this.settings.peers[peerId].keepAlive}\n`
  1493. }
  1494. return txt;
  1495. }
  1496. genLink(address = '', port = this.port, forceTls = 'same', remark = '', client) {
  1497. switch (this.protocol) {
  1498. case Protocols.VMESS:
  1499. return this.genVmessLink(address, port, forceTls, remark, client.id, client.security);
  1500. case Protocols.VLESS:
  1501. return this.genVLESSLink(address, port, forceTls, remark, client.id, client.flow);
  1502. case Protocols.SHADOWSOCKS:
  1503. return this.genSSLink(address, port, forceTls, remark, this.isSSMultiUser ? client.password : '');
  1504. case Protocols.TROJAN:
  1505. return this.genTrojanLink(address, port, forceTls, remark, client.password);
  1506. default: return '';
  1507. }
  1508. }
  1509. genAllLinks(remark = '', remarkModel = '-ieo', client) {
  1510. let result = [];
  1511. let email = client ? client.email : '';
  1512. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1513. let port = this.port;
  1514. const separationChar = remarkModel.charAt(0);
  1515. const orderChars = remarkModel.slice(1);
  1516. let orders = {
  1517. 'i': remark,
  1518. 'e': email,
  1519. 'o': '',
  1520. };
  1521. if (ObjectUtil.isArrEmpty(this.stream.externalProxy)) {
  1522. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1523. result.push({
  1524. remark: r,
  1525. link: this.genLink(addr, port, 'same', r, client)
  1526. });
  1527. } else {
  1528. this.stream.externalProxy.forEach((ep) => {
  1529. orders['o'] = ep.remark;
  1530. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1531. result.push({
  1532. remark: r,
  1533. link: this.genLink(ep.dest, ep.port, ep.forceTls, r, client)
  1534. });
  1535. });
  1536. }
  1537. return result;
  1538. }
  1539. genInboundLinks(remark = '', remarkModel = '-ieo') {
  1540. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1541. if (this.clients) {
  1542. let links = [];
  1543. this.clients.forEach((client) => {
  1544. this.genAllLinks(remark, remarkModel, client).forEach(l => {
  1545. links.push(l.link);
  1546. })
  1547. });
  1548. return links.join('\r\n');
  1549. } else {
  1550. if (this.protocol == Protocols.SHADOWSOCKS && !this.isSSMultiUser) return this.genSSLink(addr, this.port, 'same', remark);
  1551. if (this.protocol == Protocols.WIREGUARD) {
  1552. let links = [];
  1553. this.settings.peers.forEach((p, index) => {
  1554. links.push(this.getWireguardLink(addr, this.port, remark + remarkModel.charAt(0) + (index + 1), index));
  1555. });
  1556. return links.join('\r\n');
  1557. }
  1558. return '';
  1559. }
  1560. }
  1561. static fromJson(json = {}) {
  1562. return new Inbound(
  1563. json.port,
  1564. json.listen,
  1565. json.protocol,
  1566. Inbound.Settings.fromJson(json.protocol, json.settings),
  1567. StreamSettings.fromJson(json.streamSettings),
  1568. json.tag,
  1569. Sniffing.fromJson(json.sniffing),
  1570. Allocate.fromJson(json.allocate),
  1571. json.clientStats
  1572. )
  1573. }
  1574. toJson() {
  1575. let streamSettings;
  1576. if (this.canEnableStream() || this.stream?.sockopt) {
  1577. streamSettings = this.stream.toJson();
  1578. }
  1579. return {
  1580. port: this.port,
  1581. listen: this.listen,
  1582. protocol: this.protocol,
  1583. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1584. streamSettings: streamSettings,
  1585. tag: this.tag,
  1586. sniffing: this.sniffing.toJson(),
  1587. allocate: this.allocate.toJson(),
  1588. clientStats: this.clientStats
  1589. };
  1590. }
  1591. }
  1592. Inbound.Settings = class extends XrayCommonClass {
  1593. constructor(protocol) {
  1594. super();
  1595. this.protocol = protocol;
  1596. }
  1597. static getSettings(protocol) {
  1598. switch (protocol) {
  1599. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1600. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1601. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1602. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1603. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1604. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1605. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1606. case Protocols.WIREGUARD: return new Inbound.WireguardSettings(protocol);
  1607. default: return null;
  1608. }
  1609. }
  1610. static fromJson(protocol, json) {
  1611. switch (protocol) {
  1612. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1613. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1614. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1615. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1616. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1617. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1618. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1619. case Protocols.WIREGUARD: return Inbound.WireguardSettings.fromJson(json);
  1620. default: return null;
  1621. }
  1622. }
  1623. toJson() {
  1624. return {};
  1625. }
  1626. };
  1627. Inbound.VmessSettings = class extends Inbound.Settings {
  1628. constructor(protocol,
  1629. vmesses = [new Inbound.VmessSettings.VMESS()]) {
  1630. super(protocol);
  1631. this.vmesses = vmesses;
  1632. }
  1633. indexOfVmessById(id) {
  1634. return this.vmesses.findIndex(VMESS => VMESS.id === id);
  1635. }
  1636. addVmess(VMESS) {
  1637. if (this.indexOfVmessById(VMESS.id) >= 0) {
  1638. return false;
  1639. }
  1640. this.vmesses.push(VMESS);
  1641. }
  1642. delVmess(VMESS) {
  1643. const i = this.indexOfVmessById(VMESS.id);
  1644. if (i >= 0) {
  1645. this.vmesses.splice(i, 1);
  1646. }
  1647. }
  1648. static fromJson(json = {}) {
  1649. return new Inbound.VmessSettings(
  1650. Protocols.VMESS,
  1651. json.clients.map(client => Inbound.VmessSettings.VMESS.fromJson(client)),
  1652. );
  1653. }
  1654. toJson() {
  1655. return {
  1656. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1657. };
  1658. }
  1659. };
  1660. Inbound.VmessSettings.VMESS = class extends XrayCommonClass {
  1661. constructor(
  1662. id = RandomUtil.randomUUID(),
  1663. security = USERS_SECURITY.AUTO,
  1664. email = RandomUtil.randomLowerAndNum(8),
  1665. limitIp = 0,
  1666. totalGB = 0,
  1667. expiryTime = 0,
  1668. enable = true,
  1669. tgId = '',
  1670. subId = RandomUtil.randomLowerAndNum(16),
  1671. comment = '',
  1672. reset = 0,
  1673. created_at = undefined,
  1674. updated_at = undefined
  1675. ) {
  1676. super();
  1677. this.id = id;
  1678. this.security = security;
  1679. this.email = email;
  1680. this.limitIp = limitIp;
  1681. this.totalGB = totalGB;
  1682. this.expiryTime = expiryTime;
  1683. this.enable = enable;
  1684. this.tgId = tgId;
  1685. this.subId = subId;
  1686. this.comment = comment;
  1687. this.reset = reset;
  1688. this.created_at = created_at;
  1689. this.updated_at = updated_at;
  1690. }
  1691. static fromJson(json = {}) {
  1692. return new Inbound.VmessSettings.VMESS(
  1693. json.id,
  1694. json.security,
  1695. json.email,
  1696. json.limitIp,
  1697. json.totalGB,
  1698. json.expiryTime,
  1699. json.enable,
  1700. json.tgId,
  1701. json.subId,
  1702. json.comment,
  1703. json.reset,
  1704. json.created_at,
  1705. json.updated_at,
  1706. );
  1707. }
  1708. get _expiryTime() {
  1709. if (this.expiryTime === 0 || this.expiryTime === "") {
  1710. return null;
  1711. }
  1712. if (this.expiryTime < 0) {
  1713. return this.expiryTime / -86400000;
  1714. }
  1715. return moment(this.expiryTime);
  1716. }
  1717. set _expiryTime(t) {
  1718. if (t == null || t === "") {
  1719. this.expiryTime = 0;
  1720. } else {
  1721. this.expiryTime = t.valueOf();
  1722. }
  1723. }
  1724. get _totalGB() {
  1725. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1726. }
  1727. set _totalGB(gb) {
  1728. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1729. }
  1730. };
  1731. Inbound.VLESSSettings = class extends Inbound.Settings {
  1732. constructor(
  1733. protocol,
  1734. vlesses = [new Inbound.VLESSSettings.VLESS()],
  1735. decryption = 'none',
  1736. fallbacks = []
  1737. ) {
  1738. super(protocol);
  1739. this.vlesses = vlesses;
  1740. this.decryption = decryption;
  1741. this.fallbacks = fallbacks;
  1742. }
  1743. addFallback() {
  1744. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1745. }
  1746. delFallback(index) {
  1747. this.fallbacks.splice(index, 1);
  1748. }
  1749. // decryption should be set to static value
  1750. static fromJson(json = {}) {
  1751. return new Inbound.VLESSSettings(
  1752. Protocols.VLESS,
  1753. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1754. json.decryption || 'none',
  1755. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),);
  1756. }
  1757. toJson() {
  1758. return {
  1759. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1760. decryption: this.decryption,
  1761. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1762. };
  1763. }
  1764. };
  1765. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1766. constructor(
  1767. id = RandomUtil.randomUUID(),
  1768. flow = '',
  1769. email = RandomUtil.randomLowerAndNum(8),
  1770. limitIp = 0,
  1771. totalGB = 0,
  1772. expiryTime = 0,
  1773. enable = true,
  1774. tgId = '',
  1775. subId = RandomUtil.randomLowerAndNum(16),
  1776. comment = '',
  1777. reset = 0,
  1778. created_at = undefined,
  1779. updated_at = undefined
  1780. ) {
  1781. super();
  1782. this.id = id;
  1783. this.flow = flow;
  1784. this.email = email;
  1785. this.limitIp = limitIp;
  1786. this.totalGB = totalGB;
  1787. this.expiryTime = expiryTime;
  1788. this.enable = enable;
  1789. this.tgId = tgId;
  1790. this.subId = subId;
  1791. this.comment = comment;
  1792. this.reset = reset;
  1793. this.created_at = created_at;
  1794. this.updated_at = updated_at;
  1795. }
  1796. static fromJson(json = {}) {
  1797. return new Inbound.VLESSSettings.VLESS(
  1798. json.id,
  1799. json.flow,
  1800. json.email,
  1801. json.limitIp,
  1802. json.totalGB,
  1803. json.expiryTime,
  1804. json.enable,
  1805. json.tgId,
  1806. json.subId,
  1807. json.comment,
  1808. json.reset,
  1809. json.created_at,
  1810. json.updated_at,
  1811. );
  1812. }
  1813. get _expiryTime() {
  1814. if (this.expiryTime === 0 || this.expiryTime === "") {
  1815. return null;
  1816. }
  1817. if (this.expiryTime < 0) {
  1818. return this.expiryTime / -86400000;
  1819. }
  1820. return moment(this.expiryTime);
  1821. }
  1822. set _expiryTime(t) {
  1823. if (t == null || t === "") {
  1824. this.expiryTime = 0;
  1825. } else {
  1826. this.expiryTime = t.valueOf();
  1827. }
  1828. }
  1829. get _totalGB() {
  1830. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1831. }
  1832. set _totalGB(gb) {
  1833. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1834. }
  1835. };
  1836. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1837. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1838. super();
  1839. this.name = name;
  1840. this.alpn = alpn;
  1841. this.path = path;
  1842. this.dest = dest;
  1843. this.xver = xver;
  1844. }
  1845. toJson() {
  1846. let xver = this.xver;
  1847. if (!Number.isInteger(xver)) {
  1848. xver = 0;
  1849. }
  1850. return {
  1851. name: this.name,
  1852. alpn: this.alpn,
  1853. path: this.path,
  1854. dest: this.dest,
  1855. xver: xver,
  1856. }
  1857. }
  1858. static fromJson(json = []) {
  1859. const fallbacks = [];
  1860. for (let fallback of json) {
  1861. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1862. fallback.name,
  1863. fallback.alpn,
  1864. fallback.path,
  1865. fallback.dest,
  1866. fallback.xver,
  1867. ))
  1868. }
  1869. return fallbacks;
  1870. }
  1871. };
  1872. Inbound.TrojanSettings = class extends Inbound.Settings {
  1873. constructor(protocol,
  1874. trojans = [new Inbound.TrojanSettings.Trojan()],
  1875. fallbacks = [],) {
  1876. super(protocol);
  1877. this.trojans = trojans;
  1878. this.fallbacks = fallbacks;
  1879. }
  1880. addFallback() {
  1881. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1882. }
  1883. delFallback(index) {
  1884. this.fallbacks.splice(index, 1);
  1885. }
  1886. static fromJson(json = {}) {
  1887. return new Inbound.TrojanSettings(
  1888. Protocols.TROJAN,
  1889. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1890. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1891. }
  1892. toJson() {
  1893. return {
  1894. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1895. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks)
  1896. };
  1897. }
  1898. };
  1899. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1900. constructor(
  1901. password = RandomUtil.randomSeq(10),
  1902. email = RandomUtil.randomLowerAndNum(8),
  1903. limitIp = 0,
  1904. totalGB = 0,
  1905. expiryTime = 0,
  1906. enable = true,
  1907. tgId = '',
  1908. subId = RandomUtil.randomLowerAndNum(16),
  1909. comment = '',
  1910. reset = 0,
  1911. created_at = undefined,
  1912. updated_at = undefined
  1913. ) {
  1914. super();
  1915. this.password = password;
  1916. this.email = email;
  1917. this.limitIp = limitIp;
  1918. this.totalGB = totalGB;
  1919. this.expiryTime = expiryTime;
  1920. this.enable = enable;
  1921. this.tgId = tgId;
  1922. this.subId = subId;
  1923. this.comment = comment;
  1924. this.reset = reset;
  1925. this.created_at = created_at;
  1926. this.updated_at = updated_at;
  1927. }
  1928. toJson() {
  1929. return {
  1930. password: this.password,
  1931. email: this.email,
  1932. limitIp: this.limitIp,
  1933. totalGB: this.totalGB,
  1934. expiryTime: this.expiryTime,
  1935. enable: this.enable,
  1936. tgId: this.tgId,
  1937. subId: this.subId,
  1938. comment: this.comment,
  1939. reset: this.reset,
  1940. created_at: this.created_at,
  1941. updated_at: this.updated_at,
  1942. };
  1943. }
  1944. static fromJson(json = {}) {
  1945. return new Inbound.TrojanSettings.Trojan(
  1946. json.password,
  1947. json.email,
  1948. json.limitIp,
  1949. json.totalGB,
  1950. json.expiryTime,
  1951. json.enable,
  1952. json.tgId,
  1953. json.subId,
  1954. json.comment,
  1955. json.reset,
  1956. json.created_at,
  1957. json.updated_at,
  1958. );
  1959. }
  1960. get _expiryTime() {
  1961. if (this.expiryTime === 0 || this.expiryTime === "") {
  1962. return null;
  1963. }
  1964. if (this.expiryTime < 0) {
  1965. return this.expiryTime / -86400000;
  1966. }
  1967. return moment(this.expiryTime);
  1968. }
  1969. set _expiryTime(t) {
  1970. if (t == null || t === "") {
  1971. this.expiryTime = 0;
  1972. } else {
  1973. this.expiryTime = t.valueOf();
  1974. }
  1975. }
  1976. get _totalGB() {
  1977. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1978. }
  1979. set _totalGB(gb) {
  1980. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1981. }
  1982. };
  1983. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1984. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1985. super();
  1986. this.name = name;
  1987. this.alpn = alpn;
  1988. this.path = path;
  1989. this.dest = dest;
  1990. this.xver = xver;
  1991. }
  1992. toJson() {
  1993. let xver = this.xver;
  1994. if (!Number.isInteger(xver)) {
  1995. xver = 0;
  1996. }
  1997. return {
  1998. name: this.name,
  1999. alpn: this.alpn,
  2000. path: this.path,
  2001. dest: this.dest,
  2002. xver: xver,
  2003. }
  2004. }
  2005. static fromJson(json = []) {
  2006. const fallbacks = [];
  2007. for (let fallback of json) {
  2008. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  2009. fallback.name,
  2010. fallback.alpn,
  2011. fallback.path,
  2012. fallback.dest,
  2013. fallback.xver,
  2014. ))
  2015. }
  2016. return fallbacks;
  2017. }
  2018. };
  2019. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  2020. constructor(protocol,
  2021. method = SSMethods.BLAKE3_AES_256_GCM,
  2022. password = RandomUtil.randomShadowsocksPassword(),
  2023. network = 'tcp,udp',
  2024. shadowsockses = [new Inbound.ShadowsocksSettings.Shadowsocks()],
  2025. ivCheck = false,
  2026. ) {
  2027. super(protocol);
  2028. this.method = method;
  2029. this.password = password;
  2030. this.network = network;
  2031. this.shadowsockses = shadowsockses;
  2032. this.ivCheck = ivCheck;
  2033. }
  2034. static fromJson(json = {}) {
  2035. return new Inbound.ShadowsocksSettings(
  2036. Protocols.SHADOWSOCKS,
  2037. json.method,
  2038. json.password,
  2039. json.network,
  2040. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  2041. json.ivCheck,
  2042. );
  2043. }
  2044. toJson() {
  2045. return {
  2046. method: this.method,
  2047. password: this.password,
  2048. network: this.network,
  2049. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses),
  2050. ivCheck: this.ivCheck,
  2051. };
  2052. }
  2053. };
  2054. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  2055. constructor(
  2056. method = '',
  2057. password = RandomUtil.randomShadowsocksPassword(),
  2058. email = RandomUtil.randomLowerAndNum(8),
  2059. limitIp = 0,
  2060. totalGB = 0,
  2061. expiryTime = 0,
  2062. enable = true,
  2063. tgId = '',
  2064. subId = RandomUtil.randomLowerAndNum(16),
  2065. comment = '',
  2066. reset = 0,
  2067. created_at = undefined,
  2068. updated_at = undefined
  2069. ) {
  2070. super();
  2071. this.method = method;
  2072. this.password = password;
  2073. this.email = email;
  2074. this.limitIp = limitIp;
  2075. this.totalGB = totalGB;
  2076. this.expiryTime = expiryTime;
  2077. this.enable = enable;
  2078. this.tgId = tgId;
  2079. this.subId = subId;
  2080. this.comment = comment;
  2081. this.reset = reset;
  2082. this.created_at = created_at;
  2083. this.updated_at = updated_at;
  2084. }
  2085. toJson() {
  2086. return {
  2087. method: this.method,
  2088. password: this.password,
  2089. email: this.email,
  2090. limitIp: this.limitIp,
  2091. totalGB: this.totalGB,
  2092. expiryTime: this.expiryTime,
  2093. enable: this.enable,
  2094. tgId: this.tgId,
  2095. subId: this.subId,
  2096. comment: this.comment,
  2097. reset: this.reset,
  2098. created_at: this.created_at,
  2099. updated_at: this.updated_at,
  2100. };
  2101. }
  2102. static fromJson(json = {}) {
  2103. return new Inbound.ShadowsocksSettings.Shadowsocks(
  2104. json.method,
  2105. json.password,
  2106. json.email,
  2107. json.limitIp,
  2108. json.totalGB,
  2109. json.expiryTime,
  2110. json.enable,
  2111. json.tgId,
  2112. json.subId,
  2113. json.comment,
  2114. json.reset,
  2115. json.created_at,
  2116. json.updated_at,
  2117. );
  2118. }
  2119. get _expiryTime() {
  2120. if (this.expiryTime === 0 || this.expiryTime === "") {
  2121. return null;
  2122. }
  2123. if (this.expiryTime < 0) {
  2124. return this.expiryTime / -86400000;
  2125. }
  2126. return moment(this.expiryTime);
  2127. }
  2128. set _expiryTime(t) {
  2129. if (t == null || t === "") {
  2130. this.expiryTime = 0;
  2131. } else {
  2132. this.expiryTime = t.valueOf();
  2133. }
  2134. }
  2135. get _totalGB() {
  2136. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  2137. }
  2138. set _totalGB(gb) {
  2139. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  2140. }
  2141. };
  2142. Inbound.DokodemoSettings = class extends Inbound.Settings {
  2143. constructor(
  2144. protocol,
  2145. address,
  2146. port,
  2147. portMap = [],
  2148. network = 'tcp,udp',
  2149. followRedirect = false
  2150. ) {
  2151. super(protocol);
  2152. this.address = address;
  2153. this.port = port;
  2154. this.portMap = portMap;
  2155. this.network = network;
  2156. this.followRedirect = followRedirect;
  2157. }
  2158. static fromJson(json = {}) {
  2159. return new Inbound.DokodemoSettings(
  2160. Protocols.DOKODEMO,
  2161. json.address,
  2162. json.port,
  2163. XrayCommonClass.toHeaders(json.portMap),
  2164. json.network,
  2165. json.followRedirect,
  2166. );
  2167. }
  2168. toJson() {
  2169. return {
  2170. address: this.address,
  2171. port: this.port,
  2172. portMap: XrayCommonClass.toV2Headers(this.portMap, false),
  2173. network: this.network,
  2174. followRedirect: this.followRedirect,
  2175. };
  2176. }
  2177. };
  2178. Inbound.SocksSettings = class extends Inbound.Settings {
  2179. constructor(protocol, auth = 'password', accounts = [new Inbound.SocksSettings.SocksAccount()], udp = false, ip = '127.0.0.1') {
  2180. super(protocol);
  2181. this.auth = auth;
  2182. this.accounts = accounts;
  2183. this.udp = udp;
  2184. this.ip = ip;
  2185. }
  2186. addAccount(account) {
  2187. this.accounts.push(account);
  2188. }
  2189. delAccount(index) {
  2190. this.accounts.splice(index, 1);
  2191. }
  2192. static fromJson(json = {}) {
  2193. let accounts;
  2194. if (json.auth === 'password') {
  2195. accounts = json.accounts.map(
  2196. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  2197. )
  2198. }
  2199. return new Inbound.SocksSettings(
  2200. Protocols.SOCKS,
  2201. json.auth,
  2202. accounts,
  2203. json.udp,
  2204. json.ip,
  2205. );
  2206. }
  2207. toJson() {
  2208. return {
  2209. auth: this.auth,
  2210. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  2211. udp: this.udp,
  2212. ip: this.ip,
  2213. };
  2214. }
  2215. };
  2216. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  2217. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2218. super();
  2219. this.user = user;
  2220. this.pass = pass;
  2221. }
  2222. static fromJson(json = {}) {
  2223. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  2224. }
  2225. };
  2226. Inbound.HttpSettings = class extends Inbound.Settings {
  2227. constructor(
  2228. protocol,
  2229. accounts = [new Inbound.HttpSettings.HttpAccount()],
  2230. allowTransparent = false,
  2231. ) {
  2232. super(protocol);
  2233. this.accounts = accounts;
  2234. this.allowTransparent = allowTransparent;
  2235. }
  2236. addAccount(account) {
  2237. this.accounts.push(account);
  2238. }
  2239. delAccount(index) {
  2240. this.accounts.splice(index, 1);
  2241. }
  2242. static fromJson(json = {}) {
  2243. return new Inbound.HttpSettings(
  2244. Protocols.HTTP,
  2245. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  2246. json.allowTransparent,
  2247. );
  2248. }
  2249. toJson() {
  2250. return {
  2251. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2252. allowTransparent: this.allowTransparent,
  2253. };
  2254. }
  2255. };
  2256. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2257. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2258. super();
  2259. this.user = user;
  2260. this.pass = pass;
  2261. }
  2262. static fromJson(json = {}) {
  2263. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2264. }
  2265. };
  2266. Inbound.WireguardSettings = class extends XrayCommonClass {
  2267. constructor(
  2268. protocol,
  2269. mtu = 1420,
  2270. secretKey = Wireguard.generateKeypair().privateKey,
  2271. peers = [new Inbound.WireguardSettings.Peer()],
  2272. noKernelTun = false
  2273. ) {
  2274. super(protocol);
  2275. this.mtu = mtu;
  2276. this.secretKey = secretKey;
  2277. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  2278. this.peers = peers;
  2279. this.noKernelTun = noKernelTun;
  2280. }
  2281. addPeer() {
  2282. this.peers.push(new Inbound.WireguardSettings.Peer(null, null, '', ['10.0.0.' + (this.peers.length + 2)]));
  2283. }
  2284. delPeer(index) {
  2285. this.peers.splice(index, 1);
  2286. }
  2287. static fromJson(json = {}) {
  2288. return new Inbound.WireguardSettings(
  2289. Protocols.WIREGUARD,
  2290. json.mtu,
  2291. json.secretKey,
  2292. json.peers.map(peer => Inbound.WireguardSettings.Peer.fromJson(peer)),
  2293. json.noKernelTun,
  2294. );
  2295. }
  2296. toJson() {
  2297. return {
  2298. mtu: this.mtu ?? undefined,
  2299. secretKey: this.secretKey,
  2300. peers: Inbound.WireguardSettings.Peer.toJsonArray(this.peers),
  2301. noKernelTun: this.noKernelTun,
  2302. };
  2303. }
  2304. };
  2305. Inbound.WireguardSettings.Peer = class extends XrayCommonClass {
  2306. constructor(privateKey, publicKey, psk = '', allowedIPs = ['10.0.0.2/32'], keepAlive = 0) {
  2307. super();
  2308. this.privateKey = privateKey
  2309. this.publicKey = publicKey;
  2310. if (!this.publicKey) {
  2311. [this.publicKey, this.privateKey] = Object.values(Wireguard.generateKeypair())
  2312. }
  2313. this.psk = psk;
  2314. allowedIPs.forEach((a, index) => {
  2315. if (a.length > 0 && !a.includes('/')) allowedIPs[index] += '/32';
  2316. })
  2317. this.allowedIPs = allowedIPs;
  2318. this.keepAlive = keepAlive;
  2319. }
  2320. static fromJson(json = {}) {
  2321. return new Inbound.WireguardSettings.Peer(
  2322. json.privateKey,
  2323. json.publicKey,
  2324. json.preSharedKey,
  2325. json.allowedIPs,
  2326. json.keepAlive
  2327. );
  2328. }
  2329. toJson() {
  2330. this.allowedIPs.forEach((a, index) => {
  2331. if (a.length > 0 && !a.includes('/')) this.allowedIPs[index] += '/32';
  2332. });
  2333. return {
  2334. privateKey: this.privateKey,
  2335. publicKey: this.publicKey,
  2336. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  2337. allowedIPs: this.allowedIPs,
  2338. keepAlive: this.keepAlive ?? undefined,
  2339. };
  2340. }
  2341. };