xray.js 81 KB

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