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_UNSAFE: "unsafe",
  59. };
  60. const ALPN_OPTION = {
  61. H3: "h3",
  62. H2: "h2",
  63. HTTP1: "http/1.1",
  64. };
  65. const SNIFFING_OPTION = {
  66. HTTP: "http",
  67. TLS: "tls",
  68. QUIC: "quic",
  69. FAKEDNS: "fakedns"
  70. };
  71. const USAGE_OPTION = {
  72. ENCIPHERMENT: "encipherment",
  73. VERIFY: "verify",
  74. ISSUE: "issue",
  75. };
  76. const DOMAIN_STRATEGY_OPTION = {
  77. AS_IS: "AsIs",
  78. USE_IP: "UseIP",
  79. USE_IPV6V4: "UseIPv6v4",
  80. USE_IPV6: "UseIPv6",
  81. USE_IPV4V6: "UseIPv4v6",
  82. USE_IPV4: "UseIPv4",
  83. FORCE_IP: "ForceIP",
  84. FORCE_IPV6V4: "ForceIPv6v4",
  85. FORCE_IPV6: "ForceIPv6",
  86. FORCE_IPV4V6: "ForceIPv4v6",
  87. FORCE_IPV4: "ForceIPv4",
  88. };
  89. const TCP_CONGESTION_OPTION = {
  90. BBR: "bbr",
  91. CUBIC: "cubic",
  92. RENO: "reno",
  93. };
  94. const USERS_SECURITY = {
  95. AES_128_GCM: "aes-128-gcm",
  96. CHACHA20_POLY1305: "chacha20-poly1305",
  97. AUTO: "auto",
  98. NONE: "none",
  99. ZERO: "zero",
  100. };
  101. const MODE_OPTION = {
  102. AUTO: "auto",
  103. PACKET_UP: "packet-up",
  104. STREAM_UP: "stream-up",
  105. STREAM_ONE: "stream-one",
  106. };
  107. Object.freeze(Protocols);
  108. Object.freeze(SSMethods);
  109. Object.freeze(TLS_FLOW_CONTROL);
  110. Object.freeze(TLS_VERSION_OPTION);
  111. Object.freeze(TLS_CIPHER_OPTION);
  112. Object.freeze(UTLS_FINGERPRINT);
  113. Object.freeze(ALPN_OPTION);
  114. Object.freeze(SNIFFING_OPTION);
  115. Object.freeze(USAGE_OPTION);
  116. Object.freeze(DOMAIN_STRATEGY_OPTION);
  117. Object.freeze(TCP_CONGESTION_OPTION);
  118. Object.freeze(USERS_SECURITY);
  119. Object.freeze(MODE_OPTION);
  120. class XrayCommonClass {
  121. static toJsonArray(arr) {
  122. return arr.map(obj => obj.toJson());
  123. }
  124. static fromJson() {
  125. return new XrayCommonClass();
  126. }
  127. toJson() {
  128. return this;
  129. }
  130. toString(format = true) {
  131. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  132. }
  133. static toHeaders(v2Headers) {
  134. let newHeaders = [];
  135. if (v2Headers) {
  136. Object.keys(v2Headers).forEach(key => {
  137. let values = v2Headers[key];
  138. if (typeof (values) === 'string') {
  139. newHeaders.push({ name: key, value: values });
  140. } else {
  141. for (let i = 0; i < values.length; ++i) {
  142. newHeaders.push({ name: key, value: values[i] });
  143. }
  144. }
  145. });
  146. }
  147. return newHeaders;
  148. }
  149. static toV2Headers(headers, arr = true) {
  150. let v2Headers = {};
  151. for (let i = 0; i < headers.length; ++i) {
  152. let name = headers[i].name;
  153. let value = headers[i].value;
  154. if (ObjectUtil.isEmpty(name) || ObjectUtil.isEmpty(value)) {
  155. continue;
  156. }
  157. if (!(name in v2Headers)) {
  158. v2Headers[name] = arr ? [value] : value;
  159. } else {
  160. if (arr) {
  161. v2Headers[name].push(value);
  162. } else {
  163. v2Headers[name] = value;
  164. }
  165. }
  166. }
  167. return v2Headers;
  168. }
  169. }
  170. class TcpStreamSettings extends XrayCommonClass {
  171. constructor(
  172. acceptProxyProtocol = false,
  173. type = 'none',
  174. request = new TcpStreamSettings.TcpRequest(),
  175. response = new TcpStreamSettings.TcpResponse(),
  176. ) {
  177. super();
  178. this.acceptProxyProtocol = acceptProxyProtocol;
  179. this.type = type;
  180. this.request = request;
  181. this.response = response;
  182. }
  183. static fromJson(json = {}) {
  184. let header = json.header;
  185. if (!header) {
  186. header = {};
  187. }
  188. return new TcpStreamSettings(json.acceptProxyProtocol,
  189. header.type,
  190. TcpStreamSettings.TcpRequest.fromJson(header.request),
  191. TcpStreamSettings.TcpResponse.fromJson(header.response),
  192. );
  193. }
  194. toJson() {
  195. return {
  196. acceptProxyProtocol: this.acceptProxyProtocol,
  197. header: {
  198. type: this.type,
  199. request: this.type === 'http' ? this.request.toJson() : undefined,
  200. response: this.type === 'http' ? this.response.toJson() : undefined,
  201. },
  202. };
  203. }
  204. }
  205. TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
  206. constructor(
  207. version = '1.1',
  208. method = 'GET',
  209. path = ['/'],
  210. headers = [],
  211. ) {
  212. super();
  213. this.version = version;
  214. this.method = method;
  215. this.path = path.length === 0 ? ['/'] : path;
  216. this.headers = headers;
  217. }
  218. addPath(path) {
  219. this.path.push(path);
  220. }
  221. removePath(index) {
  222. this.path.splice(index, 1);
  223. }
  224. addHeader(name, value) {
  225. this.headers.push({ name: name, value: value });
  226. }
  227. removeHeader(index) {
  228. this.headers.splice(index, 1);
  229. }
  230. static fromJson(json = {}) {
  231. return new TcpStreamSettings.TcpRequest(
  232. json.version,
  233. json.method,
  234. json.path,
  235. XrayCommonClass.toHeaders(json.headers),
  236. );
  237. }
  238. toJson() {
  239. return {
  240. version: this.version,
  241. method: this.method,
  242. path: ObjectUtil.clone(this.path),
  243. headers: XrayCommonClass.toV2Headers(this.headers),
  244. };
  245. }
  246. };
  247. TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
  248. constructor(
  249. version = '1.1',
  250. status = '200',
  251. reason = 'OK',
  252. headers = [],
  253. ) {
  254. super();
  255. this.version = version;
  256. this.status = status;
  257. this.reason = reason;
  258. this.headers = headers;
  259. }
  260. addHeader(name, value) {
  261. this.headers.push({ name: name, value: value });
  262. }
  263. removeHeader(index) {
  264. this.headers.splice(index, 1);
  265. }
  266. static fromJson(json = {}) {
  267. return new TcpStreamSettings.TcpResponse(
  268. json.version,
  269. json.status,
  270. json.reason,
  271. XrayCommonClass.toHeaders(json.headers),
  272. );
  273. }
  274. toJson() {
  275. return {
  276. version: this.version,
  277. status: this.status,
  278. reason: this.reason,
  279. headers: XrayCommonClass.toV2Headers(this.headers),
  280. };
  281. }
  282. };
  283. class KcpStreamSettings extends XrayCommonClass {
  284. constructor(
  285. mtu = 1350,
  286. tti = 50,
  287. uplinkCapacity = 5,
  288. downlinkCapacity = 20,
  289. congestion = false,
  290. readBufferSize = 2,
  291. writeBufferSize = 2,
  292. type = 'none',
  293. seed = RandomUtil.randomSeq(10),
  294. ) {
  295. super();
  296. this.mtu = mtu;
  297. this.tti = tti;
  298. this.upCap = uplinkCapacity;
  299. this.downCap = downlinkCapacity;
  300. this.congestion = congestion;
  301. this.readBuffer = readBufferSize;
  302. this.writeBuffer = writeBufferSize;
  303. this.type = type;
  304. this.seed = seed;
  305. }
  306. static fromJson(json = {}) {
  307. return new KcpStreamSettings(
  308. json.mtu,
  309. json.tti,
  310. json.uplinkCapacity,
  311. json.downlinkCapacity,
  312. json.congestion,
  313. json.readBufferSize,
  314. json.writeBufferSize,
  315. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  316. json.seed,
  317. );
  318. }
  319. toJson() {
  320. return {
  321. mtu: this.mtu,
  322. tti: this.tti,
  323. uplinkCapacity: this.upCap,
  324. downlinkCapacity: this.downCap,
  325. congestion: this.congestion,
  326. readBufferSize: this.readBuffer,
  327. writeBufferSize: this.writeBuffer,
  328. header: {
  329. type: this.type,
  330. },
  331. seed: this.seed,
  332. };
  333. }
  334. }
  335. class WsStreamSettings extends XrayCommonClass {
  336. constructor(
  337. acceptProxyProtocol = false,
  338. path = '/',
  339. host = '',
  340. headers = [],
  341. heartbeatPeriod = 0,
  342. ) {
  343. super();
  344. this.acceptProxyProtocol = acceptProxyProtocol;
  345. this.path = path;
  346. this.host = host;
  347. this.headers = headers;
  348. this.heartbeatPeriod = heartbeatPeriod;
  349. }
  350. addHeader(name, value) {
  351. this.headers.push({ name: name, value: value });
  352. }
  353. removeHeader(index) {
  354. this.headers.splice(index, 1);
  355. }
  356. static fromJson(json = {}) {
  357. return new WsStreamSettings(
  358. json.acceptProxyProtocol,
  359. json.path,
  360. json.host,
  361. XrayCommonClass.toHeaders(json.headers),
  362. json.heartbeatPeriod,
  363. );
  364. }
  365. toJson() {
  366. return {
  367. acceptProxyProtocol: this.acceptProxyProtocol,
  368. path: this.path,
  369. host: this.host,
  370. headers: XrayCommonClass.toV2Headers(this.headers, false),
  371. heartbeatPeriod: this.heartbeatPeriod,
  372. };
  373. }
  374. }
  375. class GrpcStreamSettings extends XrayCommonClass {
  376. constructor(
  377. serviceName = "",
  378. authority = "",
  379. multiMode = false,
  380. ) {
  381. super();
  382. this.serviceName = serviceName;
  383. this.authority = authority;
  384. this.multiMode = multiMode;
  385. }
  386. static fromJson(json = {}) {
  387. return new GrpcStreamSettings(
  388. json.serviceName,
  389. json.authority,
  390. json.multiMode
  391. );
  392. }
  393. toJson() {
  394. return {
  395. serviceName: this.serviceName,
  396. authority: this.authority,
  397. multiMode: this.multiMode,
  398. }
  399. }
  400. }
  401. class HTTPUpgradeStreamSettings extends XrayCommonClass {
  402. constructor(
  403. acceptProxyProtocol = false,
  404. path = '/',
  405. host = '',
  406. headers = []
  407. ) {
  408. super();
  409. this.acceptProxyProtocol = acceptProxyProtocol;
  410. this.path = path;
  411. this.host = host;
  412. this.headers = headers;
  413. }
  414. addHeader(name, value) {
  415. this.headers.push({ name: name, value: value });
  416. }
  417. removeHeader(index) {
  418. this.headers.splice(index, 1);
  419. }
  420. static fromJson(json = {}) {
  421. return new HTTPUpgradeStreamSettings(
  422. json.acceptProxyProtocol,
  423. json.path,
  424. json.host,
  425. XrayCommonClass.toHeaders(json.headers),
  426. );
  427. }
  428. toJson() {
  429. return {
  430. acceptProxyProtocol: this.acceptProxyProtocol,
  431. path: this.path,
  432. host: this.host,
  433. headers: XrayCommonClass.toV2Headers(this.headers, false),
  434. };
  435. }
  436. }
  437. class xHTTPStreamSettings extends XrayCommonClass {
  438. constructor(
  439. path = '/',
  440. host = '',
  441. headers = [],
  442. scMaxBufferedPosts = 30,
  443. scMaxEachPostBytes = "1000000",
  444. scStreamUpServerSecs = "20-80",
  445. noSSEHeader = false,
  446. xPaddingBytes = "100-1000",
  447. mode = MODE_OPTION.AUTO,
  448. ) {
  449. super();
  450. this.path = path;
  451. this.host = host;
  452. this.headers = headers;
  453. this.scMaxBufferedPosts = scMaxBufferedPosts;
  454. this.scMaxEachPostBytes = scMaxEachPostBytes;
  455. this.scStreamUpServerSecs = scStreamUpServerSecs;
  456. this.noSSEHeader = noSSEHeader;
  457. this.xPaddingBytes = xPaddingBytes;
  458. this.mode = mode;
  459. }
  460. addHeader(name, value) {
  461. this.headers.push({ name: name, value: value });
  462. }
  463. removeHeader(index) {
  464. this.headers.splice(index, 1);
  465. }
  466. static fromJson(json = {}) {
  467. return new xHTTPStreamSettings(
  468. json.path,
  469. json.host,
  470. XrayCommonClass.toHeaders(json.headers),
  471. json.scMaxBufferedPosts,
  472. json.scMaxEachPostBytes,
  473. json.scStreamUpServerSecs,
  474. json.noSSEHeader,
  475. json.xPaddingBytes,
  476. json.mode,
  477. );
  478. }
  479. toJson() {
  480. return {
  481. path: this.path,
  482. host: this.host,
  483. headers: XrayCommonClass.toV2Headers(this.headers, false),
  484. scMaxBufferedPosts: this.scMaxBufferedPosts,
  485. scMaxEachPostBytes: this.scMaxEachPostBytes,
  486. scStreamUpServerSecs: this.scStreamUpServerSecs,
  487. noSSEHeader: this.noSSEHeader,
  488. xPaddingBytes: this.xPaddingBytes,
  489. mode: this.mode,
  490. };
  491. }
  492. }
  493. class TlsStreamSettings extends XrayCommonClass {
  494. constructor(
  495. serverName = '',
  496. minVersion = TLS_VERSION_OPTION.TLS12,
  497. maxVersion = TLS_VERSION_OPTION.TLS13,
  498. cipherSuites = '',
  499. rejectUnknownSni = false,
  500. serverNameToVerify = 'dns.google',
  501. disableSystemRoot = false,
  502. enableSessionResumption = false,
  503. certificates = [new TlsStreamSettings.Cert()],
  504. alpn = [ALPN_OPTION.H3, ALPN_OPTION.H2, ALPN_OPTION.HTTP1],
  505. settings = new TlsStreamSettings.Settings()
  506. ) {
  507. super();
  508. this.sni = serverName;
  509. this.minVersion = minVersion;
  510. this.maxVersion = maxVersion;
  511. this.cipherSuites = cipherSuites;
  512. this.rejectUnknownSni = rejectUnknownSni;
  513. this.serverNameToVerify = serverNameToVerify;
  514. this.disableSystemRoot = disableSystemRoot;
  515. this.enableSessionResumption = enableSessionResumption;
  516. this.certs = certificates;
  517. this.alpn = alpn;
  518. this.settings = settings;
  519. }
  520. addCert() {
  521. this.certs.push(new TlsStreamSettings.Cert());
  522. }
  523. removeCert(index) {
  524. this.certs.splice(index, 1);
  525. }
  526. static fromJson(json = {}) {
  527. let certs;
  528. let settings;
  529. if (!ObjectUtil.isEmpty(json.certificates)) {
  530. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  531. }
  532. if (!ObjectUtil.isEmpty(json.settings)) {
  533. settings = new TlsStreamSettings.Settings(json.settings.allowInsecure, json.settings.fingerprint, json.settings.serverName, json.settings.domains);
  534. }
  535. return new TlsStreamSettings(
  536. json.serverName,
  537. json.minVersion,
  538. json.maxVersion,
  539. json.cipherSuites,
  540. json.rejectUnknownSni,
  541. json.serverNameToVerify,
  542. json.disableSystemRoot,
  543. json.enableSessionResumption,
  544. certs,
  545. json.alpn,
  546. settings,
  547. );
  548. }
  549. toJson() {
  550. return {
  551. serverName: this.sni,
  552. minVersion: this.minVersion,
  553. maxVersion: this.maxVersion,
  554. cipherSuites: this.cipherSuites,
  555. rejectUnknownSni: this.rejectUnknownSni,
  556. serverNameToVerify: this.serverNameToVerify,
  557. disableSystemRoot: this.disableSystemRoot,
  558. enableSessionResumption: this.enableSessionResumption,
  559. certificates: TlsStreamSettings.toJsonArray(this.certs),
  560. alpn: this.alpn,
  561. settings: this.settings,
  562. };
  563. }
  564. }
  565. TlsStreamSettings.Cert = class extends XrayCommonClass {
  566. constructor(
  567. useFile = true,
  568. certificateFile = '',
  569. keyFile = '',
  570. certificate = '',
  571. key = '',
  572. ocspStapling = 3600,
  573. oneTimeLoading = false,
  574. usage = USAGE_OPTION.ENCIPHERMENT,
  575. buildChain = false,
  576. ) {
  577. super();
  578. this.useFile = useFile;
  579. this.certFile = certificateFile;
  580. this.keyFile = keyFile;
  581. this.cert = Array.isArray(certificate) ? certificate.join('\n') : certificate;
  582. this.key = Array.isArray(key) ? key.join('\n') : key;
  583. this.ocspStapling = ocspStapling;
  584. this.oneTimeLoading = oneTimeLoading;
  585. this.usage = usage;
  586. this.buildChain = buildChain
  587. }
  588. static fromJson(json = {}) {
  589. if ('certificateFile' in json && 'keyFile' in json) {
  590. return new TlsStreamSettings.Cert(
  591. true,
  592. json.certificateFile,
  593. json.keyFile, '', '',
  594. json.ocspStapling,
  595. json.oneTimeLoading,
  596. json.usage,
  597. json.buildChain,
  598. );
  599. } else {
  600. return new TlsStreamSettings.Cert(
  601. false, '', '',
  602. json.certificate.join('\n'),
  603. json.key.join('\n'),
  604. json.ocspStapling,
  605. json.oneTimeLoading,
  606. json.usage,
  607. json.buildChain,
  608. );
  609. }
  610. }
  611. toJson() {
  612. if (this.useFile) {
  613. return {
  614. certificateFile: this.certFile,
  615. keyFile: this.keyFile,
  616. ocspStapling: this.ocspStapling,
  617. oneTimeLoading: this.oneTimeLoading,
  618. usage: this.usage,
  619. buildChain: this.buildChain,
  620. };
  621. } else {
  622. return {
  623. certificate: this.cert.split('\n'),
  624. key: this.key.split('\n'),
  625. ocspStapling: this.ocspStapling,
  626. oneTimeLoading: this.oneTimeLoading,
  627. usage: this.usage,
  628. buildChain: this.buildChain,
  629. };
  630. }
  631. }
  632. };
  633. TlsStreamSettings.Settings = class extends XrayCommonClass {
  634. constructor(
  635. allowInsecure = false,
  636. fingerprint = UTLS_FINGERPRINT.UTLS_CHROME,
  637. ) {
  638. super();
  639. this.allowInsecure = allowInsecure;
  640. this.fingerprint = fingerprint;
  641. }
  642. static fromJson(json = {}) {
  643. return new TlsStreamSettings.Settings(
  644. json.allowInsecure,
  645. json.fingerprint,
  646. );
  647. }
  648. toJson() {
  649. return {
  650. allowInsecure: this.allowInsecure,
  651. fingerprint: this.fingerprint,
  652. };
  653. }
  654. };
  655. class RealityStreamSettings extends XrayCommonClass {
  656. constructor(
  657. show = false,
  658. xver = 0,
  659. dest = 'yahoo.com:443',
  660. serverNames = 'yahoo.com,www.yahoo.com',
  661. privateKey = '',
  662. minClient = '',
  663. maxClient = '',
  664. maxTimediff = 0,
  665. shortIds = RandomUtil.randomShortIds(),
  666. settings = new RealityStreamSettings.Settings()
  667. ) {
  668. super();
  669. this.show = show;
  670. this.xver = xver;
  671. this.dest = dest;
  672. this.serverNames = Array.isArray(serverNames) ? serverNames.join(",") : serverNames;
  673. this.privateKey = privateKey;
  674. this.minClient = minClient;
  675. this.maxClient = maxClient;
  676. this.maxTimediff = maxTimediff;
  677. this.shortIds = Array.isArray(shortIds) ? shortIds.join(",") : shortIds;
  678. this.settings = settings;
  679. }
  680. static fromJson(json = {}) {
  681. let settings;
  682. if (!ObjectUtil.isEmpty(json.settings)) {
  683. settings = new RealityStreamSettings.Settings(
  684. json.settings.publicKey,
  685. json.settings.fingerprint,
  686. json.settings.serverName,
  687. json.settings.spiderX
  688. );
  689. }
  690. return new RealityStreamSettings(
  691. json.show,
  692. json.xver,
  693. json.dest,
  694. json.serverNames,
  695. json.privateKey,
  696. json.minClient,
  697. json.maxClient,
  698. json.maxTimediff,
  699. json.shortIds,
  700. settings,
  701. );
  702. }
  703. toJson() {
  704. return {
  705. show: this.show,
  706. xver: this.xver,
  707. dest: this.dest,
  708. serverNames: this.serverNames.split(","),
  709. privateKey: this.privateKey,
  710. minClient: this.minClient,
  711. maxClient: this.maxClient,
  712. maxTimediff: this.maxTimediff,
  713. shortIds: this.shortIds.split(","),
  714. settings: this.settings,
  715. };
  716. }
  717. }
  718. RealityStreamSettings.Settings = class extends XrayCommonClass {
  719. constructor(
  720. publicKey = '',
  721. fingerprint = UTLS_FINGERPRINT.UTLS_CHROME,
  722. serverName = '',
  723. spiderX = '/'
  724. ) {
  725. super();
  726. this.publicKey = publicKey;
  727. this.fingerprint = fingerprint;
  728. this.serverName = serverName;
  729. this.spiderX = spiderX;
  730. }
  731. static fromJson(json = {}) {
  732. return new RealityStreamSettings.Settings(
  733. json.publicKey,
  734. json.fingerprint,
  735. json.serverName,
  736. json.spiderX,
  737. );
  738. }
  739. toJson() {
  740. return {
  741. publicKey: this.publicKey,
  742. fingerprint: this.fingerprint,
  743. serverName: this.serverName,
  744. spiderX: this.spiderX,
  745. };
  746. }
  747. };
  748. class SockoptStreamSettings extends XrayCommonClass {
  749. constructor(
  750. acceptProxyProtocol = false,
  751. tcpFastOpen = false,
  752. mark = 0,
  753. tproxy = "off",
  754. tcpMptcp = false,
  755. penetrate = false,
  756. domainStrategy = DOMAIN_STRATEGY_OPTION.USE_IP,
  757. tcpMaxSeg = 1440,
  758. dialerProxy = "",
  759. tcpKeepAliveInterval = 0,
  760. tcpKeepAliveIdle = 300,
  761. tcpUserTimeout = 10000,
  762. tcpcongestion = TCP_CONGESTION_OPTION.BBR,
  763. V6Only = false,
  764. tcpWindowClamp = 600,
  765. interfaceName = "",
  766. ) {
  767. super();
  768. this.acceptProxyProtocol = acceptProxyProtocol;
  769. this.tcpFastOpen = tcpFastOpen;
  770. this.mark = mark;
  771. this.tproxy = tproxy;
  772. this.tcpMptcp = tcpMptcp;
  773. this.penetrate = penetrate;
  774. this.domainStrategy = domainStrategy;
  775. this.tcpMaxSeg = tcpMaxSeg;
  776. this.dialerProxy = dialerProxy;
  777. this.tcpKeepAliveInterval = tcpKeepAliveInterval;
  778. this.tcpKeepAliveIdle = tcpKeepAliveIdle;
  779. this.tcpUserTimeout = tcpUserTimeout;
  780. this.tcpcongestion = tcpcongestion;
  781. this.V6Only = V6Only;
  782. this.tcpWindowClamp = tcpWindowClamp;
  783. this.interfaceName = interfaceName;
  784. }
  785. static fromJson(json = {}) {
  786. if (Object.keys(json).length === 0) return undefined;
  787. return new SockoptStreamSettings(
  788. json.acceptProxyProtocol,
  789. json.tcpFastOpen,
  790. json.mark,
  791. json.tproxy,
  792. json.tcpMptcp,
  793. json.penetrate,
  794. json.domainStrategy,
  795. json.tcpMaxSeg,
  796. json.dialerProxy,
  797. json.tcpKeepAliveInterval,
  798. json.tcpKeepAliveIdle,
  799. json.tcpUserTimeout,
  800. json.tcpcongestion,
  801. json.V6Only,
  802. json.tcpWindowClamp,
  803. json.interface,
  804. );
  805. }
  806. toJson() {
  807. return {
  808. acceptProxyProtocol: this.acceptProxyProtocol,
  809. tcpFastOpen: this.tcpFastOpen,
  810. mark: this.mark,
  811. tproxy: this.tproxy,
  812. tcpMptcp: this.tcpMptcp,
  813. penetrate: this.penetrate,
  814. domainStrategy: this.domainStrategy,
  815. tcpMaxSeg: this.tcpMaxSeg,
  816. dialerProxy: this.dialerProxy,
  817. tcpKeepAliveInterval: this.tcpKeepAliveInterval,
  818. tcpKeepAliveIdle: this.tcpKeepAliveIdle,
  819. tcpUserTimeout: this.tcpUserTimeout,
  820. tcpcongestion: this.tcpcongestion,
  821. V6Only: this.V6Only,
  822. tcpWindowClamp: this.tcpWindowClamp,
  823. interface: this.interfaceName,
  824. };
  825. }
  826. }
  827. class StreamSettings extends XrayCommonClass {
  828. constructor(network = 'tcp',
  829. security = 'none',
  830. externalProxy = [],
  831. tlsSettings = new TlsStreamSettings(),
  832. realitySettings = new RealityStreamSettings(),
  833. tcpSettings = new TcpStreamSettings(),
  834. kcpSettings = new KcpStreamSettings(),
  835. wsSettings = new WsStreamSettings(),
  836. grpcSettings = new GrpcStreamSettings(),
  837. httpupgradeSettings = new HTTPUpgradeStreamSettings(),
  838. xhttpSettings = new xHTTPStreamSettings(),
  839. sockopt = undefined,
  840. ) {
  841. super();
  842. this.network = network;
  843. this.security = security;
  844. this.externalProxy = externalProxy;
  845. this.tls = tlsSettings;
  846. this.reality = realitySettings;
  847. this.tcp = tcpSettings;
  848. this.kcp = kcpSettings;
  849. this.ws = wsSettings;
  850. this.grpc = grpcSettings;
  851. this.httpupgrade = httpupgradeSettings;
  852. this.xhttp = xhttpSettings;
  853. this.sockopt = sockopt;
  854. }
  855. get isTls() {
  856. return this.security === "tls";
  857. }
  858. set isTls(isTls) {
  859. if (isTls) {
  860. this.security = 'tls';
  861. } else {
  862. this.security = 'none';
  863. }
  864. }
  865. //for Reality
  866. get isReality() {
  867. return this.security === "reality";
  868. }
  869. set isReality(isReality) {
  870. if (isReality) {
  871. this.security = 'reality';
  872. } else {
  873. this.security = 'none';
  874. }
  875. }
  876. get sockoptSwitch() {
  877. return this.sockopt != undefined;
  878. }
  879. set sockoptSwitch(value) {
  880. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  881. }
  882. static fromJson(json = {}) {
  883. return new StreamSettings(
  884. json.network,
  885. json.security,
  886. json.externalProxy,
  887. TlsStreamSettings.fromJson(json.tlsSettings),
  888. RealityStreamSettings.fromJson(json.realitySettings),
  889. TcpStreamSettings.fromJson(json.tcpSettings),
  890. KcpStreamSettings.fromJson(json.kcpSettings),
  891. WsStreamSettings.fromJson(json.wsSettings),
  892. GrpcStreamSettings.fromJson(json.grpcSettings),
  893. HTTPUpgradeStreamSettings.fromJson(json.httpupgradeSettings),
  894. xHTTPStreamSettings.fromJson(json.xhttpSettings),
  895. SockoptStreamSettings.fromJson(json.sockopt),
  896. );
  897. }
  898. toJson() {
  899. const network = this.network;
  900. return {
  901. network: network,
  902. security: this.security,
  903. externalProxy: this.externalProxy,
  904. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  905. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  906. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  907. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  908. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  909. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  910. httpupgradeSettings: network === 'httpupgrade' ? this.httpupgrade.toJson() : undefined,
  911. xhttpSettings: network === 'xhttp' ? this.xhttp.toJson() : undefined,
  912. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  913. };
  914. }
  915. }
  916. class Sniffing extends XrayCommonClass {
  917. constructor(
  918. enabled = false,
  919. destOverride = ['http', 'tls', 'quic', 'fakedns'],
  920. metadataOnly = false,
  921. routeOnly = false) {
  922. super();
  923. this.enabled = enabled;
  924. this.destOverride = destOverride;
  925. this.metadataOnly = metadataOnly;
  926. this.routeOnly = routeOnly;
  927. }
  928. static fromJson(json = {}) {
  929. let destOverride = ObjectUtil.clone(json.destOverride);
  930. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  931. if (ObjectUtil.isEmpty(destOverride[0])) {
  932. destOverride = ['http', 'tls', 'quic', 'fakedns'];
  933. }
  934. }
  935. return new Sniffing(
  936. !!json.enabled,
  937. destOverride,
  938. json.metadataOnly,
  939. json.routeOnly,
  940. );
  941. }
  942. }
  943. class Allocate extends XrayCommonClass {
  944. constructor(
  945. strategy = "always",
  946. refresh = 5,
  947. concurrency = 3,
  948. ) {
  949. super();
  950. this.strategy = strategy;
  951. this.refresh = refresh;
  952. this.concurrency = concurrency;
  953. }
  954. static fromJson(json = {}) {
  955. return new Allocate(
  956. json.strategy,
  957. json.refresh,
  958. json.concurrency,
  959. );
  960. }
  961. }
  962. class Inbound extends XrayCommonClass {
  963. constructor(
  964. port = RandomUtil.randomIntRange(10000, 60000),
  965. listen = '',
  966. protocol = Protocols.VLESS,
  967. settings = null,
  968. streamSettings = new StreamSettings(),
  969. tag = '',
  970. sniffing = new Sniffing(),
  971. allocate = new Allocate(),
  972. clientStats = '',
  973. ) {
  974. super();
  975. this.port = port;
  976. this.listen = listen;
  977. this._protocol = protocol;
  978. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  979. this.stream = streamSettings;
  980. this.tag = tag;
  981. this.sniffing = sniffing;
  982. this.allocate = allocate;
  983. this.clientStats = clientStats;
  984. }
  985. getClientStats() {
  986. return this.clientStats;
  987. }
  988. get clients() {
  989. switch (this.protocol) {
  990. case Protocols.VMESS: return this.settings.vmesses;
  991. case Protocols.VLESS: return this.settings.vlesses;
  992. case Protocols.TROJAN: return this.settings.trojans;
  993. case Protocols.SHADOWSOCKS: return this.isSSMultiUser ? this.settings.shadowsockses : null;
  994. default: return null;
  995. }
  996. }
  997. get protocol() {
  998. return this._protocol;
  999. }
  1000. set protocol(protocol) {
  1001. this._protocol = protocol;
  1002. this.settings = Inbound.Settings.getSettings(protocol);
  1003. if (protocol === Protocols.TROJAN) {
  1004. this.tls = false;
  1005. }
  1006. }
  1007. get network() {
  1008. return this.stream.network;
  1009. }
  1010. set network(network) {
  1011. this.stream.network = network;
  1012. }
  1013. get isTcp() {
  1014. return this.network === "tcp";
  1015. }
  1016. get isWs() {
  1017. return this.network === "ws";
  1018. }
  1019. get isKcp() {
  1020. return this.network === "kcp";
  1021. }
  1022. get isGrpc() {
  1023. return this.network === "grpc";
  1024. }
  1025. get isHttpupgrade() {
  1026. return this.network === "httpupgrade";
  1027. }
  1028. get isXHTTP() {
  1029. return this.network === "xhttp";
  1030. }
  1031. // Shadowsocks
  1032. get method() {
  1033. switch (this.protocol) {
  1034. case Protocols.SHADOWSOCKS:
  1035. return this.settings.method;
  1036. default:
  1037. return "";
  1038. }
  1039. }
  1040. get isSSMultiUser() {
  1041. return this.method != SSMethods.BLAKE3_CHACHA20_POLY1305;
  1042. }
  1043. get isSS2022() {
  1044. return this.method.substring(0, 4) === "2022";
  1045. }
  1046. get serverName() {
  1047. if (this.stream.isTls) return this.stream.tls.sni;
  1048. if (this.stream.isReality) return this.stream.reality.serverNames;
  1049. return "";
  1050. }
  1051. getHeader(obj, name) {
  1052. for (const header of obj.headers) {
  1053. if (header.name.toLowerCase() === name.toLowerCase()) {
  1054. return header.value;
  1055. }
  1056. }
  1057. return "";
  1058. }
  1059. get host() {
  1060. if (this.isTcp) {
  1061. return this.getHeader(this.stream.tcp.request, 'host');
  1062. } else if (this.isWs) {
  1063. return this.stream.ws.host?.length > 0 ? this.stream.ws.host : this.getHeader(this.stream.ws, 'host');
  1064. } else if (this.isHttpupgrade) {
  1065. return this.stream.httpupgrade.host?.length > 0 ? this.stream.httpupgrade.host : this.getHeader(this.stream.httpupgrade, 'host');
  1066. } else if (this.isXHTTP) {
  1067. return this.stream.xhttp.host?.length > 0 ? this.stream.xhttp.host : this.getHeader(this.stream.xhttp, 'host');
  1068. }
  1069. return null;
  1070. }
  1071. get path() {
  1072. if (this.isTcp) {
  1073. return this.stream.tcp.request.path[0];
  1074. } else if (this.isWs) {
  1075. return this.stream.ws.path;
  1076. } else if (this.isHttpupgrade) {
  1077. return this.stream.httpupgrade.path;
  1078. } else if (this.isXHTTP) {
  1079. return this.stream.xhttp.path;
  1080. }
  1081. return null;
  1082. }
  1083. get kcpType() {
  1084. return this.stream.kcp.type;
  1085. }
  1086. get kcpSeed() {
  1087. return this.stream.kcp.seed;
  1088. }
  1089. get serviceName() {
  1090. return this.stream.grpc.serviceName;
  1091. }
  1092. isExpiry(index) {
  1093. let exp = this.clients[index].expiryTime;
  1094. return exp > 0 ? exp < new Date().getTime() : false;
  1095. }
  1096. canEnableTls() {
  1097. if (![Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol)) return false;
  1098. return ["tcp", "ws", "http", "grpc", "httpupgrade", "xhttp"].includes(this.network);
  1099. }
  1100. //this is used for xtls-rprx-vision
  1101. canEnableTlsFlow() {
  1102. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1103. return this.protocol === Protocols.VLESS;
  1104. }
  1105. return false;
  1106. }
  1107. canEnableReality() {
  1108. if (![Protocols.VLESS, Protocols.TROJAN].includes(this.protocol)) return false;
  1109. return ["tcp", "http", "grpc", "xhttp"].includes(this.network);
  1110. }
  1111. canEnableStream() {
  1112. return [Protocols.VMESS, Protocols.VLESS, Protocols.TROJAN, Protocols.SHADOWSOCKS].includes(this.protocol);
  1113. }
  1114. reset() {
  1115. this.port = RandomUtil.randomIntRange(10000, 60000);
  1116. this.listen = '';
  1117. this.protocol = Protocols.VMESS;
  1118. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1119. this.stream = new StreamSettings();
  1120. this.tag = '';
  1121. this.sniffing = new Sniffing();
  1122. this.allocate = new Allocate();
  1123. }
  1124. genVmessLink(address = '', port = this.port, forceTls, remark = '', clientId, security) {
  1125. if (this.protocol !== Protocols.VMESS) {
  1126. return '';
  1127. }
  1128. const tls = forceTls == 'same' ? this.stream.security : forceTls;
  1129. let obj = {
  1130. v: '2',
  1131. ps: remark,
  1132. add: address,
  1133. port: port,
  1134. id: clientId,
  1135. scy: security,
  1136. net: this.stream.network,
  1137. type: 'none',
  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.mode = 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(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://${safeBase64(settings.method + ':' + password.join(':'))}@${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 toFixed(this.totalGB / ONE_GB, 2);
  1690. }
  1691. set _totalGB(gb) {
  1692. this.totalGB = toFixed(gb * 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 toFixed(this.totalGB / ONE_GB, 2);
  1789. }
  1790. set _totalGB(gb) {
  1791. this.totalGB = toFixed(gb * 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 toFixed(this.totalGB / ONE_GB, 2);
  1928. }
  1929. set _totalGB(gb) {
  1930. this.totalGB = toFixed(gb * 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 = RandomUtil.randomShadowsocksPassword(),
  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 = RandomUtil.randomShadowsocksPassword(),
  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 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. };