1
0

xray.js 80 KB

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