-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconnection.sh
More file actions
executable file
·289 lines (247 loc) · 8.62 KB
/
connection.sh
File metadata and controls
executable file
·289 lines (247 loc) · 8.62 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/bash
# Automatic OVPN server connection script
# Function: Automatically input private key password, username and password into connection file and connect
# Set color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Set file path
OVPN_FILE="API/imCloud.ovpn"
CONFIG_FILE="API/connection_config.txt"
TEMP_OVPN_FILE="/tmp/imCloud_auto.ovpn"
# Function: Show usage
show_usage() {
echo "Usage:"
echo " $0 # Use configuration file to connect"
echo " $0 --setup # Set connection information (private key password, username, password)"
echo " $0 --disconnect # Disconnect"
echo ""
echo "Configuration file location: $CONFIG_FILE"
echo "OVPN file location: $OVPN_FILE"
}
# Function: Check necessary tools
check_requirements() {
if ! command -v openvpn &> /dev/null; then
sudo apt update && sudo apt upgrade -y
sudo apt install -y openvpn
fi
}
# Function: Set connection information
setup_connection() {
echo -e "${YELLOW}=== Set OVPN connection information ===${NC}"
echo ""
# Check if configuration file exists
if [ -f "$CONFIG_FILE" ]; then
echo "Found existing configuration file, will update configuration..."
source "$CONFIG_FILE"
fi
# Read private key password
if [ -z "$PRIVATE_KEY_PASSWORD" ]; then
read -sp "Please enter the private key password: " key_password
echo ""
PRIVATE_KEY_PASSWORD="$key_password"
else
read -sp "Please enter the private key password (press Enter to keep unchanged): " key_password
echo ""
if [ -n "$key_password" ]; then
PRIVATE_KEY_PASSWORD="$key_password"
fi
fi
# Read username
if [ -z "$OVPN_USERNAME" ]; then
read -p "Please enter the OVPN username: " username
OVPN_USERNAME="$username"
else
read -p "Please enter the OVPN username (currently: $OVPN_USERNAME, press Enter to keep unchanged): " username
if [ -n "$username" ]; then
OVPN_USERNAME="$username"
fi
fi
# Read password
if [ -z "$OVPN_PASSWORD" ]; then
read -sp "Please enter the OVPN password: " password
echo ""
OVPN_PASSWORD="$password"
else
read -sp "Please enter the OVPN password (press Enter to keep unchanged): " password
echo ""
if [ -n "$password" ]; then
OVPN_PASSWORD="$password"
fi
fi
# Read OVPN server address (optional)
if [ -z "$OVPN_SERVER" ]; then
read -p "Please enter the OVPN server address (optional, press Enter to skip): " server
OVPN_SERVER="$server"
else
read -p "Please enter the OVPN server address (currently: $OVPN_SERVER, press Enter to keep unchanged): " server
if [ -n "$server" ]; then
OVPN_SERVER="$server"
fi
fi
# Save configuration file
cat > "$CONFIG_FILE" << EOF
# OVPN connection configuration file
# This file contains sensitive information, please do not share or submit to version control system
PRIVATE_KEY_PASSWORD="$PRIVATE_KEY_PASSWORD"
OVPN_USERNAME="$OVPN_USERNAME"
OVPN_PASSWORD="$OVPN_PASSWORD"
OVPN_SERVER="$OVPN_SERVER"
EOF
# Set file permissions (only owner can read and write)
chmod 600 "$CONFIG_FILE"
echo -e "${GREEN}Configuration saved to $CONFIG_FILE${NC}"
echo ""
}
# Function: Create temporary OVPN file
create_temp_ovpn() {
# Check if original OVPN file exists
if [ ! -f "$OVPN_FILE" ]; then
echo -e "${YELLOW}Warning: OVPN file does not exist: $OVPN_FILE${NC}"
echo "Creating basic OVPN configuration file..."
# Create basic OVPN configuration file
cat > "$OVPN_FILE" << EOF
# OVPN connection configuration file
client
dev tun
proto udp
resolv-retry infinite
nobind
persist-key
persist-tun
verb 3
EOF
if [ -n "$OVPN_SERVER" ]; then
echo "remote $OVPN_SERVER 1194" >> "$OVPN_FILE"
fi
echo ""
echo -e "${YELLOW}Please manually edit $OVPN_FILE to add complete OVPN settings${NC}"
echo ""
fi
# Copy original OVPN file to temporary file
cp "$OVPN_FILE" "$TEMP_OVPN_FILE"
# Create authentication file (username and password)
AUTH_FILE="/tmp/ovpn_auth_$$.txt"
echo "$OVPN_USERNAME" > "$AUTH_FILE"
echo "$OVPN_PASSWORD" >> "$AUTH_FILE"
chmod 600 "$AUTH_FILE"
# Add authentication settings to OVPN file
echo "auth-user-pass $AUTH_FILE" >> "$TEMP_OVPN_FILE"
# Create private key password file
KEY_PASS_FILE="/tmp/ovpn_keypass_$$.txt"
echo "$PRIVATE_KEY_PASSWORD" > "$KEY_PASS_FILE"
chmod 600 "$KEY_PASS_FILE"
echo "$AUTH_FILE|$KEY_PASS_FILE" # Return authentication file and private key password file path for subsequent cleanup
}
# Function: Connect to OVPN
connect_ovpn() {
# Check if configuration file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${RED}Error: Configuration file does not exist: $CONFIG_FILE${NC}"
echo "Please execute: $0 --setup"
exit 1
fi
# Load configuration file
source "$CONFIG_FILE"
# Check necessary variables
if [ -z "$PRIVATE_KEY_PASSWORD" ] || [ -z "$OVPN_USERNAME" ] || [ -z "$OVPN_PASSWORD" ]; then
echo -e "${RED}Error: Configuration file is incomplete${NC}"
echo "Please execute: $0 --setup"
exit 1
fi
# Check if already connected
if pgrep -x openvpn > /dev/null; then
echo -e "${YELLOW}Warning: OVPN connection already exists${NC}"
read -p "Do you want to disconnect the existing connection and reconnect? (y/N): " answer
if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
disconnect_ovpn
else
exit 0
fi
fi
echo -e "${GREEN}Creating OVPN connection...${NC}"
# Create temporary OVPN file
FILES=$(create_temp_ovpn)
AUTH_FILE=$(echo "$FILES" | cut -d'|' -f1)
KEY_PASS_FILE=$(echo "$FILES" | cut -d'|' -f2)
# Execute OVPN connection (background execution)
echo "Using configuration file: $TEMP_OVPN_FILE"
echo "Authentication file: $AUTH_FILE"
echo "Private key password file: $KEY_PASS_FILE"
echo ""
# Execute with root permissions (if needed)
if [ "$EUID" -eq 0 ]; then
openvpn --config "$TEMP_OVPN_FILE" --askpass "$KEY_PASS_FILE" --daemon
else
echo "Trying to execute with sudo permissions..."
sudo openvpn --config "$TEMP_OVPN_FILE" --askpass "$KEY_PASS_FILE" --daemon
fi
# Wait for connection to be established
sleep 2
# Check connection status
if pgrep -x openvpn > /dev/null; then
echo -e "${GREEN}OVPN connection has been started${NC}"
echo "Use 'sudo killall openvpn' or execute '$0 --disconnect' to disconnect"
# Clean up temporary authentication files (delayed cleanup, ensure OVPN has read)
(sleep 5 && rm -f "$AUTH_FILE" "$KEY_PASS_FILE") &
else
echo -e "${RED}Error: OVPN connection failed to start${NC}"
echo "Please check:"
echo " 1. OVPN configuration file is correct"
echo " 2. Private key password is correct"
echo " 3. Username and password are correct"
echo " 4. System logs: sudo journalctl -u openvpn -n 50"
rm -f "$AUTH_FILE" "$KEY_PASS_FILE" "$TEMP_OVPN_FILE"
exit 1
fi
}
# Function: Disconnect OVPN
disconnect_ovpn() {
echo -e "${YELLOW}Disconnecting OVPN connection...${NC}"
if pgrep -x openvpn > /dev/null; then
if [ "$EUID" -eq 0 ]; then
killall openvpn
else
sudo killall openvpn
fi
sleep 1
if ! pgrep -x openvpn > /dev/null; then
echo -e "${GREEN}OVPN connection has been disconnected${NC}"
else
echo -e "${RED}Error: Unable to disconnect${NC}"
exit 1
fi
else
echo -e "${YELLOW}No active OVPN connection${NC}"
fi
# Clean up temporary files
rm -f /tmp/ovpn_auth_*.txt /tmp/ovpn_keypass_*.txt "$TEMP_OVPN_FILE"
}
# Main program
main() {
# Check necessary tools
check_requirements
# Parse command line parameters
case "$1" in
--setup|--config)
setup_connection
;;
--disconnect|--stop)
disconnect_ovpn
;;
--help|-h)
show_usage
;;
"")
connect_ovpn
;;
*)
echo -e "${RED}Error: Unknown parameter: $1${NC}"
show_usage
exit 1
;;
esac
}
# Execute main program
main "$@"