You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
1.8 KiB
83 lines
1.8 KiB
3 years ago
|
#!/bin/bash
|
||
|
|
||
|
set -e
|
||
|
set -u
|
||
|
set -f
|
||
|
set -o pipefail
|
||
|
|
||
|
echo_err(){
|
||
|
echo "$@" 1>&2
|
||
|
}
|
||
|
|
||
|
#readonly dir="${XDG_CONFIG_HOME:-$HOME/.config}"
|
||
|
readonly dir="/etc"
|
||
|
mkdir -p "$dir"
|
||
|
readonly config="$dir/doskastrc"
|
||
|
|
||
|
_zenity(){
|
||
|
zenity --width=250 --title="Doskast" "$@"
|
||
|
}
|
||
|
|
||
|
# $1: path
|
||
|
_create_config(){
|
||
|
if ! echo 'board=' > "$1"; then
|
||
|
_zenity --error --text="Ошибка при создании пустого конфига"
|
||
|
return 1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
# $1: IP
|
||
|
_write_systemd_config(){
|
||
|
mkdir -p /etc/systemd/system/doskast-student.service.d
|
||
|
cat > /etc/systemd/system/doskast-student.service.d/doskast-student-firewall.conf << EOF
|
||
|
[Service]
|
||
|
IPAddressDeny=any
|
||
|
# IP-адрес доски
|
||
|
IPAddressAllow=$1
|
||
|
EOF
|
||
|
}
|
||
|
|
||
|
corrupted=0
|
||
|
board=""
|
||
|
if ! test -f "$config"; then
|
||
|
echo_err "No config $config, creating it"
|
||
|
_create_config "$config"
|
||
|
else
|
||
|
if ! . "$config"; then
|
||
|
corrupted=1
|
||
|
fi
|
||
|
if test -z "${board:-}"; then
|
||
|
corrupted=1
|
||
|
fi
|
||
|
if [ "$corrupted" = 1 ]; then
|
||
|
echo_err "Config file $config is corrupted, recreating it..."
|
||
|
_create_config "$config"
|
||
|
fi
|
||
|
fi
|
||
|
|
||
|
# TODO: validate IP
|
||
|
if ! answer="$(zenity \
|
||
|
--entry \
|
||
|
--title="Doskast" \
|
||
|
--text="Введите IP-адрес доски:" \
|
||
|
--entry-text "$board"
|
||
|
)"; then
|
||
|
_zenity --error --text="Произошла неизвестная ошибка."
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
if test -z "$answer" ; then
|
||
|
zenity --title="Doskast" --error --text="Введено пустое знаничение. Попробуйте заново."
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
if ! _write_systemd_config "$answer"; then
|
||
|
_zenity --error --text="Ошибка при сохранении настроек eBPF-фаерволирования."
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
if echo board="$answer" > "$config"; then
|
||
|
# жаба, чтобы был нулевой код возврата при нажатии "Отмена"
|
||
|
_zenity --info --text="Настройки сохранены" || :
|
||
|
fi
|