inbound.js 73 KB

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