inbound.js 76 KB

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