inbound.js 75 KB

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