inbound.js 74 KB

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