xray.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  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. H3: "h3",
  74. H2: "h2",
  75. HTTP1: "http/1.1",
  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(acceptProxyProtocol = false, tcpFastOpen = false, mark = 0, tproxy="off") {
  727. super();
  728. this.acceptProxyProtocol = acceptProxyProtocol;
  729. this.tcpFastOpen = tcpFastOpen;
  730. this.mark = mark;
  731. this.tproxy = tproxy;
  732. }
  733. static fromJson(json = {}) {
  734. return new SockoptStreamSettings(
  735. json.acceptProxyProtocol,
  736. json.tcpFastOpen,
  737. json.mark,
  738. json.tproxy,
  739. );
  740. }
  741. toJson() {
  742. return {
  743. acceptProxyProtocol: this.acceptProxyProtocol,
  744. tcpFastOpen: this.tcpFastOpen,
  745. mark: this.mark,
  746. tproxy: this.tproxy,
  747. };
  748. }
  749. }
  750. class StreamSettings extends XrayCommonClass {
  751. constructor(network='tcp',
  752. security='none',
  753. tlsSettings=new TlsStreamSettings(),
  754. xtlsSettings=new XtlsStreamSettings(),
  755. realitySettings = new RealityStreamSettings(),
  756. tcpSettings=new TcpStreamSettings(),
  757. kcpSettings=new KcpStreamSettings(),
  758. wsSettings=new WsStreamSettings(),
  759. httpSettings=new HttpStreamSettings(),
  760. quicSettings=new QuicStreamSettings(),
  761. grpcSettings=new GrpcStreamSettings(),
  762. sockopt = undefined,
  763. ) {
  764. super();
  765. this.network = network;
  766. this.security = security;
  767. this.tls = tlsSettings;
  768. this.xtls = xtlsSettings;
  769. this.reality = realitySettings;
  770. this.tcp = tcpSettings;
  771. this.kcp = kcpSettings;
  772. this.ws = wsSettings;
  773. this.http = httpSettings;
  774. this.quic = quicSettings;
  775. this.grpc = grpcSettings;
  776. this.sockopt = sockopt;
  777. }
  778. get isTls() {
  779. return this.security === "tls";
  780. }
  781. set isTls(isTls) {
  782. if (isTls) {
  783. this.security = 'tls';
  784. } else {
  785. this.security = 'none';
  786. }
  787. }
  788. get isXtls() {
  789. return this.security === "xtls";
  790. }
  791. set isXtls(isXtls) {
  792. if (isXtls) {
  793. this.security = 'xtls';
  794. } else {
  795. this.security = 'none';
  796. }
  797. }
  798. //for Reality
  799. get isReality() {
  800. return this.security === "reality";
  801. }
  802. set isReality(isReality) {
  803. if (isReality) {
  804. this.security = 'reality';
  805. } else {
  806. this.security = 'none';
  807. }
  808. }
  809. get sockoptSwitch() {
  810. return this.sockopt != undefined;
  811. }
  812. set sockoptSwitch(value) {
  813. this.sockopt = value ? new SockoptStreamSettings() : undefined;
  814. }
  815. static fromJson(json={}) {
  816. return new StreamSettings(
  817. json.network,
  818. json.security,
  819. TlsStreamSettings.fromJson(json.tlsSettings),
  820. XtlsStreamSettings.fromJson(json.xtlsSettings),
  821. RealityStreamSettings.fromJson(json.realitySettings),
  822. TcpStreamSettings.fromJson(json.tcpSettings),
  823. KcpStreamSettings.fromJson(json.kcpSettings),
  824. WsStreamSettings.fromJson(json.wsSettings),
  825. HttpStreamSettings.fromJson(json.httpSettings),
  826. QuicStreamSettings.fromJson(json.quicSettings),
  827. GrpcStreamSettings.fromJson(json.grpcSettings),
  828. SockoptStreamSettings.fromJson(json.sockopt),
  829. );
  830. }
  831. toJson() {
  832. const network = this.network;
  833. return {
  834. network: network,
  835. security: this.security,
  836. tlsSettings: this.isTls ? this.tls.toJson() : undefined,
  837. xtlsSettings: this.isXtls ? this.xtls.toJson() : undefined,
  838. realitySettings: this.isReality ? this.reality.toJson() : undefined,
  839. tcpSettings: network === 'tcp' ? this.tcp.toJson() : undefined,
  840. kcpSettings: network === 'kcp' ? this.kcp.toJson() : undefined,
  841. wsSettings: network === 'ws' ? this.ws.toJson() : undefined,
  842. httpSettings: network === 'http' ? this.http.toJson() : undefined,
  843. quicSettings: network === 'quic' ? this.quic.toJson() : undefined,
  844. grpcSettings: network === 'grpc' ? this.grpc.toJson() : undefined,
  845. sockopt: this.sockopt != undefined ? this.sockopt.toJson() : undefined,
  846. };
  847. }
  848. }
  849. class Sniffing extends XrayCommonClass {
  850. constructor(enabled=true, destOverride=['http', 'tls', 'quic', 'fakedns']) {
  851. super();
  852. this.enabled = enabled;
  853. this.destOverride = destOverride;
  854. }
  855. static fromJson(json={}) {
  856. let destOverride = ObjectUtil.clone(json.destOverride);
  857. if (!ObjectUtil.isEmpty(destOverride) && !ObjectUtil.isArrEmpty(destOverride)) {
  858. if (ObjectUtil.isEmpty(destOverride[0])) {
  859. destOverride = ['http', 'tls', 'quic', 'fakedns'];
  860. }
  861. }
  862. return new Sniffing(
  863. !!json.enabled,
  864. destOverride,
  865. );
  866. }
  867. }
  868. class Inbound extends XrayCommonClass {
  869. constructor(port=RandomUtil.randomIntRange(10000, 60000),
  870. listen='',
  871. protocol=Protocols.VLESS,
  872. settings=null,
  873. streamSettings=new StreamSettings(),
  874. tag='',
  875. sniffing=new Sniffing(),
  876. clientStats='',
  877. ) {
  878. super();
  879. this.port = port;
  880. this.listen = listen;
  881. this._protocol = protocol;
  882. this.settings = ObjectUtil.isEmpty(settings) ? Inbound.Settings.getSettings(protocol) : settings;
  883. this.stream = streamSettings;
  884. this.tag = tag;
  885. this.sniffing = sniffing;
  886. this.clientStats = clientStats;
  887. }
  888. getClientStats() {
  889. return this.clientStats;
  890. }
  891. get protocol() {
  892. return this._protocol;
  893. }
  894. set protocol(protocol) {
  895. this._protocol = protocol;
  896. this.settings = Inbound.Settings.getSettings(protocol);
  897. if (protocol === Protocols.TROJAN) {
  898. this.tls = false;
  899. }
  900. }
  901. get tls() {
  902. return this.stream.security === 'tls';
  903. }
  904. set tls(isTls) {
  905. if (isTls) {
  906. this.stream.security = 'tls';
  907. } else {
  908. this.stream.security = 'none';
  909. }
  910. }
  911. get xtls() {
  912. return this.stream.security === 'xtls';
  913. }
  914. set xtls(isXtls) {
  915. if (isXtls) {
  916. this.stream.security = 'xtls';
  917. } else {
  918. this.stream.security = 'none';
  919. }
  920. }
  921. get reality() {
  922. return this.stream.security === 'reality';
  923. }
  924. set reality(isReality) {
  925. if (isReality) {
  926. this.stream.security = 'reality';
  927. } else {
  928. this.stream.security = 'none';
  929. }
  930. }
  931. get network() {
  932. return this.stream.network;
  933. }
  934. set network(network) {
  935. this.stream.network = network;
  936. }
  937. get isTcp() {
  938. return this.network === "tcp";
  939. }
  940. get isWs() {
  941. return this.network === "ws";
  942. }
  943. get isKcp() {
  944. return this.network === "kcp";
  945. }
  946. get isQuic() {
  947. return this.network === "quic"
  948. }
  949. get isGrpc() {
  950. return this.network === "grpc";
  951. }
  952. get isH2() {
  953. return this.network === "http";
  954. }
  955. // Shadowsocks
  956. get method() {
  957. switch (this.protocol) {
  958. case Protocols.SHADOWSOCKS:
  959. return this.settings.method;
  960. default:
  961. return "";
  962. }
  963. }
  964. get isSSMultiUser() {
  965. return this.method != SSMethods.BLAKE3_CHACHA20_POLY1305;
  966. }
  967. get isSS2022(){
  968. return this.method.substring(0,4) === "2022";
  969. }
  970. get serverName() {
  971. if (this.stream.isTls || this.stream.isXtls || this.stream.isReality) {
  972. return this.stream.tls.server;
  973. }
  974. return "";
  975. }
  976. get host() {
  977. if (this.isTcp) {
  978. return this.stream.tcp.request.getHeader("Host");
  979. } else if (this.isWs) {
  980. return this.stream.ws.getHeader("Host");
  981. } else if (this.isH2) {
  982. return this.stream.http.host[0];
  983. }
  984. return null;
  985. }
  986. get path() {
  987. if (this.isTcp) {
  988. return this.stream.tcp.request.path[0];
  989. } else if (this.isWs) {
  990. return this.stream.ws.path;
  991. } else if (this.isH2) {
  992. return this.stream.http.path[0];
  993. }
  994. return null;
  995. }
  996. get quicSecurity() {
  997. return this.stream.quic.security;
  998. }
  999. get quicKey() {
  1000. return this.stream.quic.key;
  1001. }
  1002. get quicType() {
  1003. return this.stream.quic.type;
  1004. }
  1005. get kcpType() {
  1006. return this.stream.kcp.type;
  1007. }
  1008. get kcpSeed() {
  1009. return this.stream.kcp.seed;
  1010. }
  1011. get serviceName() {
  1012. return this.stream.grpc.serviceName;
  1013. }
  1014. isExpiry(index) {
  1015. switch (this.protocol) {
  1016. case Protocols.VMESS:
  1017. if(this.settings.vmesses[index].expiryTime > 0)
  1018. return this.settings.vmesses[index].expiryTime < new Date().getTime();
  1019. return false
  1020. case Protocols.VLESS:
  1021. if(this.settings.vlesses[index].expiryTime > 0)
  1022. return this.settings.vlesses[index].expiryTime < new Date().getTime();
  1023. return false
  1024. case Protocols.TROJAN:
  1025. if(this.settings.trojans[index].expiryTime > 0)
  1026. return this.settings.trojans[index].expiryTime < new Date().getTime();
  1027. return false
  1028. case Protocols.SHADOWSOCKS:
  1029. if(this.settings.shadowsockses.length > 0 && this.settings.shadowsockses[index].expiryTime > 0)
  1030. return this.settings.shadowsockses[index].expiryTime < new Date().getTime();
  1031. return false
  1032. default:
  1033. return false;
  1034. }
  1035. }
  1036. canEnableTls() {
  1037. switch (this.protocol) {
  1038. case Protocols.VMESS:
  1039. case Protocols.VLESS:
  1040. case Protocols.TROJAN:
  1041. break;
  1042. default:
  1043. return false;
  1044. }
  1045. switch (this.network) {
  1046. case "tcp":
  1047. case "ws":
  1048. case "http":
  1049. case "quic":
  1050. case "grpc":
  1051. return true;
  1052. default:
  1053. return false;
  1054. }
  1055. }
  1056. canEnableReality() {
  1057. switch (this.protocol) {
  1058. case Protocols.VLESS:
  1059. case Protocols.TROJAN:
  1060. break;
  1061. default:
  1062. return false;
  1063. }
  1064. switch (this.network) {
  1065. case "tcp":
  1066. case "http":
  1067. case "grpc":
  1068. return true;
  1069. default:
  1070. return false;
  1071. }
  1072. }
  1073. //this is used for xtls-rprx-vision
  1074. canEnableTlsFlow() {
  1075. if (((this.stream.security === 'tls') || (this.stream.security === 'reality')) && (this.network === "tcp")) {
  1076. switch (this.protocol) {
  1077. case Protocols.VLESS:
  1078. return true;
  1079. default:
  1080. return false;
  1081. }
  1082. }
  1083. return false;
  1084. }
  1085. canSetTls() {
  1086. return this.canEnableTls();
  1087. }
  1088. canEnableXtls() {
  1089. switch (this.protocol) {
  1090. case Protocols.VLESS:
  1091. case Protocols.TROJAN:
  1092. break;
  1093. default:
  1094. return false;
  1095. }
  1096. return this.network === "tcp";
  1097. }
  1098. canEnableStream() {
  1099. switch (this.protocol) {
  1100. case Protocols.VMESS:
  1101. case Protocols.VLESS:
  1102. case Protocols.TROJAN:
  1103. case Protocols.SHADOWSOCKS:
  1104. return true;
  1105. default:
  1106. return false;
  1107. }
  1108. }
  1109. canSniffing() {
  1110. switch (this.protocol) {
  1111. case Protocols.VMESS:
  1112. case Protocols.VLESS:
  1113. case Protocols.TROJAN:
  1114. case Protocols.SHADOWSOCKS:
  1115. return true;
  1116. default:
  1117. return false;
  1118. }
  1119. }
  1120. reset() {
  1121. this.port = RandomUtil.randomIntRange(10000, 60000);
  1122. this.listen = '';
  1123. this.protocol = Protocols.VMESS;
  1124. this.settings = Inbound.Settings.getSettings(Protocols.VMESS);
  1125. this.stream = new StreamSettings();
  1126. this.tag = '';
  1127. this.sniffing = new Sniffing();
  1128. }
  1129. genVmessLink(address='', remark='', clientIndex=0) {
  1130. if (this.protocol !== Protocols.VMESS) {
  1131. return '';
  1132. }
  1133. let obj = {
  1134. v: '2',
  1135. ps: remark,
  1136. add: address,
  1137. port: this.port,
  1138. id: this.settings.vmesses[clientIndex].id,
  1139. net: this.stream.network,
  1140. type: 'none',
  1141. tls: this.stream.security,
  1142. };
  1143. let network = this.stream.network;
  1144. if (network === 'tcp') {
  1145. let tcp = this.stream.tcp;
  1146. obj.type = tcp.type;
  1147. if (tcp.type === 'http') {
  1148. let request = tcp.request;
  1149. obj.path = request.path.join(',');
  1150. let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1151. if (index >= 0) {
  1152. obj.host = request.headers[index].value;
  1153. }
  1154. }
  1155. } else if (network === 'kcp') {
  1156. let kcp = this.stream.kcp;
  1157. obj.type = kcp.type;
  1158. obj.path = kcp.seed;
  1159. } else if (network === 'ws') {
  1160. let ws = this.stream.ws;
  1161. obj.path = ws.path;
  1162. let index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1163. if (index >= 0) {
  1164. obj.host = ws.headers[index].value;
  1165. }
  1166. } else if (network === 'http') {
  1167. obj.net = 'h2';
  1168. obj.path = this.stream.http.path;
  1169. obj.host = this.stream.http.host.join(',');
  1170. } else if (network === 'quic') {
  1171. obj.type = this.stream.quic.type;
  1172. obj.host = this.stream.quic.security;
  1173. obj.path = this.stream.quic.key;
  1174. } else if (network === 'grpc') {
  1175. obj.path = this.stream.grpc.serviceName;
  1176. if (this.stream.grpc.multiMode){
  1177. obj.type = 'multi'
  1178. }
  1179. }
  1180. if (this.stream.security === 'tls') {
  1181. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1182. obj.add = this.stream.tls.server;
  1183. }
  1184. if (!ObjectUtil.isEmpty(this.stream.tls.settings.serverName)){
  1185. obj.sni = this.stream.tls.settings.serverName;
  1186. }
  1187. if (!ObjectUtil.isEmpty(this.stream.tls.settings.fingerprint)){
  1188. obj.fp = this.stream.tls.settings.fingerprint;
  1189. }
  1190. if (this.stream.tls.alpn.length>0){
  1191. obj.alpn = this.stream.tls.alpn.join(',');
  1192. }
  1193. if (this.stream.tls.settings.allowInsecure){
  1194. obj.allowInsecure = this.stream.tls.settings.allowInsecure;
  1195. }
  1196. }
  1197. return 'vmess://' + base64(JSON.stringify(obj, null, 2));
  1198. }
  1199. genVLESSLink(address = '', remark='', clientIndex=0) {
  1200. const settings = this.settings;
  1201. const uuid = settings.vlesses[clientIndex].id;
  1202. const port = this.port;
  1203. const type = this.stream.network;
  1204. const params = new Map();
  1205. params.set("type", this.stream.network);
  1206. switch (type) {
  1207. case "tcp":
  1208. const tcp = this.stream.tcp;
  1209. if (tcp.type === 'http') {
  1210. const request = tcp.request;
  1211. params.set("path", request.path.join(','));
  1212. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1213. if (index >= 0) {
  1214. const host = request.headers[index].value;
  1215. params.set("host", host);
  1216. }
  1217. params.set("headerType", 'http');
  1218. }
  1219. break;
  1220. case "kcp":
  1221. const kcp = this.stream.kcp;
  1222. params.set("headerType", kcp.type);
  1223. params.set("seed", kcp.seed);
  1224. break;
  1225. case "ws":
  1226. const ws = this.stream.ws;
  1227. params.set("path", ws.path);
  1228. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1229. if (index >= 0) {
  1230. const host = ws.headers[index].value;
  1231. params.set("host", host);
  1232. }
  1233. break;
  1234. case "http":
  1235. const http = this.stream.http;
  1236. params.set("path", http.path);
  1237. params.set("host", http.host);
  1238. break;
  1239. case "quic":
  1240. const quic = this.stream.quic;
  1241. params.set("quicSecurity", quic.security);
  1242. params.set("key", quic.key);
  1243. params.set("headerType", quic.type);
  1244. break;
  1245. case "grpc":
  1246. const grpc = this.stream.grpc;
  1247. params.set("serviceName", grpc.serviceName);
  1248. if(grpc.multiMode){
  1249. params.set("mode", "multi");
  1250. }
  1251. break;
  1252. }
  1253. if (this.tls) {
  1254. params.set("security", "tls");
  1255. params.set("fp" , this.stream.tls.settings.fingerprint);
  1256. params.set("alpn", this.stream.tls.alpn);
  1257. if(this.stream.tls.settings.allowInsecure){
  1258. params.set("allowInsecure", "1");
  1259. }
  1260. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1261. address = this.stream.tls.server;
  1262. }
  1263. if (this.stream.tls.settings.serverName !== ''){
  1264. params.set("sni", this.stream.tls.settings.serverName);
  1265. }
  1266. if (type === "tcp" && this.settings.vlesses[clientIndex].flow.length > 0) {
  1267. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1268. }
  1269. }
  1270. else if (this.xtls) {
  1271. params.set("security", "xtls");
  1272. params.set("alpn", this.stream.xtls.alpn);
  1273. if(this.stream.xtls.settings.allowInsecure){
  1274. params.set("allowInsecure", "1");
  1275. }
  1276. if (!ObjectUtil.isEmpty(this.stream.xtls.server)) {
  1277. address = this.stream.xtls.server;
  1278. }
  1279. if (this.stream.xtls.settings.serverName !== ''){
  1280. params.set("sni", this.stream.xtls.settings.serverName);
  1281. }
  1282. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1283. }
  1284. else if (this.reality) {
  1285. params.set("security", "reality");
  1286. params.set("fp", this.stream.reality.settings.fingerprint);
  1287. params.set("pbk", this.stream.reality.settings.publicKey);
  1288. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1289. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1290. }
  1291. if (this.stream.network === 'tcp' && !ObjectUtil.isEmpty(this.settings.vlesses[clientIndex].flow)) {
  1292. params.set("flow", this.settings.vlesses[clientIndex].flow);
  1293. }
  1294. if (this.stream.reality.shortIds.length > 0) {
  1295. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1296. }
  1297. if (!ObjectUtil.isEmpty(this.stream.reality.settings.serverName)) {
  1298. address = this.stream.reality.settings.serverName;
  1299. }
  1300. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1301. params.set("spx", this.stream.reality.settings.spiderX);
  1302. }
  1303. }
  1304. else {
  1305. params.set("security", "none");
  1306. }
  1307. const link = `vless://${uuid}@${address}:${port}`;
  1308. const url = new URL(link);
  1309. for (const [key, value] of params) {
  1310. url.searchParams.set(key, value)
  1311. }
  1312. url.hash = encodeURIComponent(remark);
  1313. return url.toString();
  1314. }
  1315. genSSLink(address='', remark='', clientIndex = 0) {
  1316. let settings = this.settings;
  1317. const port = this.port;
  1318. const type = this.stream.network;
  1319. const params = new Map();
  1320. params.set("type", this.stream.network);
  1321. switch (type) {
  1322. case "tcp":
  1323. const tcp = this.stream.tcp;
  1324. if (tcp.type === 'http') {
  1325. const request = tcp.request;
  1326. params.set("path", request.path.join(','));
  1327. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1328. if (index >= 0) {
  1329. const host = request.headers[index].value;
  1330. params.set("host", host);
  1331. }
  1332. params.set("headerType", 'http');
  1333. }
  1334. break;
  1335. case "kcp":
  1336. const kcp = this.stream.kcp;
  1337. params.set("headerType", kcp.type);
  1338. params.set("seed", kcp.seed);
  1339. break;
  1340. case "ws":
  1341. const ws = this.stream.ws;
  1342. params.set("path", ws.path);
  1343. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1344. if (index >= 0) {
  1345. const host = ws.headers[index].value;
  1346. params.set("host", host);
  1347. }
  1348. break;
  1349. case "http":
  1350. const http = this.stream.http;
  1351. params.set("path", http.path);
  1352. params.set("host", http.host);
  1353. break;
  1354. case "quic":
  1355. const quic = this.stream.quic;
  1356. params.set("quicSecurity", quic.security);
  1357. params.set("key", quic.key);
  1358. params.set("headerType", quic.type);
  1359. break;
  1360. case "grpc":
  1361. const grpc = this.stream.grpc;
  1362. params.set("serviceName", grpc.serviceName);
  1363. if(grpc.multiMode){
  1364. params.set("mode", "multi");
  1365. }
  1366. break;
  1367. }
  1368. let password = new Array();
  1369. if (this.isSS2022) password.push(settings.password);
  1370. if (this.isSSMultiUser) password.push(settings.shadowsockses[clientIndex].password);
  1371. let link = `ss://${safeBase64(settings.method + ':' + password.join(':'))}@${address}:${this.port}`;
  1372. const url = new URL(link);
  1373. for (const [key, value] of params) {
  1374. url.searchParams.set(key, value)
  1375. }
  1376. url.hash = encodeURIComponent(remark);
  1377. return url.toString();
  1378. }
  1379. genTrojanLink(address = '', remark = '', clientIndex = 0) {
  1380. let settings = this.settings;
  1381. const port = this.port;
  1382. const type = this.stream.network;
  1383. const params = new Map();
  1384. params.set("type", this.stream.network);
  1385. switch (type) {
  1386. case "tcp":
  1387. const tcp = this.stream.tcp;
  1388. if (tcp.type === 'http') {
  1389. const request = tcp.request;
  1390. params.set("path", request.path.join(','));
  1391. const index = request.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1392. if (index >= 0) {
  1393. const host = request.headers[index].value;
  1394. params.set("host", host);
  1395. }
  1396. params.set("headerType", 'http');
  1397. }
  1398. break;
  1399. case "kcp":
  1400. const kcp = this.stream.kcp;
  1401. params.set("headerType", kcp.type);
  1402. params.set("seed", kcp.seed);
  1403. break;
  1404. case "ws":
  1405. const ws = this.stream.ws;
  1406. params.set("path", ws.path);
  1407. const index = ws.headers.findIndex(header => header.name.toLowerCase() === 'host');
  1408. if (index >= 0) {
  1409. const host = ws.headers[index].value;
  1410. params.set("host", host);
  1411. }
  1412. break;
  1413. case "http":
  1414. const http = this.stream.http;
  1415. params.set("path", http.path);
  1416. params.set("host", http.host);
  1417. break;
  1418. case "quic":
  1419. const quic = this.stream.quic;
  1420. params.set("quicSecurity", quic.security);
  1421. params.set("key", quic.key);
  1422. params.set("headerType", quic.type);
  1423. break;
  1424. case "grpc":
  1425. const grpc = this.stream.grpc;
  1426. params.set("serviceName", grpc.serviceName);
  1427. if(grpc.multiMode){
  1428. params.set("mode", "multi");
  1429. }
  1430. break;
  1431. }
  1432. if (this.tls) {
  1433. params.set("security", "tls");
  1434. params.set("fp" , this.stream.tls.settings.fingerprint);
  1435. params.set("alpn", this.stream.tls.alpn);
  1436. if(this.stream.tls.settings.allowInsecure){
  1437. params.set("allowInsecure", "1");
  1438. }
  1439. if (!ObjectUtil.isEmpty(this.stream.tls.server)) {
  1440. address = this.stream.tls.server;
  1441. }
  1442. if (this.stream.tls.settings.serverName !== ''){
  1443. params.set("sni", this.stream.tls.settings.serverName);
  1444. }
  1445. }
  1446. else if (this.reality) {
  1447. params.set("security", "reality");
  1448. params.set("fp", this.stream.reality.settings.fingerprint);
  1449. params.set("pbk", this.stream.reality.settings.publicKey);
  1450. if (!ObjectUtil.isArrEmpty(this.stream.reality.serverNames)) {
  1451. params.set("sni", this.stream.reality.serverNames.split(",")[0]);
  1452. }
  1453. if (this.stream.reality.shortIds.length > 0) {
  1454. params.set("sid", this.stream.reality.shortIds.split(",")[0]);
  1455. }
  1456. if (!ObjectUtil.isEmpty(this.stream.reality.settings.serverName)) {
  1457. address = this.stream.reality.settings.serverName;
  1458. }
  1459. if (!ObjectUtil.isEmpty(this.stream.reality.settings.spiderX)) {
  1460. params.set("spx", this.stream.reality.settings.spiderX);
  1461. }
  1462. }
  1463. else if (this.xtls) {
  1464. params.set("security", "xtls");
  1465. params.set("alpn", this.stream.xtls.alpn);
  1466. if(this.stream.xtls.settings.allowInsecure){
  1467. params.set("allowInsecure", "1");
  1468. }
  1469. if (!ObjectUtil.isEmpty(this.stream.xtls.server)) {
  1470. address = this.stream.xtls.server;
  1471. }
  1472. if (this.stream.xtls.settings.serverName !== ''){
  1473. params.set("sni", this.stream.xtls.settings.serverName);
  1474. }
  1475. params.set("flow", this.settings.trojans[clientIndex].flow);
  1476. }
  1477. else {
  1478. params.set("security", "none");
  1479. }
  1480. const link = `trojan://${settings.trojans[clientIndex].password}@${address}:${this.port}`;
  1481. const url = new URL(link);
  1482. for (const [key, value] of params) {
  1483. url.searchParams.set(key, value)
  1484. }
  1485. url.hash = encodeURIComponent(remark);
  1486. return url.toString();
  1487. }
  1488. genLink(address='', remark='', clientIndex=0) {
  1489. switch (this.protocol) {
  1490. case Protocols.VMESS:
  1491. return this.genVmessLink(address, remark, clientIndex);
  1492. case Protocols.VLESS:
  1493. return this.genVLESSLink(address, remark, clientIndex);
  1494. case Protocols.SHADOWSOCKS:
  1495. return this.genSSLink(address, remark, clientIndex);
  1496. case Protocols.TROJAN:
  1497. return this.genTrojanLink(address, remark, clientIndex);
  1498. default: return '';
  1499. }
  1500. }
  1501. genInboundLinks(address = '', remark = '') {
  1502. let link = '';
  1503. switch (this.protocol) {
  1504. case Protocols.VMESS:
  1505. case Protocols.VLESS:
  1506. case Protocols.TROJAN:
  1507. case Protocols.SHADOWSOCKS:
  1508. JSON.parse(this.settings).clients.forEach((client,index) => {
  1509. if(this.tls && !ObjectUtil.isArrEmpty(this.stream.tls.settings.domains)){
  1510. this.stream.tls.settings.domains.forEach((domain) => {
  1511. link += this.genLink(domain.domain, [remark, client.email, domain.remark].filter(x => x.length > 0).join('-'), index) + '\r\n';
  1512. });
  1513. } else {
  1514. link += this.genLink(address, [remark, client.email].filter(x => x.length > 0).join('-'), index) + '\r\n';
  1515. }
  1516. });
  1517. return link;
  1518. default: return '';
  1519. }
  1520. }
  1521. static fromJson(json={}) {
  1522. return new Inbound(
  1523. json.port,
  1524. json.listen,
  1525. json.protocol,
  1526. Inbound.Settings.fromJson(json.protocol, json.settings),
  1527. StreamSettings.fromJson(json.streamSettings),
  1528. json.tag,
  1529. Sniffing.fromJson(json.sniffing),
  1530. json.clientStats
  1531. )
  1532. }
  1533. toJson() {
  1534. let streamSettings;
  1535. if (this.canEnableStream() || this.protocol === Protocols.TROJAN) {
  1536. streamSettings = this.stream.toJson();
  1537. }
  1538. return {
  1539. port: this.port,
  1540. listen: this.listen,
  1541. protocol: this.protocol,
  1542. settings: this.settings instanceof XrayCommonClass ? this.settings.toJson() : this.settings,
  1543. streamSettings: streamSettings,
  1544. tag: this.tag,
  1545. sniffing: this.sniffing.toJson(),
  1546. clientStats: this.clientStats
  1547. };
  1548. }
  1549. }
  1550. Inbound.Settings = class extends XrayCommonClass {
  1551. constructor(protocol) {
  1552. super();
  1553. this.protocol = protocol;
  1554. }
  1555. static getSettings(protocol) {
  1556. switch (protocol) {
  1557. case Protocols.VMESS: return new Inbound.VmessSettings(protocol);
  1558. case Protocols.VLESS: return new Inbound.VLESSSettings(protocol);
  1559. case Protocols.TROJAN: return new Inbound.TrojanSettings(protocol);
  1560. case Protocols.SHADOWSOCKS: return new Inbound.ShadowsocksSettings(protocol);
  1561. case Protocols.DOKODEMO: return new Inbound.DokodemoSettings(protocol);
  1562. case Protocols.SOCKS: return new Inbound.SocksSettings(protocol);
  1563. case Protocols.HTTP: return new Inbound.HttpSettings(protocol);
  1564. default: return null;
  1565. }
  1566. }
  1567. static fromJson(protocol, json) {
  1568. switch (protocol) {
  1569. case Protocols.VMESS: return Inbound.VmessSettings.fromJson(json);
  1570. case Protocols.VLESS: return Inbound.VLESSSettings.fromJson(json);
  1571. case Protocols.TROJAN: return Inbound.TrojanSettings.fromJson(json);
  1572. case Protocols.SHADOWSOCKS: return Inbound.ShadowsocksSettings.fromJson(json);
  1573. case Protocols.DOKODEMO: return Inbound.DokodemoSettings.fromJson(json);
  1574. case Protocols.SOCKS: return Inbound.SocksSettings.fromJson(json);
  1575. case Protocols.HTTP: return Inbound.HttpSettings.fromJson(json);
  1576. default: return null;
  1577. }
  1578. }
  1579. toJson() {
  1580. return {};
  1581. }
  1582. };
  1583. Inbound.VmessSettings = class extends Inbound.Settings {
  1584. constructor(protocol,
  1585. vmesses=[new Inbound.VmessSettings.Vmess()],
  1586. disableInsecureEncryption=false) {
  1587. super(protocol);
  1588. this.vmesses = vmesses;
  1589. this.disableInsecure = disableInsecureEncryption;
  1590. }
  1591. indexOfVmessById(id) {
  1592. return this.vmesses.findIndex(vmess => vmess.id === id);
  1593. }
  1594. addVmess(vmess) {
  1595. if (this.indexOfVmessById(vmess.id) >= 0) {
  1596. return false;
  1597. }
  1598. this.vmesses.push(vmess);
  1599. }
  1600. delVmess(vmess) {
  1601. const i = this.indexOfVmessById(vmess.id);
  1602. if (i >= 0) {
  1603. this.vmesses.splice(i, 1);
  1604. }
  1605. }
  1606. static fromJson(json={}) {
  1607. return new Inbound.VmessSettings(
  1608. Protocols.VMESS,
  1609. json.clients.map(client => Inbound.VmessSettings.Vmess.fromJson(client)),
  1610. ObjectUtil.isEmpty(json.disableInsecureEncryption) ? false : json.disableInsecureEncryption,
  1611. );
  1612. }
  1613. toJson() {
  1614. return {
  1615. clients: Inbound.VmessSettings.toJsonArray(this.vmesses),
  1616. disableInsecureEncryption: this.disableInsecure,
  1617. };
  1618. }
  1619. };
  1620. Inbound.VmessSettings.Vmess = class extends XrayCommonClass {
  1621. constructor(id=RandomUtil.randomUUID(), email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1622. super();
  1623. this.id = id;
  1624. this.email = email;
  1625. this.limitIp = limitIp;
  1626. this.totalGB = totalGB;
  1627. this.expiryTime = expiryTime;
  1628. this.enable = enable;
  1629. this.tgId = tgId;
  1630. this.subId = subId;
  1631. }
  1632. static fromJson(json={}) {
  1633. return new Inbound.VmessSettings.Vmess(
  1634. json.id,
  1635. json.email,
  1636. json.limitIp,
  1637. json.totalGB,
  1638. json.expiryTime,
  1639. json.enable,
  1640. json.tgId,
  1641. json.subId,
  1642. );
  1643. }
  1644. get _expiryTime() {
  1645. if (this.expiryTime === 0 || this.expiryTime === "") {
  1646. return null;
  1647. }
  1648. if (this.expiryTime < 0){
  1649. return this.expiryTime / -86400000;
  1650. }
  1651. return moment(this.expiryTime);
  1652. }
  1653. set _expiryTime(t) {
  1654. if (t == null || t === "") {
  1655. this.expiryTime = 0;
  1656. } else {
  1657. this.expiryTime = t.valueOf();
  1658. }
  1659. }
  1660. get _totalGB() {
  1661. return toFixed(this.totalGB / ONE_GB, 2);
  1662. }
  1663. set _totalGB(gb) {
  1664. this.totalGB = toFixed(gb * ONE_GB, 0);
  1665. }
  1666. };
  1667. Inbound.VLESSSettings = class extends Inbound.Settings {
  1668. constructor(protocol,
  1669. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1670. decryption='none',
  1671. fallbacks=[],) {
  1672. super(protocol);
  1673. this.vlesses = vlesses;
  1674. this.decryption = 'none'; // Using decryption is not implemented here
  1675. this.fallbacks = fallbacks;
  1676. }
  1677. addFallback() {
  1678. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1679. }
  1680. delFallback(index) {
  1681. this.fallbacks.splice(index, 1);
  1682. }
  1683. // decryption should be set to static value
  1684. static fromJson(json={}) {
  1685. return new Inbound.VLESSSettings(
  1686. Protocols.VLESS,
  1687. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1688. 'none',
  1689. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1690. );
  1691. }
  1692. toJson() {
  1693. return {
  1694. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1695. decryption: 'none',
  1696. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1697. };
  1698. }
  1699. };
  1700. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1701. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1702. super();
  1703. this.id = id;
  1704. this.flow = flow;
  1705. this.email = email;
  1706. this.limitIp = limitIp;
  1707. this.totalGB = totalGB;
  1708. this.expiryTime = expiryTime;
  1709. this.enable = enable;
  1710. this.tgId = tgId;
  1711. this.subId = subId;
  1712. }
  1713. static fromJson(json={}) {
  1714. return new Inbound.VLESSSettings.VLESS(
  1715. json.id,
  1716. json.flow,
  1717. json.email,
  1718. json.limitIp,
  1719. json.totalGB,
  1720. json.expiryTime,
  1721. json.enable,
  1722. json.tgId,
  1723. json.subId,
  1724. );
  1725. }
  1726. get _expiryTime() {
  1727. if (this.expiryTime === 0 || this.expiryTime === "") {
  1728. return null;
  1729. }
  1730. if (this.expiryTime < 0){
  1731. return this.expiryTime / -86400000;
  1732. }
  1733. return moment(this.expiryTime);
  1734. }
  1735. set _expiryTime(t) {
  1736. if (t == null || t === "") {
  1737. this.expiryTime = 0;
  1738. } else {
  1739. this.expiryTime = t.valueOf();
  1740. }
  1741. }
  1742. get _totalGB() {
  1743. return toFixed(this.totalGB / ONE_GB, 2);
  1744. }
  1745. set _totalGB(gb) {
  1746. this.totalGB = toFixed(gb * ONE_GB, 0);
  1747. }
  1748. };
  1749. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1750. constructor(name="", alpn='', path='', dest='', xver=0) {
  1751. super();
  1752. this.name = name;
  1753. this.alpn = alpn;
  1754. this.path = path;
  1755. this.dest = dest;
  1756. this.xver = xver;
  1757. }
  1758. toJson() {
  1759. let xver = this.xver;
  1760. if (!Number.isInteger(xver)) {
  1761. xver = 0;
  1762. }
  1763. return {
  1764. name: this.name,
  1765. alpn: this.alpn,
  1766. path: this.path,
  1767. dest: this.dest,
  1768. xver: xver,
  1769. }
  1770. }
  1771. static fromJson(json=[]) {
  1772. const fallbacks = [];
  1773. for (let fallback of json) {
  1774. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1775. fallback.name,
  1776. fallback.alpn,
  1777. fallback.path,
  1778. fallback.dest,
  1779. fallback.xver,
  1780. ))
  1781. }
  1782. return fallbacks;
  1783. }
  1784. };
  1785. Inbound.TrojanSettings = class extends Inbound.Settings {
  1786. constructor(protocol,
  1787. trojans=[new Inbound.TrojanSettings.Trojan()],
  1788. fallbacks=[],) {
  1789. super(protocol);
  1790. this.trojans = trojans;
  1791. this.fallbacks = fallbacks;
  1792. }
  1793. addTrojanFallback() {
  1794. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1795. }
  1796. delTrojanFallback(index) {
  1797. this.fallbacks.splice(index, 1);
  1798. }
  1799. static fromJson(json={}) {
  1800. return new Inbound.TrojanSettings(
  1801. Protocols.TROJAN,
  1802. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1803. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1804. }
  1805. toJson() {
  1806. return {
  1807. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1808. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1809. };
  1810. }
  1811. };
  1812. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1813. constructor(password=RandomUtil.randomSeq(10), flow='', email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1814. super();
  1815. this.password = password;
  1816. this.flow = flow;
  1817. this.email = email;
  1818. this.limitIp = limitIp;
  1819. this.totalGB = totalGB;
  1820. this.expiryTime = expiryTime;
  1821. this.enable = enable;
  1822. this.tgId = tgId;
  1823. this.subId = subId;
  1824. }
  1825. toJson() {
  1826. return {
  1827. password: this.password,
  1828. flow: this.flow,
  1829. email: this.email,
  1830. limitIp: this.limitIp,
  1831. totalGB: this.totalGB,
  1832. expiryTime: this.expiryTime,
  1833. enable: this.enable,
  1834. tgId: this.tgId,
  1835. subId: this.subId,
  1836. };
  1837. }
  1838. static fromJson(json = {}) {
  1839. return new Inbound.TrojanSettings.Trojan(
  1840. json.password,
  1841. json.flow,
  1842. json.email,
  1843. json.limitIp,
  1844. json.totalGB,
  1845. json.expiryTime,
  1846. json.enable,
  1847. json.tgId,
  1848. json.subId,
  1849. );
  1850. }
  1851. get _expiryTime() {
  1852. if (this.expiryTime === 0 || this.expiryTime === "") {
  1853. return null;
  1854. }
  1855. if (this.expiryTime < 0){
  1856. return this.expiryTime / -86400000;
  1857. }
  1858. return moment(this.expiryTime);
  1859. }
  1860. set _expiryTime(t) {
  1861. if (t == null || t === "") {
  1862. this.expiryTime = 0;
  1863. } else {
  1864. this.expiryTime = t.valueOf();
  1865. }
  1866. }
  1867. get _totalGB() {
  1868. return toFixed(this.totalGB / ONE_GB, 2);
  1869. }
  1870. set _totalGB(gb) {
  1871. this.totalGB = toFixed(gb * ONE_GB, 0);
  1872. }
  1873. };
  1874. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1875. constructor(name="", alpn='', path='', dest='', xver=0) {
  1876. super();
  1877. this.name = name;
  1878. this.alpn = alpn;
  1879. this.path = path;
  1880. this.dest = dest;
  1881. this.xver = xver;
  1882. }
  1883. toJson() {
  1884. let xver = this.xver;
  1885. if (!Number.isInteger(xver)) {
  1886. xver = 0;
  1887. }
  1888. return {
  1889. name: this.name,
  1890. alpn: this.alpn,
  1891. path: this.path,
  1892. dest: this.dest,
  1893. xver: xver,
  1894. }
  1895. }
  1896. static fromJson(json=[]) {
  1897. const fallbacks = [];
  1898. for (let fallback of json) {
  1899. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1900. fallback.name,
  1901. fallback.alpn,
  1902. fallback.path,
  1903. fallback.dest,
  1904. fallback.xver,
  1905. ))
  1906. }
  1907. return fallbacks;
  1908. }
  1909. };
  1910. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1911. constructor(protocol,
  1912. method=SSMethods.BLAKE3_AES_256_GCM,
  1913. password=RandomUtil.randomShadowsocksPassword(),
  1914. network='tcp,udp',
  1915. shadowsockses=[new Inbound.ShadowsocksSettings.Shadowsocks()]
  1916. ) {
  1917. super(protocol);
  1918. this.method = method;
  1919. this.password = password;
  1920. this.network = network;
  1921. this.shadowsockses = shadowsockses;
  1922. }
  1923. static fromJson(json={}) {
  1924. return new Inbound.ShadowsocksSettings(
  1925. Protocols.SHADOWSOCKS,
  1926. json.method,
  1927. json.password,
  1928. json.network,
  1929. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  1930. );
  1931. }
  1932. toJson() {
  1933. return {
  1934. method: this.method,
  1935. password: this.password,
  1936. network: this.network,
  1937. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses)
  1938. };
  1939. }
  1940. };
  1941. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  1942. constructor(method='', password=RandomUtil.randomShadowsocksPassword(), email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1943. super();
  1944. this.method = method;
  1945. this.password = password;
  1946. this.email = email;
  1947. this.limitIp = limitIp;
  1948. this.totalGB = totalGB;
  1949. this.expiryTime = expiryTime;
  1950. this.enable = enable;
  1951. this.tgId = tgId;
  1952. this.subId = subId;
  1953. }
  1954. toJson() {
  1955. return {
  1956. method: this.method,
  1957. password: this.password,
  1958. email: this.email,
  1959. limitIp: this.limitIp,
  1960. totalGB: this.totalGB,
  1961. expiryTime: this.expiryTime,
  1962. enable: this.enable,
  1963. tgId: this.tgId,
  1964. subId: this.subId,
  1965. };
  1966. }
  1967. static fromJson(json = {}) {
  1968. return new Inbound.ShadowsocksSettings.Shadowsocks(
  1969. json.method,
  1970. json.password,
  1971. json.email,
  1972. json.limitIp,
  1973. json.totalGB,
  1974. json.expiryTime,
  1975. json.enable,
  1976. json.tgId,
  1977. json.subId,
  1978. );
  1979. }
  1980. get _expiryTime() {
  1981. if (this.expiryTime === 0 || this.expiryTime === "") {
  1982. return null;
  1983. }
  1984. if (this.expiryTime < 0){
  1985. return this.expiryTime / -86400000;
  1986. }
  1987. return moment(this.expiryTime);
  1988. }
  1989. set _expiryTime(t) {
  1990. if (t == null || t === "") {
  1991. this.expiryTime = 0;
  1992. } else {
  1993. this.expiryTime = t.valueOf();
  1994. }
  1995. }
  1996. get _totalGB() {
  1997. return toFixed(this.totalGB / ONE_GB, 2);
  1998. }
  1999. set _totalGB(gb) {
  2000. this.totalGB = toFixed(gb * ONE_GB, 0);
  2001. }
  2002. };
  2003. Inbound.DokodemoSettings = class extends Inbound.Settings {
  2004. constructor(protocol, address, port, network='tcp,udp', followRedirect=false) {
  2005. super(protocol);
  2006. this.address = address;
  2007. this.port = port;
  2008. this.network = network;
  2009. this.followRedirect = followRedirect;
  2010. }
  2011. static fromJson(json={}) {
  2012. return new Inbound.DokodemoSettings(
  2013. Protocols.DOKODEMO,
  2014. json.address,
  2015. json.port,
  2016. json.network,
  2017. json.followRedirect,
  2018. );
  2019. }
  2020. toJson() {
  2021. return {
  2022. address: this.address,
  2023. port: this.port,
  2024. network: this.network,
  2025. followRedirect: this.followRedirect,
  2026. };
  2027. }
  2028. };
  2029. Inbound.SocksSettings = class extends Inbound.Settings {
  2030. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  2031. super(protocol);
  2032. this.auth = auth;
  2033. this.accounts = accounts;
  2034. this.udp = udp;
  2035. this.ip = ip;
  2036. }
  2037. addAccount(account) {
  2038. this.accounts.push(account);
  2039. }
  2040. delAccount(index) {
  2041. this.accounts.splice(index, 1);
  2042. }
  2043. static fromJson(json={}) {
  2044. let accounts;
  2045. if (json.auth === 'password') {
  2046. accounts = json.accounts.map(
  2047. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  2048. )
  2049. }
  2050. return new Inbound.SocksSettings(
  2051. Protocols.SOCKS,
  2052. json.auth,
  2053. accounts,
  2054. json.udp,
  2055. json.ip,
  2056. );
  2057. }
  2058. toJson() {
  2059. return {
  2060. auth: this.auth,
  2061. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  2062. udp: this.udp,
  2063. ip: this.ip,
  2064. };
  2065. }
  2066. };
  2067. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  2068. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  2069. super();
  2070. this.user = user;
  2071. this.pass = pass;
  2072. }
  2073. static fromJson(json={}) {
  2074. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  2075. }
  2076. };
  2077. Inbound.HttpSettings = class extends Inbound.Settings {
  2078. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  2079. super(protocol);
  2080. this.accounts = accounts;
  2081. }
  2082. addAccount(account) {
  2083. this.accounts.push(account);
  2084. }
  2085. delAccount(index) {
  2086. this.accounts.splice(index, 1);
  2087. }
  2088. static fromJson(json={}) {
  2089. return new Inbound.HttpSettings(
  2090. Protocols.HTTP,
  2091. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  2092. );
  2093. }
  2094. toJson() {
  2095. return {
  2096. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2097. };
  2098. }
  2099. };
  2100. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2101. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  2102. super();
  2103. this.user = user;
  2104. this.pass = pass;
  2105. }
  2106. static fromJson(json={}) {
  2107. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2108. }
  2109. };