browser-setup.js•11.3 kB
/**
* Browser Setup Script Generator
* Generates setup scripts for different browsers
*/
import fs from 'fs/promises';
import path from 'path';
export class BrowserSetup {
constructor(targetManager) {
this.targetManager = targetManager;
this.setupDir = './setup-scripts';
}
/**
* Generate all browser setup scripts
* @param {string} proxyHost - Proxy server host
* @param {number} proxyPort - Proxy server port
* @returns {Object} Generated scripts info
*/
async generateAllSetups(proxyHost = 'localhost', proxyPort = 8080) {
const scripts = {
pac: await this.generatePacFile(proxyHost, proxyPort),
chrome: this.generateChromeSetup(proxyHost, proxyPort),
firefox: this.generateFirefoxSetup(proxyHost, proxyPort),
curl: this.generateCurlSetup(proxyHost, proxyPort),
system: this.generateSystemProxySetup(proxyHost, proxyPort)
};
// Create setup directory
try {
await fs.mkdir(this.setupDir, { recursive: true });
} catch (error) {
// Directory might already exist
}
// Write all scripts to files
const files = {};
for (const [type, content] of Object.entries(scripts)) {
const filename = `${type}-proxy-setup.${this._getFileExtension(type)}`;
const filepath = path.join(this.setupDir, filename);
await fs.writeFile(filepath, content.content);
files[type] = {
filename,
filepath,
description: content.description
};
}
return {
proxyHost,
proxyPort,
targetCount: this.targetManager.getStats().enabled,
files,
generatedAt: new Date().toISOString()
};
}
/**
* Generate PAC file
* @param {string} proxyHost - Proxy server host
* @param {number} proxyPort - Proxy server port
* @returns {Object} PAC file content and info
*/
async generatePacFile(proxyHost, proxyPort) {
const pacContent = this.targetManager.generatePacFile(proxyHost, proxyPort);
return {
content: pacContent,
description: 'Proxy Auto-Configuration file for automatic proxy routing'
};
}
/**
* Generate Chrome setup script
* @param {string} proxyHost - Proxy server host
* @param {number} proxyPort - Proxy server port
* @returns {Object} Chrome setup script
*/
generateChromeSetup(proxyHost, proxyPort) {
const stats = this.targetManager.getStats();
const pacUrl = `http://${proxyHost}:${proxyPort}/proxy.pac`;
const script = `#!/bin/bash
# Chrome Proxy Setup Script
# Generated by Web Proxy MCP Server
# Targets: ${stats.enabled} enabled domains
echo "Setting up Chrome with proxy configuration..."
# Method 1: Launch Chrome with PAC file
echo "Starting Chrome with PAC configuration..."
google-chrome \\
--proxy-pac-url="${pacUrl}" \\
--disable-background-networking \\
--disable-default-apps \\
--disable-extensions \\
--disable-sync \\
--disable-translate \\
--disable-web-security \\
--user-data-dir="/tmp/chrome-proxy-session" \\
--new-window
# Method 2: Manual proxy settings (alternative)
# google-chrome \\
# --proxy-server="${proxyHost}:${proxyPort}" \\
# --user-data-dir="/tmp/chrome-proxy-session"
echo "Chrome started with proxy configuration"
echo "Monitoring ${stats.enabled} domains through proxy"
echo "Proxy server: ${proxyHost}:${proxyPort}"
`;
return {
content: script,
description: 'Shell script to launch Chrome with proxy configuration'
};
}
/**
* Generate Firefox setup script
* @param {string} proxyHost - Proxy server host
* @param {number} proxyPort - Proxy server port
* @returns {Object} Firefox setup script
*/
generateFirefoxSetup(proxyHost, proxyPort) {
const stats = this.targetManager.getStats();
// Firefox preferences for proxy
const prefsContent = `// Firefox proxy preferences
// Generated by Web Proxy MCP Server
user_pref("network.proxy.type", 2); // Use PAC
user_pref("network.proxy.autoconfig_url", "http://${proxyHost}:${proxyPort}/proxy.pac");
user_pref("network.proxy.autoconfig_retry_interval_min", 5);
user_pref("network.proxy.autoconfig_retry_interval_max", 300);
// Optional: Disable DNS over HTTPS for proxy testing
user_pref("network.trr.mode", 5);
// Monitoring ${stats.enabled} domains through proxy
// Proxy server: ${proxyHost}:${proxyPort}
`;
const script = `#!/bin/bash
# Firefox Proxy Setup Script
# Generated by Web Proxy MCP Server
echo "Setting up Firefox with proxy configuration..."
# Create temporary Firefox profile
PROFILE_DIR="/tmp/firefox-proxy-profile"
mkdir -p "$PROFILE_DIR"
# Write proxy preferences
cat > "$PROFILE_DIR/user.js" << 'EOF'
${prefsContent}EOF
echo "Starting Firefox with proxy profile..."
firefox --profile "$PROFILE_DIR" --new-instance
echo "Firefox started with proxy configuration"
echo "Profile directory: $PROFILE_DIR"
echo "PAC URL: http://${proxyHost}:${proxyPort}/proxy.pac"
`;
return {
content: script,
description: 'Shell script to launch Firefox with proxy configuration'
};
}
/**
* Generate curl setup script
* @param {string} proxyHost - Proxy server host
* @param {number} proxyPort - Proxy server port
* @returns {Object} Curl setup script
*/
generateCurlSetup(proxyHost, proxyPort) {
const targets = this.targetManager.listTargets().filter(t => t.status === 'active');
const examples = targets.slice(0, 3).map(target =>
`curl --proxy "${proxyHost}:${proxyPort}" "https://${target.domain}"`
).join('\n');
const script = `#!/bin/bash
# cURL Proxy Testing Script
# Generated by Web Proxy MCP Server
PROXY_SERVER="${proxyHost}:${proxyPort}"
echo "Testing proxy with cURL..."
echo "Proxy server: $PROXY_SERVER"
echo "Monitored domains: ${targets.length}"
echo ""
# Function to test a URL through proxy
test_proxy() {
local url="$1"
echo "Testing: $url"
curl --proxy "$PROXY_SERVER" \\
--connect-timeout 10 \\
--max-time 30 \\
--silent \\
--head \\
"$url" && echo "✓ Success" || echo "✗ Failed"
echo ""
}
# Test examples
${examples}
# Interactive mode
echo "Enter URLs to test (Ctrl+C to exit):"
while read -p "URL: " url; do
if [ ! -z "$url" ]; then
test_proxy "$url"
fi
done
`;
return {
content: script,
description: 'cURL testing script for proxy verification'
};
}
/**
* Generate system proxy setup script
* @param {string} proxyHost - Proxy server host
* @param {number} proxyPort - Proxy server port
* @returns {Object} System proxy setup script
*/
generateSystemProxySetup(proxyHost, proxyPort) {
const pacUrl = `http://${proxyHost}:${proxyPort}/proxy.pac`;
const script = `#!/bin/bash
# System Proxy Setup Script
# Generated by Web Proxy MCP Server
# WARNING: This modifies system-wide proxy settings
PROXY_SERVER="${proxyHost}:${proxyPort}"
PAC_URL="${pacUrl}"
echo "System Proxy Configuration"
echo "=========================="
echo "Proxy server: $PROXY_SERVER"
echo "PAC URL: $PAC_URL"
echo ""
# Function to set GNOME proxy (Ubuntu/Debian desktop)
set_gnome_proxy() {
echo "Setting GNOME proxy settings..."
gsettings set org.gnome.system.proxy mode 'auto'
gsettings set org.gnome.system.proxy autoconfig-url "$PAC_URL"
echo "✓ GNOME proxy configured"
}
# Function to set KDE proxy (KDE desktop)
set_kde_proxy() {
echo "Setting KDE proxy settings..."
kwriteconfig5 --file kioslaverc --group 'Proxy Settings' --key ProxyType 2
kwriteconfig5 --file kioslaverc --group 'Proxy Settings' --key 'Proxy Config Script' "$PAC_URL"
echo "✓ KDE proxy configured"
}
# Function to set environment variables
set_env_proxy() {
echo "Setting environment proxy variables..."
export http_proxy="http://$PROXY_SERVER"
export https_proxy="http://$PROXY_SERVER"
export HTTP_PROXY="http://$PROXY_SERVER"
export HTTPS_PROXY="http://$PROXY_SERVER"
# Add to shell profile for persistence (optional)
echo "# Proxy settings - added by Web Proxy MCP" >> ~/.bashrc
echo "export http_proxy=http://$PROXY_SERVER" >> ~/.bashrc
echo "export https_proxy=http://$PROXY_SERVER" >> ~/.bashrc
echo "✓ Environment variables set"
}
# Function to unset proxy
unset_proxy() {
echo "Removing proxy settings..."
# GNOME
gsettings set org.gnome.system.proxy mode 'none' 2>/dev/null
# Environment
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY
# Remove from bashrc
sed -i '/# Proxy settings - added by Web Proxy MCP/,+2d' ~/.bashrc 2>/dev/null
echo "✓ Proxy settings removed"
}
# Main menu
case "$1" in
"gnome")
set_gnome_proxy
;;
"kde")
set_kde_proxy
;;
"env")
set_env_proxy
;;
"unset")
unset_proxy
;;
*)
echo "Usage: $0 [gnome|kde|env|unset]"
echo ""
echo "Commands:"
echo " gnome - Set GNOME desktop proxy"
echo " kde - Set KDE desktop proxy"
echo " env - Set environment variables"
echo " unset - Remove all proxy settings"
echo ""
echo "Example: $0 gnome"
;;
esac
`;
return {
content: script,
description: 'System-wide proxy configuration script (Linux/macOS)'
};
}
/**
* Get file extension for script type
* @param {string} type - Script type
* @returns {string} File extension
* @private
*/
_getFileExtension(type) {
const extensions = {
pac: 'pac',
chrome: 'sh',
firefox: 'sh',
curl: 'sh',
system: 'sh'
};
return extensions[type] || 'txt';
}
/**
* Generate quick setup instructions
* @param {string} proxyHost - Proxy server host
* @param {number} proxyPort - Proxy server port
* @returns {string} Setup instructions
*/
generateQuickSetup(proxyHost, proxyPort) {
const stats = this.targetManager.getStats();
const targets = this.targetManager.listTargets()
.filter(t => t.status === 'active')
.map(t => t.domain)
.slice(0, 5);
return `🔧 Web Proxy MCP - Quick Setup Guide
==========================================
📊 Configuration:
• Proxy Server: ${proxyHost}:${proxyPort}
• Monitored Domains: ${stats.enabled}
• PAC URL: http://${proxyHost}:${proxyPort}/proxy.pac
🎯 Target Domains:
${targets.map(domain => `• ${domain}`).join('\n')}
🚀 Quick Start Options:
1️⃣ Chrome (Recommended):
chmod +x ./setup-scripts/chrome-proxy-setup.sh
./setup-scripts/chrome-proxy-setup.sh
2️⃣ Firefox:
chmod +x ./setup-scripts/firefox-proxy-setup.sh
./setup-scripts/firefox-proxy-setup.sh
3️⃣ System-wide (Linux):
chmod +x ./setup-scripts/system-proxy-setup.sh
./setup-scripts/system-proxy-setup.sh gnome
4️⃣ Testing with cURL:
chmod +x ./setup-scripts/curl-proxy-setup.sh
./setup-scripts/curl-proxy-setup.sh
📝 Manual Setup:
• PAC File: Use http://${proxyHost}:${proxyPort}/proxy.pac in browser proxy settings
• Manual Proxy: ${proxyHost}:${proxyPort} for HTTP/HTTPS
⚠️ Note: Only traffic to monitored domains will be proxied.
All other traffic goes direct to maintain normal browsing.
`;
}
}