-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathovpn
More file actions
130 lines (114 loc) · 2.95 KB
/
Copy pathovpn
File metadata and controls
130 lines (114 loc) · 2.95 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
# Default path to the OpenVPN configuration file
CONFIG_FILE="/home/user/openvpn/config.ovpn"
# Name of the OpenVPN process
VPN_PROCESS="openvpn"
# VPN IP address for checking the connection
VPN_IP_CHECK="10.10.10.10"
# Timeout for curl command (in seconds)
CURL_TIMEOUT=2
# Default run mode
RUN_MODE="daemon" # Run in background by default
# Function to connect to the VPN
connect() {
if [ "$RUN_MODE" = "daemon" ]; then
sudo $VPN_PROCESS --config "$CONFIG_FILE" --daemon
else
sudo $VPN_PROCESS --config "$CONFIG_FILE"
fi
}
# Function to disconnect from the VPN
disconnect() {
sudo pkill $VPN_PROCESS
}
# Function to restart the VPN
restart() {
disconnect
sleep 5
connect
}
# Function to check VPN status
check_status() {
vpn_ip=$(timeout $CURL_TIMEOUT curl -s $VPN_IP_CHECK/whoami)
if [[ -n $(echo "$vpn_ip" | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b") ]]; then
echo "VPN IP address: $vpn_ip"
echo "VPN status: Connected"
else
echo "VPN status: Disconnected"
fi
}
# Function to update the config file path in this script
update_config_file() {
if [[ $(id -u) -ne 0 ]]; then
echo "Error: Updating config file path requires elevated permissions."
exit 1
fi
if [[ -z "$1" ]]; then
echo "Current config file path: $CONFIG_FILE"
exit 0
else
new_path="$1"
script_path="$0"
sed -i "4s|CONFIG_FILE=.*|CONFIG_FILE=\"$new_path\"|" "$script_path"
echo "Config file path updated to $new_path."
exit 0
fi
}
# Function to display available actions
show_help() {
echo "Usage: ovpn {start|stop|restart|status} [--daemon|-d|--terminal|-t] [--config_file PATH]"
echo " --daemon, -d Run VPN in background mode"
echo " --terminal, -t Run VPN in terminal mode"
echo " --config_file PATH Update path to config file or show current path"
echo " ovpn --help Display this help message"
}
# Main script logic
if [[ $# -eq 0 || $1 == "--help" ]]; then
show_help
exit 0
fi
# Parse options for run mode and config path update
while [[ "$#" -gt 0 ]]; do
case "$1" in
"--daemon"|"-d")
RUN_MODE="daemon"
shift
;;
"--terminal"|"-t")
RUN_MODE="terminal"
shift
;;
"--config_file")
update_config_file "$2"
;;
*)
ACTION="$1"
shift
;;
esac
done
case "$ACTION" in
"start"|"connect")
if pgrep $VPN_PROCESS > /dev/null; then
echo "VPN connection is already active."
exit 1
else
connect
fi
;;
"stop"|"disconnect")
disconnect
;;
"restart")
restart
;;
"status")
check_status
;;
*)
echo "Invalid option: $ACTION"
show_help
exit 1
;;
esac
exit 0