diff --git a/renderer.py b/renderer.py index 64161db..dc125dc 100644 --- a/renderer.py +++ b/renderer.py @@ -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

""" @@ -707,7 +707,7 @@ def render_routes(): route_content = markup_pre+route_title route_content += """

- d + @@ -814,52 +814,55 @@ def render_routes(): failover_content += "
ID Machine Route

"+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 += """

- - + + """ - 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 = "fiber_manual_record" - disabled = "fiber_manual_record" + enabled = "fiber_manual_record" + disabled = "fiber_manual_record" # 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 += """ - - - - - """ + enabled_display = enabled if exit_enabled else disabled + # Build a table for all exit routes: + exit_content += """ + + + + + """ exit_content += "
Machine Enabled MachineEnabled
"""+str(machine )+"""
"""+str(enabled_display )+"""
"""+str(machine )+"""
"""+str(enabled_display)+"""

"+markup_post content = route_content + failover_content + exit_content diff --git a/static/js/custom.js b/static/js/custom.js index 056599b..e4070b0 100644 --- a/static/js/custom.js +++ b/static/js/custom.js @@ -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//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//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) {