inbound.js 77 KB

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