inbound.js 76 KB

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