From 0267a1b32a820e882fe4234a446d342f6d3e70e4 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Wed, 14 Feb 2024 11:17:54 +0100 Subject: [PATCH] [wg] auto multi-peer and qrcode #896 --- web/assets/js/model/xray.js | 40 +++++++++++++++++++++-- web/html/common/qrcode_modal.html | 19 ++++++++--- web/html/xui/form/protocol/wireguard.html | 12 +++++-- web/html/xui/inbound_info_modal.html | 40 ++++++++++++++++++++--- web/html/xui/inbounds.html | 8 ++--- 5 files changed, 100 insertions(+), 19 deletions(-) diff --git a/web/assets/js/model/xray.js b/web/assets/js/model/xray.js index 9587fbf64d..24ab2872d9 100644 --- a/web/assets/js/model/xray.js +++ b/web/assets/js/model/xray.js @@ -1347,6 +1347,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, 9.9.9.9\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: @@ -1393,6 +1415,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) => { @@ -1402,7 +1425,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 ''; } } @@ -2098,9 +2128,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; @@ -2108,6 +2142,7 @@ Inbound.WireguardSettings.Peer = class extends XrayCommonClass { static fromJson(json={}){ return new Inbound.WireguardSettings.Peer( + json.privateKey, json.publicKey, json.preSharedKey, json.allowedIPs, @@ -2117,6 +2152,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 5ea76ed680..b72ea7be03 100644 --- a/web/html/common/qrcode_modal.html +++ b/web/html/common/qrcode_modal.html @@ -36,12 +36,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/protocol/wireguard.html b/web/html/xui/form/protocol/wireguard.html index ea2c342776..c618a7703f 100644 --- a/web/html/xui/form/protocol/wireguard.html +++ b/web/html/xui/form/protocol/wireguard.html @@ -38,10 +38,16 @@ - {{ i18n "pages.xray.wireguard.publicKey" }} - + {{ i18n "pages.xray.wireguard.secretKey" }} + + + + + @@ -51,7 +57,7 @@ {{ i18n "reset" }} {{ i18n "pages.xray.wireguard.psk" }} - + diff --git a/web/html/xui/inbound_info_modal.html b/web/html/xui/inbound_info_modal.html index d09bb3332f..fee2bc78ab 100644 --- a/web/html/xui/inbound_info_modal.html +++ b/web/html/xui/inbound_info_modal.html @@ -279,24 +279,50 @@ @@ -323,7 +349,11 @@ 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); diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index a8a03b3e0c..cdf3866471 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -203,7 +203,7 @@ {{ i18n "edit" }} - + {{ i18n "qrCode" }} @@ -736,7 +736,7 @@ this.exportAllLinks(); break; case "subs": - this.exportAllLinks(); + this.exportAllSubs(); break; case "resetInbounds": this.resetAllTraffic(); @@ -1224,7 +1224,7 @@ }, }); }, - exportAllLinks() { + exportAllSubs() { let subLinks = [] for (const dbInbound of this.dbInbounds) { const clients = this.getInboundClients(dbInbound); @@ -1241,7 +1241,7 @@ [...new Set(subLinks)].join('\r\n'), 'All-Inbounds-Subs'); }, - exportAllSubs() { + exportAllLinks() { let copyText = []; for (const dbInbound of this.dbInbounds) { copyText.push(dbInbound.genInboundLinks(this.remarkModel));