diff --git a/openvpn/start.sh b/openvpn/start.sh index c6166bc..4a40cde 100755 --- a/openvpn/start.sh +++ b/openvpn/start.sh @@ -7,14 +7,14 @@ check_network=$(ifconfig | grep docker0 || true) # if network interface docker0 is present then we are running in host mode and thus must exit if [[ ! -z "${check_network}" ]]; then - echo "[crit] Network type detected as 'Host', this will cause major issues, please stop the container and switch back to 'Bridge' mode" | ts '%Y-%m-%d %H:%M:%.S' && exit 1 + echo "[crit] Network type detected as 'Host', this will cause major issues, please stop the container and switch back to 'Bridge' mode" && exit 1 fi export VPN_ENABLED=$(echo "${VPN_ENABLED}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${VPN_ENABLED}" ]]; then - echo "[info] VPN_ENABLED defined as '${VPN_ENABLED}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN_ENABLED defined as '${VPN_ENABLED}'" else - echo "[warn] VPN_ENABLED not defined,(via -e VPN_ENABLED), defaulting to 'yes'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [warn] VPN_ENABLED not defined,(via -e VPN_ENABLED), defaulting to 'yes'" export VPN_ENABLED="yes" fi @@ -29,7 +29,7 @@ if [[ $VPN_ENABLED == "yes" ]]; then exit_code_chmod=$? set -e if (( ${exit_code_chown} != 0 || ${exit_code_chmod} != 0 )); then - echo "[warn] Unable to chown/chmod /config/openvpn/, assuming SMB mountpoint" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [warn] Unable to chown/chmod /config/openvpn/, assuming SMB mountpoint" fi # wildcard search for openvpn config files (match on first result) @@ -37,9 +37,9 @@ if [[ $VPN_ENABLED == "yes" ]]; then # if ovpn file not found in /config/openvpn then exit if [[ -z "${VPN_CONFIG}" ]]; then - echo "[crit] No OpenVPN config file located in /config/openvpn/ (ovpn extension), please download from your VPN provider and then restart this container, exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1 + echo "[crit] No OpenVPN config file located in /config/openvpn/ (ovpn extension), please download from your VPN provider and then restart this container, exiting..." && exit 1 fi - echo "[info] OpenVPN config file (ovpn extension) is located at ${VPN_CONFIG}" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] OpenVPN config file (ovpn extension) is located at ${VPN_CONFIG}" # convert CRLF (windows) to LF (unix) for ovpn /usr/bin/dos2unix "${VPN_CONFIG}" 1> /dev/null @@ -47,32 +47,32 @@ if [[ $VPN_ENABLED == "yes" ]]; then # parse values from ovpn file export vpn_remote_line=$(cat "${VPN_CONFIG}" | grep -P -o -m 1 '(?<=^remote\s)[^\n\r]+' | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${vpn_remote_line}" ]]; then - echo "[info] VPN remote line defined as '${vpn_remote_line}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN remote line defined as '${vpn_remote_line}'" else - echo "[crit] VPN configuration file ${VPN_CONFIG} does not contain 'remote' line, showing contents of file before exit..." | ts '%Y-%m-%d %H:%M:%.S' + echo "[crit] VPN configuration file ${VPN_CONFIG} does not contain 'remote' line, showing contents of file before exit..." cat "${VPN_CONFIG}" && exit 1 fi export VPN_REMOTE=$(echo "${vpn_remote_line}" | grep -P -o -m 1 '^[^\s\r\n]+' | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${VPN_REMOTE}" ]]; then - echo "[info] VPN_REMOTE defined as '${VPN_REMOTE}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN_REMOTE defined as '${VPN_REMOTE}'" else - echo "[crit] VPN_REMOTE not found in ${VPN_CONFIG}, exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1 + echo "[crit] VPN_REMOTE not found in ${VPN_CONFIG}, exiting..." && exit 1 fi export VPN_PORT=$(echo "${vpn_remote_line}" | grep -P -o -m 1 '(?<=\s)\d{2,5}(?=\s)?+' | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${VPN_PORT}" ]]; then - echo "[info] VPN_PORT defined as '${VPN_PORT}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN_PORT defined as '${VPN_PORT}'" else - echo "[crit] VPN_PORT not found in ${VPN_CONFIG}, exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1 + echo "[crit] VPN_PORT not found in ${VPN_CONFIG}, exiting..." && exit 1 fi export VPN_PROTOCOL=$(cat "${VPN_CONFIG}" | grep -P -o -m 1 '(?<=^proto\s)[^\r\n]+' | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${VPN_PROTOCOL}" ]]; then - echo "[info] VPN_PROTOCOL defined as '${VPN_PROTOCOL}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN_PROTOCOL defined as '${VPN_PROTOCOL}'" else export VPN_PROTOCOL=$(echo "${vpn_remote_line}" | grep -P -o -m 1 'udp|tcp-client|tcp$' | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${VPN_PROTOCOL}" ]]; then - echo "[info] VPN_PROTOCOL defined as '${VPN_PROTOCOL}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN_PROTOCOL defined as '${VPN_PROTOCOL}'" else - echo "[warn] VPN_PROTOCOL not found in ${VPN_CONFIG}, assuming udp" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [warn] VPN_PROTOCOL not found in ${VPN_CONFIG}, assuming udp" export VPN_PROTOCOL="udp" fi fi @@ -85,33 +85,33 @@ if [[ $VPN_ENABLED == "yes" ]]; then VPN_DEVICE_TYPE=$(cat "${VPN_CONFIG}" | grep -P -o -m 1 '(?<=^dev\s)[^\r\n\d]+' | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${VPN_DEVICE_TYPE}" ]]; then export VPN_DEVICE_TYPE="${VPN_DEVICE_TYPE}0" - echo "[info] VPN_DEVICE_TYPE defined as '${VPN_DEVICE_TYPE}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN_DEVICE_TYPE defined as '${VPN_DEVICE_TYPE}'" else - echo "[crit] VPN_DEVICE_TYPE not found in ${VPN_CONFIG}, exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1 + echo "[crit] VPN_DEVICE_TYPE not found in ${VPN_CONFIG}, exiting..." && exit 1 fi # get values from env vars as defined by user export LAN_NETWORK=$(echo "${LAN_NETWORK}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${LAN_NETWORK}" ]]; then - echo "[info] LAN_NETWORK defined as '${LAN_NETWORK}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] LAN_NETWORK defined as '${LAN_NETWORK}'" else - echo "[crit] LAN_NETWORK not defined (via -e LAN_NETWORK), exiting..." | ts '%Y-%m-%d %H:%M:%.S' && exit 1 + echo "[crit] LAN_NETWORK not defined (via -e LAN_NETWORK), exiting..." && exit 1 fi export NAME_SERVERS=$(echo "${NAME_SERVERS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${NAME_SERVERS}" ]]; then - echo "[info] NAME_SERVERS defined as '${NAME_SERVERS}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] NAME_SERVERS defined as '${NAME_SERVERS}'" else - echo "[warn] NAME_SERVERS not defined (via -e NAME_SERVERS), defaulting to Google and FreeDNS name servers" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [warn] NAME_SERVERS not defined (via -e NAME_SERVERS), defaulting to Google and FreeDNS name servers" export NAME_SERVERS="8.8.8.8,37.235.1.174,8.8.4.4,37.235.1.177" fi export VPN_OPTIONS=$(echo "${VPN_OPTIONS}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') if [[ ! -z "${VPN_OPTIONS}" ]]; then - echo "[info] VPN_OPTIONS defined as '${VPN_OPTIONS}'" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN_OPTIONS defined as '${VPN_OPTIONS}'" else - echo "[info] VPN_OPTIONS not defined (via -e VPN_OPTIONS)" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] VPN_OPTIONS not defined (via -e VPN_OPTIONS)" export VPN_OPTIONS="" fi elif [[ $VPN_ENABLED == "no" ]]; then - echo "[warn] !!IMPORTANT!! You have set the VPN to disabled, you will NOT be secure!" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [warn] !!IMPORTANT!! You have set the VPN to disabled, you will NOT be secure!" fi # split comma seperated string into list from NAME_SERVERS env variable @@ -123,23 +123,23 @@ for name_server_item in "${name_server_list[@]}"; do # strip whitespace from start and end of lan_network_item name_server_item=$(echo "${name_server_item}" | sed -e 's~^[ \t]*~~;s~[ \t]*$~~') - echo "[info] Adding ${name_server_item} to resolv.conf" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] Adding ${name_server_item} to resolv.conf" echo "nameserver ${name_server_item}" >> /etc/resolv.conf done if [[ -z "${PUID}" ]]; then - echo "[info] PUID not defined. Defaulting to root user" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] PUID not defined. Defaulting to root user" export PUID="root" fi if [[ -z "${PGID}" ]]; then - echo "[info] PGID not defined. Defaulting to root group" | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] PGID not defined. Defaulting to root group" export PGID="root" fi if [[ $VPN_ENABLED == "yes" ]]; then - echo "[info] Starting OpenVPN..." | ts '%Y-%m-%d %H:%M:%.S' + echo "$(date) [info] Starting OpenVPN..." cd /config/openvpn exec openvpn --config ${VPN_CONFIG} & #exec /bin/bash /etc/openvpn/openvpn.init start &