1
0

xray.js 77 KB

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