Skip to content

Commit

Permalink
ns-api: dashboard, add ovpn-tunnels counter (#513)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsanchietti authored May 10, 2024
1 parent 5a9e354 commit 34fe3e9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
17 changes: 17 additions & 0 deletions packages/ns-api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,23 @@ Response example:
}
```

### ovpn-tunnels

Count enabled and connected OpenVPN tunnels:
```
api-cli ns.dashboard ovpn-tunnels
```

Response example:
```json
{
"result": {
"enabled": 2,
"connected": 1
}
}
```

## ns.subscription

Manage server subscription for [my.nethesis.it](https://my.nethesis.it) and [my.nethserver.com](https://my.nethserver.com).
Expand Down
24 changes: 23 additions & 1 deletion packages/ns-api/files/ns.dashboard
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ def ipsec_is_connected(id):
return False
return False


def ipsec_tunnels():
ret = {"enabled": 0, "connected": 0}
u = EUci()
Expand All @@ -276,6 +275,27 @@ def ipsec_tunnels():
ret["connected"] += 1
return {"result": ret}

def ovpn_tunnels():
u = EUci()
ret = {"enabled": 0, "connected": 0}
for section in u.get_all('openvpn'):
vpn = u.get_all("openvpn", section)
# skip custom config and roadwarrior
if not section.startswith("ns_") or 'ns_auth_mode' in vpn:
continue
if vpn.get("enabled", "0") == "1":
ret["enabled"] += 1

if vpn.get("client", "0") == "1" or vpn.get("ns_client", "0") == "1":
connected = ovpn.list_connected_clients(section, 'p2p')
if 'stats' in connected:
ret["connected"] += 1
else:
connected = ovpn.list_connected_clients(section, vpn.get("topology", "subnet"))
if len(connected) > 0:
ret["connected"] += 1
return ret

def service_status(service):
ret = {"status": "disabled"}
if service == "internet":
Expand Down Expand Up @@ -346,5 +366,7 @@ elif cmd == 'call':
ret = interface_traffic(args["interface"])
elif action == "ipsec-tunnels":
ret = ipsec_tunnels()
elif action == "ovpn-tunnels":
ret = ovpn_tunnels()

print(json.dumps({"result": ret}))

0 comments on commit 34fe3e9

Please sign in to comment.