xray.js 68 KB

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