inbound.js 76 KB

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