1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-06 16:00:28 +01:00

Merge pull request #313 from donaldzou/v4.0-fix2

Fixed #312, #311
This commit is contained in:
Donald Zou 2024-08-19 16:50:48 -04:00 committed by GitHub
commit 181b0845bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 51 additions and 18 deletions

View File

@ -33,7 +33,7 @@ import threading
from flask.json.provider import DefaultJSONProvider from flask.json.provider import DefaultJSONProvider
DASHBOARD_VERSION = 'v4.0' DASHBOARD_VERSION = 'v4.0.1.1'
CONFIGURATION_PATH = os.getenv('CONFIGURATION_PATH', '.') CONFIGURATION_PATH = os.getenv('CONFIGURATION_PATH', '.')
DB_PATH = os.path.join(CONFIGURATION_PATH, 'db') DB_PATH = os.path.join(CONFIGURATION_PATH, 'db')
if not os.path.isdir(DB_PATH): if not os.path.isdir(DB_PATH):
@ -490,6 +490,7 @@ class WireguardConfiguration:
# Create tables in database # Create tables in database
self.__createDatabase() self.__createDatabase()
self.getPeersList() self.getPeersList()
self.getRestrictedPeersList()
def __createDatabase(self): def __createDatabase(self):
existingTables = sqldb.cursor().execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall() existingTables = sqldb.cursor().execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall()
@ -697,6 +698,7 @@ class WireguardConfiguration:
sqldb.cursor().execute("UPDATE '%s_restrict_access' SET status = 'stopped' WHERE id = ?" % sqldb.cursor().execute("UPDATE '%s_restrict_access' SET status = 'stopped' WHERE id = ?" %
(self.Name,), (pf.id,)) (self.Name,), (pf.id,))
sqldb.cursor().execute("DELETE FROM '%s' WHERE id = ?" % self.Name, (pf.id,)) sqldb.cursor().execute("DELETE FROM '%s' WHERE id = ?" % self.Name, (pf.id,))
sqldb.commit()
numOfRestrictedPeers += 1 numOfRestrictedPeers += 1
except Exception as e: except Exception as e:
numOfFailedToRestrictPeers += 1 numOfFailedToRestrictPeers += 1
@ -1115,6 +1117,8 @@ class DashboardConfig:
self.__createAPIKeyTable() self.__createAPIKeyTable()
self.DashboardAPIKeys = self.__getAPIKeys() self.DashboardAPIKeys = self.__getAPIKeys()
self.APIAccessed = False self.APIAccessed = False
self.SetConfig("Server", "version", DASHBOARD_VERSION)
def __createAPIKeyTable(self): def __createAPIKeyTable(self):
existingTable = sqldb.cursor().execute("SELECT name FROM sqlite_master WHERE type='table' AND name = 'DashboardAPIKeys'").fetchall() existingTable = sqldb.cursor().execute("SELECT name FROM sqlite_master WHERE type='table' AND name = 'DashboardAPIKeys'").fetchall()
@ -1344,6 +1348,14 @@ def _getWireguardConfigurationAvailableIP(configName: str) -> tuple[bool, list[s
for i in add: for i in add:
a, c = i.split('/') a, c = i.split('/')
existedAddress.append(ipaddress.ip_address(a.replace(" ", ""))) existedAddress.append(ipaddress.ip_address(a.replace(" ", "")))
for p in configuration.getRestrictedPeersList():
if len(p.allowed_ip) > 0:
add = p.allowed_ip.split(',')
for i in add:
a, c = i.split('/')
existedAddress.append(ipaddress.ip_address(a.replace(" ", "")))
for i in address: for i in address:
addressSplit, cidr = i.split('/') addressSplit, cidr = i.split('/')
existedAddress.append(ipaddress.ip_address(addressSplit.replace(" ", ""))) existedAddress.append(ipaddress.ip_address(addressSplit.replace(" ", "")))
@ -1419,6 +1431,7 @@ def auth_req():
and f"{(APP_PREFIX if len(APP_PREFIX) > 0 else '')}" != request.path) and f"{(APP_PREFIX if len(APP_PREFIX) > 0 else '')}" != request.path)
and "validateAuthentication" not in request.path and "authenticate" not in request.path and "validateAuthentication" not in request.path and "authenticate" not in request.path
and "getDashboardConfiguration" not in request.path and "getDashboardTheme" not in request.path and "getDashboardConfiguration" not in request.path and "getDashboardTheme" not in request.path
and "getDashboardVersion" not in request.path
and "sharePeer/get" not in request.path and "sharePeer/get" not in request.path
and "isTotpEnabled" not in request.path and "isTotpEnabled" not in request.path
): ):
@ -1751,10 +1764,13 @@ def API_addPeers(configName):
return ResponseObject(False, "Please fill in all required box.") return ResponseObject(False, "Please fill in all required box.")
if not config.getStatus(): if not config.getStatus():
config.toggleConfiguration() config.toggleConfiguration()
availableIps = _getWireguardConfigurationAvailableIP(configName)
if bulkAdd: if bulkAdd:
if bulkAddAmount < 1: if bulkAddAmount < 1:
return ResponseObject(False, "Please specify amount of peers you want to add") return ResponseObject(False, "Please specify amount of peers you want to add")
availableIps = _getWireguardConfigurationAvailableIP(configName)
if not availableIps[0]: if not availableIps[0]:
return ResponseObject(False, "No more available IP can assign") return ResponseObject(False, "No more available IP can assign")
if bulkAddAmount > len(availableIps[1]): if bulkAddAmount > len(availableIps[1]):
@ -1788,6 +1804,11 @@ def API_addPeers(configName):
return ResponseObject(False, f"This peer already exist.") return ResponseObject(False, f"This peer already exist.")
name = data['name'] name = data['name']
private_key = data['private_key'] private_key = data['private_key']
for i in allowed_ips:
if i not in availableIps[1]:
return ResponseObject(False, f"This IP is not available: {i}")
config.addPeers([{"id": public_key, "allowed_ip": ''.join(allowed_ips)}]) config.addPeers([{"id": public_key, "allowed_ip": ''.join(allowed_ips)}])
# subprocess.check_output( # subprocess.check_output(
# f"wg set {config.Name} peer {public_key} allowed-ips {''.join(allowed_ips)}", # f"wg set {config.Name} peer {public_key} allowed-ips {''.join(allowed_ips)}",
@ -1857,6 +1878,10 @@ def API_getConfigurationInfo():
def API_getDashboardTheme(): def API_getDashboardTheme():
return ResponseObject(data=DashboardConfig.GetConfig("Server", "dashboard_theme")[1]) return ResponseObject(data=DashboardConfig.GetConfig("Server", "dashboard_theme")[1])
@app.route(f'{APP_PREFIX}/api/getDashboardVersion')
def API_getDashboardVersion():
return ResponseObject(data=DashboardConfig.GetConfig("Server", "version")[1])
@app.route(f'{APP_PREFIX}/api/savePeerScheduleJob/', methods=["POST"]) @app.route(f'{APP_PREFIX}/api/savePeerScheduleJob/', methods=["POST"])
def API_savePeerScheduleJob(): def API_savePeerScheduleJob():
@ -2102,6 +2127,7 @@ def backGroundThread():
c.getPeersLatestHandshake() c.getPeersLatestHandshake()
c.getPeersEndpoint() c.getPeersEndpoint()
c.getPeersList() c.getPeersList()
c.getRestrictedPeersList()
except Exception as e: except Exception as e:
print(f"[WGDashboard] Background Thread #1 Error: {str(e)}", flush=True) print(f"[WGDashboard] Background Thread #1 Error: {str(e)}", flush=True)
time.sleep(10) time.sleep(10)

