Skip to content

Commit ec59655

Browse files
committed
Move dnsmasq and nm dicts in to own files
1 parent bd8e630 commit ec59655

File tree

4 files changed

+107
-104
lines changed

4 files changed

+107
-104
lines changed

src/common/nm_dicts.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import config
2+
import uuid
3+
from common.errors import logger
4+
from common.errors import WifiInvalidConnectionType
5+
6+
7+
def get_nm_dict(conn_type, ssid, username, password):
8+
if conn_type == config.type_hotspot:
9+
# Hotspot for user to connect to device
10+
hs_dict = {
11+
'802-11-wireless': {'band': 'bg',
12+
'mode': 'ap',
13+
'ssid': config.hotspot_ssid},
14+
'connection': {'autoconnect': False,
15+
'id': config.ap_name,
16+
'interface-name': 'wlan0',
17+
'type': '802-11-wireless',
18+
'uuid': str(uuid.uuid4())},
19+
'ipv4': {'address-data':
20+
[{'address': '192.168.42.1', 'prefix': 24}],
21+
'addresses': [['192.168.42.1', 24, '0.0.0.0']],
22+
'method': 'manual'},
23+
'ipv6': {'method': 'auto'}
24+
}
25+
26+
# Include a key-mgmt string in hotspot if setting a password
27+
if password:
28+
password_key_mgmt = {'802-11-wireless-security':
29+
{'key-mgmt': 'wpa-psk', 'psk': password}}
30+
31+
hs_dict.update(password_key_mgmt)
32+
33+
return hs_dict
34+
elif conn_type == config.type_none:
35+
# No auth/'open' connection.
36+
return {
37+
'802-11-wireless': {'mode': 'infrastructure',
38+
'ssid': ssid},
39+
'connection': {'id': config.ap_name,
40+
'type': '802-11-wireless',
41+
'uuid': str(uuid.uuid4())},
42+
'ipv4': {'method': 'auto'},
43+
'ipv6': {'method': 'auto'}
44+
}
45+
elif (conn_type == config.type_wep or
46+
conn_type == config.type_wpa or
47+
conn_type == config.type_wpa2):
48+
# Hidden, WEP, WPA, WPA2, password required.
49+
return {
50+
'802-11-wireless': {'mode': 'infrastructure',
51+
'security': '802-11-wireless-security',
52+
'ssid': ssid},
53+
'802-11-wireless-security':
54+
{'key-mgmt': 'wpa-psk', 'psk': password},
55+
'connection': {'id': config.ap_name,
56+
'type': '802-11-wireless',
57+
'uuid': str(uuid.uuid4())},
58+
'ipv4': {'method': 'auto'},
59+
'ipv6': {'method': 'auto'}
60+
}
61+
elif conn_type == config.type_enterprise:
62+
# "MIT SECURE" network.
63+
return {
64+
'802-11-wireless': {'mode': 'infrastructure',
65+
'security': '802-11-wireless-security',
66+
'ssid': ssid},
67+
'802-11-wireless-security':
68+
{'auth-alg': 'open', 'key-mgmt': 'wpa-eap'},
69+
'802-1x': {'eap': ['peap'],
70+
'identity': username,
71+
'password': password,
72+
'phase2-auth': 'mschapv2'},
73+
'connection': {'id': config.ap_name,
74+
'type': '802-11-wireless',
75+
'uuid': str(uuid.uuid4())},
76+
'ipv4': {'method': 'auto'},
77+
'ipv6': {'method': 'auto'}
78+
}
79+
else:
80+
logger.error("Invalid conn_type.")
81+
raise WifiInvalidConnectionType

src/common/system.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import config
2+
import subprocess
3+
from common.errors import logger
4+
5+
6+
def dnsmasq():
7+
# Build the list of args
8+
args = ["/usr/sbin/dnsmasq",
9+
f"--address=/#/{config.DEFAULT_GATEWAY}",
10+
f"--dhcp-range={config.DEFAULT_DHCP_RANGE}",
11+
f"--dhcp-option=option:router,{config.DEFAULT_GATEWAY}",
12+
f"--interface={config.DEFAULT_INTERFACE}",
13+
"--keep-in-foreground",
14+
"--bind-dynamic",
15+
"--except-interface=lo",
16+
"--conf-file",
17+
"--no-hosts"]
18+
19+
try:
20+
subprocess.Popen(args)
21+
except Exception:
22+
logger.exception('Failed to start dnsmasq.')

src/common/wifi.py

Lines changed: 3 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
import socket
44
import subprocess
55
import time
6-
import uuid
76
from common.errors import logger
87
from common.errors import WifiConnectionFailed
98
from common.errors import WifiHotspotStartFailed
10-
from common.errors import WifiInvalidConnectionType
119
from common.errors import WifiNoSuitableDevice
10+
from common.nm_dicts import get_nm_dict
1211

1312

