inbound.js 82 KB

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