Skip to content

Commit a418179

Browse files
committed
Support variable interface names
1 parent 53e2183 commit a418179

File tree

7 files changed

+33
-42
lines changed

7 files changed

+33
-42
lines changed

README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,15 @@ Alternatively, if you would rather have your backend use specified ports instead
4545

4646
## Changing the default network interface
4747

48-
By default, the first available Wi-Fi network interface available will be used. For the vast majority of cases there is only one Wi-Fi network interface (`wlan0`) and therefore this is no issue. Similarly, if you plug in a Wi-Fi dongle to a device without its own built-in Wi-Fi, the Wi-Fi dongle will be used by default.
48+
By default, the first available Wi-Fi network interface available will be used. For the vast majority of cases there is only one Wi-Fi network interface and therefore this is no issue. Similarly, if you plug in a Wi-Fi dongle to a device without its own built-in Wi-Fi, the Wi-Fi dongle will be used by default.
4949

50-
If however, you have a device with built in Wi-Fi and a Wi-Fi dongle, you will have a device with two network interfaces (usually `wlan0` and `wlan1`). For these instances, or on other occasions where you have a complex network interface setup, you can specify which network interface you would like Py Wi-Fi Connect to use by setting the environment variable shown in the `docker-compose.yml` file:
50+
If however, you have a device with built in Wi-Fi and a Wi-Fi dongle, you will have a device with two network interfaces. For these instances, or on other occasions where you have a complex network interface setup, you can specify which network interface you would like Py Wi-Fi Connect to use by setting the environment variable shown in the `docker-compose.yml` file:
5151

5252
```
5353
PWC_INTERFACE: "wlan0"
5454
```
5555

56-
To allow for automatic detection, set the variable to `auto` or remove the variable from your `docker-compose.yml` file:
57-
58-
```
59-
PWC_INTERFACE: "auto"
60-
```
56+
To allow for automatic detection, remove the variable from your `docker-compose.yml` file.
6157

6258
This setting can also be controlled using the `/set_interface` endpoint.
6359

@@ -233,9 +229,7 @@ Allows setting the hotspot SSID. Using this endpoint will store the passed strin
233229

234230
By default the Wi-Fi network interface is auto-detected. If you need to specify a network interface, you can do so using this endpoint.
235231

236-
To set back to auto-detection, pass `auto` as the value.
237-
238-
Changing the setting will only last until the next restart of the container, when it will resort back to the default setting set by the environment variable in the container or `auto` if there is no environment variable in the container.
232+
Changing the setting will only last until the next restart of the container, when it will resort back to the setting set by the environment variable in the container or detect the interface autocratically if there is no environment variable in the container.
239233

240234
#### POST
241235

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717
#PWC_AC_PASSWORD: "your-password" # Optional. Must be 8 characters or more.
1818

1919
## Wi-Fi Interface ##
20-
#PWC_INTERFACE: "auto"
20+
#PWC_INTERFACE: "wlan0" # By default it automatically detects the interface.
2121

2222
## Enable/Disable LED interaction ##
2323
#PWC_LED: "on"

src/common/nm_dicts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_nm_dict(conn_type, ssid, username, password):
1616
"connection": {
1717
"autoconnect": False,
1818
"id": config.ap_name,
19-
"interface-name": "wlan0",
19+
"interface-name": config.interface,
2020
"type": "802-11-wireless",
2121
"uuid": str(uuid.uuid4()),
2222
},

src/common/system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def dnsmasq():
1111
f"--address=/#/{config.DEFAULT_GATEWAY}",
1212
f"--dhcp-range={config.DEFAULT_DHCP_RANGE}",
1313
f"--dhcp-option=option:router,{config.DEFAULT_GATEWAY}",
14-
f"--interface={config.DEFAULT_INTERFACE}",
14+
f"--interface={config.interface}",
1515
"--keep-in-foreground",
1616
"--bind-dynamic",
1717
"--except-interface=lo",

