From 61489077d7d5740250702c68846abae6a3598c6f Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Sun, 18 Feb 2024 00:46:49 +0330 Subject: [PATCH] [wg] auto multi-peer and qrcode Co-Authored-By: Alireza Ahmadi --- web/assets/js/model/xray.js | 40 ++++++++++++++++++- web/html/common/qrcode_modal.html | 19 ++++++--- web/html/xui/form/outbound.html | 35 +++++------------ web/html/xui/form/protocol/wireguard.html | 12 ++++-- web/html/xui/inbound_info_modal.html | 48 +++++++++++++++++------ web/html/xui/inbounds.html | 2 +- 6 files changed, 108 insertions(+), 48 deletions(-) diff --git a/web/assets/js/model/xray.js b/web/assets/js/model/xray.js index 7018fb75..d7d1fa0d 100644 --- a/web/assets/js/model/xray.js +++ b/web/assets/js/model/xray.js @@ -1534,6 +1534,28 @@ class Inbound extends XrayCommonClass { return url.toString(); } + getWireguardLink(address, port, remark, peerId) { + let txt = `[Interface]\n` + txt += `PrivateKey = ${this.settings.peers[peerId].privateKey}\n` + txt += `Address = ${this.settings.peers[peerId].allowedIPs[0]}\n` + txt += `DNS = 1.1.1.1, 1.0.0.1\n` + if (this.settings.mtu) { + txt += `MTU = ${this.settings.mtu}\n` + } + txt += `\n# ${remark}\n` + txt += `[Peer]\n` + txt += `PublicKey = ${this.settings.pubKey}\n` + txt += `AllowedIPs = 0.0.0.0/0, ::/0\n` + txt += `Endpoint = ${address}:${port}` + if (this.settings.peers[peerId].psk) { + txt += `\nPresharedKey = ${this.settings.peers[peerId].psk}` + } + if (this.settings.peers[peerId].keepAlive) { + txt += `\nPersistentKeepalive = ${this.settings.peers[peerId].keepAlive}\n` + } + return txt; + } + genLink(address='', port=this.port, forceTls='same', remark='', client) { switch (this.protocol) { case Protocols.VMESS: @@ -1580,6 +1602,7 @@ class Inbound extends XrayCommonClass { } genInboundLinks(remark = '', remarkModel = '-ieo') { + let addr = !ObjectUtil.isEmpty(this.listen) && this.listen !== "0.0.0.0" ? this.listen : location.hostname; if(this.clients){ let links = []; this.clients.forEach((client) => { @@ -1589,7 +1612,14 @@ class Inbound extends XrayCommonClass { }); return links.join('\r\n'); } else { - if(this.protocol == Protocols.SHADOWSOCKS && !this.isSSMultiUser) return this.genSSLink(this.listen, this.port, 'same', remark); + if(this.protocol == Protocols.SHADOWSOCKS && !this.isSSMultiUser) return this.genSSLink(addr, this.port, 'same', remark); + if(this.protocol == Protocols.WIREGUARD) { + let links = []; + this.settings.peers.forEach((p,index) => { + links.push(this.getWireguardLink(addr,this.port,remark + remarkModel.charAt(0) + (index+1),index)); + }); + return links.join('\r\n'); + } return ''; } } @@ -2297,9 +2327,13 @@ Inbound.WireguardSettings = class extends XrayCommonClass { }; Inbound.WireguardSettings.Peer = class extends XrayCommonClass { - constructor(publicKey=Wireguard.generateKeypair().publicKey, psk='', allowedIPs=['0.0.0.0/0','::/0'], keepAlive=0) { + constructor(privateKey, publicKey, psk='', allowedIPs=['10.0.0.0/24'], keepAlive=0) { super(); + this.privateKey = privateKey this.publicKey = publicKey; + if (!this.publicKey){ + [this.publicKey, this.privateKey] = Object.values(Wireguard.generateKeypair()) + } this.psk = psk; this.allowedIPs = allowedIPs; this.keepAlive = keepAlive; @@ -2307,6 +2341,7 @@ Inbound.WireguardSettings.Peer = class extends XrayCommonClass { static fromJson(json={}){ return new Inbound.WireguardSettings.Peer( + json.privateKey, json.publicKey, json.preSharedKey, json.allowedIPs, @@ -2316,6 +2351,7 @@ Inbound.WireguardSettings.Peer = class extends XrayCommonClass { toJson() { return { + privateKey: this.privateKey, publicKey: this.publicKey, preSharedKey: this.psk.length>0 ? this.psk : undefined, allowedIPs: this.allowedIPs, diff --git a/web/html/common/qrcode_modal.html b/web/html/common/qrcode_modal.html index 3c4fd929..3bda4c29 100644 --- a/web/html/common/qrcode_modal.html +++ b/web/html/common/qrcode_modal.html @@ -35,12 +35,21 @@ this.client = client; this.subId = ''; this.qrcodes = []; - this.inbound.genAllLinks(this.dbInbound.remark, app.remarkModel, client).forEach(l => { - this.qrcodes.push({ - remark: l.remark, - link: l.link + if (this.inbound.protocol == Protocols.WIREGUARD){ + this.inbound.genInboundLinks(dbInbound.remark).split('\r\n').forEach((l,index) =>{ + this.qrcodes.push({ + remark: "Peer " + (index+1), + link: l + }); }); - }); + } else { + this.inbound.genAllLinks(this.dbInbound.remark, app.remarkModel, client).forEach(l => { + this.qrcodes.push({ + remark: l.remark, + link: l.link + }); + }); + } this.visible = true; }, close: function () { diff --git a/web/html/xui/form/outbound.html b/web/html/xui/form/outbound.html index fb6a7af7..469c42b9 100644 --- a/web/html/xui/form/outbound.html +++ b/web/html/xui/form/outbound.html @@ -134,28 +134,10 @@ - - + - - + @@ -189,7 +171,6 @@ - - - {{ i18n "pages.xray.wireguard.psk" }} - + diff --git a/web/html/xui/inbound_info_modal.html b/web/html/xui/inbound_info_modal.html index cdcabad9..23f8bd47 100644 --- a/web/html/xui/inbound_info_modal.html +++ b/web/html/xui/inbound_info_modal.html @@ -179,10 +179,10 @@ @@ -319,7 +345,6 @@ index: null, isExpired: false, subLink: '', - tgLink: '', show(dbInbound, index) { this.index = index; this.inbound = dbInbound.toInbound(); @@ -327,14 +352,15 @@ this.clientSettings = this.inbound.clients ? this.inbound.clients[index] : null; this.isExpired = this.inbound.clients ? this.inbound.isExpiry(index): this.dbInbound.isExpiry; this.clientStats = this.inbound.clients ? this.dbInbound.clientStats.find(row => row.email === this.clientSettings.email) : []; - this.links = this.inbound.genAllLinks(this.dbInbound.remark, app.remarkModel, this.clientSettings); + if (this.inbound.protocol == Protocols.WIREGUARD){ + this.links = this.inbound.genInboundLinks(dbInbound.remark).split('\r\n') + } else { + this.links = this.inbound.genAllLinks(this.dbInbound.remark, app.remarkModel, this.clientSettings); + } if (this.clientSettings) { if (this.clientSettings.subId) { this.subLink = this.genSubLink(this.clientSettings.subId); } - if (this.clientSettings.tgId) { - this.tgLink = "https://t.me/" + this.clientSettings.tgId; - } } this.visible = true; }, diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index aab05462..93f25730 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -200,7 +200,7 @@ {{ i18n "edit" }} - + {{ i18n "qrCode" }}