inbound.js 83 KB

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