inbound.js 74 KB

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