xray.js 67 KB

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