xray.js 75 KB

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