inbound.js 73 KB

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