src/common/wifi.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import config
22
import NetworkManager as Pnm # Python NetworkManager
3+
import os
34
import socket
45
import subprocess
56
import time
@@ -94,7 +95,9 @@ def check_internet_status(host="8.8.8.8", port=53, timeout=5):
9495
def check_wifi_status():
9596
try:
9697
run = subprocess.run(
97-
["iw", "dev", "wlan0", "link"], capture_output=True, text=True
98+
["iw", "dev", config.interface, "link"],
99+
capture_output=True,
100+
text=True,
98101
).stdout.rstrip()
99102
except Exception:
100103
logger.exception(
@@ -131,7 +134,7 @@ def connect(
131134
if conn_type != config.type_hotspot:
132135
logger.info(f"Attempting connection to {ssid}")
133136

134-
# Wait for ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
137+
# Wait for ADDRCONF(NETDEV_CHANGE): link becomes ready
135138
loop_count = 0
136139
while not check_device_state():
137140
time.sleep(1)
@@ -224,16 +227,16 @@ def get_connection_id():
224227

225228
def get_device():
226229
# Configured interface variable takes precedent.
227-
if config.interface.lower() != config.auto_interface:
228-
logger.debug(f"Interface {config.interface} selected.")
230+
if "PWC_INTERFACE" in os.environ:
231+
logger.debug(f"Interface {os.environ['PWC_INTERFACE']} selected.")
229232
for device in Pnm.NetworkManager.GetDevices():
230233
if device.DeviceType != Pnm.NM_DEVICE_TYPE_WIFI:
231234
continue
232235
# For each Wi-Fi network interface, check the interface name
233236
# against the one configured in config.interface
234237
if (
235238
device.Udi[device.Udi.rfind("/") + 1 :].lower()
236-
== config.interface.lower()
239+
== os.environ["PWC_INTERFACE"]
237240
):
238241
return device
239242

@@ -302,7 +305,7 @@ def refresh_networks(retries=5):
302305
while run < max_runs:
303306
try:
304307
time.sleep(3)
305-
subprocess.check_output(["iw", "dev", "wlan0", "scan"])
308+
subprocess.check_output(["iw", "dev", config.interface, "scan"])
306309
except subprocess.CalledProcessError:
307310
logger.warning("IW resource busy. Retrying...")
308311
continue
@@ -316,7 +319,7 @@ def refresh_networks(retries=5):
316319
run += 1
317320

318321
logger.warning(
319-
"IW is not accessible. This can happen on some devices "
322+
"IW is unable to complete the request. This can happen on some devices "
320323
"and is usually nothing to worry about."
321324
)
322325
return False

src/config.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import os
22
from dotenv import dotenv_values
33

4+
# Set dev env variables
5+
if (
6+
"FLASK_ENV" in os.environ
7+
and os.environ["FLASK_ENV"].lower() == "production"
8+
):
9+
dev_mode = False
10+
else:
11+
dev_mode = True
12+
413
# Check db directory exists
514
if not os.path.exists("db"):
615
os.makedirs("db")
@@ -49,24 +58,13 @@
4958
else:
5059
auto_connect_kargs = False
5160

52-
# Set default interface
53-
auto_interface = "auto"
54-
if (
55-
"PWC_INTERFACE" in os.environ
56-
and os.environ["PWC_INTERFACE"].lower() != auto_interface
57-
):
58-
interface = os.environ["PWC_INTERFACE"]
59-
else:
60-
interface = auto_interface
61-
6261
# Default access point name. No need to change these under usual operation as
6362
# they are for use inside the app only. PWC is acronym for 'Py Wi-Fi Connect'.
6463
ap_name = "PWC"
6564

6665
# dnsmasq variables
6766
DEFAULT_GATEWAY = "192.168.42.1"
6867
DEFAULT_DHCP_RANGE = "192.168.42.2,192.168.42.254"
69-
DEFAULT_INTERFACE = "wlan0" # use 'ip link show' to see list of interfaces
7068

7169
# Wi-Fi modes. No need to rename these, they are used only as labels.
7270
type_hotspot = "HOTSPOT"
@@ -75,12 +73,3 @@
7573
type_wpa = "WPA"
7674
type_wpa2 = "WPA2"
7775
type_enterprise = "ENTERPRISE"
78-
79-
# Set dev env variables
80-
if (
81-
"FLASK_ENV" in os.environ
82-
and os.environ["FLASK_ENV"].lower() == "production"
83-
):
84-
dev_mode = False
85-
else:
86-
dev_mode = True

src/run.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import config
2+
import os
23
import time
34
from common.errors import errors
45
from common.errors import logger
@@ -8,6 +9,7 @@
89
from common.wifi import check_device_state
910
from common.wifi import check_wifi_status
1011
from common.wifi import connect
12+
from common.wifi import get_device
1113
from common.wifi import refresh_networks
1214
from config import host
1315
from config import port
@@ -24,6 +26,9 @@
2426
from resources.wifi_routes import wifi_set_interface
2527
from waitress import serve
2628

29+
# Set default interface
30+
device = get_device()
31+
config.interface = device.Udi[device.Udi.rfind("/") + 1 :].lower()
2732

2833
# Create Flask app instance
2934
app = Flask(__name__)
@@ -44,8 +49,8 @@
4449
time.sleep(10)
4550

4651
# Log interface status
47-
if config.interface.lower() != config.auto_interface:
48-
logger.info(f"Interface set to {config.interface}")
52+
if "PWC_INTERFACE" in os.environ:
53+
logger.info(f"Interface set to {os.environ['PWC_INTERFACE']}")
4954

5055
# If the Wi-Fi connection or device is already active, do nothing
5156
if check_wifi_status() or check_device_state():

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy