inbound.js 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  1. const Protocols = {
  2. VMESS: 'vmess',
  3. VLESS: 'vless',
  4. TROJAN: 'trojan',
  5. SHADOWSOCKS: 'shadowsocks',
  6. DOKODEMO: 'dokodemo-door',
  7. SOCKS: 'socks',
  8. HTTP: 'http',
  9. WIREGUARD: 'wireguard',
  10. };
  11. const SSMethods = {
  12. AES_256_GCM: 'aes-256-gcm',
  13. AES_128_GCM: 'aes-128-gcm',
  14. CHACHA20_POLY1305: 'chacha20-poly1305',
  15. CHACHA20_IETF_POLY1305: 'chacha20-ietf-poly1305',
  16. XCHACHA20_POLY1305: 'xchacha20-poly1305',
  17. XCHACHA20_IETF_POLY1305: 'xchacha20-ietf-poly1305',
  18. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  19. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  20. BLAKE3_CHACHA20_POLY1305: '2022-blake3-chacha20-poly1305',
  21. };
  22. const TLS_FLOW_CONTROL = {
  23. VISION: "xtls-rprx-vision",
  24. VISION_UDP443: "xtls-rprx-vision-udp443",
  25. };
  26. const TLS_VERSION_OPTION = {
  27. TLS10: "1.0",
  28. TLS11: "1.1",
  29. TLS12: "1.2",
  30. TLS13: "1.3",
  31. };
  32. const TLS_CIPHER_OPTION = {
  33. RSA_AES_128_CBC: "TLS_RSA_WITH_AES_128_CBC_SHA",
  34. RSA_AES_256_CBC: "TLS_RSA_WITH_AES_256_CBC_SHA",
  35. RSA_AES_128_GCM: "TLS_RSA_WITH_AES_128_GCM_SHA256",
  36. RSA_AES_256_GCM: "TLS_RSA_WITH_AES_256_GCM_SHA384",
  37. AES_128_GCM: "TLS_AES_128_GCM_SHA256",
  38. AES_256_GCM: "TLS_AES_256_GCM_SHA384",
  39. CHACHA20_POLY1305: "TLS_CHACHA20_POLY1305_SHA256",
  40. ECDHE_ECDSA_AES_128_CBC: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  41. ECDHE_ECDSA_AES_256_CBC: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  42. ECDHE_RSA_AES_128_CBC: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  43. ECDHE_RSA_AES_256_CBC: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  44. ECDHE_ECDSA_AES_128_GCM: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  45. ECDHE_ECDSA_AES_256_GCM: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  46. ECDHE_RSA_AES_128_GCM: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  47. ECDHE_RSA_AES_256_GCM: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  48. ECDHE_ECDSA_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
  49. ECDHE_RSA_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
  50. };
  51. const UTLS_FINGERPRINT = {
  52. UTLS_CHROME: "chrome",
  53. UTLS_FIREFOX: "firefox",
  54. UTLS_SAFARI: "safari",
  55. UTLS_IOS: "ios",
  56. UTLS_android: "android",
  57. UTLS_EDGE: "edge",
  58. UTLS_360: "360",
  59. UTLS_QQ: "qq",
  60. UTLS_RANDOM: "random",
  61. UTLS_RANDOMIZED: "randomized",
  62. };
  63. const ALPN_OPTION = {
  64. H3: "h3",
  65. H2: "h2",
  66. HTTP1: "http/1.1",
  67. };
  68. const SNIFFING_OPTION = {
  69. HTTP: "http",
  70. TLS: "tls",
  71. QUIC: "quic",
  72. FAKEDNS: "fakedns"
  73. };
  74. const USAGE_OPTION = {
  75. ENCIPHERMENT: "encipherment",
  76. VERIFY: "verify",
  77. ISSUE: "issue",
  78. };
  79. const DOMAIN_STRATEGY_OPTION = {
  80. AS_IS: "AsIs",
  81. USE_IP: "UseIP",
  82. USE_IPV6V4: "UseIPv6v4",
  83. USE_IPV6: "UseIPv6",
  84. USE_IPV4V6: "UseIPv4v6",
  85. USE_IPV4: "UseIPv4",
  86. FORCE_IP: "ForceIP",
  87. FORCE_IPV6V4: "ForceIPv6v4",
  88. FORCE_IPV6: "ForceIPv6",
  89. FORCE_IPV4V6: "ForceIPv4v6",
  90. FORCE_IPV4: "ForceIPv4",
  91. };
  92. const TCP_CONGESTION_OPTION = {
  93. BBR: "bbr",
  94. CUBIC: "cubic",
  95. RENO: "reno",
  96. };
  97. const USERS_SECURITY = {
  98. AES_128_GCM: "aes-128-gcm",
  99. CHACHA20_POLY1305: "chacha20-poly1305",
  100. AUTO: "auto",
  101. NONE: "none",
  102. ZERO: "zero",
  103. };
  104. Object.freeze(Protocols);
  105. Object.freeze(SSMethods);
  106. Object.freeze(TLS_FLOW_CONTROL);
  107. Object.freeze(TLS_VERSION_OPTION);
  108. Object.freeze(TLS_CIPHER_OPTION);
  109. Object.freeze(UTLS_FINGERPRINT);
  110. Object.freeze(ALPN_OPTION);
  111. Object.freeze(SNIFFING_OPTION);
  112. Object.freeze(USAGE_OPTION);
  113. Object.freeze(DOMAIN_STRATEGY_OPTION);
  114. Object.freeze(TCP_CONGESTION_OPTION);
  115. Object.freeze(USERS_SECURITY);
  116. class XrayCommonClass {
  117. static toJsonArray(arr) {
  118. return arr.map(obj => obj.toJson());
  119. }
  120. static fromJson() {
  121. return new XrayCommonClass();
  122. }
  123. toJson() {
  124. return this;
  125. }
  126. toString(format = true) {
  127. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  128. }
  129. static toHeaders(v2Headers) {
  130. let newHeaders = [];
  131. if (v2Headers) {
  132. Object.keys(v2Headers).forEach(key => {
  133. let values = v2Headers[key];
  134. if (typeof (values) === 'string') {
  135. newHeaders.push({ name: key, value: values });
  136. } else {
  137. for (let i = 0; i < values.length; ++i) {
  138. newHeaders.push({ name: key, value: values[i] });
  139. }
  140. }
  141. });
  142. }
  143. return newHeaders;
  144. }
  145. static toV2Headers(headers, arr = true) {
  146. let v2Headers = {};
  147. for (let i = 0; i < headers.length; ++i) {
  148. let name = headers[i].name;
  149. let value = headers[i].value;
  150. if (ObjectUtil.isEmpty(name) || ObjectUtil.isEmpty(value)) {
  151. continue;
  152. }
  153. if (!(name in v2Headers)) {
  154. v2Headers[name] = arr ? [value] : value;
  155. } else {
  156. if (arr) {
  157. v2Headers[name].push(value);
  158. } else {
  159. v2Headers[name] = value;
  160. }
  161. }
  162. }
  163. return v2Headers;
  164. }
  165. }
  166. class TcpStreamSettings extends XrayCommonClass {
  167. constructor(
  168. acceptProxyProtocol = false,
  169. type = 'none',
  170. request = new TcpStreamSettings.TcpRequest(),
  171. response = new TcpStreamSettings.TcpResponse(),
  172. ) {
  173. super();
  174. this.acceptProxyProtocol = acceptProxyProtocol;
  175. this.type = type;
  176. this.request = request;
  177. this.response = response;
  178. }
  179. static fromJson(json = {}) {
  180. let header = json.header;
  181. if (!header) {
  182. header = {};
  183. }
  184. return new TcpStreamSettings(json.acceptProxyProtocol,
  185. header.type,
  186. TcpStreamSettings.TcpRequest.fromJson(header.request),
  187. TcpStreamSettings.TcpResponse.fromJson(header.response),
  188. );
  189. }
  190. toJson() {
  191. return {
  192. acceptProxyProtocol: this.acceptProxyProtocol,
  193. header: {
  194. type: this.type,
  195. request: this.type === 'http' ? this.request.toJson() : undefined,
  196. response: this.type === 'http' ? this.response.toJson() : undefined,
  197. },
  198. };
  199. }
  200. }
  201. TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
  202. constructor(
  203. version = '1.1',
  204. method = 'GET',
  205. path = ['/'],
  206. headers = [],
  207. ) {
  208. super();
  209. this.version = version;
  210. this.method = method;
  211. this.path = path.length === 0 ? ['/'] : path;
  212. this.headers = headers;
  213. }
  214. addPath(path) {
  215. this.path.push(path);
  216. }
  217. removePath(index) {
  218. this.path.splice(index, 1);
  219. }
  220. addHeader(name, value) {
  221. this.headers.push({ name: name, value: value });
  222. }
  223. removeHeader(index) {
  224. this.headers.splice(index, 1);
  225. }
  226. static fromJson(json = {}) {
  227. return new TcpStreamSettings.TcpRequest(
  228. json.version,
  229. json.method,
  230. json.path,
  231. XrayCommonClass.toHeaders(json.headers),
  232. );
  233. }
  234. toJson() {
  235. return {
  236. version: this.version,
  237. method: this.method,
  238. path: ObjectUtil.clone(this.path),
  239. headers: XrayCommonClass.toV2Headers(this.headers),
  240. };
  241. }
  242. };
  243. TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
  244. constructor(
  245. version = '1.1',
  246. status = '200',
  247. reason = 'OK',
  248. headers = [],
  249. ) {
  250. super();
  251. this.version = version;
  252. this.status = status;
  253. this.reason = reason;
  254. this.headers = headers;
  255. }
  256. addHeader(name, value) {
  257. this.headers.push({ name: name, value: value });
  258. }
  259. removeHeader(index) {
  260. this.headers.splice(index, 1);
  261. }
  262. static fromJson(json = {}) {
  263. return new TcpStreamSettings.TcpResponse(
  264. json.version,
  265. json.status,
  266. json.reason,
  267. XrayCommonClass.toHeaders(json.headers),
  268. );
  269. }
  270. toJson() {
  271. return {
  272. version: this.version,
  273. status: this.status,
  274. reason: this.reason,
  275. headers: XrayCommonClass.toV2Headers(this.headers),
  276. };
  277. }
  278. };
  279. class KcpStreamSettings extends XrayCommonClass {
  280. constructor(
  281. mtu = 1350,
  282. tti = 50,
  283. uplinkCapacity = 5,
  284. downlinkCapacity = 20,
  285. congestion = false,
  286. readBufferSize = 2,
  287. writeBufferSize = 2,
  288. type = 'none',
  289. seed = RandomUtil.randomSeq(10),
  290. ) {
  291. super();
  292. this.mtu = mtu;
  293. this.tti = tti;
  294. this.upCap = uplinkCapacity;
  295. this.downCap = downlinkCapacity;
  296. this.congestion = congestion;
  297. this.readBuffer = readBufferSize;
  298. this.writeBuffer = writeBufferSize;
  299. this.type = type;
  300. this.seed = seed;
  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. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  312. json.seed,
  313. );
  314. }
  315. toJson() {
  316. return {
  317. mtu: this.mtu,
  318. tti: this.tti,
  319. uplinkCapacity: this.upCap,
  320. downlinkCapacity: this.downCap,
  321. congestion: this.congestion,
  322. readBufferSize: this.readBuffer,
  323. writeBufferSize: this.writeBuffer,
  324. header: {
  325. type: this.type,
  326. },
  327. seed: this.seed,
  328. };
  329. }
  330. }
  331. class WsStreamSettings extends XrayCommonClass {
  332. constructor(
  333. acceptProxyProtocol = false,
  334. path = '/',
  335. host = '',
  336. headers = []
  337. ) {
  338. super();
  339. this.acceptProxyProtocol = acceptProxyProtocol;
  340. this.path = path;
  341. this.host = host;
  342. this.headers = headers;
  343. }
  344. addHeader(name, value) {
  345. this.headers.push({ name: name, value: value });
  346. }
  347. removeHeader(index) {
  348. this.headers.splice(index, 1);
  349. }
  350. static fromJson(json = {}) {
  351. return new WsStreamSettings(
  352. json.acceptProxyProtocol,
  353. json.path,
  354. json.host,
  355. XrayCommonClass.toHeaders(json.headers),
  356. );
  357. }
  358. toJson() {
  359. return {
  360. acceptProxyProtocol: this.acceptProxyProtocol,
  361. path: this.path,
  362. host: this.host,
  363. headers: XrayCommonClass.toV2Headers(this.headers, false),
  364. };
  365. }
  366. }
  367. class HttpStreamSettings extends XrayCommonClass {
  368. constructor(
  369. path = '/',
  370. host = [''],
  371. ) {
  372. super();
  373. this.path = path;
  374. this.host = host.length === 0 ? [''] : host;
  375. }
  376. addHost(host) {
  377. this.host.push(host);
  378. }
  379. removeHost(index) {
  380. this.host.splice(index, 1);
  381. }
  382. static fromJson(json = {}) {
  383. return new HttpStreamSettings(json.path, json.host);
  384. }
  385. toJson() {
  386. let host = [];
  387. for (let i = 0; i < this.host.length; ++i) {
  388. if (!ObjectUtil.isEmpty(this.host[i])) {
  389. host.push(this.host[i]);
  390. }
  391. }
  392. return {
  393. path: this.path,
  394. host: host,
  395. }
  396. }
  397. }
  398. class GrpcStreamSettings extends XrayCommonClass {
  399. constructor(
  400. serviceName = "",
  401. authority = "",
  402. multiMode = false,
  403. ) {
  404. super();
  405. this.serviceName = serviceName;
  406. this.authority = authority;
  407. this.multiMode = multiMode;
  408. }
  409. static fromJson(json = {}) {
  410. return new GrpcStreamSettings(
  411. json.serviceName,
  412. json.authority,
  413. json.multiMode
  414. );
  415. }
  416. toJson() {
  417. return {
  418. serviceName: this.serviceName,
  419. authority: this.authority,
  420. multiMode: this.multiMode,
  421. }
  422. }
  423. }
  424. class HTTPUpgradeStreamSettings extends XrayCommonClass {
  425. constructor(
  426. acceptProxyProtocol = false,
  427. path = '/',
  428. host = '',
  429. headers = []
  430. ) {
  431. super();
  432. this.acceptProxyProtocol = acceptProxyProtocol;
  433. this.path = path;
  434. this.host = host;
  435. this.headers = headers;
  436. }
  437. addHeader(name, value) {
  438. this.headers.push({ name: name, value: value });
  439. }
  440. removeHeader(index) {
  441. this.headers.splice(index, 1);
  442. }
  443. static fromJson(json = {}) {
  444. return new HTTPUpgradeStreamSettings(
  445. json.acceptProxyProtocol,
  446. json.path,
  447. json.host,
  448. XrayCommonClass.toHeaders(json.headers),
  449. );
  450. }
  451. toJson() {
  452. return {
  453. acceptProxyProtocol: this.acceptProxyProtocol,
  454. path: this.path,
  455. host: this.host,
  456. headers: XrayCommonClass.toV2Headers(this.headers, false),
  457. };
  458. }
  459. }
  460. class SplitHTTPStreamSettings extends XrayCommonClass {
  461. constructor(
  462. path = '/',
  463. host = '',
  464. headers = [],
  465. scMaxConcurrentPosts = "100-200",
  466. scMaxEachPostBytes = "1000000-2000000",
  467. scMinPostsIntervalMs = "10-50",
  468. noSSEHeader = false,
  469. xPaddingBytes = "100-1000",
  470. xmux = {
  471. maxConcurrency: "16-32",
  472. maxConnections: 0,
  473. cMaxReuseTimes: "64-128",
  474. cMaxLifetimeMs: 0
  475. }
  476. ) {
  477. super();
  478. this.path = path;
  479. this.host = host;
  480. this.headers = headers;
  481. this.scMaxConcurrentPosts = scMaxConcurrentPosts;
  482. this.scMaxEachPostBytes = scMaxEachPostBytes;
  483. this.scMinPostsIntervalMs = scMinPostsIntervalMs;
  484. this.noSSEHeader = noSSEHeader;
  485. this.xPaddingBytes = xPaddingBytes;
  486. this.xmux = xmux;
  487. }
  488. addHeader(name, value) {
  489. this.headers.push({ name: name, value: value });
  490. }
  491. removeHeader(index) {
  492. this.headers.splice(index, 1);
  493. }
  494. static fromJson(json = {}) {
  495. return new SplitHTTPStreamSettings(
  496. json.path,
  497. json.host,
  498. XrayCommonClass.toHeaders(json.headers),
  499. json.scMaxConcurrentPosts,
  500. json.scMaxEachPostBytes,
  501. json.scMinPostsIntervalMs,
  502. json.noSSEHeader,
  503. json.xPaddingBytes,
  504. json.xmux,
  505. );
  506. }
  507. toJson() {
  508. return {
  509. path: this.path,
  510. host: this.host,
  511. headers: XrayCommonClass.toV2Headers(this.headers, false),
  512. scMaxConcurrentPosts: this.scMaxConcurrentPosts,
  513. scMaxEachPostBytes: this.scMaxEachPostBytes,
  514. scMinPostsIntervalMs: this.scMinPostsIntervalMs,
  515. noSSEHeader: this.noSSEHeader,
  516. xPaddingBytes: this.xPaddingBytes,
  517. xmux: {
  518. maxConcurrency: this.xmux.maxConcurrency,
  519. maxConnections: this.xmux.maxConnections,
  520. cMaxReuseTimes: this.xmux.cMaxReuseTimes,
  521. cMaxLifetimeMs: this.xmux.cMaxLifetimeMs
  522. }
  523. };
  524. }
  525. }
  526. class TlsStreamSettings extends XrayCommonClass {
  527. constructor(
  528. serverName = '',
  529. minVersion = TLS_VERSION_OPTION.TLS12,
  530. maxVersion = TLS_VERSION_OPTION.TLS13,
  531. cipherSuites = '',
  532. rejectUnknownSni = false,
  533. disableSystemRoot = false,
  534. enableSessionResumption = false,
  535. certificates = [new TlsStreamSettings.Cert()],
  536. alpn = [ALPN_OPTION.H3, ALPN_OPTION.H2, ALPN_OPTION.HTTP1],
  537. settings = new TlsStreamSettings.Settings()
  538. ) {
  539. super();
  540. this.sni = serverName;
  541. this.minVersion = minVersion;
  542. this.maxVersion = maxVersion;
  543. this.cipherSuites = cipherSuites;
  544. this.rejectUnknownSni = rejectUnknownSni;
  545. this.disableSystemRoot = disableSystemRoot;
  546. this.enableSessionResumption = enableSessionResumption;
  547. this.certs = certificates;
  548. this.alpn = alpn;
  549. this.settings = settings;
  550. }
  551. addCert() {
  552. this.certs.push(new TlsStreamSettings.Cert());
  553. }
  554. removeCert(index) {
  555. this.certs.splice(index, 1);
  556. }
  557. static fromJson(json = {}) {
  558. let certs;
  559. let settings;
  560. if (!ObjectUtil.isEmpty(json.certificates)) {
  561. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  562. }
  563. if (!ObjectUtil.isEmpty(json.settings)) {
  564. settings = new TlsStreamSettings.Settings(json.settings.allowInsecure, json.settings.fingerprint, json.settings.serverName, json.settings.domains);
  565. }
  566. return new TlsStreamSettings(
  567. json.serverName,
  568. json.minVersion,
  569. json.maxVersion,
  570. json.cipherSuites,
  571. json.rejectUnknownSni,
  572. json.disableSystemRoot,
  573. json.enableSessionResumption,
  574. certs,
  575. json.alpn,
  576. settings,
  577. );
  578. }
  579. toJson() {
  580. return {
  581. serverName: this.sni,
  582. minVersion: this.minVersion,
  583. maxVersion: this.maxVersion,
  584. cipherSuites: this.cipherSuites,
  585. rejectUnknownSni: this.rejectUnknownSni,
  586. disableSystemRoot: this.disableSystemRoot,
  587. enableSessionResumption: this.enableSessionResumption,
  588. certificates: TlsStreamSettings.toJsonArray(this.certs),
  589. alpn: this.alpn,
  590. settings: this.settings,
  591. };
  592. }
  593. }
  594. TlsStreamSettings.Cert = class extends XrayCommonClass {
  595. constructor(
  596. useFile = true,
  597. certificateFile = '',
  598. keyFile = '',
  599. certificate = '',
  600. key = '',
  601. ocspStapling = 3600,
  602. oneTimeLoading = false,
  603. usage = USAGE_OPTION.ENCIPHERMENT,
  604. buildChain = false,
  605. ) {
  606. super();
  607. this.useFile = useFile;
  608. this.certFile = certificateFile;
  609. this.keyFile = keyFile;
  610. this.cert = Array.isArray(certificate) ? certificate.join('\n') : certificate;
  611. this.key = Array.isArray(key) ? key.join('\n') : key;
  612. this.ocspStapling = ocspStapling;
  613. this.oneTimeLoading = oneTimeLoading;
  614. this.usage = usage;
  615. this.buildChain = buildChain
  616. }
  617. static fromJson(json = {}) {
  618. if ('certificateFile' in json && 'keyFile' in json) {
  619. return new TlsStreamSettings.Cert(
  620. true,
  621. json.certificateFile,
  622. json.keyFile, '', '',
  623. json.ocspStapling,
  624. json.oneTimeLoading,
  625. json.usage,
  626. json.buildChain,
  627. );
  628. } else {
  629. return new TlsStreamSettings.Cert(
  630. false, '', '',
  631. json.certificate.join('\n'),
  632. json.key.join('\n'),
  633. json.ocspStapling,
  634. json.oneTimeLoading,
  635. json.usage,
  636. json.buildChain,
  637. );
  638. }
  639. }
  640. toJson() {
  641. if (this.useFile) {
  642. return {
  643. certificateFile: this.certFile,
  644. keyFile: this.keyFile,
  645. ocspStapling: this.ocspStapling,
  646. oneTimeLoading: this.oneTimeLoading,
  647. usage: this.usage,
  648. buildChain: this.buildChain,
  649. };
  650. } else {
  651. return {
  652. certificate: this.cert.split('\n'),
  653. key: this.key.split('\n'),
  654. ocspStapling: this.ocspStapling,
  655. oneTimeLoading: this.oneTimeLoading,
  656. usage: this.usage,
  657. buildChain: this.buildChain,
  658. };
  659. }
  660. }
  661. };
  662. TlsStreamSettings.Settings = class extends XrayCommonClass {
  663. constructor(allowInsecure = false, fingerprint = '') {
  664. super();
  665. this.allowInsecure = allowInsecure;
  666. this.fingerprint = fingerprint;
  667. }
  668. static fromJson(json = {}) {
  669. return new TlsStreamSettings.Settings(
  670. json.allowInsecure,
  671. json.fingerprint,
  672. );
  673. }
  674. toJson() {
  675. return {
  676. allowInsecure: this.allowInsecure,
  677. fingerprint: this.fingerprint,
  678. };
  679. }
  680. };
  681. class RealityStreamSettings extends XrayCommonClass {
  682. constructor(
  683. show = false,
  684. xver = 0,
  685. dest = 'yahoo.com:443',
  686. serverNames = 'yahoo.com,www.yahoo.com',
  687. privateKey = '',
  688. minClient = '',
  689. maxClient = '',
  690. maxTimediff = 0,
  691. shortIds = RandomUtil.randomShortIds(),
  692. settings = new RealityStreamSettings.Settings()
  693. ) {
  694. super();
  695. this.show = show;
  696. this.xver = xver;
  697. this.dest = dest;
  698. this.serverNames = Array.isArray(serverNames) ? serverNames.join(",") : serverNames;
  699. this.privateKey = privateKey;
  700. this.minClient = minClient;
  701. this.maxClient = maxClient;
  702. this.maxTimediff = maxTimediff;
  703. this.shortIds = Array.isArray(shortIds) ? shortIds.join(",") : shortIds;
  704. this.settings = settings;
  705. }
  706. static fromJson(json = {}) {
  707. let settings;
  708. if (!ObjectUtil.isEmpty(json.settings)) {
  709. settings = new RealityStreamSettings.Settings(
  710. json.settings.publicKey,
  711. json.settings.fingerprint,
  712. json.settings.serverName,
  713. json.settings.spiderX
  714. );
  715. }
  716. return new RealityStreamSettings(
  717. json.show,
  718. json.xver,
  719. json.dest,
  720. json.serverNames,
  721. json.privateKey,
  722. json.minClient,
  723. json.maxClient,
  724. json.maxTimediff,
  725. json.shortIds,
  726. json.settings,
  727. );
  728. }
  729. toJson() {
  730. return {
  731. show: this.show,
  732. xver: this.xver,
  733. dest: this.dest,
  734. serverNames: this.serverNames.split(","),
  735. privateKey: this.privateKey,
  736. minClient: this.minClient,
  737. maxClient: this.maxClient,
  738. maxTimediff: this.maxTimediff,
  739. shortIds: this.shortIds.split(","),
  740. settings: this.settings,
  741. };
  742. }
  743. }
  744. RealityStreamSettings.Settings = class extends XrayCommonClass {
  745. constructor(
  746. publicKey = '',
  747. fingerprint = UTLS_FINGERPRINT.UTLS_RANDOM,
  748. serverName = '',
  749. spiderX = '/'
  750. ) {
  751. super();
  752. this.publicKey = publicKey;
  753. this.fingerprint = fingerprint;
  754. this.serverName = serverName;
  755. this.spiderX = spiderX;
  756. }
  757. static fromJson(json = {}) {
  758. return new RealityStreamSettings.Settings(
  759. json.publicKey,
  760. json.fingerprint,
  761. json.serverName,
  762. json.spiderX,
  763. );
  764. }
  765. toJson() {
  766. return {
  767. publicKey: this.publicKey,
  768. fingerprint: this.fingerprint,
  769. serverName: this.serverName,
  770. spiderX: this.spiderX,
  771. };
  772. }
  773. };
  774. class SockoptStreamSettings extends XrayCommonClass {
  775. constructor(
  776. acceptProxyProtocol = false,
  777. tcpFastOpen = false,
  778. mark = 0,
  779. tproxy = "off",
  780. tcpMptcp = false,
  781. tcpNoDelay = false,
  782. domainStrategy = DOMAIN_STRATEGY_OPTION.USE_IP,
  783. tcpMaxSeg = 1440,
  784. dialerProxy = "",
  785. tcpKeepAliveInterval = 0,
  786. tcpKeepAliveIdle = 300,
  787. tcpUserTimeout = 10000,
  788. tcpcongestion = TCP_CONGESTION_OPTION.BBR,
  789. V6Only = false,
  790. tcpWindowClamp = 600,
  791. interfaceName = "",
  792. ) {
  793. super();
  794. this.acceptProxyProtocol = acceptProxyProtocol;
  795. this.tcpFastOpen = tcpFastOpen;
  796. this.mark = mark;
  797. this.tproxy = tproxy;
  798. this.tcpMptcp = tcpMptcp;
  799. this.tcpNoDelay = tcpNoDelay;
  800. this.domainStrategy = domainStrategy;
  801. this.tcpMaxSeg = tcpMaxSeg;
  802. this.dialerProxy = dialerProxy;
  803. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  804. this.tcpKeepAliveIdle = tcpKeepAliveIdle;
  805. this.tcpUserTimeout = tcpUserTimeout;
  806. this.tcpcongestion = tcpcongestion;
  807. this.V6Only = V6Only;
  808. this.tcpWindowClamp = tcpWindowClamp;
  809. this.interfaceName = interfaceName;
  810. }
  811. static fromJson(json = {}) {
  812. if (Object.keys(json).length === 0) return undefined;
  813. return new SockoptStreamSettings(
  814. json.acceptProxyProtocol,
  815. json.tcpFastOpen,
  816. json.mark,
  817. json.tproxy,
  818. json.tcpMptcp,
  819. json.tcpNoDelay,
  820. json.domainStrategy,
  821. json.tcpMaxSeg,
  822. json.dialerProxy,
  823. json.tcpKeepAliveInterval,
  824. json.tcpKeepAliveIdle,
  825. json.tcpUserTimeout,
  826. json.tcpcongestion,
  827. json.V6Only,
  828. json.tcpWindowClamp,
  829. json.interface,
  830. );
  831. }
  832. toJson() {
  833. return {
  834. acceptProxyProtocol: this.acceptProxyProtocol,
  835. tcpFastOpen: this.tcpFastOpen,
  836. mark: this.mark,
  837. tproxy: this.tproxy,
  838. tcpMptcp: this.tcpMptcp,
  839. tcpNoDelay: this.tcpNoDelay,
  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. }
  852. }
  853. class StreamSettings extends XrayCommonClass {
  854. constructor(network = 'tcp',
  855. security = 'none',
  856. externalProxy = [],
  857. tlsSettings = new TlsStreamSettings(),
  858. realitySettings = new RealityStreamSettings(),
  859. tcpSettings = new TcpStreamSettings(),
  860. kcpSettings = new KcpStreamSettings(),
  861. wsSettings = new WsStreamSettings(),
  862. httpSettings = new HttpStreamSettings(),
  863. grpcSettings = new GrpcStreamSettings(),
  864. httpupgradeSettings = new HTTPUpgradeStreamSettings(),
  865. splithttpSettings = new SplitHTTPStreamSettings(),
  866. sockopt = undefined,
  867. ) {
  868. super();
  869. this.network = network;
  870. this.security = security;
  871. this.externalProxy = externalProxy;
  872. this.tls = tlsSettings;
  873. this.reality = realitySettings;
  874. this.tcp = tcpSettings;
  875. this.kcp = kcpSettings;
  876. this.ws = wsSettings;
  877. this.http = httpSettings;
  878. this.grpc = grpcSettings;
  879. this.httpupgrade = httpupgradeSettings;
  880. this.splithttp = splithttpSettings;
  881. this.sockopt = sockopt;
  882. }
  883. get isTls() {
  884. return this.security === "tls";
  885. }
  886. set isTls(isTls) {
  887. if (isTls) {
  888. this.security = 'tls';
  889. } else {
  890. this.security = 'none';
  891. }
  892. }
  893. //for Reality
  894. get isReality() {
  895. return this.security === "reality";
  896. }
  897. set isReality(isReality) {
  898. if (isReality) {
  899. this.security = 'reality';
  900. } else {
  901. this.security = 'none';
  902. }
  903. }
  904. get sockoptSwitch() {
  905. return this.sockopt != undefined;
  906. }
  907. set sockoptSwitch(value) {
  908. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  909. }
  910. static fromJson(json = {}) {
  911. return new StreamSettings(
  912. json.network,
  913. json.security,
  914. json.externalProxy,
  915. TlsStreamSettings.fromJson(json.tlsSettings),
  916. RealityStreamSettings.fromJson(json.realitySettings),
  917. TcpStreamSettings.fromJson(json.tcpSettings),
  918. KcpStreamSettings.fromJson(json.kcpSettings),
  919. WsStreamSettings.fromJson(json.wsSettings),
  920. HttpStreamSettings.fromJson(json.httpSettings),
  921. GrpcStreamSettings.fromJson(json.grpcSettings),
  922. HTTPUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  923. SplitHTTPStreamSettings.fromJson(json.splithttpSettings),
  924. SockoptStreamSettings.fromJson(json.sockopt),
  925. );
  926. }
  927. toJson() {
  928. const network = this.network;
  929. return {
  930. network: network,
  931. security: this.security,
  932. externalProxy: this.externalProxy,
  933. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  934. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  935. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  936. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  937. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  938. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  939. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  940. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  941. splithttpSettings: network === 'splithttp' ? this.splithttp.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 Allocate extends XrayCommonClass {
  974. constructor(
  975. strategy = "always",
  976. refresh = 5,
  977. concurrency = 3,
  978. ) {
  979. super();
  980. this.strategy = strategy;
  981. this.refresh = refresh;
  982. this.concurrency = concurrency;
  983. }
  984. static fromJson(json = {}) {
  985. return new Allocate(
  986. json.strategy,
  987. json.refresh,
  988. json.concurrency,
  989. );
  990. }
  991. }
  992. class Inbound extends XrayCommonClass {
  993. constructor(
  994. port = RandomUtil.randomIntRange(10000, 60000),
  995. listen = '',
  996. protocol = Protocols.VLESS,
  997. settings = null,
  998. streamSettings = new StreamSettings(),
  999. tag = '',
  1000. sniffing = new Sniffing(),
  1001. allocate = new Allocate(),
  1002. clientStats = '',
  1003. ) {
  1004. super();
  1005. this.port = port;
  1006. this.listen = listen;
  1007. this._protocol = protocol;
  1008. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  1009. this.stream = streamSettings;
  1010. this.tag = tag;
  1011. this.sniffing = sniffing;
  1012. this.allocate = allocate;
  1013. this.clientStats = clientStats;
  1014. }
  1015. getClientStats() {
  1016. return this.clientStats;
  1017. }
  1018. get clients() {
  1019. switch (this.protocol) {
  1020. case Protocols.VMESS: return this.settings.vmesses;
  1021. case Protocols.VLESS: return this.settings.vlesses;
  1022. case Protocols.TROJAN: return this.settings.trojans;
  1023. case Protocols.SHADOWSOCKS: return this.isSSMultiUser ? this.settings.shadowsockses : null;
  1024. default: return null;
  1025. }
  1026. }
  1027. get protocol() {
  1028. return this._protocol;
  1029. }
  1030. set protocol(protocol) {
  1031. this._protocol = protocol;
  1032. this.settings = Inbound.Settings.getSettings(protocol);
  1033. if (protocol === Protocols.TROJAN) {
  1034. this.tls = false;
  1035. }
  1036. }
  1037. get network() {
  1038. return this.stream.network;
  1039. }
  1040. set network(network) {
  1041. this.stream.network = network;
  1042. }
  1043. get isTcp() {
  1044. return this.network === "tcp";
  1045. }
  1046. get isWs() {
  1047. return this.network === "ws";
  1048. }
  1049. get isKcp() {
  1050. return this.network === "kcp";
  1051. }
  1052. get isGrpc() {
  1053. return this.network === "grpc";
  1054. }
  1055. get isH2() {
  1056. return this.network === "http";
  1057. }
  1058. get isHttpupgrade() {
  1059. return this.network === "httpupgrade";
  1060. }
  1061. get isSplithttp() {
  1062. return this.network === "splithttp";
  1063. }
  1064. // Shadowsocks
  1065. get method() {
  1066. switch (this.protocol) {
  1067. case Protocols.SHADOWSOCKS:
  1068. return this.settings.method;
  1069. default:
  1070. return "";
  1071. }
  1072. }
  1073. get isSSMultiUser() {
  1074. return this.method != SSMethods.BLAKE3_CHACHA20_POLY1305;
  1075. }
  1076. get isSS2022() {
  1077. return this.method.substring(0, 4) === "2022";
  1078. }
  1079. get serverName() {
  1080. if (this.stream.isTls) return this.stream.tls.sni;
  1081. if (this.stream.isReality) return this.stream.reality.serverNames;
  1082. return "";
  1083. }
  1084. getHeader(obj, name) {
  1085. for (const header of obj.headers) {
  1086. if (header.name.toLowerCase() === name.toLowerCase()) {
  1087. return header.value;
  1088. }
  1089. }
  1090. return "";
  1091. }
  1092. get host() {
  1093. if (this.isTcp) {
  1094. return this.getHeader(this.stream.tcp.request, 'host');
  1095. } else if (this.isWs) {
  1096. return this.stream.ws.host?.length > 0 ? this.stream.ws.host : this.getHeader(this.stream.ws, 'host');
  1097. } else if (this.isH2) {
  1098. return this.stream.http.host[0];
  1099. } else if (this.isHttpupgrade) {
  1100. return this.stream.httpupgrade.host?.length > 0 ? this.stream.httpupgrade.host : this.getHeader(this.stream.httpupgrade, 'host');
  1101. } else if (this.isSplithttp) {
  1102. return this.stream.splithttp.host?.length > 0 ? this.stream.splithttp.host : this.getHeader(this.stream.splithttp, 'host');
  1103. }
  1104. return null;
  1105. }
  1106. get path() {
  1107. if (this.isTcp) {
  1108. return this.stream.tcp.request.path[0];
  1109. } else if (this.isWs) {
  1110. return this.stream.ws.path;
  1111. } else if (this.isH2) {
  1112. return this.stream.http.path;
  1113. } else if (this.isHttpupgrade) {
  1114. return this.stream.httpupgrade.path;
  1115. } else if (this.isSplithttp) {
  1116. return this.stream.splithttp.path;
  1117. }
  1118. return null;
  1119. }
  1120. get kcpType() {
  1121. return this.stream.kcp.type;
  1122. }
  1123. get kcpSeed() {
  1124. return this.stream.kcp.seed;
  1125. }
  1126. get serviceName() {
  1127. return this.stream.grpc.serviceName;
  1128. }
  1129. isExpiry(index) {
  1130. let exp = this.clients[index].expiryTime;
  1131. return exp > 0 ? exp < new Date().getTime() : false;
  1132. }
  1133. canEnableTls() {
  1134. if (![Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol)) return false;
  1135. return ["tcp", "ws", "http", "grpc", "httpupgrade", "splithttp"].includes(this.network);
  1136. }
  1137. //this is used for xtls-rprx-vision
  1138. canEnableTlsFlow() {
  1139. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1140. return this.protocol === Protocols.VLESS;
  1141. }
  1142. return false;
  1143. }
  1144. canEnableReality() {
  1145. if (![Protocols.VLESS, Protocols.TROJAN].includes(this.protocol)) return false;
  1146. return ["tcp", "http", "grpc", "splithttp"].includes(this.network);
  1147. }
  1148. canEnableStream() {
  1149. return [Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol);
  1150. }
  1151. reset() {
  1152. this.port = RandomUtil.randomIntRange(10000, 60000);
  1153. this.listen = '';
  1154. this.protocol = Protocols.VMESS;
  1155. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1156. this.stream = new StreamSettings();
  1157. this.tag = '';
  1158. this.sniffing = new Sniffing();
  1159. this.allocate = new Allocate();
  1160. }
  1161. genVmessLink(address = '', port = this.port, forceTls, remark = '', clientId, security) {
  1162. if (this.protocol !== Protocols.VMESS) {
  1163. return '';
  1164. }
  1165. const tls = forceTls == 'same' ? this.stream.security : forceTls;
  1166. let obj = {
  1167. v: '2',
  1168. ps: remark,
  1169. add: address,
  1170. port: port,
  1171. id: clientId,
  1172. scy: security,
  1173. net: this.stream.network,
  1174. type: 'none',
  1175. tls: tls,
  1176. };
  1177. const network = this.stream.network;
  1178. if (network === 'tcp') {
  1179. const tcp = this.stream.tcp;
  1180. obj.type = tcp.type;
  1181. if (tcp.type === 'http') {
  1182. const request = tcp.request;
  1183. obj.path = request.path.join(',');
  1184. const host = this.getHeader(request, 'host');
  1185. if (host) obj.host = host;
  1186. }
  1187. } else if (network === 'kcp') {
  1188. const kcp = this.stream.kcp;
  1189. obj.type = kcp.type;
  1190. obj.path = kcp.seed;
  1191. } else if (network === 'ws') {
  1192. const ws = this.stream.ws;
  1193. obj.path = ws.path;
  1194. obj.host = ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host');
  1195. } else if (network === 'http') {
  1196. obj.net = 'h2';
  1197. obj.path = this.stream.http.path;
  1198. obj.host = this.stream.http.host.join(',');
  1199. } else if (network === 'grpc') {
  1200. obj.path = this.stream.grpc.serviceName;
  1201. obj.authority = this.stream.grpc.authority;
  1202. if (this.stream.grpc.multiMode) {
  1203. obj.type = 'multi'
  1204. }
  1205. } else if (network === 'httpupgrade') {
  1206. const httpupgrade = this.stream.httpupgrade;
  1207. obj.path = httpupgrade.path;
  1208. obj.host = httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host');
  1209. } else if (network === 'splithttp') {
  1210. const splithttp = this.stream.splithttp;
  1211. obj.path = splithttp.path;
  1212. obj.host = splithttp.host?.length > 0 ? splithttp.host : this.getHeader(splithttp, 'host');
  1213. }
  1214. if (security === 'tls') {
  1215. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1216. obj.sni = this.stream.tls.sni;
  1217. }
  1218. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)) {
  1219. obj.fp = this.stream.tls.settings.fingerprint;
  1220. }
  1221. if (this.stream.tls.alpn.length > 0) {
  1222. obj.alpn = this.stream.tls.alpn.join(',');
  1223. }
  1224. if (this.stream.tls.settings.allowInsecure) {
  1225. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1226. }
  1227. }
  1228. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  1229. }
  1230. genVLESSLink(address = '', port = this.port, forceTls, remark = '', clientId, flow) {
  1231. const uuid = clientId;
  1232. const type = this.stream.network;
  1233. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1234. const params = new Map();
  1235. params.set("type", this.stream.network);
  1236. switch (type) {
  1237. case "tcp":
  1238. const tcp = this.stream.tcp;
  1239. if (tcp.type === 'http') {
  1240. const request = tcp.request;
  1241. params.set("path", request.path.join(','));
  1242. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1243. if (index >= 0) {
  1244. const host = request.headers[index].value;
  1245. params.set("host", host);
  1246. }
  1247. params.set("headerType", 'http');
  1248. }
  1249. break;
  1250. case "kcp":
  1251. const kcp = this.stream.kcp;
  1252. params.set("headerType", kcp.type);
  1253. params.set("seed", kcp.seed);
  1254. break;
  1255. case "ws":
  1256. const ws = this.stream.ws;
  1257. params.set("path", ws.path);
  1258. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1259. break;
  1260. case "http":
  1261. const http = this.stream.http;
  1262. params.set("path", http.path);
  1263. params.set("host", http.host);
  1264. break;
  1265. case "grpc":
  1266. const grpc = this.stream.grpc;
  1267. params.set("serviceName", grpc.serviceName);
  1268. params.set("authority", grpc.authority);
  1269. if (grpc.multiMode) {
  1270. params.set("mode", "multi");
  1271. }
  1272. break;
  1273. case "httpupgrade":
  1274. const httpupgrade = this.stream.httpupgrade;
  1275. params.set("path", httpupgrade.path);
  1276. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1277. break;
  1278. case "splithttp":
  1279. const splithttp = this.stream.splithttp;
  1280. params.set("path", splithttp.path);
  1281. params.set("host", splithttp.host?.length > 0 ? splithttp.host : this.getHeader(splithttp, 'host'));
  1282. break;
  1283. }
  1284. if (security === 'tls') {
  1285. params.set("security", "tls");
  1286. if (this.stream.isTls) {
  1287. params.set("fp", this.stream.tls.settings.fingerprint);
  1288. params.set("alpn", this.stream.tls.alpn);
  1289. if (this.stream.tls.settings.allowInsecure) {
  1290. params.set("allowInsecure", "1");
  1291. }
  1292. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1293. params.set("sni", this.stream.tls.sni);
  1294. }
  1295. if (type == "tcp" && !ObjectUtil.isEmpty(flow)) {
  1296. params.set("flow", flow);
  1297. }
  1298. }
  1299. }
  1300. else if (security === 'reality') {
  1301. params.set("security", "reality");
  1302. params.set("pbk", this.stream.reality.settings.publicKey);
  1303. params.set("fp", this.stream.reality.settings.fingerprint);
  1304. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1305. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1306. }
  1307. if (this.stream.reality.shortIds.length > 0) {
  1308. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1309. }
  1310. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1311. params.set("spx", this.stream.reality.settings.spiderX);
  1312. }
  1313. if (type == 'tcp' && !ObjectUtil.isEmpty(flow)) {
  1314. params.set("flow", flow);
  1315. }
  1316. }
  1317. else {
  1318. params.set("security", "none");
  1319. }
  1320. const link = `vless://${uuid}@${address}:${port}`;
  1321. const url = new URL(link);
  1322. for (const [key, value] of params) {
  1323. url.searchParams.set(key, value)
  1324. }
  1325. url.hash = encodeURIComponent(remark);
  1326. return url.toString();
  1327. }
  1328. genSSLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1329. let settings = this.settings;
  1330. const type = this.stream.network;
  1331. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1332. const params = new Map();
  1333. params.set("type", this.stream.network);
  1334. switch (type) {
  1335. case "tcp":
  1336. const tcp = this.stream.tcp;
  1337. if (tcp.type === 'http') {
  1338. const request = tcp.request;
  1339. params.set("path", request.path.join(','));
  1340. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1341. if (index >= 0) {
  1342. const host = request.headers[index].value;
  1343. params.set("host", host);
  1344. }
  1345. params.set("headerType", 'http');
  1346. }
  1347. break;
  1348. case "kcp":
  1349. const kcp = this.stream.kcp;
  1350. params.set("headerType", kcp.type);
  1351. params.set("seed", kcp.seed);
  1352. break;
  1353. case "ws":
  1354. const ws = this.stream.ws;
  1355. params.set("path", ws.path);
  1356. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1357. break;
  1358. case "http":
  1359. const http = this.stream.http;
  1360. params.set("path", http.path);
  1361. params.set("host", http.host);
  1362. break;
  1363. case "grpc":
  1364. const grpc = this.stream.grpc;
  1365. params.set("serviceName", grpc.serviceName);
  1366. params.set("authority", grpc.authority);
  1367. if (grpc.multiMode) {
  1368. params.set("mode", "multi");
  1369. }
  1370. break;
  1371. case "httpupgrade":
  1372. const httpupgrade = this.stream.httpupgrade;
  1373. params.set("path", httpupgrade.path);
  1374. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1375. break;
  1376. case "splithttp":
  1377. const splithttp = this.stream.splithttp;
  1378. params.set("path", splithttp.path);
  1379. params.set("host", splithttp.host?.length > 0 ? splithttp.host : this.getHeader(splithttp, 'host'));
  1380. break;
  1381. }
  1382. if (security === 'tls') {
  1383. params.set("security", "tls");
  1384. if (this.stream.isTls) {
  1385. params.set("fp", this.stream.tls.settings.fingerprint);
  1386. params.set("alpn", this.stream.tls.alpn);
  1387. if (this.stream.tls.settings.allowInsecure) {
  1388. params.set("allowInsecure", "1");
  1389. }
  1390. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1391. params.set("sni", this.stream.tls.sni);
  1392. }
  1393. }
  1394. }
  1395. let password = new Array();
  1396. if (this.isSS2022) password.push(settings.password);
  1397. if (this.isSSMultiUser) password.push(clientPassword);
  1398. let link = `ss://${safeBase64(settings.method + ':' + password.join(':'))}@${address}:${port}`;
  1399. const url = new URL(link);
  1400. for (const [key, value] of params) {
  1401. url.searchParams.set(key, value)
  1402. }
  1403. url.hash = encodeURIComponent(remark);
  1404. return url.toString();
  1405. }
  1406. genTrojanLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1407. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1408. const type = this.stream.network;
  1409. const params = new Map();
  1410. params.set("type", this.stream.network);
  1411. switch (type) {
  1412. case "tcp":
  1413. const tcp = this.stream.tcp;
  1414. if (tcp.type === 'http') {
  1415. const request = tcp.request;
  1416. params.set("path", request.path.join(','));
  1417. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1418. if (index >= 0) {
  1419. const host = request.headers[index].value;
  1420. params.set("host", host);
  1421. }
  1422. params.set("headerType", 'http');
  1423. }
  1424. break;
  1425. case "kcp":
  1426. const kcp = this.stream.kcp;
  1427. params.set("headerType", kcp.type);
  1428. params.set("seed", kcp.seed);
  1429. break;
  1430. case "ws":
  1431. const ws = this.stream.ws;
  1432. params.set("path", ws.path);
  1433. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1434. break;
  1435. case "http":
  1436. const http = this.stream.http;
  1437. params.set("path", http.path);
  1438. params.set("host", http.host);
  1439. break;
  1440. case "grpc":
  1441. const grpc = this.stream.grpc;
  1442. params.set("serviceName", grpc.serviceName);
  1443. params.set("authority", grpc.authority);
  1444. if (grpc.multiMode) {
  1445. params.set("mode", "multi");
  1446. }
  1447. break;
  1448. case "httpupgrade":
  1449. const httpupgrade = this.stream.httpupgrade;
  1450. params.set("path", httpupgrade.path);
  1451. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1452. break;
  1453. case "splithttp":
  1454. const splithttp = this.stream.splithttp;
  1455. params.set("path", splithttp.path);
  1456. params.set("host", splithttp.host?.length > 0 ? splithttp.host : this.getHeader(splithttp, 'host'));
  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 (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1468. params.set("sni", this.stream.tls.sni);
  1469. }
  1470. }
  1471. }
  1472. else if (security === 'reality') {
  1473. params.set("security", "reality");
  1474. params.set("pbk", this.stream.reality.settings.publicKey);
  1475. params.set("fp", this.stream.reality.settings.fingerprint);
  1476. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1477. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1478. }
  1479. if (this.stream.reality.shortIds.length > 0) {
  1480. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1481. }
  1482. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1483. params.set("spx", this.stream.reality.settings.spiderX);
  1484. }
  1485. }
  1486. else {
  1487. params.set("security", "none");
  1488. }
  1489. const link = `trojan://${clientPassword}@${address}:${port}`;
  1490. const url = new URL(link);
  1491. for (const [key, value] of params) {
  1492. url.searchParams.set(key, value)
  1493. }
  1494. url.hash = encodeURIComponent(remark);
  1495. return url.toString();
  1496. }
  1497. getWireguardLink(address, port, remark, peerId) {
  1498. let txt = `[Interface]\n`
  1499. txt += `PrivateKey = ${this.settings.peers[peerId].privateKey}\n`
  1500. txt += `Address = ${this.settings.peers[peerId].allowedIPs[0]}\n`
  1501. txt += `DNS = 1.1.1.1, 1.0.0.1\n`
  1502. if (this.settings.mtu) {
  1503. txt += `MTU = ${this.settings.mtu}\n`
  1504. }
  1505. txt += `\n# ${remark}\n`
  1506. txt += `[Peer]\n`
  1507. txt += `PublicKey = ${this.settings.pubKey}\n`
  1508. txt += `AllowedIPs = 0.0.0.0/0, ::/0\n`
  1509. txt += `Endpoint = ${address}:${port}`
  1510. if (this.settings.peers[peerId].psk) {
  1511. txt += `\nPresharedKey = ${this.settings.peers[peerId].psk}`
  1512. }
  1513. if (this.settings.peers[peerId].keepAlive) {
  1514. txt += `\nPersistentKeepalive = ${this.settings.peers[peerId].keepAlive}\n`
  1515. }
  1516. return txt;
  1517. }
  1518. genLink(address = '', port = this.port, forceTls = 'same', remark = '', client) {
  1519. switch (this.protocol) {
  1520. case Protocols.VMESS:
  1521. return this.genVmessLink(address, port, forceTls, remark, client.id, client.security);
  1522. case Protocols.VLESS:
  1523. return this.genVLESSLink(address, port, forceTls, remark, client.id, client.flow);
  1524. case Protocols.SHADOWSOCKS:
  1525. return this.genSSLink(address, port, forceTls, remark, this.isSSMultiUser ? client.password : '');
  1526. case Protocols.TROJAN:
  1527. return this.genTrojanLink(address, port, forceTls, remark, client.password);
  1528. default: return '';
  1529. }
  1530. }
  1531. genAllLinks(remark = '', remarkModel = '-ieo', client) {
  1532. let result = [];
  1533. let email = client ? client.email : '';
  1534. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1535. let port = this.port;
  1536. const separationChar = remarkModel.charAt(0);
  1537. const orderChars = remarkModel.slice(1);
  1538. let orders = {
  1539. 'i': remark,
  1540. 'e': email,
  1541. 'o': '',
  1542. };
  1543. if (ObjectUtil.isArrEmpty(this.stream.externalProxy)) {
  1544. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1545. result.push({
  1546. remark: r,
  1547. link: this.genLink(addr, port, 'same', r, client)
  1548. });
  1549. } else {
  1550. this.stream.externalProxy.forEach((ep) => {
  1551. orders['o'] = ep.remark;
  1552. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1553. result.push({
  1554. remark: r,
  1555. link: this.genLink(ep.dest, ep.port, ep.forceTls, r, client)
  1556. });
  1557. });
  1558. }
  1559. return result;
  1560. }
  1561. genInboundLinks(remark = '', remarkModel = '-ieo') {
  1562. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1563. if (this.clients) {
  1564. let links = [];
  1565. this.clients.forEach((client) => {
  1566. this.genAllLinks(remark, remarkModel, client).forEach(l => {
  1567. links.push(l.link);
  1568. })
  1569. });
  1570. return links.join('\r\n');
  1571. } else {
  1572. if (this.protocol == Protocols.SHADOWSOCKS && !this.isSSMultiUser) return this.genSSLink(addr, this.port, 'same', remark);
  1573. if (this.protocol == Protocols.WIREGUARD) {
  1574. let links = [];
  1575. this.settings.peers.forEach((p, index) => {
  1576. links.push(this.getWireguardLink(addr, this.port, remark + remarkModel.charAt(0) + (index + 1), index));
  1577. });
  1578. return links.join('\r\n');
  1579. }
  1580. return '';
  1581. }
  1582. }
  1583. static fromJson(json = {}) {
  1584. return new Inbound(
  1585. json.port,
  1586. json.listen,
  1587. json.protocol,
  1588. Inbound.Settings.fromJson(json.protocol, json.settings),
  1589. StreamSettings.fromJson(json.streamSettings),
  1590. json.tag,
  1591. Sniffing.fromJson(json.sniffing),
  1592. Allocate.fromJson(json.allocate),
  1593. json.clientStats
  1594. )
  1595. }
  1596. toJson() {
  1597. let streamSettings;
  1598. if (this.canEnableStream()) {
  1599. streamSettings = this.stream.toJson();
  1600. }
  1601. return {
  1602. port: this.port,
  1603. listen: this.listen,
  1604. protocol: this.protocol,
  1605. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1606. streamSettings: streamSettings,
  1607. tag: this.tag,
  1608. sniffing: this.sniffing.toJson(),
  1609. allocate: this.allocate.toJson(),
  1610. clientStats: this.clientStats
  1611. };
  1612. }
  1613. }
  1614. Inbound.Settings = class extends XrayCommonClass {
  1615. constructor(protocol) {
  1616. super();
  1617. this.protocol = protocol;
  1618. }
  1619. static getSettings(protocol) {
  1620. switch (protocol) {
  1621. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1622. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1623. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1624. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1625. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1626. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1627. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1628. case Protocols.WIREGUARD: return new Inbound.WireguardSettings(protocol);
  1629. default: return null;
  1630. }
  1631. }
  1632. static fromJson(protocol, json) {
  1633. switch (protocol) {
  1634. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1635. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1636. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1637. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1638. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1639. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1640. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1641. case Protocols.WIREGUARD: return Inbound.WireguardSettings.fromJson(json);
  1642. default: return null;
  1643. }
  1644. }
  1645. toJson() {
  1646. return {};
  1647. }
  1648. };
  1649. Inbound.VmessSettings = class extends Inbound.Settings {
  1650. constructor(protocol,
  1651. vmesses = [new Inbound.VmessSettings.VMESS()]) {
  1652. super(protocol);
  1653. this.vmesses = vmesses;
  1654. }
  1655. indexOfVmessById(id) {
  1656. return this.vmesses.findIndex(VMESS => VMESS.id === id);
  1657. }
  1658. addVmess(VMESS) {
  1659. if (this.indexOfVmessById(VMESS.id) >= 0) {
  1660. return false;
  1661. }
  1662. this.vmesses.push(VMESS);
  1663. }
  1664. delVmess(VMESS) {
  1665. const i = this.indexOfVmessById(VMESS.id);
  1666. if (i >= 0) {
  1667. this.vmesses.splice(i, 1);
  1668. }
  1669. }
  1670. static fromJson(json = {}) {
  1671. return new Inbound.VmessSettings(
  1672. Protocols.VMESS,
  1673. json.clients.map(client => Inbound.VmessSettings.VMESS.fromJson(client)),
  1674. );
  1675. }
  1676. toJson() {
  1677. return {
  1678. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1679. };
  1680. }
  1681. };
  1682. Inbound.VmessSettings.VMESS = class extends XrayCommonClass {
  1683. constructor(
  1684. id = RandomUtil.randomUUID(),
  1685. security = USERS_SECURITY.AUTO,
  1686. email = RandomUtil.randomLowerAndNum(8),
  1687. limitIp = 0,
  1688. totalGB = 0,
  1689. expiryTime = 0,
  1690. enable = true,
  1691. tgId = '',
  1692. subId = RandomUtil.randomLowerAndNum(16),
  1693. reset = 0
  1694. ) {
  1695. super();
  1696. this.id = id;
  1697. this.security = security;
  1698. this.email = email;
  1699. this.limitIp = limitIp;
  1700. this.totalGB = totalGB;
  1701. this.expiryTime = expiryTime;
  1702. this.enable = enable;
  1703. this.tgId = tgId;
  1704. this.subId = subId;
  1705. this.reset = reset;
  1706. }
  1707. static fromJson(json = {}) {
  1708. return new Inbound.VmessSettings.VMESS(
  1709. json.id,
  1710. json.security,
  1711. json.email,
  1712. json.limitIp,
  1713. json.totalGB,
  1714. json.expiryTime,
  1715. json.enable,
  1716. json.tgId,
  1717. json.subId,
  1718. json.reset,
  1719. );
  1720. }
  1721. get _expiryTime() {
  1722. if (this.expiryTime === 0 || this.expiryTime === "") {
  1723. return null;
  1724. }
  1725. if (this.expiryTime < 0) {
  1726. return this.expiryTime / -86400000;
  1727. }
  1728. return moment(this.expiryTime);
  1729. }
  1730. set _expiryTime(t) {
  1731. if (t == null || t === "") {
  1732. this.expiryTime = 0;
  1733. } else {
  1734. this.expiryTime = t.valueOf();
  1735. }
  1736. }
  1737. get _totalGB() {
  1738. return toFixed(this.totalGB / ONE_GB, 2);
  1739. }
  1740. set _totalGB(gb) {
  1741. this.totalGB = toFixed(gb * ONE_GB, 0);
  1742. }
  1743. };
  1744. Inbound.VLESSSettings = class extends Inbound.Settings {
  1745. constructor(
  1746. protocol,
  1747. vlesses = [new Inbound.VLESSSettings.VLESS()],
  1748. decryption = 'none',
  1749. fallbacks = []
  1750. ) {
  1751. super(protocol);
  1752. this.vlesses = vlesses;
  1753. this.decryption = decryption;
  1754. this.fallbacks = fallbacks;
  1755. }
  1756. addFallback() {
  1757. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1758. }
  1759. delFallback(index) {
  1760. this.fallbacks.splice(index, 1);
  1761. }
  1762. // decryption should be set to static value
  1763. static fromJson(json = {}) {
  1764. return new Inbound.VLESSSettings(
  1765. Protocols.VLESS,
  1766. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1767. json.decryption || 'none',
  1768. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),);
  1769. }
  1770. toJson() {
  1771. return {
  1772. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1773. decryption: this.decryption,
  1774. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1775. };
  1776. }
  1777. };
  1778. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1779. constructor(
  1780. id = RandomUtil.randomUUID(),
  1781. flow = '',
  1782. email = RandomUtil.randomLowerAndNum(8),
  1783. limitIp = 0,
  1784. totalGB = 0,
  1785. expiryTime = 0,
  1786. enable = true,
  1787. tgId = '',
  1788. subId = RandomUtil.randomLowerAndNum(16),
  1789. reset = 0
  1790. ) {
  1791. super();
  1792. this.id = id;
  1793. this.flow = flow;
  1794. this.email = email;
  1795. this.limitIp = limitIp;
  1796. this.totalGB = totalGB;
  1797. this.expiryTime = expiryTime;
  1798. this.enable = enable;
  1799. this.tgId = tgId;
  1800. this.subId = subId;
  1801. this.reset = reset;
  1802. }
  1803. static fromJson(json = {}) {
  1804. return new Inbound.VLESSSettings.VLESS(
  1805. json.id,
  1806. json.flow,
  1807. json.email,
  1808. json.limitIp,
  1809. json.totalGB,
  1810. json.expiryTime,
  1811. json.enable,
  1812. json.tgId,
  1813. json.subId,
  1814. json.reset,
  1815. );
  1816. }
  1817. get _expiryTime() {
  1818. if (this.expiryTime === 0 || this.expiryTime === "") {
  1819. return null;
  1820. }
  1821. if (this.expiryTime < 0) {
  1822. return this.expiryTime / -86400000;
  1823. }
  1824. return moment(this.expiryTime);
  1825. }
  1826. set _expiryTime(t) {
  1827. if (t == null || t === "") {
  1828. this.expiryTime = 0;
  1829. } else {
  1830. this.expiryTime = t.valueOf();
  1831. }
  1832. }
  1833. get _totalGB() {
  1834. return toFixed(this.totalGB / ONE_GB, 2);
  1835. }
  1836. set _totalGB(gb) {
  1837. this.totalGB = toFixed(gb * ONE_GB, 0);
  1838. }
  1839. };
  1840. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1841. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1842. super();
  1843. this.name = name;
  1844. this.alpn = alpn;
  1845. this.path = path;
  1846. this.dest = dest;
  1847. this.xver = xver;
  1848. }
  1849. toJson() {
  1850. let xver = this.xver;
  1851. if (!Number.isInteger(xver)) {
  1852. xver = 0;
  1853. }
  1854. return {
  1855. name: this.name,
  1856. alpn: this.alpn,
  1857. path: this.path,
  1858. dest: this.dest,
  1859. xver: xver,
  1860. }
  1861. }
  1862. static fromJson(json = []) {
  1863. const fallbacks = [];
  1864. for (let fallback of json) {
  1865. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1866. fallback.name,
  1867. fallback.alpn,
  1868. fallback.path,
  1869. fallback.dest,
  1870. fallback.xver,
  1871. ))
  1872. }
  1873. return fallbacks;
  1874. }
  1875. };
  1876. Inbound.TrojanSettings = class extends Inbound.Settings {
  1877. constructor(protocol,
  1878. trojans = [new Inbound.TrojanSettings.Trojan()],
  1879. fallbacks = [],) {
  1880. super(protocol);
  1881. this.trojans = trojans;
  1882. this.fallbacks = fallbacks;
  1883. }
  1884. addFallback() {
  1885. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1886. }
  1887. delFallback(index) {
  1888. this.fallbacks.splice(index, 1);
  1889. }
  1890. static fromJson(json = {}) {
  1891. return new Inbound.TrojanSettings(
  1892. Protocols.TROJAN,
  1893. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1894. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1895. }
  1896. toJson() {
  1897. return {
  1898. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1899. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks)
  1900. };
  1901. }
  1902. };
  1903. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1904. constructor(
  1905. password = RandomUtil.randomSeq(10),
  1906. email = RandomUtil.randomLowerAndNum(8),
  1907. limitIp = 0,
  1908. totalGB = 0,
  1909. expiryTime = 0,
  1910. enable = true,
  1911. tgId = '',
  1912. subId = RandomUtil.randomLowerAndNum(16),
  1913. reset = 0
  1914. ) {
  1915. super();
  1916. this.password = password;
  1917. this.email = email;
  1918. this.limitIp = limitIp;
  1919. this.totalGB = totalGB;
  1920. this.expiryTime = expiryTime;
  1921. this.enable = enable;
  1922. this.tgId = tgId;
  1923. this.subId = subId;
  1924. this.reset = reset;
  1925. }
  1926. toJson() {
  1927. return {
  1928. password: this.password,
  1929. email: this.email,
  1930. limitIp: this.limitIp,
  1931. totalGB: this.totalGB,
  1932. expiryTime: this.expiryTime,
  1933. enable: this.enable,
  1934. tgId: this.tgId,
  1935. subId: this.subId,
  1936. reset: this.reset,
  1937. };
  1938. }
  1939. static fromJson(json = {}) {
  1940. return new Inbound.TrojanSettings.Trojan(
  1941. json.password,
  1942. json.email,
  1943. json.limitIp,
  1944. json.totalGB,
  1945. json.expiryTime,
  1946. json.enable,
  1947. json.tgId,
  1948. json.subId,
  1949. json.reset,
  1950. );
  1951. }
  1952. get _expiryTime() {
  1953. if (this.expiryTime === 0 || this.expiryTime === "") {
  1954. return null;
  1955. }
  1956. if (this.expiryTime < 0) {
  1957. return this.expiryTime / -86400000;
  1958. }
  1959. return moment(this.expiryTime);
  1960. }
  1961. set _expiryTime(t) {
  1962. if (t == null || t === "") {
  1963. this.expiryTime = 0;
  1964. } else {
  1965. this.expiryTime = t.valueOf();
  1966. }
  1967. }
  1968. get _totalGB() {
  1969. return toFixed(this.totalGB / ONE_GB, 2);
  1970. }
  1971. set _totalGB(gb) {
  1972. this.totalGB = toFixed(gb * ONE_GB, 0);
  1973. }
  1974. };
  1975. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1976. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1977. super();
  1978. this.name = name;
  1979. this.alpn = alpn;
  1980. this.path = path;
  1981. this.dest = dest;
  1982. this.xver = xver;
  1983. }
  1984. toJson() {
  1985. let xver = this.xver;
  1986. if (!Number.isInteger(xver)) {
  1987. xver = 0;
  1988. }
  1989. return {
  1990. name: this.name,
  1991. alpn: this.alpn,
  1992. path: this.path,
  1993. dest: this.dest,
  1994. xver: xver,
  1995. }
  1996. }
  1997. static fromJson(json = []) {
  1998. const fallbacks = [];
  1999. for (let fallback of json) {
  2000. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  2001. fallback.name,
  2002. fallback.alpn,
  2003. fallback.path,
  2004. fallback.dest,
  2005. fallback.xver,
  2006. ))
  2007. }
  2008. return fallbacks;
  2009. }
  2010. };
  2011. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  2012. constructor(protocol,
  2013. method = SSMethods.BLAKE3_AES_256_GCM,
  2014. password = RandomUtil.randomShadowsocksPassword(),
  2015. network = 'tcp,udp',
  2016. shadowsockses = [new Inbound.ShadowsocksSettings.Shadowsocks()],
  2017. ivCheck = false,
  2018. ) {
  2019. super(protocol);
  2020. this.method = method;
  2021. this.password = password;
  2022. this.network = network;
  2023. this.shadowsockses = shadowsockses;
  2024. this.ivCheck = ivCheck;
  2025. }
  2026. static fromJson(json = {}) {
  2027. return new Inbound.ShadowsocksSettings(
  2028. Protocols.SHADOWSOCKS,
  2029. json.method,
  2030. json.password,
  2031. json.network,
  2032. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  2033. json.ivCheck,
  2034. );
  2035. }
  2036. toJson() {
  2037. return {
  2038. method: this.method,
  2039. password: this.password,
  2040. network: this.network,
  2041. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses),
  2042. ivCheck: this.ivCheck,
  2043. };
  2044. }
  2045. };
  2046. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  2047. constructor(
  2048. method = '',
  2049. password = RandomUtil.randomShadowsocksPassword(),
  2050. email = RandomUtil.randomLowerAndNum(8),
  2051. limitIp = 0,
  2052. totalGB = 0,
  2053. expiryTime = 0,
  2054. enable = true,
  2055. tgId = '',
  2056. subId = RandomUtil.randomLowerAndNum(16),
  2057. reset = 0
  2058. ) {
  2059. super();
  2060. this.method = method;
  2061. this.password = password;
  2062. this.email = email;
  2063. this.limitIp = limitIp;
  2064. this.totalGB = totalGB;
  2065. this.expiryTime = expiryTime;
  2066. this.enable = enable;
  2067. this.tgId = tgId;
  2068. this.subId = subId;
  2069. this.reset = reset;
  2070. }
  2071. toJson() {
  2072. return {
  2073. method: this.method,
  2074. password: this.password,
  2075. email: this.email,
  2076. limitIp: this.limitIp,
  2077. totalGB: this.totalGB,
  2078. expiryTime: this.expiryTime,
  2079. enable: this.enable,
  2080. tgId: this.tgId,
  2081. subId: this.subId,
  2082. reset: this.reset,
  2083. };
  2084. }
  2085. static fromJson(json = {}) {
  2086. return new Inbound.ShadowsocksSettings.Shadowsocks(
  2087. json.method,
  2088. json.password,
  2089. json.email,
  2090. json.limitIp,
  2091. json.totalGB,
  2092. json.expiryTime,
  2093. json.enable,
  2094. json.tgId,
  2095. json.subId,
  2096. json.reset,
  2097. );
  2098. }
  2099. get _expiryTime() {
  2100. if (this.expiryTime === 0 || this.expiryTime === "") {
  2101. return null;
  2102. }
  2103. if (this.expiryTime < 0) {
  2104. return this.expiryTime / -86400000;
  2105. }
  2106. return moment(this.expiryTime);
  2107. }
  2108. set _expiryTime(t) {
  2109. if (t == null || t === "") {
  2110. this.expiryTime = 0;
  2111. } else {
  2112. this.expiryTime = t.valueOf();
  2113. }
  2114. }
  2115. get _totalGB() {
  2116. return toFixed(this.totalGB / ONE_GB, 2);
  2117. }
  2118. set _totalGB(gb) {
  2119. this.totalGB = toFixed(gb * ONE_GB, 0);
  2120. }
  2121. };
  2122. Inbound.DokodemoSettings = class extends Inbound.Settings {
  2123. constructor(
  2124. protocol,
  2125. address,
  2126. port,
  2127. network = 'tcp,udp',
  2128. followRedirect = false
  2129. ) {
  2130. super(protocol);
  2131. this.address = address;
  2132. this.port = port;
  2133. this.network = network;
  2134. this.followRedirect = followRedirect;
  2135. }
  2136. static fromJson(json = {}) {
  2137. return new Inbound.DokodemoSettings(
  2138. Protocols.DOKODEMO,
  2139. json.address,
  2140. json.port,
  2141. json.network,
  2142. json.followRedirect,
  2143. );
  2144. }
  2145. toJson() {
  2146. return {
  2147. address: this.address,
  2148. port: this.port,
  2149. network: this.network,
  2150. followRedirect: this.followRedirect,
  2151. };
  2152. }
  2153. };
  2154. Inbound.SocksSettings = class extends Inbound.Settings {
  2155. constructor(protocol, auth = 'password', accounts = [new Inbound.SocksSettings.SocksAccount()], udp = false, ip = '127.0.0.1') {
  2156. super(protocol);
  2157. this.auth = auth;
  2158. this.accounts = accounts;
  2159. this.udp = udp;
  2160. this.ip = ip;
  2161. }
  2162. addAccount(account) {
  2163. this.accounts.push(account);
  2164. }
  2165. delAccount(index) {
  2166. this.accounts.splice(index, 1);
  2167. }
  2168. static fromJson(json = {}) {
  2169. let accounts;
  2170. if (json.auth === 'password') {
  2171. accounts = json.accounts.map(
  2172. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  2173. )
  2174. }
  2175. return new Inbound.SocksSettings(
  2176. Protocols.SOCKS,
  2177. json.auth,
  2178. accounts,
  2179. json.udp,
  2180. json.ip,
  2181. );
  2182. }
  2183. toJson() {
  2184. return {
  2185. auth: this.auth,
  2186. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  2187. udp: this.udp,
  2188. ip: this.ip,
  2189. };
  2190. }
  2191. };
  2192. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  2193. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2194. super();
  2195. this.user = user;
  2196. this.pass = pass;
  2197. }
  2198. static fromJson(json = {}) {
  2199. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  2200. }
  2201. };
  2202. Inbound.HttpSettings = class extends Inbound.Settings {
  2203. constructor(
  2204. protocol,
  2205. accounts = [new Inbound.HttpSettings.HttpAccount()],
  2206. allowTransparent = false,
  2207. ) {
  2208. super(protocol);
  2209. this.accounts = accounts;
  2210. this.allowTransparent = allowTransparent;
  2211. }
  2212. addAccount(account) {
  2213. this.accounts.push(account);
  2214. }
  2215. delAccount(index) {
  2216. this.accounts.splice(index, 1);
  2217. }
  2218. static fromJson(json = {}) {
  2219. return new Inbound.HttpSettings(
  2220. Protocols.HTTP,
  2221. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  2222. json.allowTransparent,
  2223. );
  2224. }
  2225. toJson() {
  2226. return {
  2227. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2228. allowTransparent: this.allowTransparent,
  2229. };
  2230. }
  2231. };
  2232. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2233. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2234. super();
  2235. this.user = user;
  2236. this.pass = pass;
  2237. }
  2238. static fromJson(json = {}) {
  2239. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2240. }
  2241. };
  2242. Inbound.WireguardSettings = class extends XrayCommonClass {
  2243. constructor(
  2244. protocol,
  2245. mtu = 1420,
  2246. secretKey = Wireguard.generateKeypair().privateKey,
  2247. peers = [new Inbound.WireguardSettings.Peer()],
  2248. noKernelTun = false
  2249. ) {
  2250. super(protocol);
  2251. this.mtu = mtu;
  2252. this.secretKey = secretKey;
  2253. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  2254. this.peers = peers;
  2255. this.noKernelTun = noKernelTun;
  2256. }
  2257. addPeer() {
  2258. this.peers.push(new Inbound.WireguardSettings.Peer(null, null, '', ['10.0.0.' + (this.peers.length + 2)]));
  2259. }
  2260. delPeer(index) {
  2261. this.peers.splice(index, 1);
  2262. }
  2263. static fromJson(json = {}) {
  2264. return new Inbound.WireguardSettings(
  2265. Protocols.WIREGUARD,
  2266. json.mtu,
  2267. json.secretKey,
  2268. json.peers.map(peer => Inbound.WireguardSettings.Peer.fromJson(peer)),
  2269. json.noKernelTun,
  2270. );
  2271. }
  2272. toJson() {
  2273. return {
  2274. mtu: this.mtu ?? undefined,
  2275. secretKey: this.secretKey,
  2276. peers: Inbound.WireguardSettings.Peer.toJsonArray(this.peers),
  2277. noKernelTun: this.noKernelTun,
  2278. };
  2279. }
  2280. };
  2281. Inbound.WireguardSettings.Peer = class extends XrayCommonClass {
  2282. constructor(privateKey, publicKey, psk = '', allowedIPs = ['10.0.0.2/32'], keepAlive = 0) {
  2283. super();
  2284. this.privateKey = privateKey
  2285. this.publicKey = publicKey;
  2286. if (!this.publicKey) {
  2287. [this.publicKey, this.privateKey] = Object.values(Wireguard.generateKeypair())
  2288. }
  2289. this.psk = psk;
  2290. allowedIPs.forEach((a, index) => {
  2291. if (a.length > 0 && !a.includes('/')) allowedIPs[index] += '/32';
  2292. })
  2293. this.allowedIPs = allowedIPs;
  2294. this.keepAlive = keepAlive;
  2295. }
  2296. static fromJson(json = {}) {
  2297. return new Inbound.WireguardSettings.Peer(
  2298. json.privateKey,
  2299. json.publicKey,
  2300. json.preSharedKey,
  2301. json.allowedIPs,
  2302. json.keepAlive
  2303. );
  2304. }
  2305. toJson() {
  2306. this.allowedIPs.forEach((a, index) => {
  2307. if (a.length > 0 && !a.includes('/')) this.allowedIPs[index] += '/32';
  2308. });
  2309. return {
  2310. privateKey: this.privateKey,
  2311. publicKey: this.publicKey,
  2312. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  2313. allowedIPs: this.allowedIPs,
  2314. keepAlive: this.keepAlive ?? undefined,
  2315. };
  2316. }
  2317. };