xray.js 80 KB

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