xray.js 67 KB

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