xray.js 67 KB

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