openfortivpn: tabify shell scripts
Signed-off-by: Aaron Goodman <aaronjg@stanford.edu>
This commit is contained in:
parent
f01714a250
commit
7d170b8f64
2 changed files with 121 additions and 121 deletions
|
@ -14,16 +14,16 @@ config=$1; shift
|
||||||
killed=0
|
killed=0
|
||||||
|
|
||||||
trap_with_arg() {
|
trap_with_arg() {
|
||||||
func="$1" ; shift
|
func="$1" ; shift
|
||||||
for sig ; do
|
for sig ; do
|
||||||
trap "$func $sig" "$sig"
|
trap "$func $sig" "$sig"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
func_trap() {
|
func_trap() {
|
||||||
logger "openfortivpn-wrapper[$$]" "$config: sending signal ${1}"
|
logger "openfortivpn-wrapper[$$]" "$config: sending signal ${1}"
|
||||||
killed=1
|
killed=1
|
||||||
kill -${1} $child 2>/dev/null
|
kill -${1} $child 2>/dev/null
|
||||||
}
|
}
|
||||||
|
|
||||||
trap_with_arg func_trap INT TERM KILL
|
trap_with_arg func_trap INT TERM KILL
|
||||||
|
@ -33,11 +33,11 @@ start_time=$(date '+%s')
|
||||||
/usr/sbin/openfortivpn "$@" < $pwfile 2>/dev/null &
|
/usr/sbin/openfortivpn "$@" < $pwfile 2>/dev/null &
|
||||||
child=$!
|
child=$!
|
||||||
wait $child || {
|
wait $child || {
|
||||||
[ "$killed" = 1 ] && exit 0
|
[ "$killed" = 1 ] && exit 0
|
||||||
current_time=$(date '+%s')
|
current_time=$(date '+%s')
|
||||||
elapsed=$(($current_time-$start_time))
|
elapsed=$(($current_time-$start_time))
|
||||||
. /lib/netifd/netifd-proto.sh
|
. /lib/netifd/netifd-proto.sh
|
||||||
proto_notify_error "$config" "Failed to connect after $elapsed seconds."
|
proto_notify_error "$config" "Failed to connect after $elapsed seconds."
|
||||||
proto_block_restart "$config"
|
proto_block_restart "$config"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,23 +6,23 @@ init_proto "$@"
|
||||||
IPv4_REGEX="((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
|
IPv4_REGEX="((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
|
||||||
|
|
||||||
append_args() {
|
append_args() {
|
||||||
while [ $# -gt 0 ]; do
|
while [ $# -gt 0 ]; do
|
||||||
append cmdline "'${1//\'/\'\\\'\'}'"
|
append cmdline "'${1//\'/\'\\\'\'}'"
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
proto_openfortivpn_init_config() {
|
proto_openfortivpn_init_config() {
|
||||||
proto_config_add_string "peeraddr"
|
proto_config_add_string "peeraddr"
|
||||||
proto_config_add_int "port"
|
proto_config_add_int "port"
|
||||||
proto_config_add_string "tunlink"
|
proto_config_add_string "tunlink"
|
||||||
proto_config_add_string "local_ip"
|
proto_config_add_string "local_ip"
|
||||||
proto_config_add_string "username"
|
proto_config_add_string "username"
|
||||||
proto_config_add_string "password"
|
proto_config_add_string "password"
|
||||||
proto_config_add_string "trusted_cert"
|
proto_config_add_string "trusted_cert"
|
||||||
proto_config_add_string "remote_status_check"
|
proto_config_add_string "remote_status_check"
|
||||||
no_device=1
|
no_device=1
|
||||||
available=1
|
available=1
|
||||||
}
|
}
|
||||||
|
|
||||||
proto_openfortivpn_setup() {
|
proto_openfortivpn_setup() {
|
||||||
|
@ -31,110 +31,110 @@ proto_openfortivpn_setup() {
|
||||||
local msg ifname ip server_ips pwfile callfile
|
local msg ifname ip server_ips pwfile callfile
|
||||||
|
|
||||||
local host peeraddr port tunlink local_ip username password trusted_cert \
|
local host peeraddr port tunlink local_ip username password trusted_cert \
|
||||||
remote_status_check
|
remote_status_check
|
||||||
json_get_vars host peeraddr port tunlink local_ip username password trusted_cert \
|
json_get_vars host peeraddr port tunlink local_ip username password trusted_cert \
|
||||||
remote_status_check
|
remote_status_check
|
||||||
|
|
||||||
ifname="vpn-$config"
|
ifname="vpn-$config"
|
||||||
|
|
||||||
|
|
||||||
[ -n "$tunlink" ] && {
|
[ -n "$tunlink" ] && {
|
||||||
network_get_device iface_device_name "$tunlink"
|
network_get_device iface_device_name "$tunlink"
|
||||||
network_is_up "$tunlink" || {
|
network_is_up "$tunlink" || {
|
||||||
msg="$tunlink is not up $iface_device_up"
|
msg="$tunlink is not up $iface_device_up"
|
||||||
logger -t "openfortivpn" "$config: $msg"
|
logger -t "openfortivpn" "$config: $msg"
|
||||||
proto_notify_error "$config" "$msg"
|
proto_notify_error "$config" "$msg"
|
||||||
proto_block_restart "$config"
|
proto_block_restart "$config"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if echo "$peeraddr" | grep -q -E "$IPv4_REGEX"; then
|
if echo "$peeraddr" | grep -q -E "$IPv4_REGEX"; then
|
||||||
server_ips="$peeraddr"
|
server_ips="$peeraddr"
|
||||||
elif command -v resolveip >/dev/null ; then
|
elif command -v resolveip >/dev/null ; then
|
||||||
server_ips="$(resolveip -4 -t 10 "$peeraddr")"
|
server_ips="$(resolveip -4 -t 10 "$peeraddr")"
|
||||||
[ $? -eq 0 ] || {
|
[ $? -eq 0 ] || {
|
||||||
msg="$config: failed to resolve server ip for $peeraddr"
|
msg="$config: failed to resolve server ip for $peeraddr"
|
||||||
logger -t "openfortivpn" "$msg"
|
logger -t "openfortivpn" "$msg"
|
||||||
sleep 10
|
sleep 10
|
||||||
proto_notify_error "$config" "$msg"
|
proto_notify_error "$config" "$msg"
|
||||||
proto_setup_failed "$config"
|
proto_setup_failed "$config"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
logger -t "openfortivpn" "resolveip not present, could not resolve $peeraddr"
|
logger -t "openfortivpn" "resolveip not present, could not resolve $peeraddr"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
[ "$remote_status_check" = "curl" ] && {
|
[ "$remote_status_check" = "curl" ] && {
|
||||||
curl -k --head -s --connect-timeout 10 ${tunlink:+--interface} $iface_device_name https://$peeraddr > /dev/null || {
|
curl -k --head -s --connect-timeout 10 ${tunlink:+--interface} $iface_device_name https://$peeraddr > /dev/null || {
|
||||||
msg="failed to reach https://$peeraddr${tunlink:+ on $iface_device_name}"
|
msg="failed to reach https://$peeraddr${tunlink:+ on $iface_device_name}"
|
||||||
logger -t "openfortivpn" "$config: $msg"
|
logger -t "openfortivpn" "$config: $msg"
|
||||||
sleep 10
|
sleep 10
|
||||||
proto_notify_error "$config" "$msg"
|
proto_notify_error "$config" "$msg"
|
||||||
proto_setup_failed "$config"
|
proto_setup_failed "$config"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
[ "$remote_status_check" = "ping" ] && {
|
[ "$remote_status_check" = "ping" ] && {
|
||||||
ping ${tunlink:+-I} $iface_device_name -c 1 -w 10 $peeraddr > /dev/null 2>&1 || {
|
ping ${tunlink:+-I} $iface_device_name -c 1 -w 10 $peeraddr > /dev/null 2>&1 || {
|
||||||
msg="$config: failed to ping $peeraddr on $iface_device_name"
|
msg="$config: failed to ping $peeraddr on $iface_device_name"
|
||||||
logger -t "openfortvpn" "$config: $msg"
|
logger -t "openfortvpn" "$config: $msg"
|
||||||
sleep 10
|
sleep 10
|
||||||
proto_notify_error "$config" "$msg"
|
proto_notify_error "$config" "$msg"
|
||||||
proto_setup_failed "$config"
|
proto_setup_failed "$config"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ -n "$server_ips" ]; then
|
if [ -n "$server_ips" ]; then
|
||||||
for ip in $server_ips; do
|
for ip in $server_ips; do
|
||||||
logger -p 6 -t "openfortivpn" "$config: adding host dependency for $ip on $tunlink at $config"
|
logger -p 6 -t "openfortivpn" "$config: adding host dependency for $ip on $tunlink at $config"
|
||||||
proto_add_host_dependency "$config" "$ip" "$tunlink"
|
proto_add_host_dependency "$config" "$ip" "$tunlink"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# uclient-fetch cannot bind to interface, so perform check after adding host dependency
|
# uclient-fetch cannot bind to interface, so perform check after adding host dependency
|
||||||
[ "$remote_status_check" = "fetch" ] && {
|
[ "$remote_status_check" = "fetch" ] && {
|
||||||
uclient-fetch --no-check-certificate -q -s --timeout=10 https://$peeraddr > /dev/null 2>&1 || {
|
uclient-fetch --no-check-certificate -q -s --timeout=10 https://$peeraddr > /dev/null 2>&1 || {
|
||||||
msg="$config: failed to reach ${server_ip:-$peeraddr} on $iface_device_name"
|
msg="$config: failed to reach ${server_ip:-$peeraddr} on $iface_device_name"
|
||||||
logger -t "openfortvpn" "$config: $msg"
|
logger -t "openfortvpn" "$config: $msg"
|
||||||
sleep 10
|
sleep 10
|
||||||
proto_notify_error "$config" "$msg"
|
proto_notify_error "$config" "$msg"
|
||||||
proto_setup_failed "$config"
|
proto_setup_failed "$config"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[ -n "$port" ] && port=":$port"
|
[ -n "$port" ] && port=":$port"
|
||||||
append_args "$peeraddr$port" --pppd-ifname="$ifname" --use-syslog -c /dev/null
|
append_args "$peeraddr$port" --pppd-ifname="$ifname" --use-syslog -c /dev/null
|
||||||
append_args "--set-dns=0"
|
append_args "--set-dns=0"
|
||||||
append_args "--no-routes"
|
append_args "--no-routes"
|
||||||
append_args "--pppd-use-peerdns=1"
|
append_args "--pppd-use-peerdns=1"
|
||||||
|
|
||||||
[ -n "$tunlink" ] && {
|
[ -n "$tunlink" ] && {
|
||||||
append_args "--ifname=$iface_device_name"
|
append_args "--ifname=$iface_device_name"
|
||||||
}
|
}
|
||||||
|
|
||||||
[ -n "$trusted_cert" ] && append_args "--trusted-cert=$trusted_cert"
|
[ -n "$trusted_cert" ] && append_args "--trusted-cert=$trusted_cert"
|
||||||
[ -n "$username" ] && append_args -u "$username"
|
[ -n "$username" ] && append_args -u "$username"
|
||||||
[ -n "$password" ] && {
|
[ -n "$password" ] && {
|
||||||
umask 077
|
umask 077
|
||||||
mkdir -p '/var/etc/openfortivpn'
|
mkdir -p '/var/etc/openfortivpn'
|
||||||
pwfile="/var/etc/openfortivpn/$config.passwd"
|
pwfile="/var/etc/openfortivpn/$config.passwd"
|
||||||
echo "$password" > "$pwfile"
|
echo "$password" > "$pwfile"
|
||||||
}
|
}
|
||||||
|
|
||||||
[ -n "$local_ip" ] || local_ip=192.0.2.1
|
[ -n "$local_ip" ] || local_ip=192.0.2.1
|
||||||
[ -e '/etc/ppp/peers' ] || mkdir -p '/etc/ppp/peers'
|
[ -e '/etc/ppp/peers' ] || mkdir -p '/etc/ppp/peers'
|
||||||
[ -e '/etc/ppp/peers/openfortivpn' ] || {
|
[ -e '/etc/ppp/peers/openfortivpn' ] || {
|
||||||
ln -s -T '/var/etc/openfortivpn/peers' '/etc/ppp/peers/openfortivpn' 2> /dev/null
|
ln -s -T '/var/etc/openfortivpn/peers' '/etc/ppp/peers/openfortivpn' 2> /dev/null
|
||||||
mkdir -p '/var/etc/openfortivpn/peers'
|
mkdir -p '/var/etc/openfortivpn/peers'
|
||||||
}
|
}
|
||||||
|
|
||||||
callfile="/var/etc/openfortivpn/peers/$config"
|
callfile="/var/etc/openfortivpn/peers/$config"
|
||||||
echo "115200
|
echo "115200
|
||||||
:$local_ip
|
:$local_ip
|
||||||
noipdefault
|
noipdefault
|
||||||
noaccomp
|
noaccomp
|
||||||
|
@ -148,22 +148,22 @@ lcp-max-configure 40
|
||||||
ip-up-script /lib/netifd/openfortivpn-ppp-up
|
ip-up-script /lib/netifd/openfortivpn-ppp-up
|
||||||
ip-down-script /lib/netifd/ppp-down
|
ip-down-script /lib/netifd/ppp-down
|
||||||
mru 1354" > $callfile
|
mru 1354" > $callfile
|
||||||
append_args "--pppd-call=openfortivpn/$config"
|
append_args "--pppd-call=openfortivpn/$config"
|
||||||
|
|
||||||
logger -p 6 -t openfortivpn "$config: executing 'openfortivpn $cmdline'"
|
logger -p 6 -t openfortivpn "$config: executing 'openfortivpn $cmdline'"
|
||||||
eval "proto_run_command '$config' /usr/sbin/openfortivpn-wrapper '$pwfile' '$config' $cmdline"
|
eval "proto_run_command '$config' /usr/sbin/openfortivpn-wrapper '$pwfile' '$config' $cmdline"
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
proto_openfortivpn_teardown() {
|
proto_openfortivpn_teardown() {
|
||||||
local config="$1"
|
local config="$1"
|
||||||
|
|
||||||
pwfile="/var/etc/openfortivpn/$config.passwd"
|
pwfile="/var/etc/openfortivpn/$config.passwd"
|
||||||
callfile="/var/etc/openfortivpn/peers/$config"
|
callfile="/var/etc/openfortivpn/peers/$config"
|
||||||
|
|
||||||
rm -f $pwfile
|
rm -f $pwfile
|
||||||
rm -f $callfile
|
rm -f $callfile
|
||||||
proto_kill_command "$config" 2
|
proto_kill_command "$config" 2
|
||||||
}
|
}
|
||||||
|
|
||||||
add_protocol openfortivpn
|
add_protocol openfortivpn
|
||||||
|
|
Loading…
Reference in a new issue