View File

@ -36,8 +36,11 @@ export default {
addAllowedIp(ip){ addAllowedIp(ip){
if(this.store.checkCIDR(ip)){ if(this.store.checkCIDR(ip)){
this.data.allowed_ips.push(ip); this.data.allowed_ips.push(ip);
this.customAvailableIp = ''
return true; return true;
} }
this.allowedIpFormatError = true;
this.dashboardStore.newMessage('WGDashboard', 'Allowed IP is invalid', 'danger')
return false; return false;
} }
}, },
@ -80,10 +83,7 @@ export default {
:disabled="bulk"> :disabled="bulk">
<button class="btn btn-outline-success btn-sm rounded-end-3" <button class="btn btn-outline-success btn-sm rounded-end-3"
:disabled="bulk || !this.customAvailableIp" :disabled="bulk || !this.customAvailableIp"
@click="this.addAllowedIp(this.customAvailableIp) @click="this.addAllowedIp(this.customAvailableIp)"
? this.customAvailableIp = '' :
this.allowedIpFormatError = true;
this.dashboardStore.newMessage('WGDashboard', 'Allowed IP is invalid', 'danger')"
type="button" id="button-addon2"> type="button" id="button-addon2">
<i class="bi bi-plus-lg"></i> <i class="bi bi-plus-lg"></i>
</button> </button>

View File

@ -200,7 +200,8 @@ export default {
}) })
this.loading = false; this.loading = false;
if (this.configurationPeers.length > 0){ if (this.configurationPeers.length > 0){
const sent = this.configurationPeers.map(x => x.total_sent + x.cumu_sent).reduce((x,y) => x + y).toFixed(4); const sent = this.configurationPeers.map(x => x.total_sent + x.cumu_sent)
.reduce((x,y) => x + y).toFixed(4);
const receive = this.configurationPeers.map(x => x.total_receive + x.cumu_receive).reduce((x,y) => x + y).toFixed(4); const receive = this.configurationPeers.map(x => x.total_receive + x.cumu_receive).reduce((x,y) => x + y).toFixed(4);
if ( if (
this.historyDataSentDifference[this.historyDataSentDifference.length - 1] !== sent this.historyDataSentDifference[this.historyDataSentDifference.length - 1] !== sent
@ -259,13 +260,13 @@ export default {
connectedPeers: this.configurationPeers.filter(x => x.status === "running").length, connectedPeers: this.configurationPeers.filter(x => x.status === "running").length,
totalUsage: this.configurationPeers.length > 0 ? totalUsage: this.configurationPeers.length > 0 ?
this.configurationPeers.filter(x => !x.restricted) this.configurationPeers.filter(x => !x.restricted)
.map(x => x.total_data + x.cumu_data).reduce((a, b) => a + b).toFixed(4) : 0, .map(x => x.total_data + x.cumu_data).reduce((a, b) => a + b, 0).toFixed(4) : 0,
totalReceive: this.configurationPeers.length > 0 ? totalReceive: this.configurationPeers.length > 0 ?
this.configurationPeers.filter(x => !x.restricted) this.configurationPeers.filter(x => !x.restricted)
.map(x => x.total_receive + x.cumu_receive).reduce((a, b) => a + b).toFixed(4) : 0, .map(x => x.total_receive + x.cumu_receive).reduce((a, b) => a + b, 0).toFixed(4) : 0,
totalSent: this.configurationPeers.length > 0 ? totalSent: this.configurationPeers.length > 0 ?
this.configurationPeers.filter(x => !x.restricted) this.configurationPeers.filter(x => !x.restricted)
.map(x => x.total_sent + x.cumu_sent).reduce((a, b) => a + b).toFixed(4) : 0 .map(x => x.total_sent + x.cumu_sent).reduce((a, b) => a + b, 0).toFixed(4) : 0
} }
return k return k

View File

@ -11,16 +11,22 @@ export default {
const store = DashboardConfigurationStore() const store = DashboardConfigurationStore()
let theme = "dark" let theme = "dark"
let totpEnabled = false; let totpEnabled = false;
let version = undefined;
if (!store.IsElectronApp){ if (!store.IsElectronApp){
await fetchGet("/api/getDashboardTheme", {}, (res) => { await Promise.all([
theme = res.data fetchGet("/api/getDashboardTheme", {}, (res) => {
}); theme = res.data
await fetchGet("/api/isTotpEnabled", {}, (res) => { }),
totpEnabled = res.data fetchGet("/api/isTotpEnabled", {}, (res) => {
}); totpEnabled = res.data
}),
fetchGet("/api/getDashboardVersion", {}, (res) => {
version = res.data
})
]);
} }
store.removeActiveCrossServer(); store.removeActiveCrossServer();
return {store, theme, totpEnabled} return {store, theme, totpEnabled, version}
}, },
data(){ data(){
return { return {
@ -146,7 +152,7 @@ export default {
</div> </div>
</div> </div>
<small class="text-muted pb-3 d-block w-100 text-center mt-3"> <small class="text-muted pb-3 d-block w-100 text-center mt-3">
WGDashboard v4.0 | Developed with by WGDashboard {{ this.version }} | Developed with by
<a href="https://github.com/donaldzou" target="_blank"><strong>Donald Zou</strong></a> <a href="https://github.com/donaldzou" target="_blank"><strong>Donald Zou</strong></a>
</small> </small>
<div class="messageCentre text-body position-absolute end-0 m-3"> <div class="messageCentre text-body position-absolute end-0 m-3">