inbound.js 83 KB

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