inbound.js 74 KB

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