-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpn.sh
executable file
·140 lines (116 loc) · 4.4 KB
/
vpn.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
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
131
132
133
134
135
136
137
138
139
140
#!/bin/env bash
#
# vpn.sh - A script to manage OpenVPN connections on EC2
#
# Copyright (C) 2024 Javed Habib (jaeaeich) <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Determine the location of the .vpnrc file
if [ -d "$HOME/.config" ] && [ -f "$HOME/.config/vpn/.vpnrc" ]; then
VPNRC_FILE="$HOME/.config/vpn/.vpnrc"
source "$VPNRC_FILE"
elif [ -f "$HOME/.vpnrc" ]; then
VPNRC_FILE="$HOME/.vpnrc"
source "$VPNRC_FILE"
else
echo "Info: .vpnrc file not found in either ~/.config/vpn/ or ~/"
echo "Proceeding with default values.."
# Default configuration
REGION="ap-southeast-1"
PROFILE_NAME="client.ovpn"
INSTANCE_NAME="openvpn"
USERNAME=openvpn
PASSWORD=my_password
fi
# Path to ovpn profile
CURR_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
PATH_TO_OVPN_CLIENT_CONFIG="$CURR_DIR/$PROFILE_NAME"
# Helper function to get instance state
get_instance_state() {
aws ec2 describe-instances \
--instance-ids "$INSTANCE_ID" \
--query "Reservations[].Instances[].State.Name" \
--output text \
--region "$REGION"
}
# Helper function to check for errors
check_for_error() {
if [ -z "$1" ] || [ "$1" == "None" ]; then
echo "Error: $2"
exit 1
fi
}
# Ensure the OpenVPN client config file exists
if [ ! -f "$PATH_TO_OVPN_CLIENT_CONFIG" ]; then
echo "Error: OpenVPN client config file not found at $PATH_TO_OVPN_CLIENT_CONFIG."
exit 1
fi
# Get the instance ID for the instance with the name 'openvpn'
INSTANCE_ID=$(aws ec2 describe-instances \
--region "$REGION" \
--filters "Name=tag:Name,Values=$INSTANCE_NAME" \
--query "Reservations[].Instances[].InstanceId" \
--output text)
check_for_error "$INSTANCE_ID" "No instance ID found for the instance with name '$INSTANCE_NAME'."
# Get the current instance state
STATE=$(get_instance_state)
if [[ "$STATE" == "running" ]]; then
echo "Did you forget to stop EC2? ooh the bill!!!"
echo "Shutting down OpenVPN server..."
# Attempt to stop the instance
STOP_OUTPUT=$(aws ec2 stop-instances --instance-ids "$INSTANCE_ID" --output text --region "$REGION")
STOP_STATUS=$(echo "$STOP_OUTPUT" | grep 'stopping')
if [ -n "$STOP_STATUS" ]; then
echo "Stop request successful. The instance is now in the process of stopping."
else
echo "Error: Failed to send stop request. Response: $STOP_OUTPUT"
exit 1
fi
echo "Waiting for the instance to stop..."
while [[ "$STATE" != "stopped" ]]; do
sleep 10
STATE=$(get_instance_state)
echo "Current instance state: $STATE"
done
echo "The instance has been successfully stopped."
exit 0
fi
if [[ "$STATE" != "stopped" ]]; then
echo "State is: $STATE"
exit 1
fi
# Start the instance
aws ec2 start-instances --instance-ids "$INSTANCE_ID" --output text --region "$REGION"
# Wait for the instance to boot up
echo "Waiting for the instance to start..."
while [[ "$STATE" != "running" ]]; do
sleep 10
STATE=$(get_instance_state)
echo "Current instance state: $STATE"
done
# Get the public IP address of the instance
INSTANCE_PUBLIC_IP=$(aws ec2 describe-instances \
--region "$REGION" \
--instance-ids "$INSTANCE_ID" \
--query "Reservations[].Instances[].PublicIpAddress" \
--output text)
check_for_error "$INSTANCE_PUBLIC_IP" "No public IP address found for instance ID $INSTANCE_ID."
echo "Public IP Address: $INSTANCE_PUBLIC_IP"
# Extract the previous IP from the client config file
PREV_IP=$(grep remote "$PATH_TO_OVPN_CLIENT_CONFIG" | head -n 1 | awk '{print $2}')
check_for_error "$PREV_IP" "No previous IP address found in the OpenVPN client config file."
# Replace the previous IP with the new IP in the config file
sed -i "s/$PREV_IP/$INSTANCE_PUBLIC_IP/g" "$PATH_TO_OVPN_CLIENT_CONFIG"
# Start the OpenVPN session using the updated config
printf "$USERNAME\n$PASSWORD\n" | openvpn3 session-start --config "$PATH_TO_OVPN_CLIENT_CONFIG"