|
3 | 3 | import socket
|
4 | 4 | import subprocess
|
5 | 5 | import time
|
6 |
| -import uuid |
7 | 6 | from common.errors import logger
|
8 | 7 | from common.errors import WifiConnectionFailed
|
9 | 8 | from common.errors import WifiHotspotStartFailed
|
10 |
| -from common.errors import WifiInvalidConnectionType |
11 | 9 | from common.errors import WifiNoSuitableDevice
|
| 10 | +from common.nm_dicts import get_nm_dict |
12 | 11 |
|
13 | 12 |
|
14 | 13 | # Import DBus mainloop for NetworkManager use
|
@@ -55,86 +54,8 @@ def connect(conn_type=config.type_hotspot,
|
55 | 54 | password = config.hotspot_password
|
56 | 55 |
|
57 | 56 | 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) |
138 | 59 |
|
139 | 60 | NetworkManager.Settings.AddConnection(conn_dict)
|
140 | 61 | logger.info(f"Added connection of type {conn_type}")
|
@@ -191,27 +112,6 @@ def connect(conn_type=config.type_hotspot,
|
191 | 112 | raise WifiConnectionFailed
|
192 | 113 |
|
193 | 114 |
|
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 |
| - |
215 | 115 | def forget(create_new_hotspot=False):
|
216 | 116 | # Find and delete the hotspot connection
|
217 | 117 | try:
|
|
0 commit comments