xray.js 81 KB

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