Logic for exit nodes for routes page

This commit is contained in:
iFargle
2023-03-24 14:12:21 +09:00
parent fc95182d12
commit b17ac9dc94
2 changed files with 62 additions and 60 deletions

View File

@@ -299,7 +299,7 @@ def thread_machine_content(machine, machine_content, idx, all_routes, failover_p
class='waves-effect waves-light btn-small """+exit_enabled_color+""" lighten-2 tooltipped'
data-position='top' data-tooltip='Click to """+exit_tooltip+"""'
id='"""+machine["id"]+"""-exit'
onclick="toggle_exit("""+exit_routes[0]+""", """+exit_routes[1]+""", '"""+machine["id"]+"""-exit', '"""+str(exit_route_enabled)+"""')">
onclick="toggle_exit("""+exit_routes[0]+""", """+exit_routes[1]+""", '"""+machine["id"]+"""-exit', '"""+str(exit_route_enabled)+"""', 'machines')">
Exit Route
</p>
"""
@@ -707,7 +707,7 @@ def render_routes():
route_content = markup_pre+route_title
route_content += """<p><table>
<thead>
<tr>d
<tr>
<th>ID </th>
<th>Machine </th>
<th>Route </th>
@@ -814,52 +814,55 @@ def render_routes():
failover_content += "</tbody></table></p>"+markup_post
# Step 3: Get exit nodes only:
exit_node_list = []
# Get a list of nodes with exit routes:
for route in all_routes["routes"]:
# For every exit route found, store the machine name in an array:
if prefix == "0.0.0.0/0" or prefix == "::/0":
if machine not in exit_node_list:
exit_node_list.append(machine)
# Exit node display building:
# Display by machine, not by route
exit_content = markup_pre+exit_title
exit_content += """<p><table>
<thead>
<tr>
<th>Machine </th>
<th>Enabled </th>
<th>Machine</th>
<th>Enabled</th>
</tr>
</thead>
<tbody>
"""
for route in all_routes["routes"]:
# Get relevant info:
route_id = route["id"]
machine = route["machine"]["givenName"]
prefix = route["prefix"]
is_enabled = route["enabled"]
is_primary = route["isPrimary"]
is_failover = False
is_exit = False
# Get exit route ID's for each node in the list:
# onclick="toggle_exit("""+exit_routes[0]+""", """+exit_routes[1]+""", '"""+machine["id"]+"""-exit', '"""+str(exit_route_enabled)+"""')
for node in exit_node_list:
node_exit_routes = []
exit_enabled = False
for route in all_routes["routes"]:
# Get all exit route ID's for that node:
route_id = route["id"]
machine = route["machine"]["givenName"]
if machine == node:
if route_id == "0.0.0.0/0" or route_id == "::/0":
node_exit_routes.append(route_id)
if route["enabled"]:
exit_enabled = True
# Set up the display code:
enabled = "<a href='#'><i class='material-icons green-text text-lighten-2'>fiber_manual_record</i></a>"
disabled = "<a href='#'><i class='material-icons red-text text-lighten-1'>fiber_manual_record</i></a>"
enabled = "<a href='#'><i id='"+node["id"]+"-exit' onclick='toggle_exit("+node_exit_routes[0]+","+node_exit_routes[1]+","+node['id']+", '"+node['id']+"-exit', 'routes')' class='material-icons green-text text-lighten-2 tooltipped' data-tooltip='Click to disable'>fiber_manual_record</i></a>"
disabled = "<a href='#'><i id='"+node["id"]+"-exit' onclick='toggle_exit("+node_exit_routes[0]+","+node_exit_routes[1]+","+node['id']+", '"+node['id']+"-exit', 'routes')' class='material-icons red-text text-lighten-2 tooltipped' data-tooltip='Click to enable' >fiber_manual_record</i></a>"
# Set the displays:
enabled_display = disabled
primary_display = disabled
failover_display = disabled
exit_display = disabled
if is_enabled: enabled_display = enabled
if is_primary: primary_display = enabled
# Check if a prefix is an Exit route:
if prefix == "0.0.0.0/0": # I assume nodes have both ::/0 and 0.0.0.0/0
is_exit = True
exit_display = True
if is_exit:
# Build a simple table for all non-exit routes:
exit_content += """
<tr>
<td>"""+str(machine )+"""</td>
<td width="60px"><center>"""+str(enabled_display )+"""</center></td>
</tr>
"""
enabled_display = enabled if exit_enabled else disabled
# Build a table for all exit routes:
exit_content += """
<tr>
<td>"""+str(machine )+"""</td>
<td width="60px"><center>"""+str(enabled_display)+"""</center></td>
</tr>
"""
exit_content += "</tbody></table></p>"+markup_post
content = route_content + failover_content + exit_content

View File

@@ -756,9 +756,30 @@ function delete_machine(machine_id) {
})
}
function toggle_exit(route1, route2, exit_id, current_state) {
function toggle_exit(route1, route2, exit_id, current_state, page) {
var data1 = {"route_id": route1, "current_state": current_state}
var data2 = {"route_id": route2, "current_state": current_state}
var element = document.getElementById(exit_id);
var disabledClass = ""
var enabledClass = ""
if (page == "machines") {
disabledClass = "waves-effect waves-light btn-small red lighten-2 tooltipped";
enabledClass = "waves-effect waves-light btn-small green lighten-2 tooltipped";
}
if (page == "routes") {
disabledClass = "material-icons red-text text-lighten-2 tooltipped";
enabledClass = "material-icons green-text text-lighten-2 tooltipped";
}
var disabledTooltip = "Click to enable"
var enabledTooltip = "Click to disable"
var disableState = "False"
var enableState = "True"
var action_taken = "unchanged.";
$.ajax({
type:"POST",
url: "api/update_route",
@@ -772,26 +793,14 @@ function toggle_exit(route1, route2, exit_id, current_state) {
contentType: "application/json",
success: function(response) {
// Response is a JSON object containing the Headscale API response of /v1/api/machines/<id>/route
var element = document.getElementById(exit_id);
var disabledClass = "waves-effect waves-light btn-small red lighten-2 tooltipped";
var enabledClass = "waves-effect waves-light btn-small green lighten-2 tooltipped";
var disabledTooltip = "Click to enable"
var enabledTooltip = "Click to disable"
var disableState = "False"
var enableState = "True"
var action_taken = "unchanged.";
if (element.className == disabledClass) {
// 1. Change the class to change the color of the icon
// 2. Change the "action taken" for the M.toast popup
// 3. Change the tooltip to say "Click to enable/disable"
element.className = enabledClass
var action_taken = "enabled."
action_taken = "enabled."
element.setAttribute('data-tooltip', enabledTooltip)
element.setAttribute('onclick', 'toggle_exit('+route1+', '+route2+', "'+exit_id+'", "'+enableState+'")')
} else if (element.className == enabledClass) {
element.className = disabledClass
var action_taken = "disabled."
action_taken = "disabled."
element.setAttribute('data-tooltip', disabledTooltip)
element.setAttribute('onclick', 'toggle_exit('+route1+', '+route2+', "'+exit_id+'", "'+disableState+'")')
}
@@ -818,12 +827,6 @@ function toggle_route(route_id, current_state, page) {
enabledClass = "material-icons green-text text-lighten-2 tooltipped";
}
console.log("In toggle_routes: ")
console.log("Editing route "+route_id+" for page: "+page+" (enabled? "+current_state+")")
console.log("disabledClass: "+disabledClass)
console.log("enabledClass: "+enabledClass)
console.log("Data: "+data)
var disabledTooltip = "Click to enable"
var enabledTooltip = "Click to disable"
var disableState = "False"
@@ -835,13 +838,9 @@ function toggle_route(route_id, current_state, page) {
data: JSON.stringify(data),
contentType: "application/json",
success: function(response) {
// Response is a JSON object containing the Headscale API response of /v1/api/machines/<id>/route
if (element.className == disabledClass) {
// 1. Change the class to change the color of the icon
element.className = enabledClass
// 2. Change the "action taken" for the M.toast popup
action_taken = "enabled."
// 3. Change the tooltip to say "Click to enable/disable"
element.setAttribute('data-tooltip', enabledTooltip)
element.setAttribute('onclick', 'toggle_route('+route_id+', "'+enableState+'", "'+page+'")')
} else if (element.className == enabledClass) {