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