inbound.js 81 KB

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