xray.js 80 KB

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