forked from tuya-cloudcutter/tuya-cloudcutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
executable file
·128 lines (105 loc) · 4.33 KB
/
common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env bash
AP_MATCHED_NAME=""
AP_CONNECTED_ENDING=""
FIRST_WIFI=$(nmcli device status | grep " wifi " | head -n1 | awk -F ' ' '{print $1}')
if [ "${WIFI_ADAPTER}" == "" ]; then
WIFI_ADAPTER="${FIRST_WIFI}"
fi
if [ "${WIFI_ADAPTER}" == "" ]; then
echo "[!] Unable to auto-detect wifi adapter. Please use the '-w' argument to pass in a wifi adapter."
echo "See '$0 -h' for more information."
exit 1
fi
SUPPORTS_AP=$(nmcli -f wifi-properties device show ${WIFI_ADAPTER} | grep WIFI-PROPERTIES.AP | awk -F ' ' '{print $2}')
# We don't want to hard stop here because localization could lead to false positives, but warn if AP mode does not appear supported.
if [ "${SUPPORTS_AP}" != "yes" ]; then
echo "[!] WARNING: Selected wifi AP support: ${SUPPORTS_AP}"
echo "AP support is manditory for tuya-cloudcutter to work. If this is blank or 'no' your adapter doesn't support this feature."
read -n 1 -s -r -p "Press any key to continue, or CTRL+C to exit"
fi
function run_helper_script () {
if [ -f "scripts/${1}.sh" ]; then
echo "Running helper script '${1}'"
source "scripts/${1}.sh"
fi
}
reset_nm () {
if [ -z ${RESETNM+x} ]; then
return 0
else
echo "Wiping NetworkManager configs"
rm -f /etc/NetworkManager/system-connections/*.nmconnection*
service NetworkManager restart
return 0
fi
}
wifi_connect () {
FIRST_RUN=true
for i in {1..5}
do
AP_MATCHED_NAME=""
# Turn on WiFi, and wait for SSID to show up
reset_nm
sleep 1
service NetworkManager start; nmcli device set ${WIFI_ADAPTER} managed yes # Make sure we turn on managed mode again in case we didn't recover it in the trap below
nmcli radio wifi off
sleep 1
nmcli radio wifi on
while [ "${AP_MATCHED_NAME}" == "" ]
do
if [ ${FIRST_RUN} == true ]; then
echo "Scanning for open Tuya SmartLife AP"
FIRST_RUN=false
else
echo -n "."
fi
RESCAN_ARG="--rescan yes"
if [ "${DISABLE_RESCAN}" == "true" ]; then
RESCAN_ARG=""
fi
# Search for an AP ending with - and 4 hexidecimal characters that has no security mode, unless we've already connected to one, in which case we look for that specific one
SSID_REGEX="-[A-F0-9]{4}"
if [ "${AP_CONNECTED_ENDING}" != "" ]; then
SSID_REGEX="${AP_CONNECTED_ENDING}"
fi
AP_MATCHED_NAME=$(nmcli -t -f SSID,SECURITY dev wifi list ${RESCAN_ARG} ifname ${WIFI_ADAPTER} | grep -E ^.*${SSID_REGEX}:$ | awk -F ':' '{print $1}' | head -n1)
done
echo -e "\nFound access point name: \"${AP_MATCHED_NAME}\", trying to connect..."
nmcli dev wifi connect "${AP_MATCHED_NAME}" ifname ${WIFI_ADAPTER} name "${AP_MATCHED_NAME}"
# Check if successfully connected
# Note, we were previously checking GENERAL.STATE and comparing to != "activated" but that has internationalization problems
# There does not appear to be a numeric status code we can check
# This may need updating if Tuya or one of their sub-vendors ever change their AP mode gateway IP
AP_GATEWAY=$(nmcli -f IP4.GATEWAY con show "${AP_MATCHED_NAME}" | awk -F ' ' '{print $2}' | grep -o 192\.168\.17[65]\.1)
if [ "${AP_GATEWAY}" != "192.168.175.1" ] && [ "${AP_GATEWAY}" != "192.168.176.1" ]; then
if [ "${AP_GATEWAY}" != "" ]; then
echo "Expected AP gateway = 192.168.175.1 or 192.168.176.1 but got ${AP_GATEWAY}"
fi
if [[ "${i}" == "5" ]]; then
echo "Error, could not connect to SSID."
return 1
fi
else
AP_CONNECTED_ENDING=${AP_MATCHED_NAME: -5}
break
fi
sleep 1
done
echo "Connected to access point."
return 0
}
build_docker () {
docker build --network=host -t cloudcutter .
if [ ! $? -eq 0 ]; then
echo "Failed to build Docker image, stopping script"
exit 1
fi
}
run_in_docker () {
docker rm cloudcutter >/dev/null 2>&1
docker run --rm --name cloudcutter --network=host -ti --privileged -v "$(pwd):/work" cloudcutter "${@}"
}
# Docker prep
echo "Building cloudcutter docker image"
build_docker
echo "Successfully built docker image"