1
0

inbound.js 76 KB

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