-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_functions_ufw.sh
76 lines (70 loc) · 2.22 KB
/
_functions_ufw.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
#!/bin/bash -eu
# Don't load it several times
set +u
${_FUNCTIONS_UFW_LOADED:-false} && return
set -u
# if the script was started from the base directory, then the
# expansion returns a period
if test "${SCRIPT_DIR}" == "."; then
SCRIPT_DIR="$PWD"
# if the script was not called with an absolute path, then we need to add the
# current working directory to the relative path of the script
elif test "${SCRIPT_DIR:0:1}" != "/"; then
SCRIPT_DIR="$PWD/${SCRIPT_DIR}"
fi
# #############################################################################
# Load shared functions
# #############################################################################
source "${SCRIPT_DIR}/_functions_core.sh"
# Open a port in the firewall
# $1 : Port number
# $2 : Port description
# $3 : Dev Mode (warning message instead of error)
do_ufw_open_port(){
local _port=$1
shift
local _description=$1
shift
local _dev_mode=$1
shift
# Open firewall ports
if ! ${_dev_mode}; then
if [ -e /usr/sbin/ufw ]; then
echo_info "Opening firewall port ${_description} (${_port}) ..."
sudo /usr/sbin/ufw allow ${_port}
echo_info "Done."
else
echo_error "/usr/sbin/ufw unavailable. Impossible to open port ${_description} (${_port}). Did you install UFW ?"
fi
else
echo_warn "Development Mode: We don't open firewall port ${_description} (${_port})."
fi
}
# Close a port in the firewall
# $1 : Port number
# $2 : Port description
# $3 : Dev Mode (warning message instead of error)
do_ufw_close_port(){
local _port=$1
shift
local _description=$1
shift
local _dev_mode=$1
shift
# Close firewall ports
if ! ${_dev_mode}; then
if [ -e /usr/sbin/ufw ]; then
echo_info "Closing firewall port ${_description} (${_port}) ..."
sudo /usr/sbin/ufw delete allow ${_port}
echo_info "Done."
else
echo_error "/usr/sbin/ufw unavailable. Impossible to close port ${_description} (${_port}). Did you install UFW ?"
fi
else
echo_warn "Development Mode: We don't close firewall port ${_description} (${_port})."
fi
}
# #############################################################################
# Env var to not load it several times
_FUNCTIONS_UFW_LOADED=true
echo_debug "_functions_ufw.sh Loaded"