-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_cisco_proxy.sh
More file actions
executable file
·354 lines (317 loc) · 12.9 KB
/
Copy pathsetup_cisco_proxy.sh
File metadata and controls
executable file
·354 lines (317 loc) · 12.9 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/bin/bash
set -e
# Colors for output
GREEN='\033[1;32m'
BLUE='\033[1;34m'
YELLOW='\033[1;33m'
RED='\033[1;31m'
NC='\033[0m' # No Color
# Safety check - ensure script is run only in Docker container
check_docker_environment() {
echo -e "\033[1;31m⚠️ WARNING: This script should ONLY be used within a Docker container! ⚠️\033[0m"
echo -e "\033[1;31m Running this script on your host system may modify system-wide proxy settings.\033[0m"
echo ""
# Check if we're likely in a container
if [ -f /.dockerenv ] || grep -q 'docker\|lxc' /proc/1/cgroup 2>/dev/null; then
echo -e "${GREEN}✓ Docker container environment detected.${NC}"
else
echo -e "${RED}✗ This does not appear to be a Docker container environment.${NC}"
echo -e "${YELLOW} Are you sure you want to proceed? This may affect your host system.${NC}"
fi
echo ""
read -p "Are you running this inside a Docker container? [y/N]: " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "${RED}Aborting for safety. Please run this script only within a Docker container.${NC}"
exit 1
fi
echo -e "${GREEN}Proceeding with proxy setup...${NC}"
echo ""
}
# --------------------------------- Install Cisco proxy ---------------------------------
PROXY_SETTINGS_START="# --- Cisco Proxy Settings START ---"
PROXY_SETTINGS_END="# --- Cisco Proxy Settings END ---"
PROXY_SETTINGS="${PROXY_SETTINGS_START}
export HTTP_PROXY=http://146.112.255.50:80
export HTTPS_PROXY=http://146.112.255.50:443
export NO_PROXY=localhost,127.0.0.1,.philips.com,.github.com,github.com,api.github.com,*.github.com,*.githubusercontent.com
export http_proxy=http://146.112.255.50:80
export https_proxy=http://146.112.255.50:443
export no_proxy=localhost,127.0.0.1,.philips.com,.github.com,github.com,api.github.com,*.github.com,*.githubusercontent.com
export REQUESTS_CA_BUNDLE=/etc/ssl/certs/combined-certs.pem
export SSL_CERT_FILE=/etc/ssl/certs/combined-certs.pem
export NODE_EXTRA_CA_CERTS=/etc/ssl/certs/combined-certs.pem
# Node.js settings to fix VS Code Copilot connection hanging
export NODE_OPTIONS=\"--max-http-header-size=32768\"
# Disable problematic keep-alive behavior in corporate proxy environment
export UV_THREADPOOL_SIZE=16
# alias pip='env -u SSL_CERT_FILE -u REQUESTS_CA_BUNDLE pip'
${PROXY_SETTINGS_END}"
BASHRC="/root/.bashrc"
CERT_PATH="/usr/local/share/ca-certificates/ciscoumbrella.crt"
PEM_PATH="/etc/ssl/certs/ciscoumbrella.pem"
COMBINED_CERT_PATH="/etc/ssl/certs/combined-certs.pem"
CER_TMP="/ciscoumbrella.cer"
PIP_INSTALL_SCRIPT="pip_install.sh"
# Function to download and install individual Cisco certificate
setup_individual_cert() {
echo -e "${BLUE}Downloading and installing Cisco Umbrella certificate...${NC}"
curl -fsSL -o "$CER_TMP" http://www.cisco.com/security/pki/certs/ciscoumbrellaroot.cer \
&& openssl x509 -inform DER -in "$CER_TMP" -out "$CERT_PATH" \
&& update-ca-certificates \
&& rm -f "$CER_TMP"
echo -e "${BLUE}Certificate installed at $CERT_PATH.${NC}"
}
# Function to create combined certificate bundle (certifi + corporate CA)
setup_combined_cert() {
echo -e "${BLUE}Creating combined certificate bundle (certifi + Cisco CA)...${NC}"
# First, ensure the individual certificate is installed
if [ ! -f "$CERT_PATH" ]; then
setup_individual_cert
fi
# Check if Python and certifi are available
if command -v python3 >/dev/null 2>&1; then
PYTHON_CMD="python3"
elif command -v python >/dev/null 2>&1; then
PYTHON_CMD="python"
else
echo -e "${YELLOW}Warning: Python not found. Falling back to individual certificate setup.${NC}"
echo -e "${YELLOW}This may cause issues with tunneled domains.${NC}"
return 1
fi
# Create combined certificate bundle
echo -e "${BLUE}Building combined certificate bundle...${NC}"
$PYTHON_CMD - <<'PY'
import sys
try:
import certifi
import shutil
import os
dst = "/etc/ssl/certs/combined-certs.pem"
shutil.copyfile(certifi.where(), dst)
print(f"Base (certifi) copied to: {dst}")
# Ensure proper permissions
os.chmod(dst, 0o644)
except ImportError:
print("Error: certifi package not found. Please install it first.")
sys.exit(1)
except Exception as e:
print(f"Error creating combined certificate bundle: {e}")
sys.exit(1)
PY
if [ $? -eq 0 ]; then
# Append the Cisco certificate to the combined bundle
if [ -f "$PEM_PATH" ]; then
cat "$PEM_PATH" >> "$COMBINED_CERT_PATH"
echo -e "${BLUE}Cisco certificate appended to combined bundle.${NC}"
echo -e "${GREEN}Combined certificate bundle created at $COMBINED_CERT_PATH${NC}"
return 0
else
echo -e "${RED}Error: Cisco certificate not found at $PEM_PATH${NC}"
return 1
fi
else
echo -e "${RED}Error: Failed to create combined certificate bundle.${NC}"
return 1
fi
}
# Function to clean up certificate files
cleanup_certificates() {
echo -e "${BLUE}Cleaning up certificate files...${NC}"
# Remove individual certificate
if [ -f "$CERT_PATH" ]; then
rm -f "$CERT_PATH"
echo -e "${BLUE}Individual certificate removed from $CERT_PATH.${NC}"
fi
# Remove combined certificate bundle
if [ -f "$COMBINED_CERT_PATH" ]; then
rm -f "$COMBINED_CERT_PATH"
echo -e "${BLUE}Combined certificate bundle removed from $COMBINED_CERT_PATH.${NC}"
fi
# Update CA certificates
update-ca-certificates --fresh
}
# Function to update /etc/environment
update_etc_environment() {
local env_file="/etc/environment"
if grep -q "$PROXY_SETTINGS_START" "$env_file" 2>/dev/null; then
echo -e "${YELLOW}Proxy settings already present in $env_file. Skipping.${NC}"
else
echo -e "${BLUE}Adding proxy settings to $env_file...${NC}"
# Convert export statements to simple VAR=value format for /etc/environment
cat >> "$env_file" << EOF
$PROXY_SETTINGS_START
HTTP_PROXY=http://146.112.255.50:80
HTTPS_PROXY=http://146.112.255.50:443
NO_PROXY=localhost,127.0.0.1,.philips.com,.github.com,github.com,api.github.com,*.github.com,*.githubusercontent.com
http_proxy=http://146.112.255.50:80
https_proxy=http://146.112.255.50:443
no_proxy=localhost,127.0.0.1,.philips.com,.github.com,github.com,api.github.com,*.github.com,*.githubusercontent.com
REQUESTS_CA_BUNDLE=/etc/ssl/certs/combined-certs.pem
SSL_CERT_FILE=/etc/ssl/certs/combined-certs.pem
NODE_EXTRA_CA_CERTS=/etc/ssl/certs/combined-certs.pem
NODE_OPTIONS=\"--max-http-header-size=32768\"
UV_THREADPOOL_SIZE=16
$PROXY_SETTINGS_END
EOF
echo -e "${BLUE}Proxy settings added to $env_file.${NC}"
fi
}
# Function to remove proxy settings from /etc/environment
remove_from_etc_environment() {
local env_file="/etc/environment"
if grep -q "$PROXY_SETTINGS_START" "$env_file" 2>/dev/null; then
echo -e "${BLUE}Removing proxy settings from $env_file...${NC}"
sed -i "/$PROXY_SETTINGS_START/,/$PROXY_SETTINGS_END/d" "$env_file"
echo -e "${BLUE}Proxy settings removed from $env_file.${NC}"
else
echo -e "${YELLOW}No proxy settings found in $env_file.${NC}"
fi
}
show_help() {
echo -e "\033[1mUsage:\033[0m $0 [--set|--unset|--help] [--etc-env]"
echo ""
echo -e "\033[1;31m⚠️ WARNING: This script should ONLY be used within a Docker container! ⚠️\033[0m"
echo -e "\033[1;31m Do NOT run this script on your host system.\033[0m"
echo ""
echo -e "\033[1mDescription:\033[0m"
echo " Configure Cisco corporate proxy settings and certificate for the container."
echo ""
echo -e "\033[1mOptions:\033[0m"
echo " --set Set up Cisco proxy settings and install certificate (default if no arg)"
echo " --unset Remove Cisco proxy settings and uninstall certificate"
echo " --etc-env Also update /etc/environment with proxy settings (optional)"
echo " --help Show this help message"
echo ""
echo -e "\033[1mExamples:\033[0m"
echo " $0 --set # Set up proxy (bashrc only)"
echo " $0 --set --etc-env # Set up proxy (bashrc + /etc/environment)"
echo " $0 --unset --etc-env # Remove proxy (bashrc + /etc/environment)"
}
set_proxy() {
echo ""
echo -e "${BLUE}=== Setting up Cisco Proxy ===${NC}"
if grep -q "$PROXY_SETTINGS_START" "$BASHRC"; then
echo -e "${YELLOW}Proxy settings already present in $BASHRC. Skipping append.${NC}"
else
echo "$PROXY_SETTINGS" >> "$BASHRC"
echo -e "${BLUE}Proxy settings appended to $BASHRC.${NC}"
fi
# Update /etc/environment for system-wide proxy settings (optional)
if [ "$UPDATE_ETC_ENV" = "true" ]; then
update_etc_environment
fi
# # Update PIP_CMD in pip_install.sh
# if [ -f "$PIP_INSTALL_SCRIPT" ]; then
# echo -e "${BLUE}Updating PIP_CMD in $PIP_INSTALL_SCRIPT...${NC}"
# sed -i 's/^PIP_CMD="pip"/PIP_CMD="env -u SSL_CERT_FILE -u REQUESTS_CA_BUNDLE pip"/' "$PIP_INSTALL_SCRIPT"
# echo -e "${BLUE}PIP_CMD updated in $PIP_INSTALL_SCRIPT.${NC}"
# else
# echo -e "${YELLOW}Warning: $PIP_INSTALL_SCRIPT not found. Skipping PIP_CMD update.${NC}"
# fi
# Set up certificate bundle
if [ ! -f "$COMBINED_CERT_PATH" ]; then
echo -e "${BLUE}Setting up certificate bundle...${NC}"
if setup_combined_cert; then
echo -e "${GREEN}✓ Combined certificate bundle configured successfully.${NC}"
echo -e "${BLUE} This bundle includes both standard CAs and the Cisco certificate.${NC}"
echo -e "${BLUE} This should work with both corporate proxy and tunneled domains.${NC}"
else
echo -e "${YELLOW}⚠ Falling back to individual certificate setup.${NC}"
echo -e "${YELLOW} Note: This may cause issues with tunneled domains.${NC}"
if [ ! -f "$CERT_PATH" ]; then
setup_individual_cert
fi
fi
else
echo -e "${YELLOW}Combined certificate bundle already exists at $COMBINED_CERT_PATH.${NC}"
fi
echo ""
echo -e "${GREEN}Proxy setup completed successfully!${NC}"
echo -e "${BLUE}Next step:${NC}"
echo -e "• ${YELLOW}Run ${NC}\`source $BASHRC\`${YELLOW} or restart your shell to apply changes.${NC}"
if [ "$UPDATE_ETC_ENV" = "true" ]; then
echo -e "• ${YELLOW}Note: System-wide environment variables (from /etc/environment) will be available after container restart.${NC}"
fi
}
unset_proxy() {
echo ""
echo -e "${BLUE}=== Removing Cisco Proxy ===${NC}"
if grep -q "$PROXY_SETTINGS_START" "$BASHRC"; then
sed -i "/$PROXY_SETTINGS_START/,/$PROXY_SETTINGS_END/d" "$BASHRC"
echo -e "${BLUE}Proxy settings removed from $BASHRC.${NC}"
else
echo -e "${YELLOW}No proxy settings found in $BASHRC.${NC}"
fi
# Remove proxy settings from /etc/environment (optional)
if [ "$UPDATE_ETC_ENV" = "true" ]; then
remove_from_etc_environment
fi
# # Restore original PIP_CMD in pip_install.sh
# if [ -f "$PIP_INSTALL_SCRIPT" ]; then
# echo -e "${BLUE}Restoring original PIP_CMD in $PIP_INSTALL_SCRIPT...${NC}"
# sed -i 's/^PIP_CMD="env -u SSL_CERT_FILE -u REQUESTS_CA_BUNDLE pip"/PIP_CMD="pip"/' "$PIP_INSTALL_SCRIPT"
# echo -e "${BLUE}PIP_CMD restored in $PIP_INSTALL_SCRIPT.${NC}"
# else
# echo -e "${YELLOW}Warning: $PIP_INSTALL_SCRIPT not found. Skipping PIP_CMD restore.${NC}"
# fi
# Clean up certificate files
cleanup_certificates
echo ""
echo -e "${GREEN}Proxy removal completed successfully!${NC}"
echo -e "${BLUE}Next step:${NC}"
echo -e "• ${YELLOW}Run ${NC}\`source $BASHRC\`${YELLOW} or restart your shell to apply changes.${NC}"
if [ "$UPDATE_ETC_ENV" = "true" ]; then
echo -e "• ${YELLOW}Note: System-wide environment variables will be fully removed after container restart.${NC}"
fi
}
# Parse command line arguments
UPDATE_ETC_ENV="false"
ACTION=""
# Process all arguments
while [[ $# -gt 0 ]]; do
case $1 in
--set)
ACTION="set"
shift
;;
--unset)
ACTION="unset"
shift
;;
--etc-env)
UPDATE_ETC_ENV="true"
shift
;;
--help|-h)
show_help
exit 0
;;
"")
# Default action if no arguments
ACTION="set"
shift
;;
*)
echo -e "${RED}Unknown argument: $1${NC}"
show_help
exit 1
;;
esac
done
# Set default action if none specified
if [ -z "$ACTION" ]; then
ACTION="set"
fi
case "$ACTION" in
set)
# Run safety check before proceeding
check_docker_environment
set_proxy
;;
unset)
# Run safety check before proceeding
check_docker_environment
unset_proxy
;;
esac