1413
# Import DBus mainloop for NetworkManager use
@@ -55,86 +54,8 @@ def connect(conn_type=config.type_hotspot,
5554
password = config.hotspot_password
5655

5756
try:
58-
# Hotspot for user to connect to device
59-
hotspot_dict = {
60-
'802-11-wireless': {'band': 'bg',
61-
'mode': 'ap',
62-
'ssid': config.hotspot_ssid},
63-
'connection': {'autoconnect': False,
64-
'id': config.ap_name,
65-
'interface-name': 'wlan0',
66-
'type': '802-11-wireless',
67-
'uuid': str(uuid.uuid4())},
68-
'ipv4': {'address-data':
69-
[{'address': '192.168.42.1', 'prefix': 24}],
70-
'addresses': [['192.168.42.1', 24, '0.0.0.0']],
71-
'method': 'manual'},
72-
'ipv6': {'method': 'auto'}
73-
}
74-
75-
# Include a key-mgmt string in hotspot if setting a password
76-
if password:
77-
password_key_mgmt = {'802-11-wireless-security':
78-
{'key-mgmt': 'wpa-psk', 'psk': password}}
79-
80-
hotspot_dict.update(password_key_mgmt)
81-
82-
# "MIT SECURE" network.
83-
enterprise_dict = {
84-
'802-11-wireless': {'mode': 'infrastructure',
85-
'security': '802-11-wireless-security',
86-
'ssid': ssid},
87-
'802-11-wireless-security':
88-
{'auth-alg': 'open', 'key-mgmt': 'wpa-eap'},
89-
'802-1x': {'eap': ['peap'],
90-
'identity': username,
91-
'password': password,
92-
'phase2-auth': 'mschapv2'},
93-
'connection': {'id': config.ap_name,
94-
'type': '802-11-wireless',
95-
'uuid': str(uuid.uuid4())},
96-
'ipv4': {'method': 'auto'},
97-
'ipv6': {'method': 'auto'}
98-
}
99-
100-
# No auth/'open' connection.
101-
no_auth_dict = {
102-
'802-11-wireless': {'mode': 'infrastructure',
103-
'ssid': ssid},
104-
'connection': {'id': config.ap_name,
105-
'type': '802-11-wireless',
106-
'uuid': str(uuid.uuid4())},
107-
'ipv4': {'method': 'auto'},
108-
'ipv6': {'method': 'auto'}
109-
}
110-
111-
# Hidden, WEP, WPA, WPA2, password required.
112-
passwd_dict = {
113-
'802-11-wireless': {'mode': 'infrastructure',
114-
'security': '802-11-wireless-security',
115-
'ssid': ssid},
116-
'802-11-wireless-security':
117-
{'key-mgmt': 'wpa-psk', 'psk': password},
118-
'connection': {'id': config.ap_name,
119-
'type': '802-11-wireless',
120-
'uuid': str(uuid.uuid4())},
121-
'ipv4': {'method': 'auto'},
122-
'ipv6': {'method': 'auto'}
123-
}
124-
125-
if conn_type == config.type_hotspot:
126-
conn_dict = hotspot_dict
127-
elif conn_type == config.type_none:
128-
conn_dict = no_auth_dict
129-
elif (conn_type == config.type_wep or
130-
conn_type == config.type_wpa or
131-
conn_type == config.type_wpa2):
132-
conn_dict = passwd_dict
133-
elif conn_type == config.type_enterprise:
134-
conn_dict = enterprise_dict
135-
else:
136-
logger.error("Invalid conn_type.")
137-
raise WifiInvalidConnectionType
57+
# Get the correct config based on type requested
58+
conn_dict = get_nm_dict(conn_type, ssid, username, password)
13859

13960
NetworkManager.Settings.AddConnection(conn_dict)
14061
logger.info(f"Added connection of type {conn_type}")
@@ -191,27 +112,6 @@ def connect(conn_type=config.type_hotspot,
191112
raise WifiConnectionFailed
192113

193114

194-
def dnsmasq():
195-
# Build the list of args
196-
path = "/usr/sbin/dnsmasq"
197-
args = [path]
198-
args.append(f"--address=/#/{config.DEFAULT_GATEWAY}")
199-
args.append(f"--dhcp-range={config.DEFAULT_DHCP_RANGE}")
200-
args.append(f"--dhcp-option=option:router,{config.DEFAULT_GATEWAY}")
201-
args.append(f"--interface={config.DEFAULT_INTERFACE}")
202-
args.append("--keep-in-foreground")
203-
args.append("--bind-dynamic")
204-
args.append("--except-interface=lo")
205-
args.append("--conf-file")
206-
args.append("--no-hosts")
207-
208-
try:
209-
subprocess.Popen(args)
210-
except Exception:
211-
logger.exception('Failed to start dnsmasq.')
212-
# Allow pass to facilitate software updates
213-
214-
215115
def forget(create_new_hotspot=False):
216116
# Find and delete the hotspot connection
217117
try:

src/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import time
55
from common.errors import errors
66
from common.errors import logger
7+
from common.system import dnsmasq
78
from common.wifi import check_wifi_status
8-
from common.wifi import dnsmasq
99
from common.wifi import connect
1010
from common.wifi import refresh_networks
1111
from resources.system_routes import system_health_check

0 commit comments

Comments
 (0)