xray.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  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)) {
  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. }
  1634. static fromJson(json={}) {
  1635. return new Inbound.VmessSettings.Vmess(
  1636. json.id,
  1637. json.email,
  1638. json.limitIp,
  1639. json.totalGB,
  1640. json.expiryTime,
  1641. json.enable,
  1642. json.tgId,
  1643. json.subId,
  1644. );
  1645. }
  1646. get _expiryTime() {
  1647. if (this.expiryTime === 0 || this.expiryTime === "") {
  1648. return null;
  1649. }
  1650. if (this.expiryTime < 0){
  1651. return this.expiryTime / -86400000;
  1652. }
  1653. return moment(this.expiryTime);
  1654. }
  1655. set _expiryTime(t) {
  1656. if (t == null || t === "") {
  1657. this.expiryTime = 0;
  1658. } else {
  1659. this.expiryTime = t.valueOf();
  1660. }
  1661. }
  1662. get _totalGB() {
  1663. return toFixed(this.totalGB / ONE_GB, 2);
  1664. }
  1665. set _totalGB(gb) {
  1666. this.totalGB = toFixed(gb * ONE_GB, 0);
  1667. }
  1668. };
  1669. Inbound.VLESSSettings = class extends Inbound.Settings {
  1670. constructor(protocol,
  1671. vlesses=[new Inbound.VLESSSettings.VLESS()],
  1672. decryption='none',
  1673. fallbacks=[],) {
  1674. super(protocol);
  1675. this.vlesses = vlesses;
  1676. this.decryption = 'none'; // Using decryption is not implemented here
  1677. this.fallbacks = fallbacks;
  1678. }
  1679. addFallback() {
  1680. this.fallbacks.push(new Inbound.VLESSSettings.Fallback());
  1681. }
  1682. delFallback(index) {
  1683. this.fallbacks.splice(index, 1);
  1684. }
  1685. // decryption should be set to static value
  1686. static fromJson(json={}) {
  1687. return new Inbound.VLESSSettings(
  1688. Protocols.VLESS,
  1689. json.clients.map(client => Inbound.VLESSSettings.VLESS.fromJson(client)),
  1690. 'none',
  1691. Inbound.VLESSSettings.Fallback.fromJson(json.fallbacks),
  1692. );
  1693. }
  1694. toJson() {
  1695. return {
  1696. clients: Inbound.VLESSSettings.toJsonArray(this.vlesses),
  1697. decryption: 'none',
  1698. fallbacks: Inbound.VLESSSettings.toJsonArray(this.fallbacks),
  1699. };
  1700. }
  1701. };
  1702. Inbound.VLESSSettings.VLESS = class extends XrayCommonClass {
  1703. constructor(id=RandomUtil.randomUUID(), flow='', email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1704. super();
  1705. this.id = id;
  1706. this.flow = flow;
  1707. this.email = email;
  1708. this.limitIp = limitIp;
  1709. this.totalGB = totalGB;
  1710. this.expiryTime = expiryTime;
  1711. this.enable = enable;
  1712. this.tgId = tgId;
  1713. this.subId = subId;
  1714. }
  1715. static fromJson(json={}) {
  1716. return new Inbound.VLESSSettings.VLESS(
  1717. json.id,
  1718. json.flow,
  1719. json.email,
  1720. json.limitIp,
  1721. json.totalGB,
  1722. json.expiryTime,
  1723. json.enable,
  1724. json.tgId,
  1725. json.subId,
  1726. );
  1727. }
  1728. get _expiryTime() {
  1729. if (this.expiryTime === 0 || this.expiryTime === "") {
  1730. return null;
  1731. }
  1732. if (this.expiryTime < 0){
  1733. return this.expiryTime / -86400000;
  1734. }
  1735. return moment(this.expiryTime);
  1736. }
  1737. set _expiryTime(t) {
  1738. if (t == null || t === "") {
  1739. this.expiryTime = 0;
  1740. } else {
  1741. this.expiryTime = t.valueOf();
  1742. }
  1743. }
  1744. get _totalGB() {
  1745. return toFixed(this.totalGB / ONE_GB, 2);
  1746. }
  1747. set _totalGB(gb) {
  1748. this.totalGB = toFixed(gb * ONE_GB, 0);
  1749. }
  1750. };
  1751. Inbound.VLESSSettings.Fallback = class extends XrayCommonClass {
  1752. constructor(name="", alpn='', path='', dest='', xver=0) {
  1753. super();
  1754. this.name = name;
  1755. this.alpn = alpn;
  1756. this.path = path;
  1757. this.dest = dest;
  1758. this.xver = xver;
  1759. }
  1760. toJson() {
  1761. let xver = this.xver;
  1762. if (!Number.isInteger(xver)) {
  1763. xver = 0;
  1764. }
  1765. return {
  1766. name: this.name,
  1767. alpn: this.alpn,
  1768. path: this.path,
  1769. dest: this.dest,
  1770. xver: xver,
  1771. }
  1772. }
  1773. static fromJson(json=[]) {
  1774. const fallbacks = [];
  1775. for (let fallback of json) {
  1776. fallbacks.push(new Inbound.VLESSSettings.Fallback(
  1777. fallback.name,
  1778. fallback.alpn,
  1779. fallback.path,
  1780. fallback.dest,
  1781. fallback.xver,
  1782. ))
  1783. }
  1784. return fallbacks;
  1785. }
  1786. };
  1787. Inbound.TrojanSettings = class extends Inbound.Settings {
  1788. constructor(protocol,
  1789. trojans=[new Inbound.TrojanSettings.Trojan()],
  1790. fallbacks=[],) {
  1791. super(protocol);
  1792. this.trojans = trojans;
  1793. this.fallbacks = fallbacks;
  1794. }
  1795. addTrojanFallback() {
  1796. this.fallbacks.push(new Inbound.TrojanSettings.Fallback());
  1797. }
  1798. delTrojanFallback(index) {
  1799. this.fallbacks.splice(index, 1);
  1800. }
  1801. static fromJson(json={}) {
  1802. return new Inbound.TrojanSettings(
  1803. Protocols.TROJAN,
  1804. json.clients.map(client => Inbound.TrojanSettings.Trojan.fromJson(client)),
  1805. Inbound.TrojanSettings.Fallback.fromJson(json.fallbacks),);
  1806. }
  1807. toJson() {
  1808. return {
  1809. clients: Inbound.TrojanSettings.toJsonArray(this.trojans),
  1810. fallbacks: Inbound.TrojanSettings.toJsonArray(this.fallbacks),
  1811. };
  1812. }
  1813. };
  1814. Inbound.TrojanSettings.Trojan = class extends XrayCommonClass {
  1815. constructor(password=RandomUtil.randomSeq(10), flow='', email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1816. super();
  1817. this.password = password;
  1818. this.flow = flow;
  1819. this.email = email;
  1820. this.limitIp = limitIp;
  1821. this.totalGB = totalGB;
  1822. this.expiryTime = expiryTime;
  1823. this.enable = enable;
  1824. this.tgId = tgId;
  1825. this.subId = subId;
  1826. }
  1827. toJson() {
  1828. return {
  1829. password: this.password,
  1830. flow: this.flow,
  1831. email: this.email,
  1832. limitIp: this.limitIp,
  1833. totalGB: this.totalGB,
  1834. expiryTime: this.expiryTime,
  1835. enable: this.enable,
  1836. tgId: this.tgId,
  1837. subId: this.subId,
  1838. };
  1839. }
  1840. static fromJson(json = {}) {
  1841. return new Inbound.TrojanSettings.Trojan(
  1842. json.password,
  1843. json.flow,
  1844. json.email,
  1845. json.limitIp,
  1846. json.totalGB,
  1847. json.expiryTime,
  1848. json.enable,
  1849. json.tgId,
  1850. json.subId,
  1851. );
  1852. }
  1853. get _expiryTime() {
  1854. if (this.expiryTime === 0 || this.expiryTime === "") {
  1855. return null;
  1856. }
  1857. if (this.expiryTime < 0){
  1858. return this.expiryTime / -86400000;
  1859. }
  1860. return moment(this.expiryTime);
  1861. }
  1862. set _expiryTime(t) {
  1863. if (t == null || t === "") {
  1864. this.expiryTime = 0;
  1865. } else {
  1866. this.expiryTime = t.valueOf();
  1867. }
  1868. }
  1869. get _totalGB() {
  1870. return toFixed(this.totalGB / ONE_GB, 2);
  1871. }
  1872. set _totalGB(gb) {
  1873. this.totalGB = toFixed(gb * ONE_GB, 0);
  1874. }
  1875. };
  1876. Inbound.TrojanSettings.Fallback = class extends XrayCommonClass {
  1877. constructor(name="", alpn='', path='', dest='', xver=0) {
  1878. super();
  1879. this.name = name;
  1880. this.alpn = alpn;
  1881. this.path = path;
  1882. this.dest = dest;
  1883. this.xver = xver;
  1884. }
  1885. toJson() {
  1886. let xver = this.xver;
  1887. if (!Number.isInteger(xver)) {
  1888. xver = 0;
  1889. }
  1890. return {
  1891. name: this.name,
  1892. alpn: this.alpn,
  1893. path: this.path,
  1894. dest: this.dest,
  1895. xver: xver,
  1896. }
  1897. }
  1898. static fromJson(json=[]) {
  1899. const fallbacks = [];
  1900. for (let fallback of json) {
  1901. fallbacks.push(new Inbound.TrojanSettings.Fallback(
  1902. fallback.name,
  1903. fallback.alpn,
  1904. fallback.path,
  1905. fallback.dest,
  1906. fallback.xver,
  1907. ))
  1908. }
  1909. return fallbacks;
  1910. }
  1911. };
  1912. Inbound.ShadowsocksSettings = class extends Inbound.Settings {
  1913. constructor(protocol,
  1914. method=SSMethods.BLAKE3_AES_256_GCM,
  1915. password=RandomUtil.randomShadowsocksPassword(),
  1916. network='tcp,udp',
  1917. shadowsockses=[new Inbound.ShadowsocksSettings.Shadowsocks()]
  1918. ) {
  1919. super(protocol);
  1920. this.method = method;
  1921. this.password = password;
  1922. this.network = network;
  1923. this.shadowsockses = shadowsockses;
  1924. }
  1925. static fromJson(json={}) {
  1926. return new Inbound.ShadowsocksSettings(
  1927. Protocols.SHADOWSOCKS,
  1928. json.method,
  1929. json.password,
  1930. json.network,
  1931. json.clients.map(client => Inbound.ShadowsocksSettings.Shadowsocks.fromJson(client)),
  1932. );
  1933. }
  1934. toJson() {
  1935. return {
  1936. method: this.method,
  1937. password: this.password,
  1938. network: this.network,
  1939. clients: Inbound.ShadowsocksSettings.toJsonArray(this.shadowsockses)
  1940. };
  1941. }
  1942. };
  1943. Inbound.ShadowsocksSettings.Shadowsocks = class extends XrayCommonClass {
  1944. constructor(method='', password=RandomUtil.randomShadowsocksPassword(), email=RandomUtil.randomLowerAndNum(8),limitIp=0, totalGB=0, expiryTime=0, enable=true, tgId='', subId=RandomUtil.randomLowerAndNum(16)) {
  1945. super();
  1946. this.method = method;
  1947. this.password = password;
  1948. this.email = email;
  1949. this.limitIp = limitIp;
  1950. this.totalGB = totalGB;
  1951. this.expiryTime = expiryTime;
  1952. this.enable = enable;
  1953. this.tgId = tgId;
  1954. this.subId = subId;
  1955. }
  1956. toJson() {
  1957. return {
  1958. method: this.method,
  1959. password: this.password,
  1960. email: this.email,
  1961. limitIp: this.limitIp,
  1962. totalGB: this.totalGB,
  1963. expiryTime: this.expiryTime,
  1964. enable: this.enable,
  1965. tgId: this.tgId,
  1966. subId: this.subId,
  1967. };
  1968. }
  1969. static fromJson(json = {}) {
  1970. return new Inbound.ShadowsocksSettings.Shadowsocks(
  1971. json.method,
  1972. json.password,
  1973. json.email,
  1974. json.limitIp,
  1975. json.totalGB,
  1976. json.expiryTime,
  1977. json.enable,
  1978. json.tgId,
  1979. json.subId,
  1980. );
  1981. }
  1982. get _expiryTime() {
  1983. if (this.expiryTime === 0 || this.expiryTime === "") {
  1984. return null;
  1985. }
  1986. if (this.expiryTime < 0){
  1987. return this.expiryTime / -86400000;
  1988. }
  1989. return moment(this.expiryTime);
  1990. }
  1991. set _expiryTime(t) {
  1992. if (t == null || t === "") {
  1993. this.expiryTime = 0;
  1994. } else {
  1995. this.expiryTime = t.valueOf();
  1996. }
  1997. }
  1998. get _totalGB() {
  1999. return toFixed(this.totalGB / ONE_GB, 2);
  2000. }
  2001. set _totalGB(gb) {
  2002. this.totalGB = toFixed(gb * ONE_GB, 0);
  2003. }
  2004. };
  2005. Inbound.DokodemoSettings = class extends Inbound.Settings {
  2006. constructor(protocol, address, port, network='tcp,udp', followRedirect=false) {
  2007. super(protocol);
  2008. this.address = address;
  2009. this.port = port;
  2010. this.network = network;
  2011. this.followRedirect = followRedirect;
  2012. }
  2013. static fromJson(json={}) {
  2014. return new Inbound.DokodemoSettings(
  2015. Protocols.DOKODEMO,
  2016. json.address,
  2017. json.port,
  2018. json.network,
  2019. json.followRedirect,
  2020. );
  2021. }
  2022. toJson() {
  2023. return {
  2024. address: this.address,
  2025. port: this.port,
  2026. network: this.network,
  2027. followRedirect: this.followRedirect,
  2028. };
  2029. }
  2030. };
  2031. Inbound.SocksSettings = class extends Inbound.Settings {
  2032. constructor(protocol, auth='password', accounts=[new Inbound.SocksSettings.SocksAccount()], udp=false, ip='127.0.0.1') {
  2033. super(protocol);
  2034. this.auth = auth;
  2035. this.accounts = accounts;
  2036. this.udp = udp;
  2037. this.ip = ip;
  2038. }
  2039. addAccount(account) {
  2040. this.accounts.push(account);
  2041. }
  2042. delAccount(index) {
  2043. this.accounts.splice(index, 1);
  2044. }
  2045. static fromJson(json={}) {
  2046. let accounts;
  2047. if (json.auth === 'password') {
  2048. accounts = json.accounts.map(
  2049. account => Inbound.SocksSettings.SocksAccount.fromJson(account)
  2050. )
  2051. }
  2052. return new Inbound.SocksSettings(
  2053. Protocols.SOCKS,
  2054. json.auth,
  2055. accounts,
  2056. json.udp,
  2057. json.ip,
  2058. );
  2059. }
  2060. toJson() {
  2061. return {
  2062. auth: this.auth,
  2063. accounts: this.auth === 'password' ? this.accounts.map(account => account.toJson()) : undefined,
  2064. udp: this.udp,
  2065. ip: this.ip,
  2066. };
  2067. }
  2068. };
  2069. Inbound.SocksSettings.SocksAccount = class extends XrayCommonClass {
  2070. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  2071. super();
  2072. this.user = user;
  2073. this.pass = pass;
  2074. }
  2075. static fromJson(json={}) {
  2076. return new Inbound.SocksSettings.SocksAccount(json.user, json.pass);
  2077. }
  2078. };
  2079. Inbound.HttpSettings = class extends Inbound.Settings {
  2080. constructor(protocol, accounts=[new Inbound.HttpSettings.HttpAccount()]) {
  2081. super(protocol);
  2082. this.accounts = accounts;
  2083. }
  2084. addAccount(account) {
  2085. this.accounts.push(account);
  2086. }
  2087. delAccount(index) {
  2088. this.accounts.splice(index, 1);
  2089. }
  2090. static fromJson(json={}) {
  2091. return new Inbound.HttpSettings(
  2092. Protocols.HTTP,
  2093. json.accounts.map(account => Inbound.HttpSettings.HttpAccount.fromJson(account)),
  2094. );
  2095. }
  2096. toJson() {
  2097. return {
  2098. accounts: Inbound.HttpSettings.toJsonArray(this.accounts),
  2099. };
  2100. }
  2101. };
  2102. Inbound.HttpSettings.HttpAccount = class extends XrayCommonClass {
  2103. constructor(user=RandomUtil.randomSeq(10), pass=RandomUtil.randomSeq(10)) {
  2104. super();
  2105. this.user = user;
  2106. this.pass = pass;
  2107. }
  2108. static fromJson(json={}) {
  2109. return new Inbound.HttpSettings.HttpAccount(json.user, json.pass);
  2110. }
  2111. };