xray.js 80 KB

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