1
0

inbound.js 76 KB

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