b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | |
| 3 | wps_catch_credentials() { |
| 4 | local iface ifaces ifc ifname ssid encryption key radio radios |
| 5 | local found=0 |
| 6 | |
| 7 | . /usr/share/libubox/jshn.sh |
| 8 | ubus -S -t 30 listen wps_credentials | while read creds; do |
| 9 | json_init |
| 10 | json_load "$creds" |
| 11 | json_select wps_credentials || continue |
| 12 | json_get_vars ifname ssid key encryption |
| 13 | local ifcname="$ifname" |
| 14 | json_init |
| 15 | json_load "$(ubus -S call network.wireless status)" |
| 16 | json_get_keys radios |
| 17 | for radio in $radios; do |
| 18 | json_select $radio |
| 19 | json_select interfaces |
| 20 | json_get_keys ifaces |
| 21 | for ifc in $ifaces; do |
| 22 | json_select $ifc |
| 23 | json_get_vars ifname |
| 24 | [ "$ifname" = "$ifcname" ] && { |
| 25 | ubus -S call uci set "{\"config\":\"wireless\", \"type\":\"wifi-iface\", \ |
| 26 | \"match\": { \"device\": \"$radio\", \"encryption\": \"wps\" }, \ |
| 27 | \"values\": { \"encryption\": \"$encryption\", \ |
| 28 | \"ssid\": \"$ssid\", \ |
| 29 | \"key\": \"$key\" } }" |
| 30 | ubus -S call uci commit '{"config": "wireless"}' |
| 31 | ubus -S call uci apply |
| 32 | } |
| 33 | json_select .. |
| 34 | done |
| 35 | json_select .. |
| 36 | json_select .. |
| 37 | done |
| 38 | done |
| 39 | } |
| 40 | |
| 41 | if [ "$ACTION" = "released" ] && [ "$BUTTON" = "wps" ]; then |
| 42 | # If the button was pressed for 3 seconds or more, trigger WPS on |
| 43 | # wpa_supplicant only, no matter if hostapd is running or not. If |
| 44 | # was pressed for less than 3 seconds, try triggering on |
| 45 | # hostapd. If there is no hostapd instance to trigger it on or WPS |
| 46 | # is not enabled on them, trigger it on wpa_supplicant. |
| 47 | if [ "$SEEN" -lt 3 ] ; then |
| 48 | wps_done=0 |
| 49 | ubusobjs="$( ubus -S list hostapd.* )" |
| 50 | for ubusobj in $ubusobjs; do |
| 51 | ubus -S call $ubusobj wps_start && wps_done=1 |
| 52 | done |
| 53 | [ $wps_done = 0 ] || return 0 |
| 54 | fi |
| 55 | wps_done=0 |
| 56 | ubusobjs="$( ubus -S list wpa_supplicant.* )" |
| 57 | for ubusobj in $ubusobjs; do |
| 58 | ifname="$(echo $ubusobj | cut -d'.' -f2 )" |
| 59 | multi_ap="" |
| 60 | if [ -e "/var/run/wpa_supplicant-${ifname}.conf.is_multiap" ]; then |
| 61 | ubus -S call $ubusobj wps_start '{ "multi_ap": true }' && wps_done=1 |
| 62 | else |
| 63 | ubus -S call $ubusobj wps_start && wps_done=1 |
| 64 | fi |
| 65 | done |
| 66 | [ $wps_done = 0 ] || wps_catch_credentials & |
| 67 | fi |
| 68 | |
| 69 | return 0 |