xray.js 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227
  1. const Protocols = {
  2. VMESS: 'vmess',
  3. VLESS: 'vless',
  4. TROJAN: 'trojan',
  5. SHADOWSOCKS: 'shadowsocks',
  6. DOKODEMO: 'dokodemo-door',
  7. MTPROTO: 'mtproto',
  8. SOCKS: 'socks',
  9. HTTP: 'http',
  10. };
  11. const VmessMethods = {
  12. AES_128_GCM: 'aes-128-gcm',
  13. CHACHA20_POLY1305: 'chacha20-poly1305',
  14. AUTO: 'auto',
  15. NONE: 'none',
  16. };
  17. const SSMethods = {
  18. BLAKE3_AES_128_GCM: '2022-blake3-aes-128-gcm',
  19. BLAKE3_AES_256_GCM: '2022-blake3-aes-256-gcm',
  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. HTTP1: "http/1.1",
  68. H2: "h2",
  69. H3: "h3",
  70. };
  71. const SNIFFING_OPTION = {
  72. HTTP: "http",
  73. TLS: "tls",
  74. QUIC: "quic",
  75. };
  76. Object.freeze(Protocols);
  77. Object.freeze(VmessMethods);
  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(path='/', host=['']) {
  340. super();
  341. this.path = path;
  342. this.host = host.length === 0 ? [''] : host;
  343. }
  344. addHost(host) {
  345. this.host.push(host);
  346. }
  347. removeHost(index) {
  348. this.host.splice(index, 1);
  349. }
  350. static fromJson(json={}) {
  351. return new HttpStreamSettings(json.path, json.host);
  352. }
  353. toJson() {
  354. let host = [];
  355. for (let i = 0; i < this.host.length; ++i) {
  356. if (!ObjectUtil.isEmpty(this.host[i])) {
  357. host.push(this.host[i]);
  358. }
  359. }
  360. return {
  361. path: this.path,
  362. host: host,
  363. }
  364. }
  365. }
  366. class QuicStreamSettings extends XrayCommonClass {
  367. constructor(security=VmessMethods.NONE,
  368. key='', type='none') {
  369. super();
  370. this.security = security;
  371. this.key = key;
  372. this.type = type;
  373. }
  374. static fromJson(json={}) {
  375. return new QuicStreamSettings(
  376. json.security,
  377. json.key,
  378. json.header ? json.header.type : 'none',
  379. );
  380. }
  381. toJson() {
  382. return {
  383. security: this.security,
  384. key: this.key,
  385. header: {
  386. type: this.type,
  387. }
  388. }
  389. }
  390. }
  391. class GrpcStreamSettings extends XrayCommonClass {
  392. constructor(
  393. serviceName="",
  394. multiMode=false
  395. ) {
  396. super();
  397. this.serviceName = serviceName;
  398. this.multiMode = multiMode;
  399. }
  400. static fromJson(json={}) {
  401. return new GrpcStreamSettings(
  402. json.serviceName,
  403. json.multiMode
  404. );
  405. }
  406. toJson() {
  407. return {
  408. serviceName: this.serviceName,
  409. multiMode: this.multiMode
  410. }
  411. }
  412. }
  413. class TlsStreamSettings extends XrayCommonClass {
  414. constructor(serverName='',
  415. minVersion = TLS_VERSION_OPTION.TLS12,
  416. maxVersion = TLS_VERSION_OPTION.TLS13,
  417. cipherSuites = '',
  418. certificates=[new TlsStreamSettings.Cert()],
  419. alpn=[ALPN_OPTION.H2,ALPN_OPTION.HTTP1],
  420. settings=new TlsStreamSettings.Settings()) {
  421. super();
  422. this.server = serverName;
  423. this.minVersion = minVersion;
  424. this.maxVersion = maxVersion;
  425. this.cipherSuites = cipherSuites;
  426. this.certs = certificates;
  427. this.alpn = alpn;
  428. this.settings = settings;
  429. }
  430. addCert(cert) {
  431. this.certs.push(cert);
  432. }
  433. removeCert(index) {
  434. this.certs.splice(index, 1);
  435. }
  436. static fromJson(json={}) {
  437. let certs;
  438. let settings;
  439. if (!ObjectUtil.isEmpty(json.certificates)) {
  440. certs = json.certificates.map(cert => TlsStreamSettings.Cert.fromJson(cert));
  441. }
  442. if (!ObjectUtil.isEmpty(json.settings)) {
  443. settings = new TlsStreamSettings.Settings(json.settings.allowInsecure , json.settings.fingerprint, json.settings.serverName);
  444. }
  445. return new TlsStreamSettings(
  446. json.serverName,
  447. json.minVersion,
  448. json.maxVersion,
  449. json.cipherSuites,
  450. certs,
  451. json.alpn,
  452. settings,
  453. );
  454. }
  455. toJson() {
  456. return {
  457. serverName: this.server,
  458. minVersion: this.minVersion,
  459. maxVersion: this.maxVersion,
  460. cipherSuites: this.cipherSuites,
  461. certificates: TlsStreamSettings.toJsonArray(this.certs),
  462. alpn: this.alpn,
  463. settings: this.settings,
  464. };
  465. }
  466. }
  467. TlsStreamSettings.Cert = class extends XrayCommonClass {
  468. constructor(useFile=true, certificateFile='', keyFile='', certificate='', key='') {
  469. super();
  470. this.useFile = useFile;
  471. this.certFile = certificateFile;
  472. this.keyFile = keyFile;
  473. this.cert = certificate instanceof Array ? certificate.join('\n') : certificate;
  474. this.key = key instanceof Array ? key.join('\n') : key;
  475. }
  476. static fromJson(json={}) {
  477. if ('certificateFile' in json && 'keyFile' in json) {
  478. return new TlsStreamSettings.Cert(
  479. true,
  480. json.certificateFile,
  481. json.keyFile,
  482. );
  483. } else {
  484. return new TlsStreamSettings.Cert(
  485. false, '', '',
  486. json.certificate.join('\n'),
  487. json.key.join('\n'),
  488. );
  489. }
  490. }
  491. toJson() {
  492. if (this.useFile) {
  493. return {
  494. certificateFile: this.certFile,
  495. keyFile: this.keyFile,
  496. };
  497. } else {
  498. return {
  499. certificate: this.cert.split('\n'),
  500. key: this.key.split('\n'),
  501. };
  502. }
  503. }
  504. };
  505. TlsStreamSettings.Settings = class extends XrayCommonClass {
  506. constructor(allowInsecure = false, fingerprint = '', serverName = '') {
  507. super();
  508. this.allowInsecure = allowInsecure;
  509. this.fingerprint = fingerprint;
  510. this.serverName = serverName;
  511. }
  512. static fromJson(json = {}) {
  513. return new TlsStreamSettings.Settings(
  514. json.allowInsecure,
  515. json.fingerprint,
  516. json.servername,
  517. );
  518. }
  519. toJson() {
  520. return {
  521. allowInsecure: this.allowInsecure,
  522. fingerprint: this.fingerprint,
  523. serverName: this.serverName,
  524. };
  525. }
  526. };
  527. class XtlsStreamSettings extends XrayCommonClass {
  528. constructor(serverName='',
  529. certificates=[new XtlsStreamSettings.Cert()],
  530. alpn=[ALPN_OPTION.H2,ALPN_OPTION.HTTP1],
  531. settings=new XtlsStreamSettings.Settings()) {
  532. super();
  533. this.server = serverName;
  534. this.certs = certificates;
  535. this.alpn = alpn;
  536. this.settings = settings;
  537. }
  538. addCert(cert) {
  539. this.certs.push(cert);
  540. }
  541. removeCert(index) {
  542. this.certs.splice(index, 1);
  543. }
  544. static fromJson(json={}) {
  545. let certs;
  546. let settings;
  547. if (!ObjectUtil.isEmpty(json.certificates)) {
  548. certs = json.certificates.map(cert => XtlsStreamSettings.Cert.fromJson(cert));
  549. }
  550. if (!ObjectUtil.isEmpty(json.settings)) {
  551. settings = new XtlsStreamSettings.Settings(json.settings.allowInsecure , json.settings.serverName);
  552. }
  553. return new XtlsStreamSettings(
  554. json.serverName,
  555. certs,
  556. json.alpn,
  557. settings,
  558. );
  559. }
  560. toJson() {
  561. return {
  562. serverName: this.server,
  563. certificates: XtlsStreamSettings.toJsonArray(this.certs),
  564. alpn: this.alpn,
  565. settings: this.settings,
  566. };
  567. }
  568. }
  569. XtlsStreamSettings.Cert = class extends XrayCommonClass {
  570. constructor(useFile=true, certificateFile='', keyFile='', certificate='', key='') {
  571. super();
  572. this.useFile = useFile;
  573. this.certFile = certificateFile;
  574. this.keyFile = keyFile;
  575. this.cert = certificate instanceof Array ? certificate.join('\n') : certificate;
  576. this.key = key instanceof Array ? key.join('\n') : key;
  577. }
  578. static fromJson(json={}) {
  579. if ('certificateFile' in json && 'keyFile' in json) {
  580. return new XtlsStreamSettings.Cert(
  581. true,
  582. json.certificateFile,
  583. json.keyFile,
  584. );
  585. } else {
  586. return new XtlsStreamSettings.Cert(
  587. false, '', '',
  588. json.certificate.join('\n'),
  589. json.key.join('\n'),
  590. );
  591. }
  592. }
  593. toJson() {
  594. if (this.useFile) {
  595. return {
  596. certificateFile: this.certFile,
  597. keyFile: this.keyFile,
  598. };
  599. } else {
  600. return {
  601. certificate: this.cert.split('\n'),
  602. key: this.key.split('\n'),
  603. };
  604. }
  605. }
  606. };
  607. XtlsStreamSettings.Settings = class extends XrayCommonClass {
  608. constructor(allowInsecure = false, serverName = '') {
  609. super();
  610. this.allowInsecure = allowInsecure;
  611. this.serverName = serverName;
  612. }
  613. static fromJson(json = {}) {
  614. return new XtlsStreamSettings.Settings(
  615. json.allowInsecure,
  616. json.servername,
  617. );
  618. }
  619. toJson() {
  620. return {
  621. allowInsecure: this.allowInsecure,
  622. serverName: this.serverName,
  623. };
  624. }
  625. };
  626. class RealityStreamSettings extends XrayCommonClass {
  627. constructor(
  628. show = false,xver = 0,
  629. dest = 'yahoo.com:443',
  630. serverNames = 'yahoo.com,www.yahoo.com',
  631. privateKey = '',
  632. minClient = '',
  633. maxClient = '',
  634. maxTimediff = 0,
  635. shortIds = RandomUtil.randowShortId(),
  636. settings= new RealityStreamSettings.Settings()
  637. ){
  638. super();
  639. this.show = show;
  640. this.xver = xver;
  641. this.dest = dest;
  642. this.serverNames = serverNames instanceof Array ? serverNames.join(",") : serverNames;
  643. this.privateKey = privateKey;
  644. this.minClient = minClient;
  645. this.maxClient = maxClient;
  646. this.maxTimediff = maxTimediff;
  647. this.shortIds = shortIds instanceof Array ? shortIds.join(",") : shortIds;
  648. this.settings = settings;
  649. }
  650. static fromJson(json = {}) {
  651. let settings;
  652. if (!ObjectUtil.isEmpty(json.settings)) {
  653. settings = new RealityStreamSettings.Settings(json.settings.publicKey , json.settings.fingerprint, json.settings.serverName);
  654. }
  655. return new RealityStreamSettings(
  656. json.show,
  657. json.xver,
  658. json.dest,
  659. json.serverNames,
  660. json.privateKey,
  661. json.minClient,
  662. json.maxClient,
  663. json.maxTimediff,
  664. json.shortIds,
  665. json.settings,
  666. );
  667. }
  668. toJson() {
  669. return {
  670. show: this.show,
  671. xver: this.xver,
  672. dest: this.dest,
  673. serverNames: this.serverNames.split(","),
  674. privateKey: this.privateKey,
  675. minClient: this.minClient,
  676. maxClient: this.maxClient,
  677. maxTimediff: this.maxTimediff,
  678. shortIds: this.shortIds.split(","),
  679. settings: this.settings,
  680. };
  681. }
  682. }
  683. RealityStreamSettings.Settings = class extends XrayCommonClass {
  684. constructor(publicKey = '', fingerprint = UTLS_FINGERPRINT.UTLS_FIREFOX, serverName = '') {
  685. super();
  686. this.publicKey = publicKey;
  687. this.fingerprint = fingerprint;
  688. this.serverName = serverName;
  689. }
  690. static fromJson(json = {}) {
  691. return new RealityStreamSettings.Settings(
  692. json.publicKey,
  693. json.fingerprint,
  694. json.serverName,
  695. );
  696. }
  697. toJson() {
  698. return {
  699. publicKey: this.publicKey,
  700. fingerprint: this.fingerprint,
  701. serverName: this.serverName,
  702. };
  703. }
  704. };
  705. class StreamSettings extends XrayCommonClass {
  706. constructor(network='tcp',
  707. security='none',
  708. tlsSettings=new TlsStreamSettings(),
  709. xtlsSettings=new XtlsStreamSettings(),
  710. realitySettings = new RealityStreamSettings(),
  711. tcpSettings=new TcpStreamSettings(),
  712. kcpSettings=new KcpStreamSettings(),
  713. wsSettings=new WsStreamSettings(),
  714. httpSettings=new HttpStreamSettings(),
  715. quicSettings=new QuicStreamSettings(),
  716. grpcSettings=new GrpcStreamSettings(),
  717. ) {
  718. super();
  719. this.network = network;
  720. this.security = security;
  721. this.tls = tlsSettings;
  722. this.xtls = xtlsSettings;
  723. this.reality = realitySettings;
  724. this.tcp = tcpSettings;
  725. this.kcp = kcpSettings;
  726. this.ws = wsSettings;
  727. this.http = httpSettings;
  728. this.quic = quicSettings;
  729. this.grpc = grpcSettings;
  730. }
  731. get isTls() {
  732. return this.security === "tls";
  733. }
  734. set isTls(isTls) {
  735. if (isTls) {
  736. this.security = 'tls';
  737. } else {
  738. this.security = 'none';
  739. }
  740. }
  741. get isXtls() {
  742. return this.security === "xtls";
  743. }
  744. set isXtls(isXtls) {
  745. if (isXtls) {
  746. this.security = 'xtls';
  747. } else {
  748. this.security = 'none';
  749. }
  750. }
  751. //for Reality
  752. get isReality() {
  753. return this.security === "reality";
  754. }
  755. set isReality(isReality) {
  756. if (isReality) {
  757. this.security = 'reality';
  758. } else {
  759. this.security = 'none';
  760. }
  761. }
  762. static fromJson(json={}) {
  763. return new StreamSettings(
  764. json.network,
  765. json.security,
  766. TlsStreamSettings.fromJson(json.tlsSettings),
  767. XtlsStreamSettings.fromJson(json.xtlsSettings),
  768. RealityStreamSettings.fromJson(json.realitySettings),
  769. TcpStreamSettings.fromJson(json.tcpSettings),
  770. KcpStreamSettings.fromJson(json.kcpSettings),
  771. WsStreamSettings.fromJson(json.wsSettings),
  772. HttpStreamSettings.fromJson(json.httpSettings),
  773. QuicStreamSettings.fromJson(json.quicSettings),
  774. GrpcStreamSettings.fromJson(json.grpcSettings),
  775. );
  776. }
  777. toJson() {
  778. const network = this.network;
  779. return {
  780. network: network,
  781. security: this.security,
  782. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  783. xtlsSettings: this.isXtls ? this.xtls.toJson() : undefined,
  784. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  785. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  786. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  787. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  788. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  789. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  790. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  791. };
  792. }
  793. }
  794. class Sniffing extends XrayCommonClass {
  795. constructor(enabled=true, destOverride=['http', 'tls', 'quic']) {
  796. super();
  797. this.enabled = enabled;
  798. this.destOverride = destOverride;
  799. }
  800. static fromJson(json={}) {
  801. let destOverride = ObjectUtil.clone(json.destOverride);
  802. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  803. if (ObjectUtil.isEmpty(destOverride[0])) {
  804. destOverride = ['http', 'tls', 'quic'];
  805. }
  806. }
  807. return new Sniffing(
  808. !!json.enabled,
  809. destOverride,
  810. );
  811. }
  812. }
  813. class Inbound extends XrayCommonClass {
  814. constructor(port=RandomUtil.randomIntRange(10000, 60000),
  815. listen='',
  816. protocol=Protocols.VLESS,
  817. settings=null,
  818. streamSettings=new StreamSettings(),
  819. tag='',
  820. sniffing=new Sniffing(),
  821. clientStats='',
  822. ) {
  823. super();
  824. this.port = port;
  825. this.listen = listen;
  826. this._protocol = protocol;
  827. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  828. this.stream = streamSettings;
  829. this.tag = tag;
  830. this.sniffing = sniffing;
  831. this.clientStats = clientStats;
  832. }
  833. getClientStats() {
  834. return this.clientStats;
  835. }
  836. get protocol() {
  837. return this._protocol;
  838. }
  839. set protocol(protocol) {
  840. this._protocol = protocol;
  841. this.settings = Inbound.Settings.getSettings(protocol);
  842. if (protocol === Protocols.TROJAN) {
  843. this.tls = false;
  844. }
  845. }
  846. get tls() {
  847. return this.stream.security === 'tls';
  848. }
  849. set tls(isTls) {
  850. if (isTls) {
  851. this.stream.security = 'tls';
  852. } else {
  853. this.stream.security = 'none';
  854. }
  855. }
  856. get xtls() {
  857. return this.stream.security === 'xtls';
  858. }
  859. set xtls(isXtls) {
  860. if (isXtls) {
  861. this.stream.security = 'xtls';
  862. } else {
  863. this.stream.security = 'none';
  864. }
  865. }
  866. //for Reality
  867. get reality() {
  868. return this.stream.security === 'reality';
  869. }
  870. set reality(isReality) {
  871. if (isReality) {
  872. this.stream.security = 'reality';
  873. } else {
  874. this.stream.security = 'none';
  875. }
  876. }
  877. get network() {
  878. return this.stream.network;
  879. }
  880. set network(network) {
  881. this.stream.network = network;
  882. }
  883. get isTcp() {
  884. return this.network === "tcp";
  885. }
  886. get isWs() {
  887. return this.network === "ws";
  888. }
  889. get isKcp() {
  890. return this.network === "kcp";
  891. }
  892. get isQuic() {
  893. return this.network === "quic"
  894. }
  895. get isGrpc() {
  896. return this.network === "grpc";
  897. }
  898. get isH2() {
  899. return this.network === "http";
  900. }
  901. // Shadowsocks
  902. get method() {
  903. switch (this.protocol) {
  904. case Protocols.SHADOWSOCKS:
  905. return this.settings.method;
  906. default:
  907. return "";
  908. }
  909. }
  910. get serverName() {
  911. if (this.stream.isTls || this.stream.isXtls || this.stream.isReality) {
  912. return this.stream.tls.server;
  913. }
  914. return "";
  915. }
  916. get host() {
  917. if (this.isTcp) {
  918. return this.stream.tcp.request.getHeader("Host");
  919. } else if (this.isWs) {
  920. return this.stream.ws.getHeader("Host");
  921. } else if (this.isH2) {
  922. return this.stream.http.host[0];
  923. }
  924. return null;
  925. }
  926. get path() {
  927. if (this.isTcp) {
  928. return this.stream.tcp.request.path[0];
  929. } else if (this.isWs) {
  930. return this.stream.ws.path;
  931. } else if (this.isH2) {
  932. return this.stream.http.path[0];
  933. }
  934. return null;
  935. }
  936. get quicSecurity() {
  937. return this.stream.quic.security;
  938. }
  939. get quicKey() {
  940. return this.stream.quic.key;
  941. }
  942. get quicType() {
  943. return this.stream.quic.type;
  944. }
  945. get kcpType() {
  946. return this.stream.kcp.type;
  947. }
  948. get kcpSeed() {
  949. return this.stream.kcp.seed;
  950. }
  951. get serviceName() {
  952. return this.stream.grpc.serviceName;
  953. }
  954. isExpiry(index) {
  955. switch (this.protocol) {
  956. case Protocols.VMESS:
  957. if(this.settings.vmesses[index].expiryTime > 0)
  958. return this.settings.vmesses[index].expiryTime < new Date().getTime();
  959. return false
  960. case Protocols.VLESS:
  961. if(this.settings.vlesses[index].expiryTime > 0)
  962. return this.settings.vlesses[index].expiryTime < new Date().getTime();
  963. return false
  964. case Protocols.TROJAN:
  965. if(this.settings.trojans[index].expiryTime > 0)
  966. return this.settings.trojans[index].expiryTime < new Date().getTime();
  967. return false
  968. case Protocols.SHADOWSOCKS:
  969. if(this.settings.shadowsockses[index].expiryTime > 0)
  970. return this.settings.shadowsockses[index].expiryTime < new Date().getTime();
  971. return false
  972. default:
  973. return false;
  974. }
  975. }
  976. canEnableTls() {
  977. switch (this.protocol) {
  978. case Protocols.VMESS:
  979. case Protocols.VLESS:
  980. case Protocols.TROJAN:
  981. break;
  982. default:
  983. return false;
  984. }
  985. switch (this.network) {
  986. case "tcp":
  987. case "ws":
  988. case "http":
  989. case "quic":
  990. case "grpc":
  991. return true;
  992. default:
  993. return false;
  994. }
  995. }
  996. canEnableReality() {
  997. switch (this.protocol) {
  998. case Protocols.VLESS:
  999. case Protocols.TROJAN:
  1000. break;
  1001. default:
  1002. return false;
  1003. }
  1004. switch (this.network) {
  1005. case "tcp":
  1006. case "http":
  1007. case "grpc":
  1008. return true;
  1009. default:
  1010. return false;
  1011. }
  1012. }
  1013. //this is used for xtls-rprx-vision
  1014. canEnableTlsFlow() {
  1015. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1016. switch (this.protocol) {
  1017. case Protocols.VLESS:
  1018. return true;
  1019. default:
  1020. return false;
  1021. }
  1022. }
  1023. return false;
  1024. }
  1025. canSetTls() {
  1026. return this.canEnableTls();
  1027. }
  1028. canEnableXtls() {
  1029. switch (this.protocol) {
  1030. case Protocols.VLESS:
  1031. case Protocols.TROJAN:
  1032. break;
  1033. default:
  1034. return false;
  1035. }
  1036. return this.network === "tcp";
  1037. }
  1038. canEnableStream() {
  1039. switch (this.protocol) {
  1040. case Protocols.VMESS:
  1041. case Protocols.VLESS:
  1042. case Protocols.TROJAN:
  1043. return true;
  1044. default:
  1045. return false;
  1046. }
  1047. }
  1048. canSniffing() {
  1049. switch (this.protocol) {
  1050. case Protocols.VMESS:
  1051. case Protocols.VLESS:
  1052. case Protocols.TROJAN:
  1053. case Protocols.SHADOWSOCKS:
  1054. return true;
  1055. default:
  1056. return false;
  1057. }
  1058. }
  1059. reset() {
  1060. this.port = RandomUtil.randomIntRange(10000, 60000);
  1061. this.listen = '';
  1062. this.protocol = Protocols.VMESS;
  1063. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1064. this.stream = new StreamSettings();
  1065. this.tag = '';
  1066. this.sniffing = new Sniffing();
  1067. }
  1068. genVmessLink(address='', remark='', clientIndex=0) {
  1069. if (this.protocol !== Protocols.VMESS) {
  1070. return '';
  1071. }
  1072. let obj = {
  1073. v: '2',
  1074. ps: remark,
  1075. add: address,
  1076. port: this.port,
  1077. id: this.settings.vmesses[clientIndex].id,
  1078. aid: this.settings.vmesses[clientIndex].alterId,
  1079. net: this.stream.network,
  1080. type: 'none',
  1081. tls: this.stream.security,
  1082. };
  1083. let network = this.stream.network;
  1084. if (network === 'tcp') {
  1085. let tcp = this.stream.tcp;
  1086. obj.type = tcp.type;
  1087. if (tcp.type === 'http') {
  1088. let request = tcp.request;
  1089. obj.path = request.path.join(',');
  1090. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1091. if (index >= 0) {
  1092. obj.host = request.headers[index].value;
  1093. }
  1094. }
  1095. } else if (network === 'kcp') {
  1096. let kcp = this.stream.kcp;
  1097. obj.type = kcp.type;
  1098. obj.path = kcp.seed;
  1099. } else if (network === 'ws') {
  1100. let ws = this.stream.ws;
  1101. obj.path = ws.path;
  1102. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1103. if (index >= 0) {
  1104. obj.host = ws.headers[index].value;
  1105. }
  1106. } else if (network === 'http') {
  1107. obj.net = 'h2';
  1108. obj.path = this.stream.http.path;
  1109. obj.host = this.stream.http.host.join(',');
  1110. } else if (network === 'quic') {
  1111. obj.type = this.stream.quic.type;
  1112. obj.host = this.stream.quic.security;
  1113. obj.path = this.stream.quic.key;
  1114. } else if (network === 'grpc') {
  1115. obj.path = this.stream.grpc.serviceName;
  1116. if (this.stream.grpc.multiMode){
  1117. obj.type = 'multi'
  1118. }
  1119. }
  1120. if (this.stream.security === 'tls') {
  1121. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1122. obj.add = this.stream.tls.server;
  1123. }
  1124. if (!ObjectUtil.isEmpty(this.stream.tls.settings.serverName)){
  1125. obj.sni = this.stream.tls.settings.serverName;
  1126. }
  1127. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)){
  1128. obj.fp = this.stream.tls.settings.fingerprint;
  1129. }
  1130. if (this.stream.tls.alpn.length>0){
  1131. obj.alpn = this.stream.tls.alpn.join(',');
  1132. }
  1133. if (this.stream.tls.settings.allowInsecure){
  1134. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1135. }
  1136. }
  1137. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  1138. }
  1139. genVLESSLink(address = '', remark='', clientIndex=0) {
  1140. const settings = this.settings;
  1141. const uuid = settings.vlesses[clientIndex].id;
  1142. const port = this.port;
  1143. const type = this.stream.network;
  1144. const params = new Map();
  1145. params.set("type", this.stream.network);
  1146. switch (type) {
  1147. case "tcp":
  1148. const tcp = this.stream.tcp;
  1149. if (tcp.type === 'http') {
  1150. const request = tcp.request;
  1151. params.set("path", request.path.join(','));
  1152. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1153. if (index >= 0) {
  1154. const host = request.headers[index].value;
  1155. params.set("host", host);
  1156. }
  1157. params.set("headerType", 'http');
  1158. }
  1159. break;
  1160. case "kcp":
  1161. const kcp = this.stream.kcp;
  1162. params.set("headerType", kcp.type);
  1163. params.set("seed", kcp.seed);
  1164. break;
  1165. case "ws":
  1166. const ws = this.stream.ws;
  1167. params.set("path", ws.path);
  1168. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1169. if (index >= 0) {
  1170. const host = ws.headers[index].value;
  1171. params.set("host", host);
  1172. }
  1173. break;
  1174. case "http":
  1175. const http = this.stream.http;
  1176. params.set("path", http.path);
  1177. params.set("host", http.host);
  1178. break;
  1179. case "quic":
  1180. const quic = this.stream.quic;
  1181. params.set("quicSecurity", quic.security);
  1182. params.set("key", quic.key);
  1183. params.set("headerType", quic.type);
  1184. break;
  1185. case "grpc":
  1186. const grpc = this.stream.grpc;
  1187. params.set("serviceName", grpc.serviceName);
  1188. if(grpc.multiMode){
  1189. params.set("mode", "multi");
  1190. }
  1191. break;
  1192. }
  1193. if (this.tls) {
  1194. params.set("security", "tls");
  1195. params.set("fp" , this.stream.tls.settings.fingerprint);
  1196. params.set("alpn", this.stream.tls.alpn);
  1197. if(this.stream.tls.settings.allowInsecure){
  1198. params.set("allowInsecure", "1");
  1199. }
  1200. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1201. address = this.stream.tls.server;
  1202. }
  1203. if (this.stream.tls.settings.serverName !== ''){
  1204. params.set("sni", this.stream.tls.settings.serverName);
  1205. }
  1206. if (type === "tcp" && this.settings.vlesses[clientIndex].flow.length > 0) {
  1207. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1208. }
  1209. }
  1210. if (this.xtls) {
  1211. params.set("security", "xtls");
  1212. params.set("alpn", this.stream.xtls.alpn);
  1213. if(this.stream.xtls.settings.allowInsecure){
  1214. params.set("allowInsecure", "1");
  1215. }
  1216. if (!ObjectUtil.isEmpty(this.stream.xtls.server)) {
  1217. address = this.stream.xtls.server;
  1218. }
  1219. if (this.stream.xtls.settings.serverName !== ''){
  1220. params.set("sni", this.stream.xtls.settings.serverName);
  1221. }
  1222. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1223. }
  1224. if (this.reality) {
  1225. params.set("security", "reality");
  1226. params.set("fp", this.stream.reality.settings.fingerprint);
  1227. params.set("pbk", this.stream.reality.settings.publicKey);
  1228. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1229. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1230. }
  1231. if (this.stream.network === 'tcp' && !ObjectUtil.isEmpty(this.settings.vlesses[clientIndex].flow)) {
  1232. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1233. }
  1234. if (this.stream.reality.shortIds.length > 0) {
  1235. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1236. }
  1237. if (!ObjectUtil.isEmpty(this.stream.reality.settings.serverName)) {
  1238. address = this.stream.reality.settings.serverName;
  1239. }
  1240. }
  1241. const link = `vless://${uuid}@${address}:${port}`;
  1242. const url = new URL(link);
  1243. for (const [key, value] of params) {
  1244. url.searchParams.set(key, value)
  1245. }
  1246. url.hash = encodeURIComponent(remark);
  1247. return url.toString();
  1248. }
  1249. genSSLink(address='', remark='', clientIndex = 0) {
  1250. let settings = this.settings;
  1251. const port = this.port;
  1252. return 'ss://' + safeBase64(settings.method + ':' + settings.password + ':' +settings.shadowsockses[clientIndex].password) + '@' + address + ':' + this.port + '#' + encodeURIComponent(remark);
  1253. }
  1254. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1255. let settings = this.settings;
  1256. const port = this.port;
  1257. const type = this.stream.network;
  1258. const params = new Map();
  1259. params.set("type", this.stream.network);
  1260. switch (type) {
  1261. case "tcp":
  1262. const tcp = this.stream.tcp;
  1263. if (tcp.type === 'http') {
  1264. const request = tcp.request;
  1265. params.set("path", request.path.join(','));
  1266. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1267. if (index >= 0) {
  1268. const host = request.headers[index].value;
  1269. params.set("host", host);
  1270. }
  1271. params.set("headerType", 'http');
  1272. }
  1273. break;
  1274. case "kcp":
  1275. const kcp = this.stream.kcp;
  1276. params.set("headerType", kcp.type);
  1277. params.set("seed", kcp.seed);
  1278. break;
  1279. case "ws":
  1280. const ws = this.stream.ws;
  1281. params.set("path", ws.path);
  1282. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1283. if (index >= 0) {
  1284. const host = ws.headers[index].value;
  1285. params.set("host", host);
  1286. }
  1287. break;
  1288. case "http":
  1289. const http = this.stream.http;
  1290. params.set("path", http.path);
  1291. params.set("host", http.host);
  1292. break;
  1293. case "quic":
  1294. const quic = this.stream.quic;
  1295. params.set("quicSecurity", quic.security);
  1296. params.set("key", quic.key);
  1297. params.set("headerType", quic.type);
  1298. break;
  1299. case "grpc":
  1300. const grpc = this.stream.grpc;
  1301. params.set("serviceName", grpc.serviceName);
  1302. if(grpc.multiMode){
  1303. params.set("mode", "multi");
  1304. }
  1305. break;
  1306. }
  1307. if (this.tls) {
  1308. params.set("security", "tls");
  1309. params.set("fp" , this.stream.tls.settings.fingerprint);
  1310. params.set("alpn", this.stream.tls.alpn);
  1311. if(this.stream.tls.settings.allowInsecure){
  1312. params.set("allowInsecure", "1");
  1313. }
  1314. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1315. address = this.stream.tls.server;
  1316. }
  1317. if (this.stream.tls.settings.serverName !== ''){
  1318. params.set("sni", this.stream.tls.settings.serverName);
  1319. }
  1320. }
  1321. if (this.reality) {
  1322. params.set("security", "reality");
  1323. params.set("fp", this.stream.reality.settings.fingerprint);
  1324. params.set("pbk", this.stream.reality.settings.publicKey);
  1325. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1326. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1327. }
  1328. if (this.stream.reality.shortIds.length > 0) {
  1329. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1330. }
  1331. if (!ObjectUtil.isEmpty(this.stream.reality.settings.serverName)) {
  1332. address = this.stream.reality.settings.serverName;
  1333. }
  1334. }
  1335. if (this.xtls) {
  1336. params.set("security", "xtls");
  1337. params.set("alpn", this.stream.xtls.alpn);
  1338. if(this.stream.xtls.settings.allowInsecure){
  1339. params.set("allowInsecure", "1");
  1340. }
  1341. if (!ObjectUtil.isEmpty(this.stream.xtls.server)) {
  1342. address = this.stream.xtls.server;
  1343. }
  1344. if (this.stream.xtls.settings.serverName !== ''){
  1345. params.set("sni", this.stream.xtls.settings.serverName);
  1346. }
  1347. params.set("flow", this.settings.trojans[clientIndex].flow);
  1348. }
  1349. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}`;
  1350. const url = new URL(link);
  1351. for (const [key, value] of params) {
  1352. url.searchParams.set(key, value)
  1353. }
  1354. url.hash = encodeURIComponent(remark);
  1355. return url.toString();
  1356. }
  1357. genLink(address='', remark='', clientIndex=0) {
  1358. switch (this.protocol) {
  1359. case Protocols.VMESS:
  1360. if (this.settings.vmesses[clientIndex].email != ""){
  1361. remark += '-' + this.settings.vmesses[clientIndex].email
  1362. }
  1363. return this.genVmessLink(address, remark, clientIndex);
  1364. case Protocols.VLESS:
  1365. if (this.settings.vlesses[clientIndex].email != ""){
  1366. remark += '-' + this.settings.vlesses[clientIndex].email
  1367. }
  1368. return this.genVLESSLink(address, remark, clientIndex);
  1369. case Protocols.SHADOWSOCKS:
  1370. if (this.settings.shadowsockses[clientIndex].email != ""){
  1371. remark = this.settings.shadowsockses[clientIndex].email
  1372. }
  1373. return this.genSSLink(address, remark, clientIndex);
  1374. case Protocols.TROJAN:
  1375. if (this.settings.trojans[clientIndex].email != ""){
  1376. remark += '-' + this.settings.trojans[clientIndex].email
  1377. }
  1378. return this.genTrojanLink(address, remark, clientIndex);
  1379. default: return '';
  1380. }
  1381. }
  1382. genInboundLinks(address = '', remark = '') {
  1383. let link = '';
  1384. switch (this.protocol) {
  1385. case Protocols.VMESS:
  1386. case Protocols.VLESS:
  1387. case Protocols.TROJAN:
  1388. JSON.parse(this.settings).clients.forEach((_,index) => {
  1389. link += this.genLink(address, remark, index) + '\r\n';
  1390. });
  1391. return link;
  1392. case Protocols.SHADOWSOCKS:
  1393. return (this.genSSLink(address, remark) + '\r\n');
  1394. default: return '';
  1395. }
  1396. }
  1397. static fromJson(json={}) {
  1398. return new Inbound(
  1399. json.port,
  1400. json.listen,
  1401. json.protocol,
  1402. Inbound.Settings.fromJson(json.protocol, json.settings),
  1403. StreamSettings.fromJson(json.streamSettings),
  1404. json.tag,
  1405. Sniffing.fromJson(json.sniffing),
  1406. json.clientStats
  1407. )
  1408. }
  1409. toJson() {
  1410. let streamSettings;
  1411. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1412. streamSettings = this.stream.toJson();
  1413. }
  1414. return {
  1415. port: this.port,
  1416. listen: this.listen,
  1417. protocol: this.protocol,
  1418. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1419. streamSettings: streamSettings,
  1420. tag: this.tag,
  1421. sniffing: this.sniffing.toJson(),
  1422. clientStats: this.clientStats
  1423. };
  1424. }
  1425. }
  1426. Inbound.Settings = class extends XrayCommonClass {
  1427. constructor(protocol) {
  1428. super();
  1429. this.protocol = protocol;
  1430. }
  1431. static getSettings(protocol) {
  1432. switch (protocol) {
  1433. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1434. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1435. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1436. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1437. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1438. case Protocols.MTPROTO: return new Inbound.MtprotoSettings(protocol);
  1439. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1440. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1441. default: return null;
  1442. }
  1443. }
  1444. static fromJson(protocol, json) {
  1445. switch (protocol) {
  1446. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1447. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1448. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1449. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1450. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1451. case Protocols.MTPROTO: return Inbound.MtprotoSettings.fromJson(json);
  1452. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1453. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1454. default: return null;
  1455. }
  1456. }
  1457. toJson() {
  1458. return {};
  1459. }
  1460. };
  1461. Inbound.VmessSettings = class extends Inbound.Settings {
  1462. constructor(protocol,
  1463. vmesses=[new Inbound.VmessSettings.Vmess()],
  1464. disableInsecureEncryption=false) {
  1465. super(protocol);
  1466. this.vmesses = vmesses;
  1467. this.disableInsecure = disableInsecureEncryption;
  1468. }
  1469. indexOfVmessById(id) {
  1470. return this.vmesses.findIndex(vmess => vmess.id === id);
  1471. }
  1472. addVmess(vmess) {
  1473. if (this.indexOfVmessById(vmess.id) >= 0) {
  1474. return false;
  1475. }
  1476. this.vmesses.push(vmess);
  1477. }
  1478. delVmess(vmess) {
  1479. const i = this.indexOfVmessById(vmess.id);
  1480. if (i >= 0) {
  1481. this.vmesses.splice(i, 1);
  1482. }
  1483. }
  1484. static fromJson(json={}) {
  1485. return new Inbound.VmessSettings(
  1486. Protocols.VMESS,
  1487. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1488. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1489. );
  1490. }
  1491. toJson() {
  1492. return {
  1493. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1494. disableInsecureEncryption: this.disableInsecure,
  1495. };
  1496. }
  1497. };
  1498. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1499. constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1500. super();
  1501. this.id = id;
  1502. this.alterId = alterId;
  1503. this.email = email;
  1504. this.limitIp = limitIp;
  1505. this.totalGB = totalGB;
  1506. this.expiryTime = expiryTime;
  1507. this.enable = enable;
  1508. this.tgId = tgId;
  1509. this.subId = subId;
  1510. }
  1511. static fromJson(json={}) {
  1512. return new Inbound.VmessSettings.Vmess(
  1513. json.id,
  1514. json.alterId,
  1515. json.email,
  1516. json.limitIp,
  1517. json.totalGB,
  1518. json.expiryTime,
  1519. json.enable,
  1520. json.tgId,
  1521. json.subId,
  1522. );
  1523. }
  1524. get _expiryTime() {
  1525. if (this.expiryTime === 0 || this.expiryTime === "") {
  1526. return null;
  1527. }
  1528. if (this.expiryTime < 0){
  1529. return this.expiryTime / -86400000;
  1530. }
  1531. return moment(this.expiryTime);
  1532. }
  1533. set _expiryTime(t) {
  1534. if (t == null || t === "") {
  1535. this.expiryTime = 0;
  1536. } else {
  1537. this.expiryTime = t.valueOf();
  1538. }
  1539. }
  1540. get _totalGB() {
  1541. return toFixed(this.totalGB / ONE_GB, 2);
  1542. }
  1543. set _totalGB(gb) {
  1544. this.totalGB = toFixed(gb * ONE_GB, 0);
  1545. }
  1546. };
  1547. Inbound.VLESSSettings = class extends Inbound.Settings {
  1548. constructor(protocol,
  1549. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1550. decryption='none',
  1551. fallbacks=[],) {
  1552. super(protocol);
  1553. this.vlesses = vlesses;
  1554. this.decryption = 'none'; // Using decryption is not implemented here
  1555. this.fallbacks = fallbacks;
  1556. }
  1557. addFallback() {
  1558. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1559. }
  1560. delFallback(index) {
  1561. this.fallbacks.splice(index, 1);
  1562. }
  1563. // decryption should be set to static value
  1564. static fromJson(json={}) {
  1565. return new Inbound.VLESSSettings(
  1566. Protocols.VLESS,
  1567. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1568. 'none',
  1569. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1570. );
  1571. }
  1572. toJson() {
  1573. return {
  1574. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1575. decryption: 'none',
  1576. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1577. };
  1578. }
  1579. };
  1580. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1581. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1582. super();
  1583. this.id = id;
  1584. this.flow = flow;
  1585. this.email = email;
  1586. this.limitIp = limitIp;
  1587. this.totalGB = totalGB;
  1588. this.expiryTime = expiryTime;
  1589. this.enable = enable;
  1590. this.tgId = tgId;
  1591. this.subId = subId;
  1592. }
  1593. static fromJson(json={}) {
  1594. return new Inbound.VLESSSettings.VLESS(
  1595. json.id,
  1596. json.flow,
  1597. json.email,
  1598. json.limitIp,
  1599. json.totalGB,
  1600. json.expiryTime,
  1601. json.enable,
  1602. json.tgId,
  1603. json.subId,
  1604. );
  1605. }
  1606. get _expiryTime() {
  1607. if (this.expiryTime === 0 || this.expiryTime === "") {
  1608. return null;
  1609. }
  1610. if (this.expiryTime < 0){
  1611. return this.expiryTime / -86400000;
  1612. }
  1613. return moment(this.expiryTime);
  1614. }
  1615. set _expiryTime(t) {
  1616. if (t == null || t === "") {
  1617. this.expiryTime = 0;
  1618. } else {
  1619. this.expiryTime = t.valueOf();
  1620. }
  1621. }
  1622. get _totalGB() {
  1623. return toFixed(this.totalGB / ONE_GB, 2);
  1624. }
  1625. set _totalGB(gb) {
  1626. this.totalGB = toFixed(gb * ONE_GB, 0);
  1627. }
  1628. };
  1629. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1630. constructor(name="", alpn='', path='', dest='', xver=0) {
  1631. super();
  1632. this.name = name;
  1633. this.alpn = alpn;
  1634. this.path = path;
  1635. this.dest = dest;
  1636. this.xver = xver;
  1637. }
  1638. toJson() {
  1639. let xver = this.xver;
  1640. if (!Number.isInteger(xver)) {
  1641. xver = 0;
  1642. }
  1643. return {
  1644. name: this.name,
  1645. alpn: this.alpn,
  1646. path: this.path,
  1647. dest: this.dest,
  1648. xver: xver,
  1649. }
  1650. }
  1651. static fromJson(json=[]) {
  1652. const fallbacks = [];
  1653. for (let fallback of json) {
  1654. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1655. fallback.name,
  1656. fallback.alpn,
  1657. fallback.path,
  1658. fallback.dest,
  1659. fallback.xver,
  1660. ))
  1661. }
  1662. return fallbacks;
  1663. }
  1664. };
  1665. Inbound.TrojanSettings = class extends Inbound.Settings {
  1666. constructor(protocol,
  1667. trojans=[new Inbound.TrojanSettings.Trojan()],
  1668. fallbacks=[],) {
  1669. super(protocol);
  1670. this.trojans = trojans;
  1671. this.fallbacks = fallbacks;
  1672. }
  1673. addTrojanFallback() {
  1674. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1675. }
  1676. delTrojanFallback(index) {
  1677. this.fallbacks.splice(index, 1);
  1678. }
  1679. static fromJson(json={}) {
  1680. return new Inbound.TrojanSettings(
  1681. Protocols.TROJAN,
  1682. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1683. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1684. }
  1685. toJson() {
  1686. return {
  1687. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1688. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1689. };
  1690. }
  1691. };
  1692. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1693. constructor(password=RandomUtil.randomSeq(10), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1694. super();
  1695. this.password = password;
  1696. this.flow = flow;
  1697. this.email = email;
  1698. this.limitIp = limitIp;
  1699. this.totalGB = totalGB;
  1700. this.expiryTime = expiryTime;
  1701. this.enable = enable;
  1702. this.tgId = tgId;
  1703. this.subId = subId;
  1704. }
  1705. toJson() {
  1706. return {
  1707. password: this.password,
  1708. flow: this.flow,
  1709. email: this.email,
  1710. limitIp: this.limitIp,
  1711. totalGB: this.totalGB,
  1712. expiryTime: this.expiryTime,
  1713. enable: this.enable,
  1714. tgId: this.tgId,
  1715. subId: this.subId,
  1716. };
  1717. }
  1718. static fromJson(json = {}) {
  1719. return new Inbound.TrojanSettings.Trojan(
  1720. json.password,
  1721. json.flow,
  1722. json.email,
  1723. json.limitIp,
  1724. json.totalGB,
  1725. json.expiryTime,
  1726. json.enable,
  1727. json.tgId,
  1728. json.subId,
  1729. );
  1730. }
  1731. get _expiryTime() {
  1732. if (this.expiryTime === 0 || this.expiryTime === "") {
  1733. return null;
  1734. }
  1735. if (this.expiryTime < 0){
  1736. return this.expiryTime / -86400000;
  1737. }
  1738. return moment(this.expiryTime);
  1739. }
  1740. set _expiryTime(t) {
  1741. if (t == null || t === "") {
  1742. this.expiryTime = 0;
  1743. } else {
  1744. this.expiryTime = t.valueOf();
  1745. }
  1746. }
  1747. get _totalGB() {
  1748. return toFixed(this.totalGB / ONE_GB, 2);
  1749. }
  1750. set _totalGB(gb) {
  1751. this.totalGB = toFixed(gb * ONE_GB, 0);
  1752. }
  1753. };
  1754. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1755. constructor(name="", alpn='', path='', dest='', xver=0) {
  1756. super();
  1757. this.name = name;
  1758. this.alpn = alpn;
  1759. this.path = path;
  1760. this.dest = dest;
  1761. this.xver = xver;
  1762. }
  1763. toJson() {
  1764. let xver = this.xver;
  1765. if (!Number.isInteger(xver)) {
  1766. xver = 0;
  1767. }
  1768. return {
  1769. name: this.name,
  1770. alpn: this.alpn,
  1771. path: this.path,
  1772. dest: this.dest,
  1773. xver: xver,
  1774. }
  1775. }
  1776. static fromJson(json=[]) {
  1777. const fallbacks = [];
  1778. for (let fallback of json) {
  1779. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1780. fallback.name,
  1781. fallback.alpn,
  1782. fallback.path,
  1783. fallback.dest,
  1784. fallback.xver,
  1785. ))
  1786. }
  1787. return fallbacks;
  1788. }
  1789. };
  1790. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1791. constructor(protocol,
  1792. method=SSMethods.BLAKE3_AES_256_GCM,
  1793. password=RandomUtil.randomShadowsocksPassword(),
  1794. network='tcp,udp',
  1795. shadowsockses=[new Inbound.ShadowsocksSettings.Shadowsocks()]
  1796. ) {
  1797. super(protocol);
  1798. this.method = method;
  1799. this.password = password;
  1800. this.network = network;
  1801. this.shadowsockses = shadowsockses;
  1802. }
  1803. static fromJson(json={}) {
  1804. return new Inbound.ShadowsocksSettings(
  1805. Protocols.SHADOWSOCKS,
  1806. json.method,
  1807. json.password,
  1808. json.network,
  1809. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  1810. );
  1811. }
  1812. toJson() {
  1813. return {
  1814. method: this.method,
  1815. password: this.password,
  1816. network: this.network,
  1817. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses)
  1818. };
  1819. }
  1820. };
  1821. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  1822. constructor(password=RandomUtil.randomShadowsocksPassword(), email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1823. super();
  1824. this.password = password;
  1825. this.email = email;
  1826. this.limitIp = limitIp;
  1827. this.totalGB = totalGB;
  1828. this.expiryTime = expiryTime;
  1829. this.enable = enable;
  1830. this.tgId = tgId;
  1831. this.subId = subId;
  1832. }
  1833. toJson() {
  1834. return {
  1835. password: this.password,
  1836. email: this.email,
  1837. limitIp: this.limitIp,
  1838. totalGB: this.totalGB,
  1839. expiryTime: this.expiryTime,
  1840. enable: this.enable,
  1841. tgId: this.tgId,
  1842. subId: this.subId,
  1843. };
  1844. }
  1845. static fromJson(json = {}) {
  1846. return new Inbound.ShadowsocksSettings.Shadowsocks(
  1847. json.password,
  1848. json.email,
  1849. json.limitIp,
  1850. json.totalGB,
  1851. json.expiryTime,
  1852. json.enable,
  1853. json.tgId,
  1854. json.subId,
  1855. );
  1856. }
  1857. get _expiryTime() {
  1858. if (this.expiryTime === 0 || this.expiryTime === "") {
  1859. return null;
  1860. }
  1861. if (this.expiryTime < 0){
  1862. return this.expiryTime / -86400000;
  1863. }
  1864. return moment(this.expiryTime);
  1865. }
  1866. set _expiryTime(t) {
  1867. if (t == null || t === "") {
  1868. this.expiryTime = 0;
  1869. } else {
  1870. this.expiryTime = t.valueOf();
  1871. }
  1872. }
  1873. get _totalGB() {
  1874. return toFixed(this.totalGB / ONE_GB, 2);
  1875. }
  1876. set _totalGB(gb) {
  1877. this.totalGB = toFixed(gb * ONE_GB, 0);
  1878. }
  1879. };
  1880. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1881. constructor(protocol, address, port, network='tcp,udp', followRedirect=false) {
  1882. super(protocol);
  1883. this.address = address;
  1884. this.port = port;
  1885. this.network = network;
  1886. this.followRedirect = followRedirect;
  1887. }
  1888. static fromJson(json={}) {
  1889. return new Inbound.DokodemoSettings(
  1890. Protocols.DOKODEMO,
  1891. json.address,
  1892. json.port,
  1893. json.network,
  1894. json.followRedirect,
  1895. );
  1896. }
  1897. toJson() {
  1898. return {
  1899. address: this.address,
  1900. port: this.port,
  1901. network: this.network,
  1902. followRedirect: this.followRedirect,
  1903. };
  1904. }
  1905. };
  1906. Inbound.MtprotoSettings = class extends Inbound.Settings {
  1907. constructor(protocol, users=[new Inbound.MtprotoSettings.MtUser()]) {
  1908. super(protocol);
  1909. this.users = users;
  1910. }
  1911. static fromJson(json={}) {
  1912. return new Inbound.MtprotoSettings(
  1913. Protocols.MTPROTO,
  1914. json.users.map(user => Inbound.MtprotoSettings.MtUser.fromJson(user)),
  1915. );
  1916. }
  1917. toJson() {
  1918. return {
  1919. users: XrayCommonClass.toJsonArray(this.users),
  1920. };
  1921. }
  1922. };
  1923. Inbound.MtprotoSettings.MtUser = class extends XrayCommonClass {
  1924. constructor(secret=RandomUtil.randomMTSecret()) {
  1925. super();
  1926. this.secret = secret;
  1927. }
  1928. static fromJson(json={}) {
  1929. return new Inbound.MtprotoSettings.MtUser(json.secret);
  1930. }
  1931. };
  1932. Inbound.SocksSettings = class extends Inbound.Settings {
  1933. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1934. super(protocol);
  1935. this.auth = auth;
  1936. this.accounts = accounts;
  1937. this.udp = udp;
  1938. this.ip = ip;
  1939. }
  1940. addAccount(account) {
  1941. this.accounts.push(account);
  1942. }
  1943. delAccount(index) {
  1944. this.accounts.splice(index, 1);
  1945. }
  1946. static fromJson(json={}) {
  1947. let accounts;
  1948. if (json.auth === 'password') {
  1949. accounts = json.accounts.map(
  1950. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1951. )
  1952. }
  1953. return new Inbound.SocksSettings(
  1954. Protocols.SOCKS,
  1955. json.auth,
  1956. accounts,
  1957. json.udp,
  1958. json.ip,
  1959. );
  1960. }
  1961. toJson() {
  1962. return {
  1963. auth: this.auth,
  1964. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1965. udp: this.udp,
  1966. ip: this.ip,
  1967. };
  1968. }
  1969. };
  1970. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1971. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1972. super();
  1973. this.user = user;
  1974. this.pass = pass;
  1975. }
  1976. static fromJson(json={}) {
  1977. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1978. }
  1979. };
  1980. Inbound.HttpSettings = class extends Inbound.Settings {
  1981. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1982. super(protocol);
  1983. this.accounts = accounts;
  1984. }
  1985. addAccount(account) {
  1986. this.accounts.push(account);
  1987. }
  1988. delAccount(index) {
  1989. this.accounts.splice(index, 1);
  1990. }
  1991. static fromJson(json={}) {
  1992. return new Inbound.HttpSettings(
  1993. Protocols.HTTP,
  1994. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1995. );
  1996. }
  1997. toJson() {
  1998. return {
  1999. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2000. };
  2001. }
  2002. };
  2003. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2004. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  2005. super();
  2006. this.user = user;
  2007. this.pass = pass;
  2008. }
  2009. static fromJson(json={}) {
  2010. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2011. }
  2012. };