xray.js 68 KB

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