xray.js 67 KB

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