inbound.js 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  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. oneTimeLoading = false,
  582. usage = USAGE_OPTION.ENCIPHERMENT,
  583. buildChain = false,
  584. ) {
  585. super();
  586. this.useFile = useFile;
  587. this.certFile = certificateFile;
  588. this.keyFile = keyFile;
  589. this.cert = Array.isArray(certificate) ? certificate.join('\n') : certificate;
  590. this.key = Array.isArray(key) ? key.join('\n') : key;
  591. this.oneTimeLoading = oneTimeLoading;
  592. this.usage = usage;
  593. this.buildChain = buildChain
  594. }
  595. static fromJson(json = {}) {
  596. if ('certificateFile' in json && 'keyFile' in json) {
  597. return new TlsStreamSettings.Cert(
  598. true,
  599. json.certificateFile,
  600. json.keyFile, '', '',
  601. json.oneTimeLoading,
  602. json.usage,
  603. json.buildChain,
  604. );
  605. } else {
  606. return new TlsStreamSettings.Cert(
  607. false, '', '',
  608. json.certificate.join('\n'),
  609. json.key.join('\n'),
  610. json.oneTimeLoading,
  611. json.usage,
  612. json.buildChain,
  613. );
  614. }
  615. }
  616. toJson() {
  617. if (this.useFile) {
  618. return {
  619. certificateFile: this.certFile,
  620. keyFile: this.keyFile,
  621. oneTimeLoading: this.oneTimeLoading,
  622. usage: this.usage,
  623. buildChain: this.buildChain,
  624. };
  625. } else {
  626. return {
  627. certificate: this.cert.split('\n'),
  628. key: this.key.split('\n'),
  629. oneTimeLoading: this.oneTimeLoading,
  630. usage: this.usage,
  631. buildChain: this.buildChain,
  632. };
  633. }
  634. }
  635. };
  636. TlsStreamSettings.Settings = class extends XrayCommonClass {
  637. constructor(
  638. allowInsecure = false,
  639. fingerprint = UTLS_FINGERPRINT.UTLS_CHROME,
  640. echConfigList = '',
  641. ) {
  642. super();
  643. this.allowInsecure = allowInsecure;
  644. this.fingerprint = fingerprint;
  645. this.echConfigList = echConfigList;
  646. }
  647. static fromJson(json = {}) {
  648. return new TlsStreamSettings.Settings(
  649. json.allowInsecure,
  650. json.fingerprint,
  651. json.echConfigList,
  652. );
  653. }
  654. toJson() {
  655. return {
  656. allowInsecure: this.allowInsecure,
  657. fingerprint: this.fingerprint,
  658. echConfigList: this.echConfigList
  659. };
  660. }
  661. };
  662. class RealityStreamSettings extends XrayCommonClass {
  663. constructor(
  664. show = false,
  665. xver = 0,
  666. dest = 'google.com:443',
  667. serverNames = 'google.com,www.google.com',
  668. privateKey = '',
  669. minClientVer = '',
  670. maxClientVer = '',
  671. maxTimediff = 0,
  672. shortIds = RandomUtil.randomShortIds(),
  673. mldsa65Seed = '',
  674. settings = new RealityStreamSettings.Settings()
  675. ) {
  676. super();
  677. this.show = show;
  678. this.xver = xver;
  679. this.dest = dest;
  680. this.serverNames = Array.isArray(serverNames) ? serverNames.join(",") : serverNames;
  681. this.privateKey = privateKey;
  682. this.minClientVer = minClientVer;
  683. this.maxClientVer = maxClientVer;
  684. this.maxTimediff = maxTimediff;
  685. this.shortIds = Array.isArray(shortIds) ? shortIds.join(",") : shortIds;
  686. this.mldsa65Seed = mldsa65Seed;
  687. this.settings = settings;
  688. }
  689. static fromJson(json = {}) {
  690. let settings;
  691. if (!ObjectUtil.isEmpty(json.settings)) {
  692. settings = new RealityStreamSettings.Settings(
  693. json.settings.publicKey,
  694. json.settings.fingerprint,
  695. json.settings.serverName,
  696. json.settings.spiderX,
  697. json.settings.mldsa65Verify,
  698. );
  699. }
  700. return new RealityStreamSettings(
  701. json.show,
  702. json.xver,
  703. json.dest,
  704. json.serverNames,
  705. json.privateKey,
  706. json.minClientVer,
  707. json.maxClientVer,
  708. json.maxTimediff,
  709. json.shortIds,
  710. json.mldsa65Seed,
  711. settings,
  712. );
  713. }
  714. toJson() {
  715. return {
  716. show: this.show,
  717. xver: this.xver,
  718. dest: this.dest,
  719. serverNames: this.serverNames.split(","),
  720. privateKey: this.privateKey,
  721. minClientVer: this.minClientVer,
  722. maxClientVer: this.maxClientVer,
  723. maxTimediff: this.maxTimediff,
  724. shortIds: this.shortIds.split(","),
  725. mldsa65Seed: this.mldsa65Seed,
  726. settings: this.settings,
  727. };
  728. }
  729. }
  730. RealityStreamSettings.Settings = class extends XrayCommonClass {
  731. constructor(
  732. publicKey = '',
  733. fingerprint = UTLS_FINGERPRINT.UTLS_CHROME,
  734. serverName = '',
  735. spiderX = '/',
  736. mldsa65Verify = ''
  737. ) {
  738. super();
  739. this.publicKey = publicKey;
  740. this.fingerprint = fingerprint;
  741. this.serverName = serverName;
  742. this.spiderX = spiderX;
  743. this.mldsa65Verify = mldsa65Verify;
  744. }
  745. static fromJson(json = {}) {
  746. return new RealityStreamSettings.Settings(
  747. json.publicKey,
  748. json.fingerprint,
  749. json.serverName,
  750. json.spiderX,
  751. json.mldsa65Verify
  752. );
  753. }
  754. toJson() {
  755. return {
  756. publicKey: this.publicKey,
  757. fingerprint: this.fingerprint,
  758. serverName: this.serverName,
  759. spiderX: this.spiderX,
  760. mldsa65Verify: this.mldsa65Verify
  761. };
  762. }
  763. };
  764. class SockoptStreamSettings extends XrayCommonClass {
  765. constructor(
  766. acceptProxyProtocol = false,
  767. tcpFastOpen = false,
  768. mark = 0,
  769. tproxy = "off",
  770. tcpMptcp = false,
  771. penetrate = false,
  772. domainStrategy = DOMAIN_STRATEGY_OPTION.USE_IP,
  773. tcpMaxSeg = 1440,
  774. dialerProxy = "",
  775. tcpKeepAliveInterval = 0,
  776. tcpKeepAliveIdle = 300,
  777. tcpUserTimeout = 10000,
  778. tcpcongestion = TCP_CONGESTION_OPTION.BBR,
  779. V6Only = false,
  780. tcpWindowClamp = 600,
  781. interfaceName = "",
  782. ) {
  783. super();
  784. this.acceptProxyProtocol = acceptProxyProtocol;
  785. this.tcpFastOpen = tcpFastOpen;
  786. this.mark = mark;
  787. this.tproxy = tproxy;
  788. this.tcpMptcp = tcpMptcp;
  789. this.penetrate = penetrate;
  790. this.domainStrategy = domainStrategy;
  791. this.tcpMaxSeg = tcpMaxSeg;
  792. this.dialerProxy = dialerProxy;
  793. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  794. this.tcpKeepAliveIdle = tcpKeepAliveIdle;
  795. this.tcpUserTimeout = tcpUserTimeout;
  796. this.tcpcongestion = tcpcongestion;
  797. this.V6Only = V6Only;
  798. this.tcpWindowClamp = tcpWindowClamp;
  799. this.interfaceName = interfaceName;
  800. }
  801. static fromJson(json = {}) {
  802. if (Object.keys(json).length === 0) return undefined;
  803. return new SockoptStreamSettings(
  804. json.acceptProxyProtocol,
  805. json.tcpFastOpen,
  806. json.mark,
  807. json.tproxy,
  808. json.tcpMptcp,
  809. json.penetrate,
  810. json.domainStrategy,
  811. json.tcpMaxSeg,
  812. json.dialerProxy,
  813. json.tcpKeepAliveInterval,
  814. json.tcpKeepAliveIdle,
  815. json.tcpUserTimeout,
  816. json.tcpcongestion,
  817. json.V6Only,
  818. json.tcpWindowClamp,
  819. json.interface,
  820. );
  821. }
  822. toJson() {
  823. return {
  824. acceptProxyProtocol: this.acceptProxyProtocol,
  825. tcpFastOpen: this.tcpFastOpen,
  826. mark: this.mark,
  827. tproxy: this.tproxy,
  828. tcpMptcp: this.tcpMptcp,
  829. penetrate: this.penetrate,
  830. domainStrategy: this.domainStrategy,
  831. tcpMaxSeg: this.tcpMaxSeg,
  832. dialerProxy: this.dialerProxy,
  833. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  834. tcpKeepAliveIdle: this.tcpKeepAliveIdle,
  835. tcpUserTimeout: this.tcpUserTimeout,
  836. tcpcongestion: this.tcpcongestion,
  837. V6Only: this.V6Only,
  838. tcpWindowClamp: this.tcpWindowClamp,
  839. interface: this.interfaceName,
  840. };
  841. }
  842. }
  843. class StreamSettings extends XrayCommonClass {
  844. constructor(network = 'tcp',
  845. security = 'none',
  846. externalProxy = [],
  847. tlsSettings = new TlsStreamSettings(),
  848. realitySettings = new RealityStreamSettings(),
  849. tcpSettings = new TcpStreamSettings(),
  850. kcpSettings = new KcpStreamSettings(),
  851. wsSettings = new WsStreamSettings(),
  852. grpcSettings = new GrpcStreamSettings(),
  853. httpupgradeSettings = new HTTPUpgradeStreamSettings(),
  854. xhttpSettings = new xHTTPStreamSettings(),
  855. sockopt = undefined,
  856. ) {
  857. super();
  858. this.network = network;
  859. this.security = security;
  860. this.externalProxy = externalProxy;
  861. this.tls = tlsSettings;
  862. this.reality = realitySettings;
  863. this.tcp = tcpSettings;
  864. this.kcp = kcpSettings;
  865. this.ws = wsSettings;
  866. this.grpc = grpcSettings;
  867. this.httpupgrade = httpupgradeSettings;
  868. this.xhttp = xhttpSettings;
  869. this.sockopt = sockopt;
  870. }
  871. get isTls() {
  872. return this.security === "tls";
  873. }
  874. set isTls(isTls) {
  875. if (isTls) {
  876. this.security = 'tls';
  877. } else {
  878. this.security = 'none';
  879. }
  880. }
  881. //for Reality
  882. get isReality() {
  883. return this.security === "reality";
  884. }
  885. set isReality(isReality) {
  886. if (isReality) {
  887. this.security = 'reality';
  888. } else {
  889. this.security = 'none';
  890. }
  891. }
  892. get sockoptSwitch() {
  893. return this.sockopt != undefined;
  894. }
  895. set sockoptSwitch(value) {
  896. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  897. }
  898. static fromJson(json = {}) {
  899. return new StreamSettings(
  900. json.network,
  901. json.security,
  902. json.externalProxy,
  903. TlsStreamSettings.fromJson(json.tlsSettings),
  904. RealityStreamSettings.fromJson(json.realitySettings),
  905. TcpStreamSettings.fromJson(json.tcpSettings),
  906. KcpStreamSettings.fromJson(json.kcpSettings),
  907. WsStreamSettings.fromJson(json.wsSettings),
  908. GrpcStreamSettings.fromJson(json.grpcSettings),
  909. HTTPUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  910. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  911. SockoptStreamSettings.fromJson(json.sockopt),
  912. );
  913. }
  914. toJson() {
  915. const network = this.network;
  916. return {
  917. network: network,
  918. security: this.security,
  919. externalProxy: this.externalProxy,
  920. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  921. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  922. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  923. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  924. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  925. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  926. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  927. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  928. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  929. };
  930. }
  931. }
  932. class Sniffing extends XrayCommonClass {
  933. constructor(
  934. enabled = false,
  935. destOverride = ['http', 'tls', 'quic', 'fakedns'],
  936. metadataOnly = false,
  937. routeOnly = false) {
  938. super();
  939. this.enabled = enabled;
  940. this.destOverride = destOverride;
  941. this.metadataOnly = metadataOnly;
  942. this.routeOnly = routeOnly;
  943. }
  944. static fromJson(json = {}) {
  945. let destOverride = ObjectUtil.clone(json.destOverride);
  946. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  947. if (ObjectUtil.isEmpty(destOverride[0])) {
  948. destOverride = ['http', 'tls', 'quic', 'fakedns'];
  949. }
  950. }
  951. return new Sniffing(
  952. !!json.enabled,
  953. destOverride,
  954. json.metadataOnly,
  955. json.routeOnly,
  956. );
  957. }
  958. }
  959. class Inbound extends XrayCommonClass {
  960. constructor(
  961. port = RandomUtil.randomInteger(10000, 60000),
  962. listen = '',
  963. protocol = Protocols.VLESS,
  964. settings = null,
  965. streamSettings = new StreamSettings(),
  966. tag = '',
  967. sniffing = new Sniffing(),
  968. clientStats = '',
  969. ) {
  970. super();
  971. this.port = port;
  972. this.listen = listen;
  973. this._protocol = protocol;
  974. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  975. this.stream = streamSettings;
  976. this.tag = tag;
  977. this.sniffing = sniffing;
  978. this.clientStats = clientStats;
  979. }
  980. getClientStats() {
  981. return this.clientStats;
  982. }
  983. get clients() {
  984. switch (this.protocol) {
  985. case Protocols.VMESS: return this.settings.vmesses;
  986. case Protocols.VLESS: return this.settings.vlesses;
  987. case Protocols.TROJAN: return this.settings.trojans;
  988. case Protocols.SHADOWSOCKS: return this.isSSMultiUser ? this.settings.shadowsockses : null;
  989. default: return null;
  990. }
  991. }
  992. get protocol() {
  993. return this._protocol;
  994. }
  995. set protocol(protocol) {
  996. this._protocol = protocol;
  997. this.settings = Inbound.Settings.getSettings(protocol);
  998. if (protocol === Protocols.TROJAN) {
  999. this.tls = false;
  1000. }
  1001. }
  1002. get network() {
  1003. return this.stream.network;
  1004. }
  1005. set network(network) {
  1006. this.stream.network = network;
  1007. }
  1008. get isTcp() {
  1009. return this.network === "tcp";
  1010. }
  1011. get isWs() {
  1012. return this.network === "ws";
  1013. }
  1014. get isKcp() {
  1015. return this.network === "kcp";
  1016. }
  1017. get isGrpc() {
  1018. return this.network === "grpc";
  1019. }
  1020. get isHttpupgrade() {
  1021. return this.network === "httpupgrade";
  1022. }
  1023. get isXHTTP() {
  1024. return this.network === "xhttp";
  1025. }
  1026. // Shadowsocks
  1027. get method() {
  1028. switch (this.protocol) {
  1029. case Protocols.SHADOWSOCKS:
  1030. return this.settings.method;
  1031. default:
  1032. return "";
  1033. }
  1034. }
  1035. get isSSMultiUser() {
  1036. return this.method != SSMethods.BLAKE3_CHACHA20_POLY1305;
  1037. }
  1038. get isSS2022() {
  1039. return this.method.substring(0, 4) === "2022";
  1040. }
  1041. get serverName() {
  1042. if (this.stream.isTls) return this.stream.tls.sni;
  1043. if (this.stream.isReality) return this.stream.reality.serverNames;
  1044. return "";
  1045. }
  1046. getHeader(obj, name) {
  1047. for (const header of obj.headers) {
  1048. if (header.name.toLowerCase() === name.toLowerCase()) {
  1049. return header.value;
  1050. }
  1051. }
  1052. return "";
  1053. }
  1054. get host() {
  1055. if (this.isTcp) {
  1056. return this.getHeader(this.stream.tcp.request, 'host');
  1057. } else if (this.isWs) {
  1058. return this.stream.ws.host?.length > 0 ? this.stream.ws.host : this.getHeader(this.stream.ws, 'host');
  1059. } else if (this.isHttpupgrade) {
  1060. return this.stream.httpupgrade.host?.length > 0 ? this.stream.httpupgrade.host : this.getHeader(this.stream.httpupgrade, 'host');
  1061. } else if (this.isXHTTP) {
  1062. return this.stream.xhttp.host?.length > 0 ? this.stream.xhttp.host : this.getHeader(this.stream.xhttp, 'host');
  1063. }
  1064. return null;
  1065. }
  1066. get path() {
  1067. if (this.isTcp) {
  1068. return this.stream.tcp.request.path[0];
  1069. } else if (this.isWs) {
  1070. return this.stream.ws.path;
  1071. } else if (this.isHttpupgrade) {
  1072. return this.stream.httpupgrade.path;
  1073. } else if (this.isXHTTP) {
  1074. return this.stream.xhttp.path;
  1075. }
  1076. return null;
  1077. }
  1078. get kcpType() {
  1079. return this.stream.kcp.type;
  1080. }
  1081. get kcpSeed() {
  1082. return this.stream.kcp.seed;
  1083. }
  1084. get serviceName() {
  1085. return this.stream.grpc.serviceName;
  1086. }
  1087. isExpiry(index) {
  1088. let exp = this.clients[index].expiryTime;
  1089. return exp > 0 ? exp < new Date().getTime() : false;
  1090. }
  1091. canEnableTls() {
  1092. if (![Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol)) return false;
  1093. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.network);
  1094. }
  1095. //this is used for xtls-rprx-vision
  1096. canEnableTlsFlow() {
  1097. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1098. return this.protocol === Protocols.VLESS;
  1099. }
  1100. return false;
  1101. }
  1102. canEnableReality() {
  1103. if (![Protocols.VLESS, Protocols.TROJAN].includes(this.protocol)) return false;
  1104. return ["tcp", "http", "grpc", "xhttp"].includes(this.network);
  1105. }
  1106. canEnableStream() {
  1107. return [Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol);
  1108. }
  1109. reset() {
  1110. this.port = RandomUtil.randomInteger(10000, 60000);
  1111. this.listen = '';
  1112. this.protocol = Protocols.VMESS;
  1113. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1114. this.stream = new StreamSettings();
  1115. this.tag = '';
  1116. this.sniffing = new Sniffing();
  1117. }
  1118. genVmessLink(address = '', port = this.port, forceTls, remark = '', clientId, security) {
  1119. if (this.protocol !== Protocols.VMESS) {
  1120. return '';
  1121. }
  1122. const tls = forceTls == 'same' ? this.stream.security : forceTls;
  1123. let obj = {
  1124. v: '2',
  1125. ps: remark,
  1126. add: address,
  1127. port: port,
  1128. id: clientId,
  1129. scy: security,
  1130. net: this.stream.network,
  1131. tls: tls,
  1132. };
  1133. const network = this.stream.network;
  1134. if (network === 'tcp') {
  1135. const tcp = this.stream.tcp;
  1136. obj.type = tcp.type;
  1137. if (tcp.type === 'http') {
  1138. const request = tcp.request;
  1139. obj.path = request.path.join(',');
  1140. const host = this.getHeader(request, 'host');
  1141. if (host) obj.host = host;
  1142. }
  1143. } else if (network === 'kcp') {
  1144. const kcp = this.stream.kcp;
  1145. obj.type = kcp.type;
  1146. obj.path = kcp.seed;
  1147. } else if (network === 'ws') {
  1148. const ws = this.stream.ws;
  1149. obj.path = ws.path;
  1150. obj.host = ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host');
  1151. } else if (network === 'grpc') {
  1152. obj.path = this.stream.grpc.serviceName;
  1153. obj.authority = this.stream.grpc.authority;
  1154. if (this.stream.grpc.multiMode) {
  1155. obj.type = 'multi'
  1156. }
  1157. } else if (network === 'httpupgrade') {
  1158. const httpupgrade = this.stream.httpupgrade;
  1159. obj.path = httpupgrade.path;
  1160. obj.host = httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host');
  1161. } else if (network === 'xhttp') {
  1162. const xhttp = this.stream.xhttp;
  1163. obj.path = xhttp.path;
  1164. obj.host = xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host');
  1165. obj.type = xhttp.mode;
  1166. }
  1167. if (tls === 'tls') {
  1168. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1169. obj.sni = this.stream.tls.sni;
  1170. }
  1171. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)) {
  1172. obj.fp = this.stream.tls.settings.fingerprint;
  1173. }
  1174. if (this.stream.tls.alpn.length > 0) {
  1175. obj.alpn = this.stream.tls.alpn.join(',');
  1176. }
  1177. if (this.stream.tls.settings.allowInsecure) {
  1178. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1179. }
  1180. }
  1181. return 'vmess://' + Base64.encode(JSON.stringify(obj, null, 2));
  1182. }
  1183. genVLESSLink(address = '', port = this.port, forceTls, remark = '', clientId, flow) {
  1184. const uuid = clientId;
  1185. const type = this.stream.network;
  1186. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1187. const params = new Map();
  1188. params.set("type", this.stream.network);
  1189. params.set("encryption", this.settings.encryption);
  1190. switch (type) {
  1191. case "tcp":
  1192. const tcp = this.stream.tcp;
  1193. if (tcp.type === 'http') {
  1194. const request = tcp.request;
  1195. params.set("path", request.path.join(','));
  1196. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1197. if (index >= 0) {
  1198. const host = request.headers[index].value;
  1199. params.set("host", host);
  1200. }
  1201. params.set("headerType", 'http');
  1202. }
  1203. break;
  1204. case "kcp":
  1205. const kcp = this.stream.kcp;
  1206. params.set("headerType", kcp.type);
  1207. params.set("seed", kcp.seed);
  1208. break;
  1209. case "ws":
  1210. const ws = this.stream.ws;
  1211. params.set("path", ws.path);
  1212. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1213. break;
  1214. case "grpc":
  1215. const grpc = this.stream.grpc;
  1216. params.set("serviceName", grpc.serviceName);
  1217. params.set("authority", grpc.authority);
  1218. if (grpc.multiMode) {
  1219. params.set("mode", "multi");
  1220. }
  1221. break;
  1222. case "httpupgrade":
  1223. const httpupgrade = this.stream.httpupgrade;
  1224. params.set("path", httpupgrade.path);
  1225. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1226. break;
  1227. case "xhttp":
  1228. const xhttp = this.stream.xhttp;
  1229. params.set("path", xhttp.path);
  1230. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1231. params.set("mode", xhttp.mode);
  1232. break;
  1233. }
  1234. if (security === 'tls') {
  1235. params.set("security", "tls");
  1236. if (this.stream.isTls) {
  1237. params.set("fp", this.stream.tls.settings.fingerprint);
  1238. params.set("alpn", this.stream.tls.alpn);
  1239. if (this.stream.tls.settings.allowInsecure) {
  1240. params.set("allowInsecure", "1");
  1241. }
  1242. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1243. params.set("sni", this.stream.tls.sni);
  1244. }
  1245. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1246. params.set("ech", this.stream.tls.settings.echConfigList);
  1247. }
  1248. if (type == "tcp" && !ObjectUtil.isEmpty(flow)) {
  1249. params.set("flow", flow);
  1250. }
  1251. }
  1252. }
  1253. else if (security === 'reality') {
  1254. params.set("security", "reality");
  1255. params.set("pbk", this.stream.reality.settings.publicKey);
  1256. params.set("fp", this.stream.reality.settings.fingerprint);
  1257. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1258. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1259. }
  1260. if (this.stream.reality.shortIds.length > 0) {
  1261. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1262. }
  1263. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1264. params.set("spx", this.stream.reality.settings.spiderX);
  1265. }
  1266. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1267. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1268. }
  1269. if (type == 'tcp' && !ObjectUtil.isEmpty(flow)) {
  1270. params.set("flow", flow);
  1271. }
  1272. }
  1273. else {
  1274. params.set("security", "none");
  1275. }
  1276. const link = `vless://${uuid}@${address}:${port}`;
  1277. const url = new URL(link);
  1278. for (const [key, value] of params) {
  1279. url.searchParams.set(key, value)
  1280. }
  1281. url.hash = encodeURIComponent(remark);
  1282. return url.toString();
  1283. }
  1284. genSSLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1285. let settings = this.settings;
  1286. const type = this.stream.network;
  1287. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1288. const params = new Map();
  1289. params.set("type", this.stream.network);
  1290. switch (type) {
  1291. case "tcp":
  1292. const tcp = this.stream.tcp;
  1293. if (tcp.type === 'http') {
  1294. const request = tcp.request;
  1295. params.set("path", request.path.join(','));
  1296. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1297. if (index >= 0) {
  1298. const host = request.headers[index].value;
  1299. params.set("host", host);
  1300. }
  1301. params.set("headerType", 'http');
  1302. }
  1303. break;
  1304. case "kcp":
  1305. const kcp = this.stream.kcp;
  1306. params.set("headerType", kcp.type);
  1307. params.set("seed", kcp.seed);
  1308. break;
  1309. case "ws":
  1310. const ws = this.stream.ws;
  1311. params.set("path", ws.path);
  1312. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1313. break;
  1314. case "grpc":
  1315. const grpc = this.stream.grpc;
  1316. params.set("serviceName", grpc.serviceName);
  1317. params.set("authority", grpc.authority);
  1318. if (grpc.multiMode) {
  1319. params.set("mode", "multi");
  1320. }
  1321. break;
  1322. case "httpupgrade":
  1323. const httpupgrade = this.stream.httpupgrade;
  1324. params.set("path", httpupgrade.path);
  1325. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1326. break;
  1327. case "xhttp":
  1328. const xhttp = this.stream.xhttp;
  1329. params.set("path", xhttp.path);
  1330. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1331. params.set("mode", xhttp.mode);
  1332. break;
  1333. }
  1334. if (security === 'tls') {
  1335. params.set("security", "tls");
  1336. if (this.stream.isTls) {
  1337. params.set("fp", this.stream.tls.settings.fingerprint);
  1338. params.set("alpn", this.stream.tls.alpn);
  1339. if (this.stream.tls.settings.allowInsecure) {
  1340. params.set("allowInsecure", "1");
  1341. }
  1342. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1343. params.set("ech", this.stream.tls.settings.echConfigList);
  1344. }
  1345. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1346. params.set("sni", this.stream.tls.sni);
  1347. }
  1348. }
  1349. }
  1350. let password = new Array();
  1351. if (this.isSS2022) password.push(settings.password);
  1352. if (this.isSSMultiUser) password.push(clientPassword);
  1353. let link = `ss://${Base64.encode(`${settings.method}:${password.join(':')}`, true)}@${address}:${port}`;
  1354. const url = new URL(link);
  1355. for (const [key, value] of params) {
  1356. url.searchParams.set(key, value)
  1357. }
  1358. url.hash = encodeURIComponent(remark);
  1359. return url.toString();
  1360. }
  1361. genTrojanLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1362. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1363. const type = this.stream.network;
  1364. const params = new Map();
  1365. params.set("type", this.stream.network);
  1366. switch (type) {
  1367. case "tcp":
  1368. const tcp = this.stream.tcp;
  1369. if (tcp.type === 'http') {
  1370. const request = tcp.request;
  1371. params.set("path", request.path.join(','));
  1372. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1373. if (index >= 0) {
  1374. const host = request.headers[index].value;
  1375. params.set("host", host);
  1376. }
  1377. params.set("headerType", 'http');
  1378. }
  1379. break;
  1380. case "kcp":
  1381. const kcp = this.stream.kcp;
  1382. params.set("headerType", kcp.type);
  1383. params.set("seed", kcp.seed);
  1384. break;
  1385. case "ws":
  1386. const ws = this.stream.ws;
  1387. params.set("path", ws.path);
  1388. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1389. break;
  1390. case "grpc":
  1391. const grpc = this.stream.grpc;
  1392. params.set("serviceName", grpc.serviceName);
  1393. params.set("authority", grpc.authority);
  1394. if (grpc.multiMode) {
  1395. params.set("mode", "multi");
  1396. }
  1397. break;
  1398. case "httpupgrade":
  1399. const httpupgrade = this.stream.httpupgrade;
  1400. params.set("path", httpupgrade.path);
  1401. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1402. break;
  1403. case "xhttp":
  1404. const xhttp = this.stream.xhttp;
  1405. params.set("path", xhttp.path);
  1406. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1407. params.set("mode", xhttp.mode);
  1408. break;
  1409. }
  1410. if (security === 'tls') {
  1411. params.set("security", "tls");
  1412. if (this.stream.isTls) {
  1413. params.set("fp", this.stream.tls.settings.fingerprint);
  1414. params.set("alpn", this.stream.tls.alpn);
  1415. if (this.stream.tls.settings.allowInsecure) {
  1416. params.set("allowInsecure", "1");
  1417. }
  1418. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1419. params.set("ech", this.stream.tls.settings.echConfigList);
  1420. }
  1421. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1422. params.set("sni", this.stream.tls.sni);
  1423. }
  1424. }
  1425. }
  1426. else if (security === 'reality') {
  1427. params.set("security", "reality");
  1428. params.set("pbk", this.stream.reality.settings.publicKey);
  1429. params.set("fp", this.stream.reality.settings.fingerprint);
  1430. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1431. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1432. }
  1433. if (this.stream.reality.shortIds.length > 0) {
  1434. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1435. }
  1436. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1437. params.set("spx", this.stream.reality.settings.spiderX);
  1438. }
  1439. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1440. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1441. }
  1442. }
  1443. else {
  1444. params.set("security", "none");
  1445. }
  1446. const link = `trojan://${clientPassword}@${address}:${port}`;
  1447. const url = new URL(link);
  1448. for (const [key, value] of params) {
  1449. url.searchParams.set(key, value)
  1450. }
  1451. url.hash = encodeURIComponent(remark);
  1452. return url.toString();
  1453. }
  1454. getWireguardLink(address, port, remark, peerId) {
  1455. let txt = `[Interface]\n`
  1456. txt += `PrivateKey = ${this.settings.peers[peerId].privateKey}\n`
  1457. txt += `Address = ${this.settings.peers[peerId].allowedIPs[0]}\n`
  1458. txt += `DNS = 1.1.1.1, 1.0.0.1\n`
  1459. if (this.settings.mtu) {
  1460. txt += `MTU = ${this.settings.mtu}\n`
  1461. }
  1462. txt += `\n# ${remark}\n`
  1463. txt += `[Peer]\n`
  1464. txt += `PublicKey = ${this.settings.pubKey}\n`
  1465. txt += `AllowedIPs = 0.0.0.0/0, ::/0\n`
  1466. txt += `Endpoint = ${address}:${port}`
  1467. if (this.settings.peers[peerId].psk) {
  1468. txt += `\nPresharedKey = ${this.settings.peers[peerId].psk}`
  1469. }
  1470. if (this.settings.peers[peerId].keepAlive) {
  1471. txt += `\nPersistentKeepalive = ${this.settings.peers[peerId].keepAlive}\n`
  1472. }
  1473. return txt;
  1474. }
  1475. genLink(address = '', port = this.port, forceTls = 'same', remark = '', client) {
  1476. switch (this.protocol) {
  1477. case Protocols.VMESS:
  1478. return this.genVmessLink(address, port, forceTls, remark, client.id, client.security);
  1479. case Protocols.VLESS:
  1480. return this.genVLESSLink(address, port, forceTls, remark, client.id, client.flow);
  1481. case Protocols.SHADOWSOCKS:
  1482. return this.genSSLink(address, port, forceTls, remark, this.isSSMultiUser ? client.password : '');
  1483. case Protocols.TROJAN:
  1484. return this.genTrojanLink(address, port, forceTls, remark, client.password);
  1485. default: return '';
  1486. }
  1487. }
  1488. genAllLinks(remark = '', remarkModel = '-ieo', client) {
  1489. let result = [];
  1490. let email = client ? client.email : '';
  1491. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1492. let port = this.port;
  1493. const separationChar = remarkModel.charAt(0);
  1494. const orderChars = remarkModel.slice(1);
  1495. let orders = {
  1496. 'i': remark,
  1497. 'e': email,
  1498. 'o': '',
  1499. };
  1500. if (ObjectUtil.isArrEmpty(this.stream.externalProxy)) {
  1501. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1502. result.push({
  1503. remark: r,
  1504. link: this.genLink(addr, port, 'same', r, client)
  1505. });
  1506. } else {
  1507. this.stream.externalProxy.forEach((ep) => {
  1508. orders['o'] = ep.remark;
  1509. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1510. result.push({
  1511. remark: r,
  1512. link: this.genLink(ep.dest, ep.port, ep.forceTls, r, client)
  1513. });
  1514. });
  1515. }
  1516. return result;
  1517. }
  1518. genInboundLinks(remark = '', remarkModel = '-ieo') {
  1519. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1520. if (this.clients) {
  1521. let links = [];
  1522. this.clients.forEach((client) => {
  1523. this.genAllLinks(remark, remarkModel, client).forEach(l => {
  1524. links.push(l.link);
  1525. })
  1526. });
  1527. return links.join('\r\n');
  1528. } else {
  1529. if (this.protocol == Protocols.SHADOWSOCKS && !this.isSSMultiUser) return this.genSSLink(addr, this.port, 'same', remark);
  1530. if (this.protocol == Protocols.WIREGUARD) {
  1531. let links = [];
  1532. this.settings.peers.forEach((p, index) => {
  1533. links.push(this.getWireguardLink(addr, this.port, remark + remarkModel.charAt(0) + (index + 1), index));
  1534. });
  1535. return links.join('\r\n');
  1536. }
  1537. return '';
  1538. }
  1539. }
  1540. static fromJson(json = {}) {
  1541. return new Inbound(
  1542. json.port,
  1543. json.listen,
  1544. json.protocol,
  1545. Inbound.Settings.fromJson(json.protocol, json.settings),
  1546. StreamSettings.fromJson(json.streamSettings),
  1547. json.tag,
  1548. Sniffing.fromJson(json.sniffing),
  1549. json.clientStats
  1550. )
  1551. }
  1552. toJson() {
  1553. let streamSettings;
  1554. if (this.canEnableStream() || this.stream?.sockopt) {
  1555. streamSettings = this.stream.toJson();
  1556. }
  1557. return {
  1558. port: this.port,
  1559. listen: this.listen,
  1560. protocol: this.protocol,
  1561. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1562. streamSettings: streamSettings,
  1563. tag: this.tag,
  1564. sniffing: this.sniffing.toJson(),
  1565. clientStats: this.clientStats
  1566. };
  1567. }
  1568. }
  1569. Inbound.Settings = class extends XrayCommonClass {
  1570. constructor(protocol) {
  1571. super();
  1572. this.protocol = protocol;
  1573. }
  1574. static getSettings(protocol) {
  1575. switch (protocol) {
  1576. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1577. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1578. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1579. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1580. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1581. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1582. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1583. case Protocols.WIREGUARD: return new Inbound.WireguardSettings(protocol);
  1584. default: return null;
  1585. }
  1586. }
  1587. static fromJson(protocol, json) {
  1588. switch (protocol) {
  1589. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1590. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1591. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1592. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1593. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1594. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1595. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1596. case Protocols.WIREGUARD: return Inbound.WireguardSettings.fromJson(json);
  1597. default: return null;
  1598. }
  1599. }
  1600. toJson() {
  1601. return {};
  1602. }
  1603. };
  1604. Inbound.VmessSettings = class extends Inbound.Settings {
  1605. constructor(protocol,
  1606. vmesses = [new Inbound.VmessSettings.VMESS()]) {
  1607. super(protocol);
  1608. this.vmesses = vmesses;
  1609. }
  1610. indexOfVmessById(id) {
  1611. return this.vmesses.findIndex(VMESS => VMESS.id === id);
  1612. }
  1613. addVmess(VMESS) {
  1614. if (this.indexOfVmessById(VMESS.id) >= 0) {
  1615. return false;
  1616. }
  1617. this.vmesses.push(VMESS);
  1618. }
  1619. delVmess(VMESS) {
  1620. const i = this.indexOfVmessById(VMESS.id);
  1621. if (i >= 0) {
  1622. this.vmesses.splice(i, 1);
  1623. }
  1624. }
  1625. static fromJson(json = {}) {
  1626. return new Inbound.VmessSettings(
  1627. Protocols.VMESS,
  1628. json.clients.map(client => Inbound.VmessSettings.VMESS.fromJson(client)),
  1629. );
  1630. }
  1631. toJson() {
  1632. return {
  1633. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1634. };
  1635. }
  1636. };
  1637. Inbound.VmessSettings.VMESS = class extends XrayCommonClass {
  1638. constructor(
  1639. id = RandomUtil.randomUUID(),
  1640. security = USERS_SECURITY.AUTO,
  1641. email = RandomUtil.randomLowerAndNum(8),
  1642. limitIp = 0,
  1643. totalGB = 0,
  1644. expiryTime = 0,
  1645. enable = true,
  1646. tgId = '',
  1647. subId = RandomUtil.randomLowerAndNum(16),
  1648. comment = '',
  1649. reset = 0,
  1650. created_at = undefined,
  1651. updated_at = undefined
  1652. ) {
  1653. super();
  1654. this.id = id;
  1655. this.security = security;
  1656. this.email = email;
  1657. this.limitIp = limitIp;
  1658. this.totalGB = totalGB;
  1659. this.expiryTime = expiryTime;
  1660. this.enable = enable;
  1661. this.tgId = tgId;
  1662. this.subId = subId;
  1663. this.comment = comment;
  1664. this.reset = reset;
  1665. this.created_at = created_at;
  1666. this.updated_at = updated_at;
  1667. }
  1668. static fromJson(json = {}) {
  1669. return new Inbound.VmessSettings.VMESS(
  1670. json.id,
  1671. json.security,
  1672. json.email,
  1673. json.limitIp,
  1674. json.totalGB,
  1675. json.expiryTime,
  1676. json.enable,
  1677. json.tgId,
  1678. json.subId,
  1679. json.comment,
  1680. json.reset,
  1681. json.created_at,
  1682. json.updated_at,
  1683. );
  1684. }
  1685. get _expiryTime() {
  1686. if (this.expiryTime === 0 || this.expiryTime === "") {
  1687. return null;
  1688. }
  1689. if (this.expiryTime < 0) {
  1690. return this.expiryTime / -86400000;
  1691. }
  1692. return moment(this.expiryTime);
  1693. }
  1694. set _expiryTime(t) {
  1695. if (t == null || t === "") {
  1696. this.expiryTime = 0;
  1697. } else {
  1698. this.expiryTime = t.valueOf();
  1699. }
  1700. }
  1701. get _totalGB() {
  1702. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1703. }
  1704. set _totalGB(gb) {
  1705. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1706. }
  1707. };
  1708. Inbound.VLESSSettings = class extends Inbound.Settings {
  1709. constructor(
  1710. protocol,
  1711. vlesses = [new Inbound.VLESSSettings.VLESS()],
  1712. decryption = "none",
  1713. encryption = "",
  1714. fallbacks = [],
  1715. ) {
  1716. super(protocol);
  1717. this.vlesses = vlesses;
  1718. this.decryption = decryption;
  1719. this.encryption = encryption;
  1720. this.fallbacks = fallbacks;
  1721. this.selectedAuth = "X25519, not Post-Quantum";
  1722. }
  1723. addFallback() {
  1724. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1725. }
  1726. delFallback(index) {
  1727. this.fallbacks.splice(index, 1);
  1728. }
  1729. static fromJson(json = {}) {
  1730. const obj = new Inbound.VLESSSettings(
  1731. Protocols.VLESS,
  1732. (json.clients || []).map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1733. json.decryption,
  1734. json.encryption,
  1735. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks || [])
  1736. );
  1737. obj.selectedAuth = json.selectedAuth || "X25519, not Post-Quantum";
  1738. return obj;
  1739. }
  1740. toJson() {
  1741. const json = {
  1742. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1743. };
  1744. if (this.decryption) {
  1745. json.decryption = this.decryption;
  1746. }
  1747. if (this.encryption) {
  1748. json.encryption = this.encryption;
  1749. }
  1750. if (this.fallbacks && this.fallbacks.length > 0) {
  1751. json.fallbacks = Inbound.VLESSSettings.toJsonArray(this.fallbacks);
  1752. }
  1753. if (this.selectedAuth) {
  1754. json.selectedAuth = this.selectedAuth;
  1755. }
  1756. return json;
  1757. }
  1758. };
  1759. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1760. constructor(
  1761. id = RandomUtil.randomUUID(),
  1762. flow = '',
  1763. email = RandomUtil.randomLowerAndNum(8),
  1764. limitIp = 0,
  1765. totalGB = 0,
  1766. expiryTime = 0,
  1767. enable = true,
  1768. tgId = '',
  1769. subId = RandomUtil.randomLowerAndNum(16),
  1770. comment = '',
  1771. reset = 0,
  1772. created_at = undefined,
  1773. updated_at = undefined
  1774. ) {
  1775. super();
  1776. this.id = id;
  1777. this.flow = flow;
  1778. this.email = email;
  1779. this.limitIp = limitIp;
  1780. this.totalGB = totalGB;
  1781. this.expiryTime = expiryTime;
  1782. this.enable = enable;
  1783. this.tgId = tgId;
  1784. this.subId = subId;
  1785. this.comment = comment;
  1786. this.reset = reset;
  1787. this.created_at = created_at;
  1788. this.updated_at = updated_at;
  1789. }
  1790. static fromJson(json = {}) {
  1791. return new Inbound.VLESSSettings.VLESS(
  1792. json.id,
  1793. json.flow,
  1794. json.email,
  1795. json.limitIp,
  1796. json.totalGB,
  1797. json.expiryTime,
  1798. json.enable,
  1799. json.tgId,
  1800. json.subId,
  1801. json.comment,
  1802. json.reset,
  1803. json.created_at,
  1804. json.updated_at,
  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. created_at = undefined,
  1906. updated_at = undefined
  1907. ) {
  1908. super();
  1909. this.password = password;
  1910. this.email = email;
  1911. this.limitIp = limitIp;
  1912. this.totalGB = totalGB;
  1913. this.expiryTime = expiryTime;
  1914. this.enable = enable;
  1915. this.tgId = tgId;
  1916. this.subId = subId;
  1917. this.comment = comment;
  1918. this.reset = reset;
  1919. this.created_at = created_at;
  1920. this.updated_at = updated_at;
  1921. }
  1922. toJson() {
  1923. return {
  1924. password: this.password,
  1925. email: this.email,
  1926. limitIp: this.limitIp,
  1927. totalGB: this.totalGB,
  1928. expiryTime: this.expiryTime,
  1929. enable: this.enable,
  1930. tgId: this.tgId,
  1931. subId: this.subId,
  1932. comment: this.comment,
  1933. reset: this.reset,
  1934. created_at: this.created_at,
  1935. updated_at: this.updated_at,
  1936. };
  1937. }
  1938. static fromJson(json = {}) {
  1939. return new Inbound.TrojanSettings.Trojan(
  1940. json.password,
  1941. json.email,
  1942. json.limitIp,
  1943. json.totalGB,
  1944. json.expiryTime,
  1945. json.enable,
  1946. json.tgId,
  1947. json.subId,
  1948. json.comment,
  1949. json.reset,
  1950. json.created_at,
  1951. json.updated_at,
  1952. );
  1953. }
  1954. get _expiryTime() {
  1955. if (this.expiryTime === 0 || this.expiryTime === "") {
  1956. return null;
  1957. }
  1958. if (this.expiryTime < 0) {
  1959. return this.expiryTime / -86400000;
  1960. }
  1961. return moment(this.expiryTime);
  1962. }
  1963. set _expiryTime(t) {
  1964. if (t == null || t === "") {
  1965. this.expiryTime = 0;
  1966. } else {
  1967. this.expiryTime = t.valueOf();
  1968. }
  1969. }
  1970. get _totalGB() {
  1971. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1972. }
  1973. set _totalGB(gb) {
  1974. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1975. }
  1976. };
  1977. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1978. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1979. super();
  1980. this.name = name;
  1981. this.alpn = alpn;
  1982. this.path = path;
  1983. this.dest = dest;
  1984. this.xver = xver;
  1985. }
  1986. toJson() {
  1987. let xver = this.xver;
  1988. if (!Number.isInteger(xver)) {
  1989. xver = 0;
  1990. }
  1991. return {
  1992. name: this.name,
  1993. alpn: this.alpn,
  1994. path: this.path,
  1995. dest: this.dest,
  1996. xver: xver,
  1997. }
  1998. }
  1999. static fromJson(json = []) {
  2000. const fallbacks = [];
  2001. for (let fallback of json) {
  2002. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  2003. fallback.name,
  2004. fallback.alpn,
  2005. fallback.path,
  2006. fallback.dest,
  2007. fallback.xver,
  2008. ))
  2009. }
  2010. return fallbacks;
  2011. }
  2012. };
  2013. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  2014. constructor(protocol,
  2015. method = SSMethods.BLAKE3_AES_256_GCM,
  2016. password = RandomUtil.randomShadowsocksPassword(),
  2017. network = 'tcp,udp',
  2018. shadowsockses = [new Inbound.ShadowsocksSettings.Shadowsocks()],
  2019. ivCheck = false,
  2020. ) {
  2021. super(protocol);
  2022. this.method = method;
  2023. this.password = password;
  2024. this.network = network;
  2025. this.shadowsockses = shadowsockses;
  2026. this.ivCheck = ivCheck;
  2027. }
  2028. static fromJson(json = {}) {
  2029. return new Inbound.ShadowsocksSettings(
  2030. Protocols.SHADOWSOCKS,
  2031. json.method,
  2032. json.password,
  2033. json.network,
  2034. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  2035. json.ivCheck,
  2036. );
  2037. }
  2038. toJson() {
  2039. return {
  2040. method: this.method,
  2041. password: this.password,
  2042. network: this.network,
  2043. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses),
  2044. ivCheck: this.ivCheck,
  2045. };
  2046. }
  2047. };
  2048. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  2049. constructor(
  2050. method = '',
  2051. password = RandomUtil.randomShadowsocksPassword(),
  2052. email = RandomUtil.randomLowerAndNum(8),
  2053. limitIp = 0,
  2054. totalGB = 0,
  2055. expiryTime = 0,
  2056. enable = true,
  2057. tgId = '',
  2058. subId = RandomUtil.randomLowerAndNum(16),
  2059. comment = '',
  2060. reset = 0,
  2061. created_at = undefined,
  2062. updated_at = undefined
  2063. ) {
  2064. super();
  2065. this.method = method;
  2066. this.password = password;
  2067. this.email = email;
  2068. this.limitIp = limitIp;
  2069. this.totalGB = totalGB;
  2070. this.expiryTime = expiryTime;
  2071. this.enable = enable;
  2072. this.tgId = tgId;
  2073. this.subId = subId;
  2074. this.comment = comment;
  2075. this.reset = reset;
  2076. this.created_at = created_at;
  2077. this.updated_at = updated_at;
  2078. }
  2079. toJson() {
  2080. return {
  2081. method: this.method,
  2082. password: this.password,
  2083. email: this.email,
  2084. limitIp: this.limitIp,
  2085. totalGB: this.totalGB,
  2086. expiryTime: this.expiryTime,
  2087. enable: this.enable,
  2088. tgId: this.tgId,
  2089. subId: this.subId,
  2090. comment: this.comment,
  2091. reset: this.reset,
  2092. created_at: this.created_at,
  2093. updated_at: this.updated_at,
  2094. };
  2095. }
  2096. static fromJson(json = {}) {
  2097. return new Inbound.ShadowsocksSettings.Shadowsocks(
  2098. json.method,
  2099. json.password,
  2100. json.email,
  2101. json.limitIp,
  2102. json.totalGB,
  2103. json.expiryTime,
  2104. json.enable,
  2105. json.tgId,
  2106. json.subId,
  2107. json.comment,
  2108. json.reset,
  2109. json.created_at,
  2110. json.updated_at,
  2111. );
  2112. }
  2113. get _expiryTime() {
  2114. if (this.expiryTime === 0 || this.expiryTime === "") {
  2115. return null;
  2116. }
  2117. if (this.expiryTime < 0) {
  2118. return this.expiryTime / -86400000;
  2119. }
  2120. return moment(this.expiryTime);
  2121. }
  2122. set _expiryTime(t) {
  2123. if (t == null || t === "") {
  2124. this.expiryTime = 0;
  2125. } else {
  2126. this.expiryTime = t.valueOf();
  2127. }
  2128. }
  2129. get _totalGB() {
  2130. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  2131. }
  2132. set _totalGB(gb) {
  2133. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  2134. }
  2135. };
  2136. Inbound.DokodemoSettings = class extends Inbound.Settings {
  2137. constructor(
  2138. protocol,
  2139. address,
  2140. port,
  2141. portMap = [],
  2142. network = 'tcp,udp',
  2143. followRedirect = false
  2144. ) {
  2145. super(protocol);
  2146. this.address = address;
  2147. this.port = port;
  2148. this.portMap = portMap;
  2149. this.network = network;
  2150. this.followRedirect = followRedirect;
  2151. }
  2152. static fromJson(json = {}) {
  2153. return new Inbound.DokodemoSettings(
  2154. Protocols.DOKODEMO,
  2155. json.address,
  2156. json.port,
  2157. XrayCommonClass.toHeaders(json.portMap),
  2158. json.network,
  2159. json.followRedirect,
  2160. );
  2161. }
  2162. toJson() {
  2163. return {
  2164. address: this.address,
  2165. port: this.port,
  2166. portMap: XrayCommonClass.toV2Headers(this.portMap, false),
  2167. network: this.network,
  2168. followRedirect: this.followRedirect,
  2169. };
  2170. }
  2171. };
  2172. Inbound.SocksSettings = class extends Inbound.Settings {
  2173. constructor(protocol, auth = 'password', accounts = [new Inbound.SocksSettings.SocksAccount()], udp = false, ip = '127.0.0.1') {
  2174. super(protocol);
  2175. this.auth = auth;
  2176. this.accounts = accounts;
  2177. this.udp = udp;
  2178. this.ip = ip;
  2179. }
  2180. addAccount(account) {
  2181. this.accounts.push(account);
  2182. }
  2183. delAccount(index) {
  2184. this.accounts.splice(index, 1);
  2185. }
  2186. static fromJson(json = {}) {
  2187. let accounts;
  2188. if (json.auth === 'password') {
  2189. accounts = json.accounts.map(
  2190. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  2191. )
  2192. }
  2193. return new Inbound.SocksSettings(
  2194. Protocols.SOCKS,
  2195. json.auth,
  2196. accounts,
  2197. json.udp,
  2198. json.ip,
  2199. );
  2200. }
  2201. toJson() {
  2202. return {
  2203. auth: this.auth,
  2204. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  2205. udp: this.udp,
  2206. ip: this.ip,
  2207. };
  2208. }
  2209. };
  2210. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  2211. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2212. super();
  2213. this.user = user;
  2214. this.pass = pass;
  2215. }
  2216. static fromJson(json = {}) {
  2217. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  2218. }
  2219. };
  2220. Inbound.HttpSettings = class extends Inbound.Settings {
  2221. constructor(
  2222. protocol,
  2223. accounts = [new Inbound.HttpSettings.HttpAccount()],
  2224. allowTransparent = false,
  2225. ) {
  2226. super(protocol);
  2227. this.accounts = accounts;
  2228. this.allowTransparent = allowTransparent;
  2229. }
  2230. addAccount(account) {
  2231. this.accounts.push(account);
  2232. }
  2233. delAccount(index) {
  2234. this.accounts.splice(index, 1);
  2235. }
  2236. static fromJson(json = {}) {
  2237. return new Inbound.HttpSettings(
  2238. Protocols.HTTP,
  2239. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  2240. json.allowTransparent,
  2241. );
  2242. }
  2243. toJson() {
  2244. return {
  2245. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2246. allowTransparent: this.allowTransparent,
  2247. };
  2248. }
  2249. };
  2250. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2251. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2252. super();
  2253. this.user = user;
  2254. this.pass = pass;
  2255. }
  2256. static fromJson(json = {}) {
  2257. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2258. }
  2259. };
  2260. Inbound.WireguardSettings = class extends XrayCommonClass {
  2261. constructor(
  2262. protocol,
  2263. mtu = 1420,
  2264. secretKey = Wireguard.generateKeypair().privateKey,
  2265. peers = [new Inbound.WireguardSettings.Peer()],
  2266. noKernelTun = false
  2267. ) {
  2268. super(protocol);
  2269. this.mtu = mtu;
  2270. this.secretKey = secretKey;
  2271. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  2272. this.peers = peers;
  2273. this.noKernelTun = noKernelTun;
  2274. }
  2275. addPeer() {
  2276. this.peers.push(new Inbound.WireguardSettings.Peer(null, null, '', ['10.0.0.' + (this.peers.length + 2)]));
  2277. }
  2278. delPeer(index) {
  2279. this.peers.splice(index, 1);
  2280. }
  2281. static fromJson(json = {}) {
  2282. return new Inbound.WireguardSettings(
  2283. Protocols.WIREGUARD,
  2284. json.mtu,
  2285. json.secretKey,
  2286. json.peers.map(peer => Inbound.WireguardSettings.Peer.fromJson(peer)),
  2287. json.noKernelTun,
  2288. );
  2289. }
  2290. toJson() {
  2291. return {
  2292. mtu: this.mtu ?? undefined,
  2293. secretKey: this.secretKey,
  2294. peers: Inbound.WireguardSettings.Peer.toJsonArray(this.peers),
  2295. noKernelTun: this.noKernelTun,
  2296. };
  2297. }
  2298. };
  2299. Inbound.WireguardSettings.Peer = class extends XrayCommonClass {
  2300. constructor(privateKey, publicKey, psk = '', allowedIPs = ['10.0.0.2/32'], keepAlive = 0) {
  2301. super();
  2302. this.privateKey = privateKey
  2303. this.publicKey = publicKey;
  2304. if (!this.publicKey) {
  2305. [this.publicKey, this.privateKey] = Object.values(Wireguard.generateKeypair())
  2306. }
  2307. this.psk = psk;
  2308. allowedIPs.forEach((a, index) => {
  2309. if (a.length > 0 && !a.includes('/')) allowedIPs[index] += '/32';
  2310. })
  2311. this.allowedIPs = allowedIPs;
  2312. this.keepAlive = keepAlive;
  2313. }
  2314. static fromJson(json = {}) {
  2315. return new Inbound.WireguardSettings.Peer(
  2316. json.privateKey,
  2317. json.publicKey,
  2318. json.preSharedKey,
  2319. json.allowedIPs,
  2320. json.keepAlive
  2321. );
  2322. }
  2323. toJson() {
  2324. this.allowedIPs.forEach((a, index) => {
  2325. if (a.length > 0 && !a.includes('/')) this.allowedIPs[index] += '/32';
  2326. });
  2327. return {
  2328. privateKey: this.privateKey,
  2329. publicKey: this.publicKey,
  2330. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  2331. allowedIPs: this.allowedIPs,
  2332. keepAlive: this.keepAlive ?? undefined,
  2333. };
  2334. }
  2335. };