inbound.js 76 KB

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