mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-03-25 09:21:10 +00:00
networks: Refactor code for showing connection
- Fix showing configured IP address in edit form. - Combine the retrival functions and organize them according to where the information is being retrived from connection/device/active connection/access point. - Add more fields to show such as ether speed, default ipv4/ipv6 connection, wifi mode, etc. - Re-format the page. Separate ipv4/ipv6 sections. Separate device information and connection information sections. - Take the action buttons to the top. - Make the activate/deactivate button work with new POST only CSRF requirement. - Update Firewall zone description messages. - Show all IP addresses of the device.
This commit is contained in:
parent
682e5d7c2d
commit
fc19b0fd3d
@ -66,71 +66,41 @@ def show(request, uuid):
|
||||
'Connection not found.'))
|
||||
return redirect(reverse_lazy('networks:index'))
|
||||
|
||||
name = connection.get_interface_name()
|
||||
connectiontype = connection.get_connection_type()
|
||||
settings_ipv4 = connection.get_setting_ip4_config()
|
||||
# Connection status
|
||||
connection_status = network.get_status_from_connection(connection)
|
||||
|
||||
mac = network.get_mac_from_device(name)
|
||||
interface = connection.get_interface_name()
|
||||
if connectiontype == '802-11-wireless':
|
||||
settings_wireless = connection.get_setting_wireless()
|
||||
ssid = settings_wireless.get_ssid().get_data()
|
||||
rate = network.get_wifi_rate(interface, ssid)
|
||||
channel = network.get_wifi_channel(interface, ssid)
|
||||
strength = network.get_wifi_signal(interface, ssid)
|
||||
linkstate = True
|
||||
# Active connection status
|
||||
try:
|
||||
active_connection = network.get_active_connection(uuid)
|
||||
active_connection_status = \
|
||||
network.get_status_from_active_connection(active_connection)
|
||||
except network.ConnectionNotFound:
|
||||
active_connection_status = {}
|
||||
active_connection = None
|
||||
|
||||
# Device status
|
||||
if active_connection and active_connection.get_devices():
|
||||
device = active_connection.get_devices()[0]
|
||||
else:
|
||||
ssid = "None"
|
||||
rate = 0
|
||||
channel = 0
|
||||
linkstate = network.get_linkstate_from_device(name)
|
||||
strength = 0
|
||||
interface_name = connection_status['interface_name']
|
||||
if interface_name:
|
||||
device = network.get_device_by_interface_name(interface_name)
|
||||
|
||||
ip = network.get_all_ip_from_device(name)
|
||||
ip6 = network.get_all_ip6_from_device(name)
|
||||
dns = network.get_namesever_from_device(name)
|
||||
dns6 = network.get_namesever6_from_device(name)
|
||||
gateway = network.get_gateway_from_device(name)
|
||||
gateway6 = network.get_gateway6_from_device(name)
|
||||
method = settings_ipv4.get_method()
|
||||
device_status = network.get_status_from_device(device)
|
||||
|
||||
zone = connection.get_setting_connection().get_zone()
|
||||
active = network.connection_is_active(uuid)
|
||||
|
||||
if network.get_primary_connection().get_id() == connection.get_id():
|
||||
primary = True
|
||||
else:
|
||||
primary = False
|
||||
|
||||
if not ip:
|
||||
ip.append("0.0.0.0/0")
|
||||
|
||||
if not ip6:
|
||||
ip6.append("::0/0")
|
||||
# Access point status
|
||||
access_point_status = None
|
||||
if connection_status['type'] == '802-11-wireless':
|
||||
access_point_status = network.get_status_from_wifi_access_point(
|
||||
device, connection_status['wireless']['ssid'])
|
||||
|
||||
return TemplateResponse(request, 'connection_show.html',
|
||||
{'title': _('Show Connection information'),
|
||||
'ip': ip,
|
||||
'ip6': ip6,
|
||||
'gateway': gateway,
|
||||
'gateway6': gateway6,
|
||||
'dns': dns,
|
||||
'dns6': dns6,
|
||||
'interface': interface,
|
||||
'mac': mac,
|
||||
'linkstate': linkstate,
|
||||
'zone': zone,
|
||||
'primary': primary,
|
||||
'subsubmenu': subsubmenu,
|
||||
'method': method,
|
||||
'connectiontype': connectiontype,
|
||||
'ssid': ssid,
|
||||
'strength': strength,
|
||||
'rate': rate,
|
||||
'channel': channel,
|
||||
'active': active,
|
||||
'uuid': uuid,
|
||||
'name': name})
|
||||
'connection': connection_status,
|
||||
'active_connection': active_connection_status,
|
||||
'device': device_status,
|
||||
'access_point': access_point_status})
|
||||
|
||||
|
||||
def edit(request, uuid):
|
||||
@ -203,9 +173,9 @@ def edit(request, uuid):
|
||||
if settings_connection.get_connection_type() != 'pppoe':
|
||||
settings_ipv4 = connection.get_setting_ip4_config()
|
||||
form_data['ipv4_method'] = settings_ipv4.get_method()
|
||||
|
||||
name = connection.get_interface_name()
|
||||
form_data['ipv4_address'] = network.get_ip_from_device(name)
|
||||
address = network.get_first_ip_address_from_connection(connection)
|
||||
if address:
|
||||
form_data['ipv4_address'] = address
|
||||
|
||||
if settings_connection.get_connection_type() == '802-11-wireless':
|
||||
settings_wireless = connection.get_setting_wireless()
|
||||
|
||||
@ -23,164 +23,279 @@
|
||||
.list-group-item .btn {
|
||||
margin: -5px 0;
|
||||
}
|
||||
.form-inline {
|
||||
display: inline;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
{% load bootstrap %}
|
||||
|
||||
{% block content %}
|
||||
{% if active %}
|
||||
<h3>Physical Link Information</h3>
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
<div>
|
||||
<a href="{% url 'networks:edit' connection.uuid %}"
|
||||
class="btn btn-primary" role="button"
|
||||
title="Edit connection">Edit</a>
|
||||
|
||||
{% if linkstate and connectiontype == "802-3-ethernet" %}
|
||||
<span class='label label-success'>cable is connected</span>
|
||||
<br/><br/>
|
||||
{% elif connectiontype == "802-3-ethernet" %}
|
||||
<span class='label label-warning'>please check cable</span>
|
||||
<br/><br/>
|
||||
{% endif %}
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
<div class="list-group">
|
||||
{% if connectiontype == "802-11-wireless" %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">SSID</span>
|
||||
<span class="connection-type-label pull-right">{{ ssid }}</span>
|
||||
</div>
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">Speed</span>
|
||||
<span class="connection-type-label pull-right">{{ rate }}</span>
|
||||
</div>
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">Signal</span>
|
||||
<span class="connection-type-label pull-right">
|
||||
<span class="btn btn-primary btn-xs pull-right">
|
||||
{{ strength }}%
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">Channel</span>
|
||||
<span class="connection-type-label pull-right">{{ channel }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">MAC adress</span>
|
||||
<span class="connection-type-label pull-right">{{ mac }}</span>
|
||||
</div>
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">Interface</span>
|
||||
<span class="connection-type-label pull-right">{{ interface }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% if active_connection %}
|
||||
<form class="form form-inline" method="post"
|
||||
action="{% url 'networks:deactivate' connection.uuid %}">
|
||||
{% csrf_token %}
|
||||
<button class="btn btn-default" type="submit">Deactivate</button>
|
||||
</form>
|
||||
{% else %}
|
||||
<form class="form form-inline" method="post"
|
||||
action="{% url 'networks:activate' connection.uuid %}">
|
||||
{% csrf_token %}
|
||||
<button class="btn btn-default" type="submit">Activate</button>
|
||||
</form>
|
||||
{% endif %}
|
||||
|
||||
<h3>IP Address Information</h3>
|
||||
<a href="{% url 'networks:delete' connection.uuid %}"
|
||||
class="btn btn-default" role="button"
|
||||
title="Delete connection">Delete</a>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
<div class="list-group">
|
||||
{% for addr in ip %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">IPv4 Address</span>
|
||||
<span class="connection-type-label pull-right">{{ addr }}
|
||||
</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% for addr in ip6 %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">IPv6 Address</span>
|
||||
<span class="connection-type-label pull-right">{{ addr }}
|
||||
</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% for server in dns %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">DNS Server</span>
|
||||
<span class="connection-type-label pull-right">{{ server }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% for server in dns6 %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">DNS Server</span>
|
||||
<span class="connection-type-label pull-right">{{ server }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% if gateway %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">IPv4 Default Gateway</span>
|
||||
<span class="connection-type-label pull-right">{{ gateway }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if gateway6 %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">IPv6 Default Gateway</span>
|
||||
<span class="connection-type-label pull-right">{{ gateway6 }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if method == "shared" %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">DHCP Server</span>
|
||||
<span class="connection-type-label pull-right">enabled</span>
|
||||
</div>
|
||||
{% elif method == "auto" %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">IP Method</span>
|
||||
<span class="connection-type-label pull-right">{{ method }}
|
||||
</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if primary %}
|
||||
<div class="list-group-item clearfix">
|
||||
<span class="connection-type-label pull-left">Default Connection</span>
|
||||
<span class="connection-type-label pull-right">
|
||||
<span class='label label-success'>yes</span>
|
||||
</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<h3>Security Information</h3>
|
||||
{% if zone == "internal" %}
|
||||
Firewall: <span class='label label-success'>{{ zone }}</span><br/><br/>
|
||||
INFO: This interface should be connected to local network. <br/>
|
||||
If you connect this interface to a public
|
||||
network, others may have access to your data.
|
||||
{% elif zone == "external" %}
|
||||
Firewall: <span class='label label-warning'>{{ zone }}</span><br/><br/>
|
||||
INFO: This interface should be connected to your internet upstream
|
||||
connection
|
||||
{% else %}
|
||||
Firewall: <span class='label label-danger'>{{ zone }}</span><br/><br/>
|
||||
WARNING: This interface is not assigned to a zone which is maintained by
|
||||
freedombox
|
||||
{% endif %}
|
||||
{% else %}
|
||||
This connections is not active. Please activate connection.
|
||||
{% endif %}
|
||||
<br/><hr>
|
||||
<a href="{% url 'networks:edit' uuid %}"
|
||||
class="btn btn-primary"
|
||||
role="button" title="Edit connection">
|
||||
Edit
|
||||
</a>
|
||||
{% if active %}
|
||||
<a href="{% url 'networks:deactivate' uuid %}"
|
||||
class="btn btn-default"
|
||||
role="button" title="Deactivate connection">
|
||||
Deactivate
|
||||
</a>
|
||||
{% else %}
|
||||
<a href="{% url 'networks:activate' uuid %}"
|
||||
class="btn btn-default"
|
||||
role="button" title="Deactivate connection">
|
||||
Activate
|
||||
</a>
|
||||
{% endif %}
|
||||
<a href="{% url 'networks:delete' uuid %}"
|
||||
class="btn btn-default"
|
||||
role="button" title="Delete connection">
|
||||
Delete
|
||||
</a>
|
||||
<h3>Connection</h3>
|
||||
|
||||
<div class="list-group">
|
||||
{% if connection.primary %}
|
||||
<div class="list-group-item">
|
||||
Primary connection
|
||||
<div class="pull-right">
|
||||
<span class="label label-success">yes</span>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="list-group-item">
|
||||
Name
|
||||
<div class="pull-right">{{ connection.id }}</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h3>Device</h3>
|
||||
|
||||
<div class="list-group">
|
||||
<div class="list-group-item">
|
||||
State
|
||||
<span class="pull-right">{{ device.state }}</span>
|
||||
</div>
|
||||
{% if device.state_reason != 'none' %}
|
||||
<div class="list-group-item">
|
||||
State reason
|
||||
<span class="pull-right">{{ device.state_reason }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="list-group-item">
|
||||
Type
|
||||
<span class="pull-right">{{ device.type }}</span>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
MAC address
|
||||
<span class="pull-right">{{ device.hw_address }}</span>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
Interface
|
||||
<span class="pull-right">{{ device.interface_name }}</span>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
Description
|
||||
<span class="pull-right">{{ device.description }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<h3>Physical Link</h3>
|
||||
|
||||
<div class="list-group">
|
||||
{% if device.ethernet %}
|
||||
<div class="list-group-item">
|
||||
Link state
|
||||
<div class="pull-right">
|
||||
{% if device.ethernet.carrier %}
|
||||
<span class='label label-success'>cable is connected</span>
|
||||
{% else %}
|
||||
<span class='label label-warning'>please check cable</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
Speed
|
||||
<span class="pull-right">{{ device.ethernet.speed }} Mbit/s</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% if connection.type == "802-11-wireless" %}
|
||||
<div class="list-group-item">
|
||||
SSID
|
||||
<span class="pull-right">{{ connection.wireless.ssid }}</span>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
Speed
|
||||
<span class="pull-right">{{ device.wireless.bitrate }} Mbit/s</span>
|
||||
</div>
|
||||
<div class="list-group-item">
|
||||
Mode
|
||||
<span class="pull-right">{{ device.wireless.mode }}</span>
|
||||
</div>
|
||||
{% if access_point.channel %}
|
||||
<div class="list-group-item">
|
||||
Signal strength
|
||||
<div class="pull-right">
|
||||
<span class="btn btn-primary btn-xs">
|
||||
{{ access_point.strength }}%
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% if access_point.channel %}
|
||||
<div class="list-group-item">
|
||||
Channel
|
||||
<span class="pull-right">{{ access_point.channel }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{% if active_connection %}
|
||||
<h3>IPv4</h3>
|
||||
|
||||
<div class="list-group">
|
||||
{% if connection.ipv4.method %}
|
||||
<div class="list-group-item">
|
||||
Method
|
||||
<span class="pull-right">{{ connection.ipv4.method }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% for address in device.ip4.addresses %}
|
||||
<div class="list-group-item">
|
||||
IP address
|
||||
<span class="pull-right">
|
||||
{{ address.address }}/{{ address.prefix }}
|
||||
</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% if device.ip4.gateway %}
|
||||
<div class="list-group-item">
|
||||
Gateway
|
||||
<span class="pull-right">{{ device.ip4.gateway }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% for server in device.ip4.nameservers %}
|
||||
<div class="list-group-item">
|
||||
DNS server
|
||||
<span class="pull-right">{{ server }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% if active_connection.ip4.default %}
|
||||
<div class="list-group-item">
|
||||
Default
|
||||
<span class="pull-right">yes</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
<h3>IPv6</h3>
|
||||
|
||||
<div class="list-group">
|
||||
{% if connection.ipv6.method %}
|
||||
<div class="list-group-item">
|
||||
Method
|
||||
<span class="pull-right">{{ connection.ipv6.method }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% for address in device.ip6.addresses %}
|
||||
<div class="list-group-item">
|
||||
IP address
|
||||
<span class="pull-right">{{ address.address }}/{{ address.prefix }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% if device.ip6.gateway %}
|
||||
<div class="list-group-item">
|
||||
Gateway
|
||||
<span class="pull-right">{{ device.ip6.gateway }}</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{% for server in device.ip6.nameservers %}
|
||||
<div class="list-group-item">
|
||||
DNS server
|
||||
<span class="pull-right">{{ server }}</span>
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% if active_connection.ip6.default %}
|
||||
<div class="list-group-item">
|
||||
Default
|
||||
<span class="pull-right">yes</span>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% else %}
|
||||
<h3>Status</h3>
|
||||
|
||||
<p>This connection is not active.</p>
|
||||
{% endif %}
|
||||
|
||||
<h3>Security</h3>
|
||||
|
||||
{% if connection.zone == "internal" %}
|
||||
<div class="list-group">
|
||||
<div class="list-group-item">
|
||||
Firewall zone
|
||||
<div class="pull-right">
|
||||
<span class="label label-success">{{ connection.zone }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info">
|
||||
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
|
||||
This interface should be connected to local network/machine. If you
|
||||
connect this interface to a public network, services meant to
|
||||
be available only internally will become available externally.
|
||||
This is a security risk.
|
||||
</div>
|
||||
{% elif connection.zone == "external" %}
|
||||
<div class="list-group">
|
||||
<div class="list-group-item">
|
||||
Firewall zone
|
||||
<div class="pull-right">
|
||||
<span class="label label-warning">{{ connection.zone }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-info">
|
||||
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
|
||||
This interface should receive your Internet connection. If
|
||||
you connect it your a local network/machine, many services
|
||||
meant to available only internally will not be available.
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="list-group">
|
||||
<div class="list-group-item">
|
||||
Firewall zone
|
||||
<div class="pull-right">
|
||||
<span class="label label-danger">{{ connection.zone }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-danger">
|
||||
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
|
||||
This interface is not maintained by FreedomBox. Its security
|
||||
status is unknown to FreedomBox. Many FreedomBox services may
|
||||
not be available on this interface. It is recommended that
|
||||
you deactivate/delete this connection and re-configure it.
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
@ -25,6 +25,7 @@ from gi.repository import NM as nm
|
||||
import logging
|
||||
import socket
|
||||
import struct
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
|
||||
@ -76,254 +77,123 @@ def get_interface_list(device_type):
|
||||
return interfaces
|
||||
|
||||
|
||||
def get_first_wifi_device():
|
||||
"""Get a list of network interface available on the system."""
|
||||
interface = "empty"
|
||||
for device in nm.Client.new(None).get_devices():
|
||||
if device.get_device_type() == nm.DeviceType.WIFI:
|
||||
interface = device.get_iface()
|
||||
return interface
|
||||
def get_status_from_connection(connection):
|
||||
"""Return the current status of a connection."""
|
||||
status = collections.defaultdict(dict)
|
||||
|
||||
return interface
|
||||
status['id'] = connection.get_id()
|
||||
status['uuid'] = connection.get_uuid()
|
||||
status['type'] = connection.get_connection_type()
|
||||
status['zone'] = connection.get_setting_connection().get_zone()
|
||||
status['interface_name'] = connection.get_interface_name()
|
||||
|
||||
status['ipv4']['method'] = connection.get_setting_ip4_config().get_method()
|
||||
status['ipv6']['method'] = connection.get_setting_ip6_config().get_method()
|
||||
|
||||
if status['type'] == '802-11-wireless':
|
||||
setting_wireless = connection.get_setting_wireless()
|
||||
status['wireless']['ssid'] = setting_wireless.get_ssid().get_data()
|
||||
|
||||
primary_connection = nm.Client.new(None).get_primary_connection()
|
||||
status['primary'] = (primary_connection.get_uuid() == connection.get_uuid())
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def get_ip_from_device(devicename):
|
||||
"""
|
||||
Get the first ip address from the network interface.
|
||||
will return "0.0.0.0" if no ip address is assiged.
|
||||
IP address is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
ip = "0.0.0.0"
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
def get_status_from_active_connection(connection):
|
||||
"""Return the current status of an active connection."""
|
||||
status = collections.defaultdict(dict)
|
||||
|
||||
status['state'] = connection.get_state().value_name
|
||||
status['ip4']['default'] = connection.get_default()
|
||||
status['ip6']['default'] = connection.get_default6()
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def get_status_from_device(device):
|
||||
"""Return a dictionary with current status of a network device."""
|
||||
if not device:
|
||||
return None
|
||||
|
||||
status = collections.defaultdict(dict)
|
||||
|
||||
ip4_config = device.get_ip4_config()
|
||||
if ip4_config:
|
||||
addresses = ip4_config.get_addresses()
|
||||
if addresses:
|
||||
ip = addresses.__getitem__(0).get_address()
|
||||
return ip
|
||||
status['ip4']['addresses'] = [{'address': address.get_address(),
|
||||
'prefix': address.get_prefix()}
|
||||
for address in addresses]
|
||||
status['ip4']['gateway'] = ip4_config.get_gateway()
|
||||
status['ip4']['nameservers'] = ip4_config.get_nameservers()
|
||||
|
||||
|
||||
def get_ip6_from_device(devicename):
|
||||
"""
|
||||
Get the first ip address from the network interface.
|
||||
will return "0.0.0.0" if no ip address is assiged.
|
||||
IP address is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
ip = "0.0.0.0"
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip6_config = device.get_ip6_config()
|
||||
if ip6_config:
|
||||
addresses = ip6_config.get_addresses()
|
||||
if addresses:
|
||||
ip = addresses.__getitem__(0).get_address()
|
||||
return ip
|
||||
status['ip6']['addresses'] = [{'address': address.get_address(),
|
||||
'prefix': address.get_prefix()}
|
||||
for address in addresses]
|
||||
status['ip6']['gateway'] = ip6_config.get_gateway()
|
||||
status['ip6']['nameservers'] = ip6_config.get_nameservers()
|
||||
|
||||
status['type'] = device.get_type_description()
|
||||
status['description'] = device.get_description()
|
||||
status['hw_address'] = device.get_hw_address()
|
||||
status['interface_name'] = device.get_iface()
|
||||
status['state'] = device.get_state().value_nick
|
||||
status['state_reason'] = device.get_state_reason().value_nick
|
||||
|
||||
if device.get_device_type() == nm.DeviceType.WIFI:
|
||||
status['wireless']['bitrate'] = device.get_bitrate() / 1000
|
||||
status['wireless']['mode'] = device.get_mode().value_nick
|
||||
|
||||
if device.get_device_type() == nm.DeviceType.ETHERNET:
|
||||
status['ethernet']['speed'] = device.get_speed()
|
||||
status['ethernet']['carrier'] = device.get_carrier()
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def get_all_ip_from_device(devicename):
|
||||
def get_status_from_wifi_access_point(device, ssid):
|
||||
"""Return the current status of an access point."""
|
||||
status = {}
|
||||
|
||||
for access_point in device.get_access_points():
|
||||
if access_point.get_ssid().get_data() == ssid:
|
||||
status['strength'] = access_point.get_strength()
|
||||
frequency = access_point.get_frequency()
|
||||
status['channel'] = _get_wifi_channel_from_frequency(frequency)
|
||||
break
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def _get_wifi_channel_from_frequency(frequency):
|
||||
"""Get the wifi channel form a particular SSID"""
|
||||
# TODO: Hard coded list of wifi frequencys and their corresponding
|
||||
# channel numbers. Search for a better solution! Even 5GHz is
|
||||
# not included yet. Only the plain frequency will show up on 5GHz
|
||||
# AP's.
|
||||
channel_map = {2412: 1, 2417: 2, 2422: 3, 2427: 4, 2432: 5, 2437: 6,
|
||||
2442: 7, 2447: 8, 2452: 9, 2457: 10, 2462: 11}
|
||||
try:
|
||||
return channel_map[frequency]
|
||||
except KeyError:
|
||||
return str(frequency / 1000) + 'GHz'
|
||||
|
||||
|
||||
def get_first_ip_address_from_connection(connection):
|
||||
"""Return the first IP address of a connection setting.
|
||||
|
||||
XXX: Work around a bug in NetworkManager/Python GI. Remove after
|
||||
the bug if fixed.
|
||||
https://bugzilla.gnome.org/show_bug.cgi?id=756380.
|
||||
"""
|
||||
Get all ipv4 addresses from device
|
||||
IP address is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
ip = []
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip4_config = device.get_ip4_config()
|
||||
if ip4_config:
|
||||
addresses = ip4_config.get_addresses()
|
||||
for address in addresses:
|
||||
netmask = str(address.get_prefix())
|
||||
ip.append(str(address.get_address() + "/" + netmask))
|
||||
return ip
|
||||
command = ['nmcli', '--terse', '--mode', 'tabular', '--fields',
|
||||
'ipv4.addresses', 'connection', 'show', connection.get_uuid()]
|
||||
|
||||
|
||||
def get_all_ip6_from_device(devicename):
|
||||
"""
|
||||
Get all ipv6 addresses from device
|
||||
IP address is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
ip = []
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip6_config = device.get_ip6_config()
|
||||
if ip6_config:
|
||||
addresses = ip6_config.get_addresses()
|
||||
for address in addresses:
|
||||
netmask = str(address.get_prefix())
|
||||
ip.append(str(address.get_address() + "/" + netmask))
|
||||
return ip
|
||||
|
||||
|
||||
def get_namesever_from_device(devicename):
|
||||
"""
|
||||
Get all nameservers which are reachable via this device.
|
||||
Nameservers is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
nameservers = ""
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip4_config = device.get_ip4_config()
|
||||
if ip4_config:
|
||||
nameservers = ip4_config.get_nameservers()
|
||||
return nameservers
|
||||
|
||||
|
||||
def get_namesever6_from_device(devicename):
|
||||
"""
|
||||
Get all nameservers which are reachable via this device.
|
||||
Nameservers is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
nameservers = ""
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip6_config = device.get_ip6_config()
|
||||
if ip6_config:
|
||||
nameservers = ip6_config.get_nameservers()
|
||||
return nameservers
|
||||
|
||||
|
||||
def get_gateway_from_device(devicename):
|
||||
"""
|
||||
Get the default Gateway for this device.
|
||||
gateway is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
gateway = ""
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip4_config = device.get_ip4_config()
|
||||
if ip4_config:
|
||||
gateway = device.get_ip4_config().get_gateway()
|
||||
return gateway
|
||||
|
||||
|
||||
def get_gateway6_from_device(devicename):
|
||||
"""
|
||||
Get the default Gateway for this device.
|
||||
gateway is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
gateway = ""
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip6_config = device.get_ip6_config()
|
||||
if ip6_config:
|
||||
gateway = device.get_ip6_config().get_gateway()
|
||||
return gateway
|
||||
|
||||
|
||||
def get_linkstate_from_device(devicename):
|
||||
"""
|
||||
Get the physical link state from this device (carrier detected)
|
||||
link state is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
linkstate = False
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip4_config = device.get_ip4_config()
|
||||
if ip4_config:
|
||||
linkstate = device.get_carrier()
|
||||
return linkstate
|
||||
|
||||
|
||||
def get_mac_from_device(devicename):
|
||||
"""
|
||||
Get the MAC address of the network interface.
|
||||
MAC address is a optional information, will not raise a exception
|
||||
if no information could be returned.
|
||||
ToDo: will not work when connection is or previously was inactive
|
||||
"""
|
||||
mac = "00:00:00:00:00:00"
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
ip4_config = device.get_ip4_config()
|
||||
if ip4_config:
|
||||
mac = device.get_hw_address()
|
||||
return mac
|
||||
|
||||
|
||||
def connection_is_active(connection_uuid):
|
||||
"""
|
||||
Return True if connection is active
|
||||
Return False if connection is inactive
|
||||
"""
|
||||
client = nm.Client.new(None)
|
||||
for connection in client.get_active_connections():
|
||||
if connection.get_uuid() == connection_uuid:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def get_primary_connection():
|
||||
"""return the name of the primary (aka internet) connection"""
|
||||
return nm.Client.new(None).get_primary_connection()
|
||||
|
||||
|
||||
def get_wifi_signal(devicename, ssid):
|
||||
"""Get the wifi signal strenght form a particular SSID"""
|
||||
signal = 0
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
for ap in device.get_access_points():
|
||||
if ap.get_ssid().get_data() == ssid:
|
||||
signal = ap.get_strength()
|
||||
return signal
|
||||
|
||||
|
||||
def get_wifi_rate(devicename, ssid):
|
||||
"""Get the wifi bitrate form a particular SSID"""
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
rate = device.get_bitrate() / 1000
|
||||
rate = str(str(rate) + "Mbit/s")
|
||||
return rate
|
||||
|
||||
|
||||
def get_wifi_channel(devicename, ssid):
|
||||
"""Get the wifi channeö form a particular SSID"""
|
||||
channel = 0
|
||||
device = nm.Client.new(None).get_device_by_iface(devicename)
|
||||
for ap in device.get_access_points():
|
||||
if ap.get_ssid().get_data() == ssid:
|
||||
frequency = ap.get_frequency()
|
||||
frequency = frequency / 1000
|
||||
|
||||
"""
|
||||
Hard coded list of wifi frequencys and their corresponding channel numbers.
|
||||
ToDo: search for a better solution! Even 5GHz is not included yet.
|
||||
Only the plain frequency will show up on 5GHz AP's.
|
||||
"""
|
||||
if frequency == 2.412:
|
||||
channel = 1
|
||||
elif frequency == 2.417:
|
||||
channel = 2
|
||||
elif frequency == 2.422:
|
||||
channel = 3
|
||||
elif frequency == 2.427:
|
||||
channel = 4
|
||||
elif frequency == 2.432:
|
||||
channel = 5
|
||||
elif frequency == 2.437:
|
||||
channel = 6
|
||||
elif frequency == 2.442:
|
||||
channel = 7
|
||||
elif frequency == 2.447:
|
||||
channel = 8
|
||||
elif frequency == 2.452:
|
||||
channel = 9
|
||||
elif frequency == 2.457:
|
||||
channel = 10
|
||||
elif frequency == 2.462:
|
||||
channel = 11
|
||||
else:
|
||||
channel = str(str(frequency) + "GHz")
|
||||
|
||||
return channel
|
||||
output = subprocess.check_output(command).decode()
|
||||
return output.strip().split(', ')[0].split('/')[0]
|
||||
|
||||
|
||||
def get_connection_list():
|
||||
@ -379,6 +249,11 @@ def get_active_connection(connection_uuid):
|
||||
raise ConnectionNotFound(connection_uuid)
|
||||
|
||||
|
||||
def get_device_by_interface_name(interface_name):
|
||||
"""Return a device by interface name."""
|
||||
return nm.Client.new(None).get_device_by_iface(interface_name)
|
||||
|
||||
|
||||
def _update_common_settings(connection, connection_uuid, name, type_,
|
||||
interface, zone):
|
||||
"""Create/edit basic settings for network manager connections.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user