1
0

inbound.js 74 KB

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