inbound.js 81 KB

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