xray.js 70 KB

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