xray.js 63 KB

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