1
0
mirror of https://github.com/donaldzou/WGDashboard.git synced 2024-11-22 15:20:09 +01:00

Added the function to remove peers

This commit is contained in:
donaldzou 2020-12-26 23:42:41 -05:00
parent edc77be8ef
commit f361e178f1
2 changed files with 52 additions and 14 deletions

View File

@ -196,13 +196,29 @@ def add_peer(config_name):
if public_key in keys: if public_key in keys:
return "Key already exist." return "Key already exist."
else: else:
status = ""
try: try:
status = subprocess.check_output("wg set "+config_name+" peer "+public_key+" allowed-ips "+allowed_ips, shell=True) status = subprocess.check_output("wg set "+config_name+" peer "+public_key+" allowed-ips "+allowed_ips, shell=True, stderr=subprocess.STDOUT)
status = subprocess.check_output("wg-quick save "+config_name, shell=True) status = subprocess.check_output("wg-quick save "+config_name, shell=True, stderr=subprocess.STDOUT)
return "Good" return "true"
except Exception: return redirect('/configuration/'+config_name) except subprocess.CalledProcessError as exc:
return exc.output.strip()
# @app.route('/remove_peer/<config_name>/<peer_id>', methods=['POST']) # return redirect('/configuration/'+config_name)
# def remove_peer(config_name, peer_id):
@app.route('/remove_peer/<config_name>', methods=['POST'])
def remove_peer(config_name):
data = request.get_json()
delete_key = data['peer_id']
keys = get_conf_peer_key(config_name)
if delete_key not in keys:
return "This key does not exist"
else:
try:
status = subprocess.check_output("wg set "+config_name+" peer "+delete_key+" remove", shell=True, stderr=subprocess.STDOUT)
status = subprocess.check_output("wg-quick save "+config_name, shell=True, stderr=subprocess.STDOUT)
return "true"
except subprocess.CalledProcessError as exc:
return exc.output.strip()
app.run(host='0.0.0.0',debug=False, port=10086) app.run(host='0.0.0.0',debug=False, port=10086)

View File

@ -99,7 +99,7 @@
</div> </div>
<div class="col-sm"> <div class="col-sm">
<small class="text-muted"><strong>PEER</strong></small> <small class="text-muted"><strong>PEER</strong></small>
<h6 style="text-transform: uppercase;"><samp>{{i}}</samp></h6> <h6><samp>{{i}}</samp></h6>
</div> </div>
<div class="w-100"></div> <div class="w-100"></div>
@ -153,6 +153,11 @@
</button> </button>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<div id="add_peer_alert" class="alert alert-danger alert-dismissible fade show d-none" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<form> <form>
<div class="form-group"> <div class="form-group">
<label for="public_key">Public Key</label> <label for="public_key">Public Key</label>
@ -182,6 +187,11 @@
</button> </button>
</div> </div>
<div class="modal-body"> <div class="modal-body">
<div id="remove_peer_alert" class="alert alert-danger alert-dismissible fade show d-none" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<h6>This action is not reversible.</h6> <h6>This action is not reversible.</h6>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
@ -204,11 +214,11 @@
$('.switch').change(function() { $('.switch').change(function() {
if ($(this).prop('checked') == false){ if ($(this).prop('checked') == false){
if (confirm('Are you sure you want to turn off this connection?')){ if (confirm('Are you sure you want to turn off this connection?')){
location.replace("/switch/"+$(this).attr('id')) location.replace("/switch/"+$(this).attr('id'));
} }
} }
else{ else{
location.replace("/switch/"+$(this).attr('id')) location.replace("/switch/"+$(this).attr('id'));
} }
}); });
@ -224,7 +234,13 @@
}, },
data: JSON.stringify({"public_key":$("#public_key").val(), "allowed_ips": $("#allowed_ips").val()}), data: JSON.stringify({"public_key":$("#public_key").val(), "allowed_ips": $("#allowed_ips").val()}),
success: function (response){ success: function (response){
console.log(response); if(response != "true"){
$("#add_peer_alert").html(response+$("#add_peer_alert").html());
$("#add_peer_alert").removeClass("d-none");
}
else{
location.reload();
}
} }
}) })
} }
@ -235,18 +251,24 @@
$("#delete_peer").attr("peer_id", peer_id); $("#delete_peer").attr("peer_id", peer_id);
}); });
$(".delete_peer").click(function(){ $("#delete_peer").click(function(){
var peer_id = $(this).attr("peer_id"); var peer_id = $(this).attr("peer_id");
var config = $(this).attr("conf_id"); var config = $(this).attr("conf_id");
$.ajax({ $.ajax({
method: "POST", method: "POST",
url: "/remove_peer/"+config+"/"+peer_id, url: "/remove_peer/"+config,
headers:{ headers:{
"Content-Type": "application/json" "Content-Type": "application/json"
}, },
data: JSON.stringify({"action": "delete"}), data: JSON.stringify({"action": "delete", "peer_id": peer_id}),
success: function (response){ success: function (response){
console.log(response); if(response != "true"){
$("#remove_peer_alert").html(response+$("#add_peer_alert").html());
$("#remove_peer_alert").removeClass("d-none");
}
else{
location.reload();
}
} }
}) })
}); });