inbound.js 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  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 Allocate extends XrayCommonClass {
  960. constructor(
  961. strategy = "always",
  962. refresh = 5,
  963. concurrency = 3,
  964. ) {
  965. super();
  966. this.strategy = strategy;
  967. this.refresh = refresh;
  968. this.concurrency = concurrency;
  969. }
  970. static fromJson(json = {}) {
  971. return new Allocate(
  972. json.strategy,
  973. json.refresh,
  974. json.concurrency,
  975. );
  976. }
  977. }
  978. class Inbound extends XrayCommonClass {
  979. constructor(
  980. port = RandomUtil.randomInteger(10000, 60000),
  981. listen = '',
  982. protocol = Protocols.VLESS,
  983. settings = null,
  984. streamSettings = new StreamSettings(),
  985. tag = '',
  986. sniffing = new Sniffing(),
  987. allocate = new Allocate(),
  988. clientStats = '',
  989. ) {
  990. super();
  991. this.port = port;
  992. this.listen = listen;
  993. this._protocol = protocol;
  994. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  995. this.stream = streamSettings;
  996. this.tag = tag;
  997. this.sniffing = sniffing;
  998. this.allocate = allocate;
  999. this.clientStats = clientStats;
  1000. }
  1001. getClientStats() {
  1002. return this.clientStats;
  1003. }
  1004. get clients() {
  1005. switch (this.protocol) {
  1006. case Protocols.VMESS: return this.settings.vmesses;
  1007. case Protocols.VLESS: return this.settings.vlesses;
  1008. case Protocols.TROJAN: return this.settings.trojans;
  1009. case Protocols.SHADOWSOCKS: return this.isSSMultiUser ? this.settings.shadowsockses : null;
  1010. default: return null;
  1011. }
  1012. }
  1013. get protocol() {
  1014. return this._protocol;
  1015. }
  1016. set protocol(protocol) {
  1017. this._protocol = protocol;
  1018. this.settings = Inbound.Settings.getSettings(protocol);
  1019. if (protocol === Protocols.TROJAN) {
  1020. this.tls = false;
  1021. }
  1022. }
  1023. get network() {
  1024. return this.stream.network;
  1025. }
  1026. set network(network) {
  1027. this.stream.network = network;
  1028. }
  1029. get isTcp() {
  1030. return this.network === "tcp";
  1031. }
  1032. get isWs() {
  1033. return this.network === "ws";
  1034. }
  1035. get isKcp() {
  1036. return this.network === "kcp";
  1037. }
  1038. get isGrpc() {
  1039. return this.network === "grpc";
  1040. }
  1041. get isHttpupgrade() {
  1042. return this.network === "httpupgrade";
  1043. }
  1044. get isXHTTP() {
  1045. return this.network === "xhttp";
  1046. }
  1047. // Shadowsocks
  1048. get method() {
  1049. switch (this.protocol) {
  1050. case Protocols.SHADOWSOCKS:
  1051. return this.settings.method;
  1052. default:
  1053. return "";
  1054. }
  1055. }
  1056. get isSSMultiUser() {
  1057. return this.method != SSMethods.BLAKE3_CHACHA20_POLY1305;
  1058. }
  1059. get isSS2022() {
  1060. return this.method.substring(0, 4) === "2022";
  1061. }
  1062. get serverName() {
  1063. if (this.stream.isTls) return this.stream.tls.sni;
  1064. if (this.stream.isReality) return this.stream.reality.serverNames;
  1065. return "";
  1066. }
  1067. getHeader(obj, name) {
  1068. for (const header of obj.headers) {
  1069. if (header.name.toLowerCase() === name.toLowerCase()) {
  1070. return header.value;
  1071. }
  1072. }
  1073. return "";
  1074. }
  1075. get host() {
  1076. if (this.isTcp) {
  1077. return this.getHeader(this.stream.tcp.request, 'host');
  1078. } else if (this.isWs) {
  1079. return this.stream.ws.host?.length > 0 ? this.stream.ws.host : this.getHeader(this.stream.ws, 'host');
  1080. } else if (this.isHttpupgrade) {
  1081. return this.stream.httpupgrade.host?.length > 0 ? this.stream.httpupgrade.host : this.getHeader(this.stream.httpupgrade, 'host');
  1082. } else if (this.isXHTTP) {
  1083. return this.stream.xhttp.host?.length > 0 ? this.stream.xhttp.host : this.getHeader(this.stream.xhttp, 'host');
  1084. }
  1085. return null;
  1086. }
  1087. get path() {
  1088. if (this.isTcp) {
  1089. return this.stream.tcp.request.path[0];
  1090. } else if (this.isWs) {
  1091. return this.stream.ws.path;
  1092. } else if (this.isHttpupgrade) {
  1093. return this.stream.httpupgrade.path;
  1094. } else if (this.isXHTTP) {
  1095. return this.stream.xhttp.path;
  1096. }
  1097. return null;
  1098. }
  1099. get kcpType() {
  1100. return this.stream.kcp.type;
  1101. }
  1102. get kcpSeed() {
  1103. return this.stream.kcp.seed;
  1104. }
  1105. get serviceName() {
  1106. return this.stream.grpc.serviceName;
  1107. }
  1108. isExpiry(index) {
  1109. let exp = this.clients[index].expiryTime;
  1110. return exp > 0 ? exp < new Date().getTime() : false;
  1111. }
  1112. canEnableTls() {
  1113. if (![Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol)) return false;
  1114. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.network);
  1115. }
  1116. //this is used for xtls-rprx-vision
  1117. canEnableTlsFlow() {
  1118. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1119. return this.protocol === Protocols.VLESS;
  1120. }
  1121. return false;
  1122. }
  1123. canEnableReality() {
  1124. if (![Protocols.VLESS, Protocols.TROJAN].includes(this.protocol)) return false;
  1125. return ["tcp", "http", "grpc", "xhttp"].includes(this.network);
  1126. }
  1127. canEnableStream() {
  1128. return [Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol);
  1129. }
  1130. reset() {
  1131. this.port = RandomUtil.randomInteger(10000, 60000);
  1132. this.listen = '';
  1133. this.protocol = Protocols.VMESS;
  1134. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1135. this.stream = new StreamSettings();
  1136. this.tag = '';
  1137. this.sniffing = new Sniffing();
  1138. this.allocate = new Allocate();
  1139. }
  1140. genVmessLink(address = '', port = this.port, forceTls, remark = '', clientId, security) {
  1141. if (this.protocol !== Protocols.VMESS) {
  1142. return '';
  1143. }
  1144. const tls = forceTls == 'same' ? this.stream.security : forceTls;
  1145. let obj = {
  1146. v: '2',
  1147. ps: remark,
  1148. add: address,
  1149. port: port,
  1150. id: clientId,
  1151. scy: security,
  1152. net: this.stream.network,
  1153. tls: tls,
  1154. };
  1155. const network = this.stream.network;
  1156. if (network === 'tcp') {
  1157. const tcp = this.stream.tcp;
  1158. obj.type = tcp.type;
  1159. if (tcp.type === 'http') {
  1160. const request = tcp.request;
  1161. obj.path = request.path.join(',');
  1162. const host = this.getHeader(request, 'host');
  1163. if (host) obj.host = host;
  1164. }
  1165. } else if (network === 'kcp') {
  1166. const kcp = this.stream.kcp;
  1167. obj.type = kcp.type;
  1168. obj.path = kcp.seed;
  1169. } else if (network === 'ws') {
  1170. const ws = this.stream.ws;
  1171. obj.path = ws.path;
  1172. obj.host = ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host');
  1173. } else if (network === 'grpc') {
  1174. obj.path = this.stream.grpc.serviceName;
  1175. obj.authority = this.stream.grpc.authority;
  1176. if (this.stream.grpc.multiMode) {
  1177. obj.type = 'multi'
  1178. }
  1179. } else if (network === 'httpupgrade') {
  1180. const httpupgrade = this.stream.httpupgrade;
  1181. obj.path = httpupgrade.path;
  1182. obj.host = httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host');
  1183. } else if (network === 'xhttp') {
  1184. const xhttp = this.stream.xhttp;
  1185. obj.path = xhttp.path;
  1186. obj.host = xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host');
  1187. obj.type = xhttp.mode;
  1188. }
  1189. if (tls === 'tls') {
  1190. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1191. obj.sni = this.stream.tls.sni;
  1192. }
  1193. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)) {
  1194. obj.fp = this.stream.tls.settings.fingerprint;
  1195. }
  1196. if (this.stream.tls.alpn.length > 0) {
  1197. obj.alpn = this.stream.tls.alpn.join(',');
  1198. }
  1199. if (this.stream.tls.settings.allowInsecure) {
  1200. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1201. }
  1202. }
  1203. return 'vmess://' + Base64.encode(JSON.stringify(obj, null, 2));
  1204. }
  1205. genVLESSLink(address = '', port = this.port, forceTls, remark = '', clientId, flow) {
  1206. const uuid = clientId;
  1207. const type = this.stream.network;
  1208. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1209. const params = new Map();
  1210. params.set("type", this.stream.network);
  1211. switch (type) {
  1212. case "tcp":
  1213. const tcp = this.stream.tcp;
  1214. if (tcp.type === 'http') {
  1215. const request = tcp.request;
  1216. params.set("path", request.path.join(','));
  1217. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1218. if (index >= 0) {
  1219. const host = request.headers[index].value;
  1220. params.set("host", host);
  1221. }
  1222. params.set("headerType", 'http');
  1223. }
  1224. break;
  1225. case "kcp":
  1226. const kcp = this.stream.kcp;
  1227. params.set("headerType", kcp.type);
  1228. params.set("seed", kcp.seed);
  1229. break;
  1230. case "ws":
  1231. const ws = this.stream.ws;
  1232. params.set("path", ws.path);
  1233. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1234. break;
  1235. case "grpc":
  1236. const grpc = this.stream.grpc;
  1237. params.set("serviceName", grpc.serviceName);
  1238. params.set("authority", grpc.authority);
  1239. if (grpc.multiMode) {
  1240. params.set("mode", "multi");
  1241. }
  1242. break;
  1243. case "httpupgrade":
  1244. const httpupgrade = this.stream.httpupgrade;
  1245. params.set("path", httpupgrade.path);
  1246. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1247. break;
  1248. case "xhttp":
  1249. const xhttp = this.stream.xhttp;
  1250. params.set("path", xhttp.path);
  1251. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1252. params.set("mode", xhttp.mode);
  1253. break;
  1254. }
  1255. if (security === 'tls') {
  1256. params.set("security", "tls");
  1257. if (this.stream.isTls) {
  1258. params.set("fp", this.stream.tls.settings.fingerprint);
  1259. params.set("alpn", this.stream.tls.alpn);
  1260. if (this.stream.tls.settings.allowInsecure) {
  1261. params.set("allowInsecure", "1");
  1262. }
  1263. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1264. params.set("sni", this.stream.tls.sni);
  1265. }
  1266. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1267. params.set("ech", this.stream.tls.settings.echConfigList);
  1268. }
  1269. if (type == "tcp" && !ObjectUtil.isEmpty(flow)) {
  1270. params.set("flow", flow);
  1271. }
  1272. }
  1273. }
  1274. else if (security === 'reality') {
  1275. params.set("security", "reality");
  1276. params.set("pbk", this.stream.reality.settings.publicKey);
  1277. params.set("fp", this.stream.reality.settings.fingerprint);
  1278. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1279. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1280. }
  1281. if (this.stream.reality.shortIds.length > 0) {
  1282. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1283. }
  1284. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1285. params.set("spx", this.stream.reality.settings.spiderX);
  1286. }
  1287. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1288. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1289. }
  1290. if (type == 'tcp' && !ObjectUtil.isEmpty(flow)) {
  1291. params.set("flow", flow);
  1292. }
  1293. }
  1294. else {
  1295. params.set("security", "none");
  1296. }
  1297. const link = `vless://${uuid}@${address}:${port}`;
  1298. const url = new URL(link);
  1299. for (const [key, value] of params) {
  1300. url.searchParams.set(key, value)
  1301. }
  1302. url.hash = encodeURIComponent(remark);
  1303. return url.toString();
  1304. }
  1305. genSSLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1306. let settings = this.settings;
  1307. const type = this.stream.network;
  1308. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1309. const params = new Map();
  1310. params.set("type", this.stream.network);
  1311. switch (type) {
  1312. case "tcp":
  1313. const tcp = this.stream.tcp;
  1314. if (tcp.type === 'http') {
  1315. const request = tcp.request;
  1316. params.set("path", request.path.join(','));
  1317. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1318. if (index >= 0) {
  1319. const host = request.headers[index].value;
  1320. params.set("host", host);
  1321. }
  1322. params.set("headerType", 'http');
  1323. }
  1324. break;
  1325. case "kcp":
  1326. const kcp = this.stream.kcp;
  1327. params.set("headerType", kcp.type);
  1328. params.set("seed", kcp.seed);
  1329. break;
  1330. case "ws":
  1331. const ws = this.stream.ws;
  1332. params.set("path", ws.path);
  1333. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1334. break;
  1335. case "grpc":
  1336. const grpc = this.stream.grpc;
  1337. params.set("serviceName", grpc.serviceName);
  1338. params.set("authority", grpc.authority);
  1339. if (grpc.multiMode) {
  1340. params.set("mode", "multi");
  1341. }
  1342. break;
  1343. case "httpupgrade":
  1344. const httpupgrade = this.stream.httpupgrade;
  1345. params.set("path", httpupgrade.path);
  1346. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1347. break;
  1348. case "xhttp":
  1349. const xhttp = this.stream.xhttp;
  1350. params.set("path", xhttp.path);
  1351. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1352. params.set("mode", xhttp.mode);
  1353. break;
  1354. }
  1355. if (security === 'tls') {
  1356. params.set("security", "tls");
  1357. if (this.stream.isTls) {
  1358. params.set("fp", this.stream.tls.settings.fingerprint);
  1359. params.set("alpn", this.stream.tls.alpn);
  1360. if (this.stream.tls.settings.allowInsecure) {
  1361. params.set("allowInsecure", "1");
  1362. }
  1363. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1364. params.set("ech", this.stream.tls.settings.echConfigList);
  1365. }
  1366. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1367. params.set("sni", this.stream.tls.sni);
  1368. }
  1369. }
  1370. }
  1371. let password = new Array();
  1372. if (this.isSS2022) password.push(settings.password);
  1373. if (this.isSSMultiUser) password.push(clientPassword);
  1374. let link = `ss://${Base64.encode(`${settings.method}:${password.join(':')}`, true)}@${address}:${port}`;
  1375. const url = new URL(link);
  1376. for (const [key, value] of params) {
  1377. url.searchParams.set(key, value)
  1378. }
  1379. url.hash = encodeURIComponent(remark);
  1380. return url.toString();
  1381. }
  1382. genTrojanLink(address = '', port = this.port, forceTls, remark = '', clientPassword) {
  1383. const security = forceTls == 'same' ? this.stream.security : forceTls;
  1384. const type = this.stream.network;
  1385. const params = new Map();
  1386. params.set("type", this.stream.network);
  1387. switch (type) {
  1388. case "tcp":
  1389. const tcp = this.stream.tcp;
  1390. if (tcp.type === 'http') {
  1391. const request = tcp.request;
  1392. params.set("path", request.path.join(','));
  1393. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1394. if (index >= 0) {
  1395. const host = request.headers[index].value;
  1396. params.set("host", host);
  1397. }
  1398. params.set("headerType", 'http');
  1399. }
  1400. break;
  1401. case "kcp":
  1402. const kcp = this.stream.kcp;
  1403. params.set("headerType", kcp.type);
  1404. params.set("seed", kcp.seed);
  1405. break;
  1406. case "ws":
  1407. const ws = this.stream.ws;
  1408. params.set("path", ws.path);
  1409. params.set("host", ws.host?.length > 0 ? ws.host : this.getHeader(ws, 'host'));
  1410. break;
  1411. case "grpc":
  1412. const grpc = this.stream.grpc;
  1413. params.set("serviceName", grpc.serviceName);
  1414. params.set("authority", grpc.authority);
  1415. if (grpc.multiMode) {
  1416. params.set("mode", "multi");
  1417. }
  1418. break;
  1419. case "httpupgrade":
  1420. const httpupgrade = this.stream.httpupgrade;
  1421. params.set("path", httpupgrade.path);
  1422. params.set("host", httpupgrade.host?.length > 0 ? httpupgrade.host : this.getHeader(httpupgrade, 'host'));
  1423. break;
  1424. case "xhttp":
  1425. const xhttp = this.stream.xhttp;
  1426. params.set("path", xhttp.path);
  1427. params.set("host", xhttp.host?.length > 0 ? xhttp.host : this.getHeader(xhttp, 'host'));
  1428. params.set("mode", xhttp.mode);
  1429. break;
  1430. }
  1431. if (security === 'tls') {
  1432. params.set("security", "tls");
  1433. if (this.stream.isTls) {
  1434. params.set("fp", this.stream.tls.settings.fingerprint);
  1435. params.set("alpn", this.stream.tls.alpn);
  1436. if (this.stream.tls.settings.allowInsecure) {
  1437. params.set("allowInsecure", "1");
  1438. }
  1439. if (this.stream.tls.settings.echConfigList?.length > 0) {
  1440. params.set("ech", this.stream.tls.settings.echConfigList);
  1441. }
  1442. if (!ObjectUtil.isEmpty(this.stream.tls.sni)) {
  1443. params.set("sni", this.stream.tls.sni);
  1444. }
  1445. }
  1446. }
  1447. else if (security === 'reality') {
  1448. params.set("security", "reality");
  1449. params.set("pbk", this.stream.reality.settings.publicKey);
  1450. params.set("fp", this.stream.reality.settings.fingerprint);
  1451. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1452. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1453. }
  1454. if (this.stream.reality.shortIds.length > 0) {
  1455. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1456. }
  1457. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1458. params.set("spx", this.stream.reality.settings.spiderX);
  1459. }
  1460. if (!ObjectUtil.isEmpty(this.stream.reality.settings.mldsa65Verify)) {
  1461. params.set("pqv", this.stream.reality.settings.mldsa65Verify);
  1462. }
  1463. }
  1464. else {
  1465. params.set("security", "none");
  1466. }
  1467. const link = `trojan://${clientPassword}@${address}:${port}`;
  1468. const url = new URL(link);
  1469. for (const [key, value] of params) {
  1470. url.searchParams.set(key, value)
  1471. }
  1472. url.hash = encodeURIComponent(remark);
  1473. return url.toString();
  1474. }
  1475. getWireguardLink(address, port, remark, peerId) {
  1476. let txt = `[Interface]\n`
  1477. txt += `PrivateKey = ${this.settings.peers[peerId].privateKey}\n`
  1478. txt += `Address = ${this.settings.peers[peerId].allowedIPs[0]}\n`
  1479. txt += `DNS = 1.1.1.1, 1.0.0.1\n`
  1480. if (this.settings.mtu) {
  1481. txt += `MTU = ${this.settings.mtu}\n`
  1482. }
  1483. txt += `\n# ${remark}\n`
  1484. txt += `[Peer]\n`
  1485. txt += `PublicKey = ${this.settings.pubKey}\n`
  1486. txt += `AllowedIPs = 0.0.0.0/0, ::/0\n`
  1487. txt += `Endpoint = ${address}:${port}`
  1488. if (this.settings.peers[peerId].psk) {
  1489. txt += `\nPresharedKey = ${this.settings.peers[peerId].psk}`
  1490. }
  1491. if (this.settings.peers[peerId].keepAlive) {
  1492. txt += `\nPersistentKeepalive = ${this.settings.peers[peerId].keepAlive}\n`
  1493. }
  1494. return txt;
  1495. }
  1496. genLink(address = '', port = this.port, forceTls = 'same', remark = '', client) {
  1497. switch (this.protocol) {
  1498. case Protocols.VMESS:
  1499. return this.genVmessLink(address, port, forceTls, remark, client.id, client.security);
  1500. case Protocols.VLESS:
  1501. return this.genVLESSLink(address, port, forceTls, remark, client.id, client.flow);
  1502. case Protocols.SHADOWSOCKS:
  1503. return this.genSSLink(address, port, forceTls, remark, this.isSSMultiUser ? client.password : '');
  1504. case Protocols.TROJAN:
  1505. return this.genTrojanLink(address, port, forceTls, remark, client.password);
  1506. default: return '';
  1507. }
  1508. }
  1509. genAllLinks(remark = '', remarkModel = '-ieo', client) {
  1510. let result = [];
  1511. let email = client ? client.email : '';
  1512. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1513. let port = this.port;
  1514. const separationChar = remarkModel.charAt(0);
  1515. const orderChars = remarkModel.slice(1);
  1516. let orders = {
  1517. 'i': remark,
  1518. 'e': email,
  1519. 'o': '',
  1520. };
  1521. if (ObjectUtil.isArrEmpty(this.stream.externalProxy)) {
  1522. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1523. result.push({
  1524. remark: r,
  1525. link: this.genLink(addr, port, 'same', r, client)
  1526. });
  1527. } else {
  1528. this.stream.externalProxy.forEach((ep) => {
  1529. orders['o'] = ep.remark;
  1530. let r = orderChars.split('').map(char => orders[char]).filter(x => x.length > 0).join(separationChar);
  1531. result.push({
  1532. remark: r,
  1533. link: this.genLink(ep.dest, ep.port, ep.forceTls, r, client)
  1534. });
  1535. });
  1536. }
  1537. return result;
  1538. }
  1539. genInboundLinks(remark = '', remarkModel = '-ieo') {
  1540. let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname;
  1541. if (this.clients) {
  1542. let links = [];
  1543. this.clients.forEach((client) => {
  1544. this.genAllLinks(remark, remarkModel, client).forEach(l => {
  1545. links.push(l.link);
  1546. })
  1547. });
  1548. return links.join('\r\n');
  1549. } else {
  1550. if (this.protocol == Protocols.SHADOWSOCKS && !this.isSSMultiUser) return this.genSSLink(addr, this.port, 'same', remark);
  1551. if (this.protocol == Protocols.WIREGUARD) {
  1552. let links = [];
  1553. this.settings.peers.forEach((p, index) => {
  1554. links.push(this.getWireguardLink(addr, this.port, remark + remarkModel.charAt(0) + (index + 1), index));
  1555. });
  1556. return links.join('\r\n');
  1557. }
  1558. return '';
  1559. }
  1560. }
  1561. static fromJson(json = {}) {
  1562. return new Inbound(
  1563. json.port,
  1564. json.listen,
  1565. json.protocol,
  1566. Inbound.Settings.fromJson(json.protocol, json.settings),
  1567. StreamSettings.fromJson(json.streamSettings),
  1568. json.tag,
  1569. Sniffing.fromJson(json.sniffing),
  1570. Allocate.fromJson(json.allocate),
  1571. json.clientStats
  1572. )
  1573. }
  1574. toJson() {
  1575. let streamSettings;
  1576. if (this.canEnableStream()) {
  1577. streamSettings = this.stream.toJson();
  1578. }
  1579. return {
  1580. port: this.port,
  1581. listen: this.listen,
  1582. protocol: this.protocol,
  1583. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1584. streamSettings: streamSettings,
  1585. tag: this.tag,
  1586. sniffing: this.sniffing.toJson(),
  1587. allocate: this.allocate.toJson(),
  1588. clientStats: this.clientStats
  1589. };
  1590. }
  1591. }
  1592. Inbound.Settings = class extends XrayCommonClass {
  1593. constructor(protocol) {
  1594. super();
  1595. this.protocol = protocol;
  1596. }
  1597. static getSettings(protocol) {
  1598. switch (protocol) {
  1599. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1600. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1601. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1602. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1603. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1604. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1605. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1606. case Protocols.WIREGUARD: return new Inbound.WireguardSettings(protocol);
  1607. default: return null;
  1608. }
  1609. }
  1610. static fromJson(protocol, json) {
  1611. switch (protocol) {
  1612. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1613. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1614. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1615. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1616. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1617. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1618. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1619. case Protocols.WIREGUARD: return Inbound.WireguardSettings.fromJson(json);
  1620. default: return null;
  1621. }
  1622. }
  1623. toJson() {
  1624. return {};
  1625. }
  1626. };
  1627. Inbound.VmessSettings = class extends Inbound.Settings {
  1628. constructor(protocol,
  1629. vmesses = [new Inbound.VmessSettings.VMESS()]) {
  1630. super(protocol);
  1631. this.vmesses = vmesses;
  1632. }
  1633. indexOfVmessById(id) {
  1634. return this.vmesses.findIndex(VMESS => VMESS.id === id);
  1635. }
  1636. addVmess(VMESS) {
  1637. if (this.indexOfVmessById(VMESS.id) >= 0) {
  1638. return false;
  1639. }
  1640. this.vmesses.push(VMESS);
  1641. }
  1642. delVmess(VMESS) {
  1643. const i = this.indexOfVmessById(VMESS.id);
  1644. if (i >= 0) {
  1645. this.vmesses.splice(i, 1);
  1646. }
  1647. }
  1648. static fromJson(json = {}) {
  1649. return new Inbound.VmessSettings(
  1650. Protocols.VMESS,
  1651. json.clients.map(client => Inbound.VmessSettings.VMESS.fromJson(client)),
  1652. );
  1653. }
  1654. toJson() {
  1655. return {
  1656. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1657. };
  1658. }
  1659. };
  1660. Inbound.VmessSettings.VMESS = class extends XrayCommonClass {
  1661. constructor(
  1662. id = RandomUtil.randomUUID(),
  1663. security = USERS_SECURITY.AUTO,
  1664. email = RandomUtil.randomLowerAndNum(8),
  1665. limitIp = 0,
  1666. totalGB = 0,
  1667. expiryTime = 0,
  1668. enable = true,
  1669. tgId = '',
  1670. subId = RandomUtil.randomLowerAndNum(16),
  1671. comment = '',
  1672. reset = 0
  1673. ) {
  1674. super();
  1675. this.id = id;
  1676. this.security = security;
  1677. this.email = email;
  1678. this.limitIp = limitIp;
  1679. this.totalGB = totalGB;
  1680. this.expiryTime = expiryTime;
  1681. this.enable = enable;
  1682. this.tgId = tgId;
  1683. this.subId = subId;
  1684. this.comment = comment;
  1685. this.reset = reset;
  1686. }
  1687. static fromJson(json = {}) {
  1688. return new Inbound.VmessSettings.VMESS(
  1689. json.id,
  1690. json.security,
  1691. json.email,
  1692. json.limitIp,
  1693. json.totalGB,
  1694. json.expiryTime,
  1695. json.enable,
  1696. json.tgId,
  1697. json.subId,
  1698. json.comment,
  1699. json.reset,
  1700. );
  1701. }
  1702. get _expiryTime() {
  1703. if (this.expiryTime === 0 || this.expiryTime === "") {
  1704. return null;
  1705. }
  1706. if (this.expiryTime < 0) {
  1707. return this.expiryTime / -86400000;
  1708. }
  1709. return moment(this.expiryTime);
  1710. }
  1711. set _expiryTime(t) {
  1712. if (t == null || t === "") {
  1713. this.expiryTime = 0;
  1714. } else {
  1715. this.expiryTime = t.valueOf();
  1716. }
  1717. }
  1718. get _totalGB() {
  1719. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1720. }
  1721. set _totalGB(gb) {
  1722. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1723. }
  1724. };
  1725. Inbound.VLESSSettings = class extends Inbound.Settings {
  1726. constructor(
  1727. protocol,
  1728. vlesses = [new Inbound.VLESSSettings.VLESS()],
  1729. decryption = 'none',
  1730. fallbacks = []
  1731. ) {
  1732. super(protocol);
  1733. this.vlesses = vlesses;
  1734. this.decryption = decryption;
  1735. this.fallbacks = fallbacks;
  1736. }
  1737. addFallback() {
  1738. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1739. }
  1740. delFallback(index) {
  1741. this.fallbacks.splice(index, 1);
  1742. }
  1743. // decryption should be set to static value
  1744. static fromJson(json = {}) {
  1745. return new Inbound.VLESSSettings(
  1746. Protocols.VLESS,
  1747. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1748. json.decryption || 'none',
  1749. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),);
  1750. }
  1751. toJson() {
  1752. return {
  1753. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1754. decryption: this.decryption,
  1755. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1756. };
  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. ) {
  1773. super();
  1774. this.id = id;
  1775. this.flow = flow;
  1776. this.email = email;
  1777. this.limitIp = limitIp;
  1778. this.totalGB = totalGB;
  1779. this.expiryTime = expiryTime;
  1780. this.enable = enable;
  1781. this.tgId = tgId;
  1782. this.subId = subId;
  1783. this.comment = comment;
  1784. this.reset = reset;
  1785. }
  1786. static fromJson(json = {}) {
  1787. return new Inbound.VLESSSettings.VLESS(
  1788. json.id,
  1789. json.flow,
  1790. json.email,
  1791. json.limitIp,
  1792. json.totalGB,
  1793. json.expiryTime,
  1794. json.enable,
  1795. json.tgId,
  1796. json.subId,
  1797. json.comment,
  1798. json.reset,
  1799. );
  1800. }
  1801. get _expiryTime() {
  1802. if (this.expiryTime === 0 || this.expiryTime === "") {
  1803. return null;
  1804. }
  1805. if (this.expiryTime < 0) {
  1806. return this.expiryTime / -86400000;
  1807. }
  1808. return moment(this.expiryTime);
  1809. }
  1810. set _expiryTime(t) {
  1811. if (t == null || t === "") {
  1812. this.expiryTime = 0;
  1813. } else {
  1814. this.expiryTime = t.valueOf();
  1815. }
  1816. }
  1817. get _totalGB() {
  1818. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1819. }
  1820. set _totalGB(gb) {
  1821. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1822. }
  1823. };
  1824. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1825. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1826. super();
  1827. this.name = name;
  1828. this.alpn = alpn;
  1829. this.path = path;
  1830. this.dest = dest;
  1831. this.xver = xver;
  1832. }
  1833. toJson() {
  1834. let xver = this.xver;
  1835. if (!Number.isInteger(xver)) {
  1836. xver = 0;
  1837. }
  1838. return {
  1839. name: this.name,
  1840. alpn: this.alpn,
  1841. path: this.path,
  1842. dest: this.dest,
  1843. xver: xver,
  1844. }
  1845. }
  1846. static fromJson(json = []) {
  1847. const fallbacks = [];
  1848. for (let fallback of json) {
  1849. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1850. fallback.name,
  1851. fallback.alpn,
  1852. fallback.path,
  1853. fallback.dest,
  1854. fallback.xver,
  1855. ))
  1856. }
  1857. return fallbacks;
  1858. }
  1859. };
  1860. Inbound.TrojanSettings = class extends Inbound.Settings {
  1861. constructor(protocol,
  1862. trojans = [new Inbound.TrojanSettings.Trojan()],
  1863. fallbacks = [],) {
  1864. super(protocol);
  1865. this.trojans = trojans;
  1866. this.fallbacks = fallbacks;
  1867. }
  1868. addFallback() {
  1869. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1870. }
  1871. delFallback(index) {
  1872. this.fallbacks.splice(index, 1);
  1873. }
  1874. static fromJson(json = {}) {
  1875. return new Inbound.TrojanSettings(
  1876. Protocols.TROJAN,
  1877. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1878. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1879. }
  1880. toJson() {
  1881. return {
  1882. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1883. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks)
  1884. };
  1885. }
  1886. };
  1887. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1888. constructor(
  1889. password = RandomUtil.randomSeq(10),
  1890. email = RandomUtil.randomLowerAndNum(8),
  1891. limitIp = 0,
  1892. totalGB = 0,
  1893. expiryTime = 0,
  1894. enable = true,
  1895. tgId = '',
  1896. subId = RandomUtil.randomLowerAndNum(16),
  1897. comment = '',
  1898. reset = 0
  1899. ) {
  1900. super();
  1901. this.password = password;
  1902. this.email = email;
  1903. this.limitIp = limitIp;
  1904. this.totalGB = totalGB;
  1905. this.expiryTime = expiryTime;
  1906. this.enable = enable;
  1907. this.tgId = tgId;
  1908. this.subId = subId;
  1909. this.comment = comment;
  1910. this.reset = reset;
  1911. }
  1912. toJson() {
  1913. return {
  1914. password: this.password,
  1915. email: this.email,
  1916. limitIp: this.limitIp,
  1917. totalGB: this.totalGB,
  1918. expiryTime: this.expiryTime,
  1919. enable: this.enable,
  1920. tgId: this.tgId,
  1921. subId: this.subId,
  1922. comment: this.comment,
  1923. reset: this.reset,
  1924. };
  1925. }
  1926. static fromJson(json = {}) {
  1927. return new Inbound.TrojanSettings.Trojan(
  1928. json.password,
  1929. json.email,
  1930. json.limitIp,
  1931. json.totalGB,
  1932. json.expiryTime,
  1933. json.enable,
  1934. json.tgId,
  1935. json.subId,
  1936. json.comment,
  1937. json.reset,
  1938. );
  1939. }
  1940. get _expiryTime() {
  1941. if (this.expiryTime === 0 || this.expiryTime === "") {
  1942. return null;
  1943. }
  1944. if (this.expiryTime < 0) {
  1945. return this.expiryTime / -86400000;
  1946. }
  1947. return moment(this.expiryTime);
  1948. }
  1949. set _expiryTime(t) {
  1950. if (t == null || t === "") {
  1951. this.expiryTime = 0;
  1952. } else {
  1953. this.expiryTime = t.valueOf();
  1954. }
  1955. }
  1956. get _totalGB() {
  1957. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  1958. }
  1959. set _totalGB(gb) {
  1960. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  1961. }
  1962. };
  1963. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1964. constructor(name = "", alpn = '', path = '', dest = '', xver = 0) {
  1965. super();
  1966. this.name = name;
  1967. this.alpn = alpn;
  1968. this.path = path;
  1969. this.dest = dest;
  1970. this.xver = xver;
  1971. }
  1972. toJson() {
  1973. let xver = this.xver;
  1974. if (!Number.isInteger(xver)) {
  1975. xver = 0;
  1976. }
  1977. return {
  1978. name: this.name,
  1979. alpn: this.alpn,
  1980. path: this.path,
  1981. dest: this.dest,
  1982. xver: xver,
  1983. }
  1984. }
  1985. static fromJson(json = []) {
  1986. const fallbacks = [];
  1987. for (let fallback of json) {
  1988. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1989. fallback.name,
  1990. fallback.alpn,
  1991. fallback.path,
  1992. fallback.dest,
  1993. fallback.xver,
  1994. ))
  1995. }
  1996. return fallbacks;
  1997. }
  1998. };
  1999. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  2000. constructor(protocol,
  2001. method = SSMethods.BLAKE3_AES_256_GCM,
  2002. password = RandomUtil.randomShadowsocksPassword(),
  2003. network = 'tcp,udp',
  2004. shadowsockses = [new Inbound.ShadowsocksSettings.Shadowsocks()],
  2005. ivCheck = false,
  2006. ) {
  2007. super(protocol);
  2008. this.method = method;
  2009. this.password = password;
  2010. this.network = network;
  2011. this.shadowsockses = shadowsockses;
  2012. this.ivCheck = ivCheck;
  2013. }
  2014. static fromJson(json = {}) {
  2015. return new Inbound.ShadowsocksSettings(
  2016. Protocols.SHADOWSOCKS,
  2017. json.method,
  2018. json.password,
  2019. json.network,
  2020. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  2021. json.ivCheck,
  2022. );
  2023. }
  2024. toJson() {
  2025. return {
  2026. method: this.method,
  2027. password: this.password,
  2028. network: this.network,
  2029. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses),
  2030. ivCheck: this.ivCheck,
  2031. };
  2032. }
  2033. };
  2034. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  2035. constructor(
  2036. method = '',
  2037. password = RandomUtil.randomShadowsocksPassword(),
  2038. email = RandomUtil.randomLowerAndNum(8),
  2039. limitIp = 0,
  2040. totalGB = 0,
  2041. expiryTime = 0,
  2042. enable = true,
  2043. tgId = '',
  2044. subId = RandomUtil.randomLowerAndNum(16),
  2045. comment = '',
  2046. reset = 0
  2047. ) {
  2048. super();
  2049. this.method = method;
  2050. this.password = password;
  2051. this.email = email;
  2052. this.limitIp = limitIp;
  2053. this.totalGB = totalGB;
  2054. this.expiryTime = expiryTime;
  2055. this.enable = enable;
  2056. this.tgId = tgId;
  2057. this.subId = subId;
  2058. this.comment = comment;
  2059. this.reset = reset;
  2060. }
  2061. toJson() {
  2062. return {
  2063. method: this.method,
  2064. password: this.password,
  2065. email: this.email,
  2066. limitIp: this.limitIp,
  2067. totalGB: this.totalGB,
  2068. expiryTime: this.expiryTime,
  2069. enable: this.enable,
  2070. tgId: this.tgId,
  2071. subId: this.subId,
  2072. comment: this.comment,
  2073. reset: this.reset,
  2074. };
  2075. }
  2076. static fromJson(json = {}) {
  2077. return new Inbound.ShadowsocksSettings.Shadowsocks(
  2078. json.method,
  2079. json.password,
  2080. json.email,
  2081. json.limitIp,
  2082. json.totalGB,
  2083. json.expiryTime,
  2084. json.enable,
  2085. json.tgId,
  2086. json.subId,
  2087. json.comment,
  2088. json.reset,
  2089. );
  2090. }
  2091. get _expiryTime() {
  2092. if (this.expiryTime === 0 || this.expiryTime === "") {
  2093. return null;
  2094. }
  2095. if (this.expiryTime < 0) {
  2096. return this.expiryTime / -86400000;
  2097. }
  2098. return moment(this.expiryTime);
  2099. }
  2100. set _expiryTime(t) {
  2101. if (t == null || t === "") {
  2102. this.expiryTime = 0;
  2103. } else {
  2104. this.expiryTime = t.valueOf();
  2105. }
  2106. }
  2107. get _totalGB() {
  2108. return NumberFormatter.toFixed(this.totalGB / SizeFormatter.ONE_GB, 2);
  2109. }
  2110. set _totalGB(gb) {
  2111. this.totalGB = NumberFormatter.toFixed(gb * SizeFormatter.ONE_GB, 0);
  2112. }
  2113. };
  2114. Inbound.DokodemoSettings = class extends Inbound.Settings {
  2115. constructor(
  2116. protocol,
  2117. address,
  2118. port,
  2119. portMap = [],
  2120. network = 'tcp,udp',
  2121. followRedirect = false
  2122. ) {
  2123. super(protocol);
  2124. this.address = address;
  2125. this.port = port;
  2126. this.portMap = portMap;
  2127. this.network = network;
  2128. this.followRedirect = followRedirect;
  2129. }
  2130. static fromJson(json = {}) {
  2131. return new Inbound.DokodemoSettings(
  2132. Protocols.DOKODEMO,
  2133. json.address,
  2134. json.port,
  2135. XrayCommonClass.toHeaders(json.portMap),
  2136. json.network,
  2137. json.followRedirect,
  2138. );
  2139. }
  2140. toJson() {
  2141. return {
  2142. address: this.address,
  2143. port: this.port,
  2144. portMap: XrayCommonClass.toV2Headers(this.portMap, false),
  2145. network: this.network,
  2146. followRedirect: this.followRedirect,
  2147. };
  2148. }
  2149. };
  2150. Inbound.SocksSettings = class extends Inbound.Settings {
  2151. constructor(protocol, auth = 'password', accounts = [new Inbound.SocksSettings.SocksAccount()], udp = false, ip = '127.0.0.1') {
  2152. super(protocol);
  2153. this.auth = auth;
  2154. this.accounts = accounts;
  2155. this.udp = udp;
  2156. this.ip = ip;
  2157. }
  2158. addAccount(account) {
  2159. this.accounts.push(account);
  2160. }
  2161. delAccount(index) {
  2162. this.accounts.splice(index, 1);
  2163. }
  2164. static fromJson(json = {}) {
  2165. let accounts;
  2166. if (json.auth === 'password') {
  2167. accounts = json.accounts.map(
  2168. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  2169. )
  2170. }
  2171. return new Inbound.SocksSettings(
  2172. Protocols.SOCKS,
  2173. json.auth,
  2174. accounts,
  2175. json.udp,
  2176. json.ip,
  2177. );
  2178. }
  2179. toJson() {
  2180. return {
  2181. auth: this.auth,
  2182. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  2183. udp: this.udp,
  2184. ip: this.ip,
  2185. };
  2186. }
  2187. };
  2188. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  2189. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2190. super();
  2191. this.user = user;
  2192. this.pass = pass;
  2193. }
  2194. static fromJson(json = {}) {
  2195. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  2196. }
  2197. };
  2198. Inbound.HttpSettings = class extends Inbound.Settings {
  2199. constructor(
  2200. protocol,
  2201. accounts = [new Inbound.HttpSettings.HttpAccount()],
  2202. allowTransparent = false,
  2203. ) {
  2204. super(protocol);
  2205. this.accounts = accounts;
  2206. this.allowTransparent = allowTransparent;
  2207. }
  2208. addAccount(account) {
  2209. this.accounts.push(account);
  2210. }
  2211. delAccount(index) {
  2212. this.accounts.splice(index, 1);
  2213. }
  2214. static fromJson(json = {}) {
  2215. return new Inbound.HttpSettings(
  2216. Protocols.HTTP,
  2217. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  2218. json.allowTransparent,
  2219. );
  2220. }
  2221. toJson() {
  2222. return {
  2223. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2224. allowTransparent: this.allowTransparent,
  2225. };
  2226. }
  2227. };
  2228. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2229. constructor(user = RandomUtil.randomSeq(10), pass = RandomUtil.randomSeq(10)) {
  2230. super();
  2231. this.user = user;
  2232. this.pass = pass;
  2233. }
  2234. static fromJson(json = {}) {
  2235. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2236. }
  2237. };
  2238. Inbound.WireguardSettings = class extends XrayCommonClass {
  2239. constructor(
  2240. protocol,
  2241. mtu = 1420,
  2242. secretKey = Wireguard.generateKeypair().privateKey,
  2243. peers = [new Inbound.WireguardSettings.Peer()],
  2244. noKernelTun = false
  2245. ) {
  2246. super(protocol);
  2247. this.mtu = mtu;
  2248. this.secretKey = secretKey;
  2249. this.pubKey = secretKey.length > 0 ? Wireguard.generateKeypair(secretKey).publicKey : '';
  2250. this.peers = peers;
  2251. this.noKernelTun = noKernelTun;
  2252. }
  2253. addPeer() {
  2254. this.peers.push(new Inbound.WireguardSettings.Peer(null, null, '', ['10.0.0.' + (this.peers.length + 2)]));
  2255. }
  2256. delPeer(index) {
  2257. this.peers.splice(index, 1);
  2258. }
  2259. static fromJson(json = {}) {
  2260. return new Inbound.WireguardSettings(
  2261. Protocols.WIREGUARD,
  2262. json.mtu,
  2263. json.secretKey,
  2264. json.peers.map(peer => Inbound.WireguardSettings.Peer.fromJson(peer)),
  2265. json.noKernelTun,
  2266. );
  2267. }
  2268. toJson() {
  2269. return {
  2270. mtu: this.mtu ?? undefined,
  2271. secretKey: this.secretKey,
  2272. peers: Inbound.WireguardSettings.Peer.toJsonArray(this.peers),
  2273. noKernelTun: this.noKernelTun,
  2274. };
  2275. }
  2276. };
  2277. Inbound.WireguardSettings.Peer = class extends XrayCommonClass {
  2278. constructor(privateKey, publicKey, psk = '', allowedIPs = ['10.0.0.2/32'], keepAlive = 0) {
  2279. super();
  2280. this.privateKey = privateKey
  2281. this.publicKey = publicKey;
  2282. if (!this.publicKey) {
  2283. [this.publicKey, this.privateKey] = Object.values(Wireguard.generateKeypair())
  2284. }
  2285. this.psk = psk;
  2286. allowedIPs.forEach((a, index) => {
  2287. if (a.length > 0 && !a.includes('/')) allowedIPs[index] += '/32';
  2288. })
  2289. this.allowedIPs = allowedIPs;
  2290. this.keepAlive = keepAlive;
  2291. }
  2292. static fromJson(json = {}) {
  2293. return new Inbound.WireguardSettings.Peer(
  2294. json.privateKey,
  2295. json.publicKey,
  2296. json.preSharedKey,
  2297. json.allowedIPs,
  2298. json.keepAlive
  2299. );
  2300. }
  2301. toJson() {
  2302. this.allowedIPs.forEach((a, index) => {
  2303. if (a.length > 0 && !a.includes('/')) this.allowedIPs[index] += '/32';
  2304. });
  2305. return {
  2306. privateKey: this.privateKey,
  2307. publicKey: this.publicKey,
  2308. preSharedKey: this.psk.length > 0 ? this.psk : undefined,
  2309. allowedIPs: this.allowedIPs,
  2310. keepAlive: this.keepAlive ?? undefined,
  2311. };
  2312. }
  2313. };