inbound.js 73 KB

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