-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcoder
More file actions
executable file
·484 lines (405 loc) · 13.1 KB
/
Copy pathwebcoder
File metadata and controls
executable file
·484 lines (405 loc) · 13.1 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/bin/bash
# webcoder - VS Code Server Setup Script
# This script downloads VS Code CLI, installs nginx, and sets up a reverse proxy
set -e
# Default values
PORT=8000
DOMAIN=""
SECURE=false
# Get real user info early (before we use sudo)
get_real_user_info() {
if [[ -n "$SUDO_USER" ]]; then
REAL_USER="$SUDO_USER"
REAL_HOME=$(getent passwd "$REAL_USER" | cut -d: -f6)
else
REAL_USER="$USER"
REAL_HOME="$HOME"
fi
}
# Initialize user info
get_real_user_info
# Generate or read authentication token
TOKEN_FILE="$REAL_HOME/.vscode/cli/token"
if [[ ! -s "$TOKEN_FILE" ]]; then
TOKEN=$(head -c 32 /dev/urandom | base64 | tr -d '/+=' | cut -c1-64)
mkdir -p "$(dirname "$TOKEN_FILE")"
echo "$TOKEN" > "$TOKEN_FILE"
else
TOKEN=$(cat "$TOKEN_FILE")
fi
VSCODE_CLI_DIR="$REAL_HOME/.vscode/cli"
VSCODE_CLI_BIN="$VSCODE_CLI_DIR/code"
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Print functions
print_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Usage function
usage() {
cat << EOF
Usage: $0 [OPTIONS]
VS Code Server Setup Script - Downloads VS Code CLI, installs nginx, and sets up reverse proxy
OPTIONS:
-p, --port PORT Port for VS Code server to listen on (default: 8000)
-d, --domain DOMAIN Domain name for nginx (default: use server IP)
-s, --secure Enable HTTPS with certbot (default: false)
-h, --help Display this help message
EXAMPLES:
$0 # Use defaults (port 8000, no domain, no SSL)
$0 -p 9000 # Use port 9000
$0 -p 8000 -d example.com # Use domain example.com
$0 -p 8000 -d example.com -s # Use domain with SSL/TLS
EOF
}
# Parse command line arguments
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
-p|--port)
# Validate port: must be numeric and in range 1-65535
if ! [[ "$2" =~ ^[0-9]+$ ]] || [ "$2" -lt 1 ] || [ "$2" -gt 65535 ]; then
print_error "Invalid port: $2. Must be an integer between 1 and 65535."
exit 1
fi
PORT="$2"
shift 2
;;
-d|--domain)
DOMAIN="$2"
shift 2
;;
-s|--secure)
SECURE=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
print_error "Unknown option: $1"
usage
exit 1
;;
esac
done
}
# Check if running as root/sudo
check_sudo() {
if [[ $EUID -ne 0 ]]; then
print_error "This script requires root privileges. Please run with sudo."
exit 1
fi
}
# Detect OS and architecture
detect_system() {
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m)
case $ARCH in
x86_64)
ARCH="x64"
;;
aarch64|arm64)
ARCH="arm64"
;;
armv7l)
ARCH="armhf"
;;
*)
print_error "Unsupported architecture: $ARCH"
exit 1
;;
esac
case $OS in
linux)
OS="alpine"
;;
darwin)
OS="darwin"
;;
*)
print_error "Unsupported OS: $OS"
exit 1
;;
esac
print_info "Detected system: $OS-$ARCH"
}
# Download and install VS Code CLI
install_vscode_cli() {
print_info "Checking VS Code CLI installation..."
if [[ -f "$VSCODE_CLI_BIN" ]]; then
print_info "VS Code CLI already installed at $VSCODE_CLI_BIN"
return 0
fi
print_info "Downloading VS Code CLI..."
mkdir -p "$VSCODE_CLI_DIR"
# Download URL for VS Code CLI
# Such as https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64
VSCODE_URL="https://code.visualstudio.com/sha/download?build=stable&os=cli-${OS}-${ARCH}"
print_info "Downloading from: $VSCODE_URL"
# Download to temporary file
TMP_FILE=$(mktemp)
if ! curl -f -L --connect-timeout 30 --max-time 300 "$VSCODE_URL" -o "$TMP_FILE"; then
print_error "Failed to download VS Code CLI"
rm -f "$TMP_FILE"
exit 1
fi
# Extract the CLI
print_info "Extracting VS Code CLI..."
if file "$TMP_FILE" | grep -q "gzip compressed"; then
tar -xzf "$TMP_FILE" -C "$VSCODE_CLI_DIR"
else
# Try to extract as tar.gz anyway
tar -xzf "$TMP_FILE" -C "$VSCODE_CLI_DIR" 2>/dev/null || {
print_error "Failed to extract VS Code CLI"
rm -f "$TMP_FILE"
exit 1
}
fi
rm -f "$TMP_FILE"
# Make executable
chmod +x "$VSCODE_CLI_BIN"
print_info "VS Code CLI installed successfully at $VSCODE_CLI_BIN"
}
# Install nginx
install_nginx() {
print_info "Checking nginx installation..."
if command -v nginx &> /dev/null; then
print_info "nginx is already installed"
return 0
fi
print_info "Installing nginx..."
# Detect package manager and install
if command -v apt-get &> /dev/null; then
apt-get update -qq
apt-get install -y nginx
elif command -v yum &> /dev/null; then
yum install -y nginx
elif command -v dnf &> /dev/null; then
dnf install -y nginx
elif command -v pacman &> /dev/null; then
pacman -Sy --noconfirm nginx
elif command -v brew &> /dev/null; then
brew install nginx
else
print_error "Could not detect package manager. Please install nginx manually."
exit 1
fi
print_info "nginx installed successfully"
}
# Get server IP
get_server_ip() {
# Try to get public IP, fallback to local IP
IP=$(curl -f -s --connect-timeout 10 --max-time 20 ifconfig.me 2>/dev/null || \
curl -f -s --connect-timeout 10 --max-time 20 icanhazip.com 2>/dev/null || \
hostname -I | awk '{print $1}')
echo "$IP"
}
# Configure nginx
configure_nginx() {
print_info "Configuring nginx..."
# Determine server name
if [[ -z "$DOMAIN" ]]; then
SERVER_NAME=$(get_server_ip)
print_info "Using IP address: $SERVER_NAME"
else
SERVER_NAME="$DOMAIN"
print_info "Using domain: $SERVER_NAME"
fi
# Create nginx configuration
NGINX_CONF="/etc/nginx/sites-available/vscode-server"
# Create sites-available and sites-enabled directories if they don't exist
mkdir -p /etc/nginx/sites-available
mkdir -p /etc/nginx/sites-enabled
cat > "$NGINX_CONF" << EOF
server {
listen 80;
server_name $SERVER_NAME;
location / {
if (\$http_cookie !~ "(^|;\\s*)tkn=$TOKEN(;|$)") {
return 401;
}
proxy_pass http://127.0.0.1:$PORT;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_read_timeout 86400;
}
}
EOF
# Enable the site
ln -sf "$NGINX_CONF" /etc/nginx/sites-enabled/vscode-server
# Test nginx configuration
if ! nginx -t; then
print_error "nginx configuration test failed"
exit 1
fi
print_info "nginx configured successfully"
# Reload nginx
if ! systemctl reload nginx 2>/dev/null; then
if ! service nginx reload 2>/dev/null; then
if ! nginx -s reload; then
print_error "Failed to reload nginx"
exit 1
fi
fi
fi
print_info "nginx reloaded"
}
# Install and configure certbot
setup_certbot() {
if [[ "$SECURE" != true ]]; then
print_info "Skipping certbot setup (not requested)"
return 0
fi
if [[ -z "$DOMAIN" ]]; then
print_error "Cannot use certbot without a domain name. Please specify -d option."
exit 1
fi
print_info "Installing certbot..."
# Install certbot
if command -v apt-get &> /dev/null; then
apt-get update -qq
apt-get install -y certbot python3-certbot-nginx
elif command -v yum &> /dev/null; then
yum install -y certbot python3-certbot-nginx
elif command -v dnf &> /dev/null; then
dnf install -y certbot python3-certbot-nginx
elif command -v pacman &> /dev/null; then
pacman -Sy --noconfirm certbot certbot-nginx
else
print_error "Could not detect package manager. Please install certbot manually."
exit 1
fi
print_info "Running certbot to obtain SSL certificate..."
print_warn "Make sure your domain $DOMAIN points to this server's IP address"
# Prompt for email if not provided
# read -p "Enter email address for Let's Encrypt notifications (press Enter to skip): " CERTBOT_EMAIL
CERTBOT_EMAIL="webcoder@example.com"
# Run certbot
if [[ -n "$CERTBOT_EMAIL" ]]; then
certbot --nginx -d "$DOMAIN" --non-interactive --agree-tos --email "$CERTBOT_EMAIL" || {
print_error "Certbot configuration failed. Please ensure:"
print_error " 1. Domain $DOMAIN points to this server"
print_error " 2. Port 80 and 443 are open"
print_error " 3. Email address is valid"
exit 1
}
else
print_warn "Proceeding without email (not recommended for production)"
certbot --nginx -d "$DOMAIN" --non-interactive --agree-tos --register-unsafely-without-email || {
print_error "Certbot configuration failed. Please ensure:"
print_error " 1. Domain $DOMAIN points to this server"
print_error " 2. Port 80 and 443 are open"
exit 1
}
fi
print_info "SSL certificate obtained and configured successfully"
}
# Create systemd service for VS Code server
create_vscode_service() {
print_info "Creating systemd service for VS Code server..."
print_info "VS Code server will use cookie-based authentication for security"
# Create systemd service file
cat > /etc/systemd/system/vscode-server.service << EOF
[Unit]
Description=VS Code Server
After=network.target
[Service]
Type=simple
User=$REAL_USER
WorkingDirectory=$REAL_HOME
Environment="HOME=$REAL_HOME"
ExecStart=$VSCODE_CLI_BIN serve-web --host 127.0.0.1 --port $PORT --without-connection-token --accept-server-license-terms
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
EOF
print_info "Systemd service created at /etc/systemd/system/vscode-server.service"
}
# Start VS Code server
start_vscode_server() {
print_info "Starting VS Code server..."
# Create systemd service
create_vscode_service
# Reload systemd daemon
systemctl daemon-reload
# Enable service to start on boot
systemctl enable vscode-server.service
# Start the service
if systemctl start vscode-server.service; then
print_info "VS Code server started successfully"
# Wait a moment for service to start
sleep 3
# Check status
if systemctl is-active --quiet vscode-server.service; then
print_info "VS Code server is running"
else
print_warn "VS Code server may not be running properly. Check with: systemctl status vscode-server.service"
fi
else
print_error "Failed to start VS Code server"
print_info "Check logs with: journalctl -u vscode-server.service -f"
exit 1
fi
print_info ""
print_info "VS Code server is accessible through nginx at:"
if [[ "$SECURE" == true ]]; then
print_info " https://$DOMAIN"
elif [[ -n "$DOMAIN" ]]; then
print_info " http://$DOMAIN"
else
print_info " http://$(get_server_ip)"
fi
print_info ""
print_info "Authentication Token: $TOKEN"
print_info "To access the server, you need to set a browser cookie:"
print_info " Name: tkn"
print_info " Value: $TOKEN"
print_info " Domain: ${SERVER_NAME}"
print_info ""
print_info "In browser developer tools console, run:"
print_info " document.cookie = 'tkn=$TOKEN; path=/; domain=$SERVER_NAME'"
print_info ""
print_info "Useful commands:"
print_info " Check status: systemctl status vscode-server.service"
print_info " View logs: journalctl -u vscode-server.service -f"
print_info " Stop server: systemctl stop vscode-server.service"
print_info " Restart server: systemctl restart vscode-server.service"
}
# Main function
main() {
print_info "Starting VS Code Server setup..."
parse_args "$@"
print_info "Configuration:"
print_info " Port: $PORT"
print_info " Domain: ${DOMAIN:-<use IP>}"
print_info " Secure: $SECURE"
check_sudo
detect_system
install_vscode_cli
install_nginx
configure_nginx
setup_certbot
start_vscode_server
print_info "Setup completed successfully!"
}
# Run main function
main "$@"