xray.js 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  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 XTLS_FLOW_CONTROL = {
  29. ORIGIN: "xtls-rprx-origin",
  30. DIRECT: "xtls-rprx-direct",
  31. };
  32. const TLS_FLOW_CONTROL = {
  33. VISION: "xtls-rprx-vision",
  34. VISION_UDP443: "xtls-rprx-vision-udp443",
  35. };
  36. const TLS_VERSION_OPTION = {
  37. TLS10: "1.0",
  38. TLS11: "1.1",
  39. TLS12: "1.2",
  40. TLS13: "1.3",
  41. };
  42. const TLS_CIPHER_OPTION = {
  43. RSA_AES_128_CBC: "TLS_RSA_WITH_AES_128_CBC_SHA",
  44. RSA_AES_256_CBC: "TLS_RSA_WITH_AES_256_CBC_SHA",
  45. RSA_AES_128_GCM: "TLS_RSA_WITH_AES_128_GCM_SHA256",
  46. RSA_AES_256_GCM: "TLS_RSA_WITH_AES_256_GCM_SHA384",
  47. AES_128_GCM: "TLS_AES_128_GCM_SHA256",
  48. AES_256_GCM: "TLS_AES_256_GCM_SHA384",
  49. CHACHA20_POLY1305: "TLS_CHACHA20_POLY1305_SHA256",
  50. ECDHE_ECDSA_AES_128_CBC: "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
  51. ECDHE_ECDSA_AES_256_CBC: "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
  52. ECDHE_RSA_AES_128_CBC: "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",
  53. ECDHE_RSA_AES_256_CBC: "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",
  54. ECDHE_ECDSA_AES_128_GCM: "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
  55. ECDHE_ECDSA_AES_256_GCM: "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  56. ECDHE_RSA_AES_128_GCM: "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  57. ECDHE_RSA_AES_256_GCM: "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",
  58. ECDHE_ECDSA_CHACHA20_POLY1305: "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",
  59. ECDHE_RSA_CHACHA20_POLY1305: "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",
  60. };
  61. const UTLS_FINGERPRINT = {
  62. UTLS_CHROME: "chrome",
  63. UTLS_FIREFOX: "firefox",
  64. UTLS_SAFARI: "safari",
  65. UTLS_IOS: "ios",
  66. UTLS_android: "android",
  67. UTLS_EDGE: "edge",
  68. UTLS_360: "360",
  69. UTLS_QQ: "qq",
  70. UTLS_RANDOM: "random",
  71. UTLS_RANDOMIZED: "randomized",
  72. };
  73. const ALPN_OPTION = {
  74. HTTP1: "http/1.1",
  75. H2: "h2",
  76. H3: "h3",
  77. };
  78. const SNIFFING_OPTION = {
  79. HTTP: "http",
  80. TLS: "tls",
  81. QUIC: "quic",
  82. };
  83. Object.freeze(Protocols);
  84. Object.freeze(VmessMethods);
  85. Object.freeze(SSMethods);
  86. Object.freeze(XTLS_FLOW_CONTROL);
  87. Object.freeze(TLS_FLOW_CONTROL);
  88. Object.freeze(TLS_VERSION_OPTION);
  89. Object.freeze(TLS_CIPHER_OPTION);
  90. Object.freeze(ALPN_OPTION);
  91. Object.freeze(SNIFFING_OPTION);
  92. class XrayCommonClass {
  93. static toJsonArray(arr) {
  94. return arr.map(obj => obj.toJson());
  95. }
  96. static fromJson() {
  97. return new XrayCommonClass();
  98. }
  99. toJson() {
  100. return this;
  101. }
  102. toString(format=true) {
  103. return format ? JSON.stringify(this.toJson(), null, 2) : JSON.stringify(this.toJson());
  104. }
  105. static toHeaders(v2Headers) {
  106. let newHeaders = [];
  107. if (v2Headers) {
  108. Object.keys(v2Headers).forEach(key => {
  109. let values = v2Headers[key];
  110. if (typeof(values) === 'string') {
  111. newHeaders.push({ name: key, value: values });
  112. } else {
  113. for (let i = 0; i < values.length; ++i) {
  114. newHeaders.push({ name: key, value: values[i] });
  115. }
  116. }
  117. });
  118. }
  119. return newHeaders;
  120. }
  121. static toV2Headers(headers, arr=true) {
  122. let v2Headers = {};
  123. for (let i = 0; i < headers.length; ++i) {
  124. let name = headers[i].name;
  125. let value = headers[i].value;
  126. if (ObjectUtil.isEmpty(name) || ObjectUtil.isEmpty(value)) {
  127. continue;
  128. }
  129. if (!(name in v2Headers)) {
  130. v2Headers[name] = arr ? [value] : value;
  131. } else {
  132. if (arr) {
  133. v2Headers[name].push(value);
  134. } else {
  135. v2Headers[name] = value;
  136. }
  137. }
  138. }
  139. return v2Headers;
  140. }
  141. }
  142. class TcpStreamSettings extends XrayCommonClass {
  143. constructor(acceptProxyProtocol=false,
  144. type='none',
  145. request=new TcpStreamSettings.TcpRequest(),
  146. response=new TcpStreamSettings.TcpResponse(),
  147. ) {
  148. super();
  149. this.acceptProxyProtocol = acceptProxyProtocol;
  150. this.type = type;
  151. this.request = request;
  152. this.response = response;
  153. }
  154. static fromJson(json={}) {
  155. let header = json.header;
  156. if (!header) {
  157. header = {};
  158. }
  159. return new TcpStreamSettings(json.acceptProxyProtocol,
  160. header.type,
  161. TcpStreamSettings.TcpRequest.fromJson(header.request),
  162. TcpStreamSettings.TcpResponse.fromJson(header.response),
  163. );
  164. }
  165. toJson() {
  166. return {
  167. acceptProxyProtocol: this.acceptProxyProtocol,
  168. header: {
  169. type: this.type,
  170. request: this.type === 'http' ? this.request.toJson() : undefined,
  171. response: this.type === 'http' ? this.response.toJson() : undefined,
  172. },
  173. };
  174. }
  175. }
  176. TcpStreamSettings.TcpRequest = class extends XrayCommonClass {
  177. constructor(version='1.1',
  178. method='GET',
  179. path=['/'],
  180. headers=[],
  181. ) {
  182. super();
  183. this.version = version;
  184. this.method = method;
  185. this.path = path.length === 0 ? ['/'] : path;
  186. this.headers = headers;
  187. }
  188. addPath(path) {
  189. this.path.push(path);
  190. }
  191. removePath(index) {
  192. this.path.splice(index, 1);
  193. }
  194. addHeader(name, value) {
  195. this.headers.push({ name: name, value: value });
  196. }
  197. getHeader(name) {
  198. for (const header of this.headers) {
  199. if (header.name.toLowerCase() === name.toLowerCase()) {
  200. return header.value;
  201. }
  202. }
  203. return null;
  204. }
  205. removeHeader(index) {
  206. this.headers.splice(index, 1);
  207. }
  208. static fromJson(json={}) {
  209. return new TcpStreamSettings.TcpRequest(
  210. json.version,
  211. json.method,
  212. json.path,
  213. XrayCommonClass.toHeaders(json.headers),
  214. );
  215. }
  216. toJson() {
  217. return {
  218. method: this.method,
  219. path: ObjectUtil.clone(this.path),
  220. headers: XrayCommonClass.toV2Headers(this.headers),
  221. };
  222. }
  223. };
  224. TcpStreamSettings.TcpResponse = class extends XrayCommonClass {
  225. constructor(version='1.1',
  226. status='200',
  227. reason='OK',
  228. headers=[],
  229. ) {
  230. super();
  231. this.version = version;
  232. this.status = status;
  233. this.reason = reason;
  234. this.headers = headers;
  235. }
  236. addHeader(name, value) {
  237. this.headers.push({ name: name, value: value });
  238. }
  239. removeHeader(index) {
  240. this.headers.splice(index, 1);
  241. }
  242. static fromJson(json={}) {
  243. return new TcpStreamSettings.TcpResponse(
  244. json.version,
  245. json.status,
  246. json.reason,
  247. XrayCommonClass.toHeaders(json.headers),
  248. );
  249. }
  250. toJson() {
  251. return {
  252. version: this.version,
  253. status: this.status,
  254. reason: this.reason,
  255. headers: XrayCommonClass.toV2Headers(this.headers),
  256. };
  257. }
  258. };
  259. class KcpStreamSettings extends XrayCommonClass {
  260. constructor(mtu=1350, tti=20,
  261. uplinkCapacity=5,
  262. downlinkCapacity=20,
  263. congestion=false,
  264. readBufferSize=2,
  265. writeBufferSize=2,
  266. type='none',
  267. seed=RandomUtil.randomSeq(10),
  268. ) {
  269. super();
  270. this.mtu = mtu;
  271. this.tti = tti;
  272. this.upCap = uplinkCapacity;
  273. this.downCap = downlinkCapacity;
  274. this.congestion = congestion;
  275. this.readBuffer = readBufferSize;
  276. this.writeBuffer = writeBufferSize;
  277. this.type = type;
  278. this.seed = seed;
  279. }
  280. static fromJson(json={}) {
  281. return new KcpStreamSettings(
  282. json.mtu,
  283. json.tti,
  284. json.uplinkCapacity,
  285. json.downlinkCapacity,
  286. json.congestion,
  287. json.readBufferSize,
  288. json.writeBufferSize,
  289. ObjectUtil.isEmpty(json.header) ? 'none' : json.header.type,
  290. json.seed,
  291. );
  292. }
  293. toJson() {
  294. return {
  295. mtu: this.mtu,
  296. tti: this.tti,
  297. uplinkCapacity: this.upCap,
  298. downlinkCapacity: this.downCap,
  299. congestion: this.congestion,
  300. readBufferSize: this.readBuffer,
  301. writeBufferSize: this.writeBuffer,
  302. header: {
  303. type: this.type,
  304. },
  305. seed: this.seed,
  306. };
  307. }
  308. }
  309. class WsStreamSettings extends XrayCommonClass {
  310. constructor(acceptProxyProtocol=false, path='/', headers=[]) {
  311. super();
  312. this.acceptProxyProtocol = acceptProxyProtocol;
  313. this.path = path;
  314. this.headers = headers;
  315. }
  316. addHeader(name, value) {
  317. this.headers.push({ name: name, value: value });
  318. }
  319. getHeader(name) {
  320. for (const header of this.headers) {
  321. if (header.name.toLowerCase() === name.toLowerCase()) {
  322. return header.value;
  323. }
  324. }
  325. return null;
  326. }
  327. removeHeader(index) {
  328. this.headers.splice(index, 1);
  329. }
  330. static fromJson(json={}) {
  331. return new WsStreamSettings(
  332. json.acceptProxyProtocol,
  333. json.path,
  334. XrayCommonClass.toHeaders(json.headers),
  335. );
  336. }
  337. toJson() {
  338. return {
  339. acceptProxyProtocol: this.acceptProxyProtocol,
  340. path: this.path,
  341. headers: XrayCommonClass.toV2Headers(this.headers, false),
  342. };
  343. }
  344. }
  345. class HttpStreamSettings extends XrayCommonClass {
  346. constructor(path='/', host=['']) {
  347. super();
  348. this.path = path;
  349. this.host = host.length === 0 ? [''] : host;
  350. }
  351. addHost(host) {
  352. this.host.push(host);
  353. }
  354. removeHost(index) {
  355. this.host.splice(index, 1);
  356. }
  357. static fromJson(json={}) {
  358. return new HttpStreamSettings(json.path, json.host);
  359. }
  360. toJson() {
  361. let host = [];
  362. for (let i = 0; i < this.host.length; ++i) {
  363. if (!ObjectUtil.isEmpty(this.host[i])) {
  364. host.push(this.host[i]);
  365. }
  366. }
  367. return {
  368. path: this.path,
  369. host: host,
  370. }
  371. }
  372. }
  373. class QuicStreamSettings extends XrayCommonClass {
  374. constructor(security=VmessMethods.NONE,
  375. key='', type='none') {
  376. super();
  377. this.security = security;
  378. this.key = key;
  379. this.type = type;
  380. }
  381. static fromJson(json={}) {
  382. return new QuicStreamSettings(
  383. json.security,
  384. json.key,
  385. json.header ? json.header.type : 'none',
  386. );
  387. }
  388. toJson() {
  389. return {
  390. security: this.security,
  391. key: this.key,
  392. header: {
  393. type: this.type,
  394. }
  395. }
  396. }
  397. }
  398. class GrpcStreamSettings extends XrayCommonClass {
  399. constructor(
  400. serviceName="",
  401. multiMode=false
  402. ) {
  403. super();
  404. this.serviceName = serviceName;
  405. this.multiMode = multiMode;
  406. }
  407. static fromJson(json={}) {
  408. return new GrpcStreamSettings(
  409. json.serviceName,
  410. json.multiMode
  411. );
  412. }
  413. toJson() {
  414. return {
  415. serviceName: this.serviceName,
  416. multiMode: this.multiMode
  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', 'quic']) {
  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', 'quic'];
  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 isH2() {
  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.isH2) {
  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.isH2) {
  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. case Protocols.SHADOWSOCKS:
  1040. break;
  1041. default:
  1042. return false;
  1043. }
  1044. switch (this.network) {
  1045. case "tcp":
  1046. case "ws":
  1047. case "http":
  1048. case "quic":
  1049. case "grpc":
  1050. return true;
  1051. default:
  1052. return false;
  1053. }
  1054. }
  1055. canEnableReality() {
  1056. switch (this.protocol) {
  1057. case Protocols.VLESS:
  1058. case Protocols.TROJAN:
  1059. break;
  1060. default:
  1061. return false;
  1062. }
  1063. switch (this.network) {
  1064. case "tcp":
  1065. case "http":
  1066. case "grpc":
  1067. return true;
  1068. default:
  1069. return false;
  1070. }
  1071. }
  1072. //this is used for xtls-rprx-vision
  1073. canEnableTlsFlow() {
  1074. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1075. switch (this.protocol) {
  1076. case Protocols.VLESS:
  1077. return true;
  1078. default:
  1079. return false;
  1080. }
  1081. }
  1082. return false;
  1083. }
  1084. canSetTls() {
  1085. return this.canEnableTls();
  1086. }
  1087. canEnableXtls() {
  1088. switch (this.protocol) {
  1089. case Protocols.VLESS:
  1090. case Protocols.TROJAN:
  1091. break;
  1092. default:
  1093. return false;
  1094. }
  1095. return this.network === "tcp";
  1096. }
  1097. canEnableStream() {
  1098. switch (this.protocol) {
  1099. case Protocols.VMESS:
  1100. case Protocols.VLESS:
  1101. case Protocols.TROJAN:
  1102. case Protocols.SHADOWSOCKS:
  1103. return true;
  1104. default:
  1105. return false;
  1106. }
  1107. }
  1108. canSniffing() {
  1109. switch (this.protocol) {
  1110. case Protocols.VMESS:
  1111. case Protocols.VLESS:
  1112. case Protocols.TROJAN:
  1113. case Protocols.SHADOWSOCKS:
  1114. return true;
  1115. default:
  1116. return false;
  1117. }
  1118. }
  1119. reset() {
  1120. this.port = RandomUtil.randomIntRange(10000, 60000);
  1121. this.listen = '';
  1122. this.protocol = Protocols.VMESS;
  1123. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1124. this.stream = new StreamSettings();
  1125. this.tag = '';
  1126. this.sniffing = new Sniffing();
  1127. }
  1128. genVmessLink(address='', remark='', clientIndex=0) {
  1129. if (this.protocol !== Protocols.VMESS) {
  1130. return '';
  1131. }
  1132. let obj = {
  1133. v: '2',
  1134. ps: remark,
  1135. add: address,
  1136. port: this.port,
  1137. id: this.settings.vmesses[clientIndex].id,
  1138. aid: this.settings.vmesses[clientIndex].alterId,
  1139. net: this.stream.network,
  1140. type: 'none',
  1141. tls: this.stream.security,
  1142. };
  1143. let network = this.stream.network;
  1144. if (network === 'tcp') {
  1145. let tcp = this.stream.tcp;
  1146. obj.type = tcp.type;
  1147. if (tcp.type === 'http') {
  1148. let request = tcp.request;
  1149. obj.path = request.path.join(',');
  1150. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1151. if (index >= 0) {
  1152. obj.host = request.headers[index].value;
  1153. }
  1154. }
  1155. } else if (network === 'kcp') {
  1156. let kcp = this.stream.kcp;
  1157. obj.type = kcp.type;
  1158. obj.path = kcp.seed;
  1159. } else if (network === 'ws') {
  1160. let ws = this.stream.ws;
  1161. obj.path = ws.path;
  1162. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1163. if (index >= 0) {
  1164. obj.host = ws.headers[index].value;
  1165. }
  1166. } else if (network === 'http') {
  1167. obj.net = 'h2';
  1168. obj.path = this.stream.http.path;
  1169. obj.host = this.stream.http.host.join(',');
  1170. } else if (network === 'quic') {
  1171. obj.type = this.stream.quic.type;
  1172. obj.host = this.stream.quic.security;
  1173. obj.path = this.stream.quic.key;
  1174. } else if (network === 'grpc') {
  1175. obj.path = this.stream.grpc.serviceName;
  1176. if (this.stream.grpc.multiMode){
  1177. obj.type = 'multi'
  1178. }
  1179. }
  1180. if (this.stream.security === 'tls') {
  1181. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1182. obj.add = this.stream.tls.server;
  1183. }
  1184. if (!ObjectUtil.isEmpty(this.stream.tls.settings.serverName)){
  1185. obj.sni = this.stream.tls.settings.serverName;
  1186. }
  1187. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)){
  1188. obj.fp = this.stream.tls.settings.fingerprint;
  1189. }
  1190. if (this.stream.tls.alpn.length>0){
  1191. obj.alpn = this.stream.tls.alpn.join(',');
  1192. }
  1193. if (this.stream.tls.settings.allowInsecure){
  1194. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1195. }
  1196. }
  1197. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  1198. }
  1199. genVLESSLink(address = '', remark='', clientIndex=0) {
  1200. const settings = this.settings;
  1201. const uuid = settings.vlesses[clientIndex].id;
  1202. const port = this.port;
  1203. const type = this.stream.network;
  1204. const params = new Map();
  1205. params.set("type", this.stream.network);
  1206. switch (type) {
  1207. case "tcp":
  1208. const tcp = this.stream.tcp;
  1209. if (tcp.type === 'http') {
  1210. const request = tcp.request;
  1211. params.set("path", request.path.join(','));
  1212. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1213. if (index >= 0) {
  1214. const host = request.headers[index].value;
  1215. params.set("host", host);
  1216. }
  1217. params.set("headerType", 'http');
  1218. }
  1219. break;
  1220. case "kcp":
  1221. const kcp = this.stream.kcp;
  1222. params.set("headerType", kcp.type);
  1223. params.set("seed", kcp.seed);
  1224. break;
  1225. case "ws":
  1226. const ws = this.stream.ws;
  1227. params.set("path", ws.path);
  1228. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1229. if (index >= 0) {
  1230. const host = ws.headers[index].value;
  1231. params.set("host", host);
  1232. }
  1233. break;
  1234. case "http":
  1235. const http = this.stream.http;
  1236. params.set("path", http.path);
  1237. params.set("host", http.host);
  1238. break;
  1239. case "quic":
  1240. const quic = this.stream.quic;
  1241. params.set("quicSecurity", quic.security);
  1242. params.set("key", quic.key);
  1243. params.set("headerType", quic.type);
  1244. break;
  1245. case "grpc":
  1246. const grpc = this.stream.grpc;
  1247. params.set("serviceName", grpc.serviceName);
  1248. if(grpc.multiMode){
  1249. params.set("mode", "multi");
  1250. }
  1251. break;
  1252. }
  1253. if (this.tls) {
  1254. params.set("security", "tls");
  1255. params.set("fp" , this.stream.tls.settings.fingerprint);
  1256. params.set("alpn", this.stream.tls.alpn);
  1257. if(this.stream.tls.settings.allowInsecure){
  1258. params.set("allowInsecure", "1");
  1259. }
  1260. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1261. address = this.stream.tls.server;
  1262. }
  1263. if (this.stream.tls.settings.serverName !== ''){
  1264. params.set("sni", this.stream.tls.settings.serverName);
  1265. }
  1266. if (type === "tcp" && this.settings.vlesses[clientIndex].flow.length > 0) {
  1267. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1268. }
  1269. }
  1270. if (this.xtls) {
  1271. params.set("security", "xtls");
  1272. params.set("alpn", this.stream.xtls.alpn);
  1273. if(this.stream.xtls.settings.allowInsecure){
  1274. params.set("allowInsecure", "1");
  1275. }
  1276. if (!ObjectUtil.isEmpty(this.stream.xtls.server)) {
  1277. address = this.stream.xtls.server;
  1278. }
  1279. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1280. }
  1281. if (this.reality) {
  1282. params.set("security", "reality");
  1283. params.set("fp", this.stream.reality.settings.fingerprint);
  1284. params.set("pbk", this.stream.reality.settings.publicKey);
  1285. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1286. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1287. }
  1288. if (this.stream.network === 'tcp' && !ObjectUtil.isEmpty(this.settings.vlesses[clientIndex].flow)) {
  1289. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1290. }
  1291. if (this.stream.reality.shortIds.length > 0) {
  1292. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1293. }
  1294. if (!ObjectUtil.isEmpty(this.stream.reality.settings.serverName)) {
  1295. address = this.stream.reality.settings.serverName;
  1296. }
  1297. }
  1298. const link = `vless://${uuid}@${address}:${port}`;
  1299. const url = new URL(link);
  1300. for (const [key, value] of params) {
  1301. url.searchParams.set(key, value)
  1302. }
  1303. url.hash = encodeURIComponent(remark);
  1304. return url.toString();
  1305. }
  1306. genSSLink(address='', remark='') {
  1307. let settings = this.settings;
  1308. const server = this.stream.tls.server;
  1309. if (!ObjectUtil.isEmpty(server)) {
  1310. address = server;
  1311. }
  1312. return 'ss://' + safeBase64(settings.method + ':' + settings.password) + `@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1313. }
  1314. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1315. let settings = this.settings;
  1316. const port = this.port;
  1317. const type = this.stream.network;
  1318. const params = new Map();
  1319. params.set("type", this.stream.network);
  1320. switch (type) {
  1321. case "tcp":
  1322. const tcp = this.stream.tcp;
  1323. if (tcp.type === 'http') {
  1324. const request = tcp.request;
  1325. params.set("path", request.path.join(','));
  1326. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1327. if (index >= 0) {
  1328. const host = request.headers[index].value;
  1329. params.set("host", host);
  1330. }
  1331. params.set("headerType", 'http');
  1332. }
  1333. break;
  1334. case "kcp":
  1335. const kcp = this.stream.kcp;
  1336. params.set("headerType", kcp.type);
  1337. params.set("seed", kcp.seed);
  1338. break;
  1339. case "ws":
  1340. const ws = this.stream.ws;
  1341. params.set("path", ws.path);
  1342. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1343. if (index >= 0) {
  1344. const host = ws.headers[index].value;
  1345. params.set("host", host);
  1346. }
  1347. break;
  1348. case "http":
  1349. const http = this.stream.http;
  1350. params.set("path", http.path);
  1351. params.set("host", http.host);
  1352. break;
  1353. case "quic":
  1354. const quic = this.stream.quic;
  1355. params.set("quicSecurity", quic.security);
  1356. params.set("key", quic.key);
  1357. params.set("headerType", quic.type);
  1358. break;
  1359. case "grpc":
  1360. const grpc = this.stream.grpc;
  1361. params.set("serviceName", grpc.serviceName);
  1362. if(grpc.multiMode){
  1363. params.set("mode", "multi");
  1364. }
  1365. break;
  1366. }
  1367. if (this.tls) {
  1368. params.set("security", "tls");
  1369. params.set("fp" , this.stream.tls.settings.fingerprint);
  1370. params.set("alpn", this.stream.tls.alpn);
  1371. if(this.stream.tls.settings.allowInsecure){
  1372. params.set("allowInsecure", "1");
  1373. }
  1374. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1375. address = this.stream.tls.server;
  1376. }
  1377. if (this.stream.tls.settings.serverName !== ''){
  1378. params.set("sni", this.stream.tls.settings.serverName);
  1379. }
  1380. }
  1381. if (this.reality) {
  1382. params.set("security", "reality");
  1383. params.set("fp", this.stream.reality.settings.fingerprint);
  1384. params.set("pbk", this.stream.reality.settings.publicKey);
  1385. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1386. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1387. }
  1388. if (this.stream.reality.shortIds.length > 0) {
  1389. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1390. }
  1391. if (!ObjectUtil.isEmpty(this.stream.reality.settings.serverName)) {
  1392. address = this.stream.reality.settings.serverName;
  1393. }
  1394. }
  1395. if (this.xtls) {
  1396. params.set("security", "xtls");
  1397. params.set("alpn", this.stream.xtls.alpn);
  1398. if(this.stream.xtls.settings.allowInsecure){
  1399. params.set("allowInsecure", "1");
  1400. }
  1401. if (!ObjectUtil.isEmpty(this.stream.xtls.server)) {
  1402. address = this.stream.xtls.server;
  1403. }
  1404. params.set("flow", this.settings.trojans[clientIndex].flow);
  1405. }
  1406. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}#${encodeURIComponent(remark)}`;
  1407. const url = new URL(link);
  1408. for (const [key, value] of params) {
  1409. url.searchParams.set(key, value)
  1410. }
  1411. url.hash = encodeURIComponent(remark);
  1412. return url.toString();
  1413. }
  1414. genLink(address='', remark='', clientIndex=0) {
  1415. switch (this.protocol) {
  1416. case Protocols.VMESS:
  1417. if (this.settings.vmesses[clientIndex].email != ""){
  1418. remark += '-' + this.settings.vmesses[clientIndex].email
  1419. }
  1420. return this.genVmessLink(address, remark, clientIndex);
  1421. case Protocols.VLESS:
  1422. if (this.settings.vlesses[clientIndex].email != ""){
  1423. remark += '-' + this.settings.vlesses[clientIndex].email
  1424. }
  1425. return this.genVLESSLink(address, remark, clientIndex);
  1426. case Protocols.SHADOWSOCKS: return this.genSSLink(address, remark);
  1427. case Protocols.TROJAN:
  1428. if (this.settings.trojans[clientIndex].email != ""){
  1429. remark += '-' + this.settings.trojans[clientIndex].email
  1430. }
  1431. return this.genTrojanLink(address, remark, clientIndex);
  1432. default: return '';
  1433. }
  1434. }
  1435. genInboundLinks(address = '', remark = '') {
  1436. let link = '';
  1437. switch (this.protocol) {
  1438. case Protocols.VMESS:
  1439. case Protocols.VLESS:
  1440. case Protocols.TROJAN:
  1441. JSON.parse(this.settings).clients.forEach((_,index) => {
  1442. link += this.genLink(address, remark, index) + '\r\n';
  1443. });
  1444. return link;
  1445. case Protocols.SHADOWSOCKS:
  1446. return (this.genSSLink(address, remark) + '\r\n');
  1447. default: return '';
  1448. }
  1449. }
  1450. static fromJson(json={}) {
  1451. return new Inbound(
  1452. json.port,
  1453. json.listen,
  1454. json.protocol,
  1455. Inbound.Settings.fromJson(json.protocol, json.settings),
  1456. StreamSettings.fromJson(json.streamSettings),
  1457. json.tag,
  1458. Sniffing.fromJson(json.sniffing),
  1459. json.clientStats
  1460. )
  1461. }
  1462. toJson() {
  1463. let streamSettings;
  1464. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1465. streamSettings = this.stream.toJson();
  1466. }
  1467. return {
  1468. port: this.port,
  1469. listen: this.listen,
  1470. protocol: this.protocol,
  1471. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1472. streamSettings: streamSettings,
  1473. tag: this.tag,
  1474. sniffing: this.sniffing.toJson(),
  1475. clientStats: this.clientStats
  1476. };
  1477. }
  1478. }
  1479. Inbound.Settings = class extends XrayCommonClass {
  1480. constructor(protocol) {
  1481. super();
  1482. this.protocol = protocol;
  1483. }
  1484. static getSettings(protocol) {
  1485. switch (protocol) {
  1486. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1487. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1488. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1489. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1490. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1491. case Protocols.MTPROTO: return new Inbound.MtprotoSettings(protocol);
  1492. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1493. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1494. default: return null;
  1495. }
  1496. }
  1497. static fromJson(protocol, json) {
  1498. switch (protocol) {
  1499. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1500. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1501. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1502. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1503. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1504. case Protocols.MTPROTO: return Inbound.MtprotoSettings.fromJson(json);
  1505. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1506. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1507. default: return null;
  1508. }
  1509. }
  1510. toJson() {
  1511. return {};
  1512. }
  1513. };
  1514. Inbound.VmessSettings = class extends Inbound.Settings {
  1515. constructor(protocol,
  1516. vmesses=[new Inbound.VmessSettings.Vmess()],
  1517. disableInsecureEncryption=false) {
  1518. super(protocol);
  1519. this.vmesses = vmesses;
  1520. this.disableInsecure = disableInsecureEncryption;
  1521. }
  1522. indexOfVmessById(id) {
  1523. return this.vmesses.findIndex(vmess => vmess.id === id);
  1524. }
  1525. addVmess(vmess) {
  1526. if (this.indexOfVmessById(vmess.id) >= 0) {
  1527. return false;
  1528. }
  1529. this.vmesses.push(vmess);
  1530. }
  1531. delVmess(vmess) {
  1532. const i = this.indexOfVmessById(vmess.id);
  1533. if (i >= 0) {
  1534. this.vmesses.splice(i, 1);
  1535. }
  1536. }
  1537. static fromJson(json={}) {
  1538. return new Inbound.VmessSettings(
  1539. Protocols.VMESS,
  1540. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1541. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1542. );
  1543. }
  1544. toJson() {
  1545. return {
  1546. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1547. disableInsecureEncryption: this.disableInsecure,
  1548. };
  1549. }
  1550. };
  1551. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1552. constructor(id=RandomUtil.randomUUID(), alterId=0, email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1553. super();
  1554. this.id = id;
  1555. this.alterId = alterId;
  1556. this.email = email;
  1557. this.limitIp = limitIp;
  1558. this.totalGB = totalGB;
  1559. this.expiryTime = expiryTime;
  1560. this.enable = enable;
  1561. this.tgId = tgId;
  1562. this.subId = subId;
  1563. }
  1564. static fromJson(json={}) {
  1565. return new Inbound.VmessSettings.Vmess(
  1566. json.id,
  1567. json.alterId,
  1568. json.email,
  1569. json.limitIp,
  1570. json.totalGB,
  1571. json.expiryTime,
  1572. json.enable,
  1573. json.tgId,
  1574. json.subId,
  1575. );
  1576. }
  1577. get _expiryTime() {
  1578. if (this.expiryTime === 0 || this.expiryTime === "") {
  1579. return null;
  1580. }
  1581. if (this.expiryTime < 0){
  1582. return this.expiryTime / -86400000;
  1583. }
  1584. return moment(this.expiryTime);
  1585. }
  1586. set _expiryTime(t) {
  1587. if (t == null || t === "") {
  1588. this.expiryTime = 0;
  1589. } else {
  1590. this.expiryTime = t.valueOf();
  1591. }
  1592. }
  1593. get _totalGB() {
  1594. return toFixed(this.totalGB / ONE_GB, 2);
  1595. }
  1596. set _totalGB(gb) {
  1597. this.totalGB = toFixed(gb * ONE_GB, 0);
  1598. }
  1599. };
  1600. Inbound.VLESSSettings = class extends Inbound.Settings {
  1601. constructor(protocol,
  1602. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1603. decryption='none',
  1604. fallbacks=[],) {
  1605. super(protocol);
  1606. this.vlesses = vlesses;
  1607. this.decryption = 'none'; // Using decryption is not implemented here
  1608. this.fallbacks = fallbacks;
  1609. }
  1610. addFallback() {
  1611. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1612. }
  1613. delFallback(index) {
  1614. this.fallbacks.splice(index, 1);
  1615. }
  1616. // decryption should be set to static value
  1617. static fromJson(json={}) {
  1618. return new Inbound.VLESSSettings(
  1619. Protocols.VLESS,
  1620. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1621. 'none',
  1622. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1623. );
  1624. }
  1625. toJson() {
  1626. return {
  1627. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1628. decryption: 'none',
  1629. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1630. };
  1631. }
  1632. };
  1633. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1634. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1635. super();
  1636. this.id = id;
  1637. this.flow = flow;
  1638. this.email = email;
  1639. this.limitIp = limitIp;
  1640. this.totalGB = totalGB;
  1641. this.expiryTime = expiryTime;
  1642. this.enable = enable;
  1643. this.tgId = tgId;
  1644. this.subId = subId;
  1645. }
  1646. static fromJson(json={}) {
  1647. return new Inbound.VLESSSettings.VLESS(
  1648. json.id,
  1649. json.flow,
  1650. json.email,
  1651. json.limitIp,
  1652. json.totalGB,
  1653. json.expiryTime,
  1654. json.enable,
  1655. json.tgId,
  1656. json.subId,
  1657. );
  1658. }
  1659. get _expiryTime() {
  1660. if (this.expiryTime === 0 || this.expiryTime === "") {
  1661. return null;
  1662. }
  1663. if (this.expiryTime < 0){
  1664. return this.expiryTime / -86400000;
  1665. }
  1666. return moment(this.expiryTime);
  1667. }
  1668. set _expiryTime(t) {
  1669. if (t == null || t === "") {
  1670. this.expiryTime = 0;
  1671. } else {
  1672. this.expiryTime = t.valueOf();
  1673. }
  1674. }
  1675. get _totalGB() {
  1676. return toFixed(this.totalGB / ONE_GB, 2);
  1677. }
  1678. set _totalGB(gb) {
  1679. this.totalGB = toFixed(gb * ONE_GB, 0);
  1680. }
  1681. };
  1682. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1683. constructor(name="", alpn='', path='', dest='', xver=0) {
  1684. super();
  1685. this.name = name;
  1686. this.alpn = alpn;
  1687. this.path = path;
  1688. this.dest = dest;
  1689. this.xver = xver;
  1690. }
  1691. toJson() {
  1692. let xver = this.xver;
  1693. if (!Number.isInteger(xver)) {
  1694. xver = 0;
  1695. }
  1696. return {
  1697. name: this.name,
  1698. alpn: this.alpn,
  1699. path: this.path,
  1700. dest: this.dest,
  1701. xver: xver,
  1702. }
  1703. }
  1704. static fromJson(json=[]) {
  1705. const fallbacks = [];
  1706. for (let fallback of json) {
  1707. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1708. fallback.name,
  1709. fallback.alpn,
  1710. fallback.path,
  1711. fallback.dest,
  1712. fallback.xver,
  1713. ))
  1714. }
  1715. return fallbacks;
  1716. }
  1717. };
  1718. Inbound.TrojanSettings = class extends Inbound.Settings {
  1719. constructor(protocol,
  1720. trojans=[new Inbound.TrojanSettings.Trojan()],
  1721. fallbacks=[],) {
  1722. super(protocol);
  1723. this.trojans = trojans;
  1724. this.fallbacks = fallbacks;
  1725. }
  1726. addTrojanFallback() {
  1727. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1728. }
  1729. delTrojanFallback(index) {
  1730. this.fallbacks.splice(index, 1);
  1731. }
  1732. static fromJson(json={}) {
  1733. return new Inbound.TrojanSettings(
  1734. Protocols.TROJAN,
  1735. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1736. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1737. }
  1738. toJson() {
  1739. return {
  1740. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1741. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1742. };
  1743. }
  1744. };
  1745. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1746. constructor(password=RandomUtil.randomSeq(10), flow='', email=RandomUtil.randomText(),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId='') {
  1747. super();
  1748. this.password = password;
  1749. this.flow = flow;
  1750. this.email = email;
  1751. this.limitIp = limitIp;
  1752. this.totalGB = totalGB;
  1753. this.expiryTime = expiryTime;
  1754. this.enable = enable;
  1755. this.tgId = tgId;
  1756. this.subId = subId;
  1757. }
  1758. toJson() {
  1759. return {
  1760. password: this.password,
  1761. flow: this.flow,
  1762. email: this.email,
  1763. limitIp: this.limitIp,
  1764. totalGB: this.totalGB,
  1765. expiryTime: this.expiryTime,
  1766. enable: this.enable,
  1767. tgId: this.tgId,
  1768. subId: this.subId,
  1769. };
  1770. }
  1771. static fromJson(json = {}) {
  1772. return new Inbound.TrojanSettings.Trojan(
  1773. json.password,
  1774. json.flow,
  1775. json.email,
  1776. json.limitIp,
  1777. json.totalGB,
  1778. json.expiryTime,
  1779. json.enable,
  1780. json.tgId,
  1781. json.subId,
  1782. );
  1783. }
  1784. get _expiryTime() {
  1785. if (this.expiryTime === 0 || this.expiryTime === "") {
  1786. return null;
  1787. }
  1788. if (this.expiryTime < 0){
  1789. return this.expiryTime / -86400000;
  1790. }
  1791. return moment(this.expiryTime);
  1792. }
  1793. set _expiryTime(t) {
  1794. if (t == null || t === "") {
  1795. this.expiryTime = 0;
  1796. } else {
  1797. this.expiryTime = t.valueOf();
  1798. }
  1799. }
  1800. get _totalGB() {
  1801. return toFixed(this.totalGB / ONE_GB, 2);
  1802. }
  1803. set _totalGB(gb) {
  1804. this.totalGB = toFixed(gb * ONE_GB, 0);
  1805. }
  1806. };
  1807. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1808. constructor(name="", alpn='', path='', dest='', xver=0) {
  1809. super();
  1810. this.name = name;
  1811. this.alpn = alpn;
  1812. this.path = path;
  1813. this.dest = dest;
  1814. this.xver = xver;
  1815. }
  1816. toJson() {
  1817. let xver = this.xver;
  1818. if (!Number.isInteger(xver)) {
  1819. xver = 0;
  1820. }
  1821. return {
  1822. name: this.name,
  1823. alpn: this.alpn,
  1824. path: this.path,
  1825. dest: this.dest,
  1826. xver: xver,
  1827. }
  1828. }
  1829. static fromJson(json=[]) {
  1830. const fallbacks = [];
  1831. for (let fallback of json) {
  1832. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1833. fallback.name,
  1834. fallback.alpn,
  1835. fallback.path,
  1836. fallback.dest,
  1837. fallback.xver,
  1838. ))
  1839. }
  1840. return fallbacks;
  1841. }
  1842. };
  1843. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1844. constructor(protocol,
  1845. method=SSMethods.BLAKE3_AES_256_GCM,
  1846. password=RandomUtil.randomSeq(44),
  1847. network='tcp,udp'
  1848. ) {
  1849. super(protocol);
  1850. this.method = method;
  1851. this.password = password;
  1852. this.network = network;
  1853. }
  1854. static fromJson(json={}) {
  1855. return new Inbound.ShadowsocksSettings(
  1856. Protocols.SHADOWSOCKS,
  1857. json.method,
  1858. json.password,
  1859. json.network,
  1860. );
  1861. }
  1862. toJson() {
  1863. return {
  1864. method: this.method,
  1865. password: this.password,
  1866. network: this.network,
  1867. };
  1868. }
  1869. };
  1870. Inbound.DokodemoSettings = class extends Inbound.Settings {
  1871. constructor(protocol, address, port, network='tcp,udp', followRedirect=false) {
  1872. super(protocol);
  1873. this.address = address;
  1874. this.port = port;
  1875. this.network = network;
  1876. this.followRedirect = followRedirect;
  1877. }
  1878. static fromJson(json={}) {
  1879. return new Inbound.DokodemoSettings(
  1880. Protocols.DOKODEMO,
  1881. json.address,
  1882. json.port,
  1883. json.network,
  1884. json.followRedirect,
  1885. );
  1886. }
  1887. toJson() {
  1888. return {
  1889. address: this.address,
  1890. port: this.port,
  1891. network: this.network,
  1892. followRedirect: this.followRedirect,
  1893. };
  1894. }
  1895. };
  1896. Inbound.MtprotoSettings = class extends Inbound.Settings {
  1897. constructor(protocol, users=[new Inbound.MtprotoSettings.MtUser()]) {
  1898. super(protocol);
  1899. this.users = users;
  1900. }
  1901. static fromJson(json={}) {
  1902. return new Inbound.MtprotoSettings(
  1903. Protocols.MTPROTO,
  1904. json.users.map(user => Inbound.MtprotoSettings.MtUser.fromJson(user)),
  1905. );
  1906. }
  1907. toJson() {
  1908. return {
  1909. users: XrayCommonClass.toJsonArray(this.users),
  1910. };
  1911. }
  1912. };
  1913. Inbound.MtprotoSettings.MtUser = class extends XrayCommonClass {
  1914. constructor(secret=RandomUtil.randomMTSecret()) {
  1915. super();
  1916. this.secret = secret;
  1917. }
  1918. static fromJson(json={}) {
  1919. return new Inbound.MtprotoSettings.MtUser(json.secret);
  1920. }
  1921. };
  1922. Inbound.SocksSettings = class extends Inbound.Settings {
  1923. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  1924. super(protocol);
  1925. this.auth = auth;
  1926. this.accounts = accounts;
  1927. this.udp = udp;
  1928. this.ip = ip;
  1929. }
  1930. addAccount(account) {
  1931. this.accounts.push(account);
  1932. }
  1933. delAccount(index) {
  1934. this.accounts.splice(index, 1);
  1935. }
  1936. static fromJson(json={}) {
  1937. let accounts;
  1938. if (json.auth === 'password') {
  1939. accounts = json.accounts.map(
  1940. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  1941. )
  1942. }
  1943. return new Inbound.SocksSettings(
  1944. Protocols.SOCKS,
  1945. json.auth,
  1946. accounts,
  1947. json.udp,
  1948. json.ip,
  1949. );
  1950. }
  1951. toJson() {
  1952. return {
  1953. auth: this.auth,
  1954. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  1955. udp: this.udp,
  1956. ip: this.ip,
  1957. };
  1958. }
  1959. };
  1960. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  1961. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1962. super();
  1963. this.user = user;
  1964. this.pass = pass;
  1965. }
  1966. static fromJson(json={}) {
  1967. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  1968. }
  1969. };
  1970. Inbound.HttpSettings = class extends Inbound.Settings {
  1971. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  1972. super(protocol);
  1973. this.accounts = accounts;
  1974. }
  1975. addAccount(account) {
  1976. this.accounts.push(account);
  1977. }
  1978. delAccount(index) {
  1979. this.accounts.splice(index, 1);
  1980. }
  1981. static fromJson(json={}) {
  1982. return new Inbound.HttpSettings(
  1983. Protocols.HTTP,
  1984. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  1985. );
  1986. }
  1987. toJson() {
  1988. return {
  1989. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  1990. };
  1991. }
  1992. };
  1993. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  1994. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  1995. super();
  1996. this.user = user;
  1997. this.pass = pass;
  1998. }
  1999. static fromJson(json={}) {
  2000. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2001. }
  2002. };