inbound.js 79 KB

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