xray.js 68 KB

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