#!/bin/sh #.Distributed under the terms of the GNU General Public License (GPL) version 2.0 #.Christian Schoenebeck . /lib/functions.sh . /etc/diag.sh . /lib/functions/network.sh #ubus iwinfo . /usr/share/libubox/jshn.sh #from gargoyle /www/ . /usr/lib/autorepeater/scan_wifi.sh UCI_CONFIG_DIR="" SECTION_ID="" VERBOSE_MODE=1 LOGFILE="" PIDFILE="" UPDFILE="" DATFILE="" ERRFILE="" CHECK_SECONDS=0 FORCE_SECONDS=0 RETRY_SECONDS=0 LAST_TIME=0 CURR_TIME=0 NEXT_TIME=0 EPOCH_TIME=0 REGISTERED_IP="" LOCAL_IP="" URL_USER="" URL_PASS="" ERR_LAST=0 ERR_UPDATE=0 PID_SLEEP=0 RUNDIR=$(uci -q get autorepeater.global.run_dir) || RUNDIR="/var/run/autorepeater" [ -d $RUNDIR ] || mkdir -p -m755 $RUNDIR LOGDIR=$(uci -q get autorepeater.global.log_dir) || LOGDIR="/var/log/autorepeater" [ -d $LOGDIR ] || mkdir -p -m755 $LOGDIR LOGLINES=$(uci -q get autorepeater.global.log_lines) || LOGLINES=250 LOGLINES=$((LOGLINES + 1)) DATE_FORMAT=$(uci -q get autorepeater.global.date_format) || DATE_FORMAT="%F %R" DATE_PROG="date +'$DATE_FORMAT'" HTML_LOG=$(uci get autorepeater.global.html_page) || HTML_LOG="autorepeater.html" HTML_LOG="$(uci get uhttpd.main.home)/$HTML_LOG" IPV4_REGEX="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" IPV6_REGEX="\(\([0-9A-Fa-f]\{1,4\}:\)\{1,\}\)\(\([0-9A-Fa-f]\{1,4\}\)\{0,1\}\)\(\(:[0-9A-Fa-f]\{1,4\}\)\{1,\}\)" ERROR_MEM_EMPTY=1 ERROR_MEM_TYPE=2 WARN_DISABLED_KNOWN=3 WARN_WEAK_KNOWN=4 NOT_MATCH_WEAK=5 NOT_MATCH_HEALTH=6 ERROR_NO_CATCH=7 [ "$(basename $0)" = "autorepeater_lucihelper.sh" ] && LUCI_HELPER="TRUE" || LUCI_HELPER="" USE_CURL=$(uci -q get autorepeater.global.use_curl) || USE_CURL=0 [ -x /usr/bin/curl ] || USE_CURL=0 #for rfkill.sh load_wireless() { mode="$(uci get wireless.${1}.mode)" [ "$mode" = "ap" ] || return 0 if [ ! ${disabled+1} ] ; then disabled="$(uci get wireless.${1}.disabled)" if [ "$disabled" = "1" ] then changeto="0" str_led_state="done" else changeto="1" str_led_state="failsafe" fi fi set_wifi $1 $changeto } set_wifi() { [ "$device" = "all" -o "$device" = "${1}" ] && { uci set "wireless.${1}.disabled=${2}" } } reload_wifi() { /sbin/wifi reload } #status_get <_id> __status_get(){ local __tmp [ -z "${DATA}" ] && return 0 __tmp="$(jsonfilter -s "${DATA}" -e "${1}=@.status[@.id='${2}'].${3}")" [ -z "${__tmp}" ] && \ unset "${1}" && \ return 1 eval "${__tmp}" } #update_secs_status #name=LastMap/LastSignal/Realcipher/LastConn #[{"id":"._","name":"type_name","fail":"fail_time","ok":"successful_time"}, #{"bssid":"11:22:33:44:55:66","signal":"60"}, #{"encryption":"scan_encryption"}, #{"fail":"fail_time","ok":"successful_time"}] #update_secs_status "${_key}" "LastConn" "ok" "$(eval ${DATE_PROG})" update_secs_status() { [ "$#" -eq 4 ] || return 0 local section_id="${1}"; shift 1 local type_name="${1}"; shift 1 local item="${1}"; shift 1 local value="${1}"; shift 1 local secs_status_file="${LOGDIR}/secs_status" local DATA="" local ERR_LAST=1 local type _key _id _dsp keys exists #value with dot . not support??? local _id="${section_id}._${type_name}" DATA="$(cat ${secs_status_file})" json_init json_load "${DATA}" json_get_type type "status" ERR_LAST=$? json_select "status" 2>/dev/null if [ "${ERR_LAST}" -ne 0 -o "${type}" != "array" ]; then json_init json_add_array "status" rm -rf "${secs_status_file}" fi #DATA=$(json_dump) #eval $(jsonfilter -s "${DATA}" -F',' -l "5" -e "dsp=@.status[@.section_id='HTTP'].name") #eval $(jsonfilter -s "${DATA}" -e "dsp=@.status[@.section_id='HTTP'].name") #echo $dsp #__status_get "_dsp" "${_id}" "${item}" #if [ -z "${_dsp}" ]; then # json_add_object "" # json_add_string "id" "${_id}" # json_add_string "${item}" "${value}" # json_close_object #else exists=0 json_get_keys keys if [ "${keys}" != "" ]; then for _key in ${keys}; do json_select ${_key} json_get_var "_dsp" "id" if [ "x${_dsp}" = "x${_id}" ]; then exists=1 json_select .. break fi json_select .. done fi if [ "${exists}" -eq 1 ]; then json_select ${_key} json_add_string "${item}" "${value}" json_select .. else json_add_object json_add_string "id" "${_id}" json_add_string "name" "${type_name}" json_add_string "${item}" "${value}" json_close_object fi #fi [ "${ERR_LAST}" -ne 0 -o "${type}" != "array" ] && json_close_array json_dump 2>/dev/null >"${secs_status_file}" #DATA=$(json_dump 2>/dev/null) #jsonfilter -s "${DATA}" -e "$.status" -q >"${secs_status_file}" json_cleanup unset DATA } ##https://github.com/OnionIoT/wifisetup/blob/master/wifisetup.sh #__dev_master_info [name] #__dev_master_info "INFO" "__ifs" "phy" #__dev_master_info "INFO" "__hwmodes" #b g n #if [[ $(list_contains "__hwmodes" "g") ]]; then # g_sta="${cur_dev}" #a n #elif [[ $(list_contains "__hwmodes" "a") ]]; then # a_sta="${cur_dev}" #fi __dev_master_info(){ local __tmp local __info eval "__info=\$${1}" [ -z "${__info}" ] && return 0 if [ -z "${3}" ]; then __tmp="$(jsonfilter -s "${__info}" -e "${2}=$.hwmodes[($.mode='Master')]")" else __tmp="$(jsonfilter -s "${__info}" -e "${2}=$.${3}")" fi [ -z "${__tmp}" ] && \ unset "${2}" && \ return 1 eval "${__tmp}" } get_sta_sec() { local __tmp local __info eval "__info=\$${1}" [ -z "${__info}" ] && return 0 __tmp=$(jsonfilter -s "${__info}" -e "${2}=$.*.interfaces[@.config.mode='sta'].section") [ -z "${__tmp}" ] && \ unset "${2}" && \ return 1 eval "${__tmp}" } get_wifi_info() { local _orig RESP __radios _radio rtype disabled hwmode _ikey _secid _ifname _mode __ret _ret RESP=$(ubus call network.wireless status) json_set_namespace "_dev_info" _orig json_load "${RESP}" json_get_keys __radios for _radio in ${__radios}; do json_get_type rtype "${_radio}" if [ "${rtype}" == "object" ]; then json_select "${_radio}" json_get_vars disabled "disabled" json_select "config" json_get_var hwmode "hwmode" json_select .. json_get_keys __ikeys "interfaces" if [ "${__ikeys}" != "" ]; then json_select "interfaces" for _ikey in ${__ikeys}; do json_select "${_ikey}" json_get_var _secid "section" json_get_var _ifname "ifname" json_select "config" json_get_var _mode "mode" #if [ "${_mode}" == "sta" ]; then __ret="\"${_ifname}\": { \"device\":\"${_radio}\", \"hwmode\":\"${hwmode}\", \"disabled\":${disabled}, \"ifname\":\"${_ifname}\", \"section\":\"${_secid}\", \"mode\":\"${_mode}\" }" if [ -z "${_ret}" ]; then _ret="${__ret}" else _ret="${_ret}, ${__ret}" fi #fi json_select .. #config json_select .. #interfacesx done json_select .. #interfaces fi json_select .. #radios fi done #json_cleanup #json_load "${_ret}" #json_dump 2>/dev/null >"${1}" json_cleanup json_set_namespace "${_orig}" eval "${1}='{ ${_ret} }'" } #pars_wifi_info