inbound.js 74 KB

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