mirror of
https://github.com/DeNNiiInc/Website-Stress-Test.git
synced 2026-04-17 12:36:00 +00:00
Compare commits
5 Commits
0beb424fce
...
a99ad54973
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a99ad54973 | ||
|
|
1fcccd10f1 | ||
|
|
9069fa2123 | ||
|
|
3f10aacf66 | ||
|
|
e8398bdb54 |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -1,3 +1,3 @@
|
||||
node_modules/
|
||||
deploy-config.json
|
||||
.env
|
||||
node_modules/
|
||||
deploy-config.json
|
||||
.env
|
||||
|
||||
@@ -1,211 +1,211 @@
|
||||
# 🚀 Proxmox Deployment Template (TurnKey Node.js)
|
||||
|
||||
**Use this guide to deploy ANY Node.js application to a TurnKey Linux LXC Container.**
|
||||
|
||||
---
|
||||
|
||||
## 📋 Prerequisites
|
||||
|
||||
1. **Project**: A Node.js application (Express, Next.js, etc.) in a Git repository.
|
||||
2. **Server**: A Proxmox TurnKey Node.js Container.
|
||||
3. **Access**: Root SSH password for the container.
|
||||
4. **Domain (Optional)**: If using Cloudflare Tunnel.
|
||||
|
||||
---
|
||||
|
||||
## 🛠️ Step 1: Prepare Your Project
|
||||
|
||||
Ensure your project is ready for production:
|
||||
|
||||
1. **Port Configuration**: Ensure your app listens on a configurable port or a fixed internal port (e.g., `4001`).
|
||||
```javascript
|
||||
// server.js
|
||||
const PORT = process.env.PORT || 4001;
|
||||
app.listen(PORT, ...);
|
||||
```
|
||||
|
||||
2. **Git Ignore**: Ensure `node_modules` and config files with secrets are ignored.
|
||||
```gitignore
|
||||
node_modules/
|
||||
.env
|
||||
config.json
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🖥️ Step 2: One-Time Server Setup
|
||||
|
||||
SSH into your new container:
|
||||
```bash
|
||||
ssh root@<YOUR_SERVER_IP>
|
||||
```
|
||||
|
||||
Run these commands to prepare the environment:
|
||||
|
||||
### 1. Install Essentials
|
||||
```bash
|
||||
apt-get update && apt-get install -y git
|
||||
```
|
||||
|
||||
### 2. Prepare Directory
|
||||
```bash
|
||||
# Standard web directory
|
||||
mkdir -p /var/www/<APP_NAME>
|
||||
cd /var/www/<APP_NAME>
|
||||
|
||||
# Clone your repo (Use Basic Auth with Token if private)
|
||||
# Format: https://<USER>:<TOKEN>@github.com/<ORG>/<REPO>.git
|
||||
git clone <YOUR_REPO_URL> .
|
||||
|
||||
# Install dependencies
|
||||
npm install
|
||||
```
|
||||
|
||||
### 3. Setup Permissions
|
||||
```bash
|
||||
# Give ownership to www-data (Nginx user)
|
||||
chown -R www-data:www-data /var/www/<APP_NAME>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ⚙️ Step 3: Application Configuration
|
||||
|
||||
### 1. Systemd Service
|
||||
Create a service file to keep your app running.
|
||||
|
||||
Create `/etc/systemd/system/<APP_NAME>.service`:
|
||||
```ini
|
||||
[Unit]
|
||||
Description=<APP_NAME> Service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
# OR use 'www-data' if app doesn't need root ports
|
||||
# User=www-data
|
||||
WorkingDirectory=/var/www/<APP_NAME>
|
||||
ExecStart=/usr/local/bin/node server.js
|
||||
Restart=always
|
||||
Environment=NODE_ENV=production
|
||||
Environment=PORT=4001
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
Enable and start:
|
||||
```bash
|
||||
systemctl daemon-reload
|
||||
systemctl enable <APP_NAME>
|
||||
systemctl start <APP_NAME>
|
||||
```
|
||||
|
||||
### 2. Nginx Reverse Proxy
|
||||
Configure Nginx to forward port 80 to your app (Port 4001).
|
||||
|
||||
Create `/etc/nginx/sites-available/<APP_NAME>`:
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name _;
|
||||
|
||||
root /var/www/<APP_NAME>;
|
||||
index index.html;
|
||||
|
||||
# Serve static files (Optional)
|
||||
location / {
|
||||
try_files $uri $uri/ =404;
|
||||
}
|
||||
|
||||
# Proxy API/Dynamic requests
|
||||
location /api {
|
||||
proxy_pass http://localhost:4001;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
proxy_set_header Host $host;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Enable site:
|
||||
```bash
|
||||
# Remove defaults
|
||||
rm -f /etc/nginx/sites-enabled/default
|
||||
rm -f /etc/nginx/sites-enabled/nodejs
|
||||
|
||||
# Link new site
|
||||
ln -s /etc/nginx/sites-available/<APP_NAME> /etc/nginx/sites-enabled/
|
||||
|
||||
# Reload
|
||||
nginx -t && systemctl reload nginx
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ☁️ Step 4: Cloudflare Tunnel (Secure Access)
|
||||
|
||||
Expose your app securely without opening router ports.
|
||||
|
||||
### 1. Install Cloudflared
|
||||
```bash
|
||||
# Add Key
|
||||
mkdir -p --mode=0755 /usr/share/keyrings
|
||||
curl -fsSL https://pkg.cloudflare.com/cloudflare-public-v2.gpg | tee /usr/share/keyrings/cloudflare-public-v2.gpg >/dev/null
|
||||
|
||||
# Add Repo
|
||||
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-public-v2.gpg] https://pkg.cloudflare.com/cloudflared any main' | tee /etc/apt/sources.list.d/cloudflared.list
|
||||
|
||||
# Install
|
||||
apt-get update && apt-get install -y cloudflared
|
||||
```
|
||||
|
||||
### 2. Create Tunnel
|
||||
```bash
|
||||
cloudflared tunnel login
|
||||
cloudflared tunnel create <TUNNEL_NAME>
|
||||
# Follow on-screen instructions to map domain -> http://localhost:4001
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔄 Step 5: Automated Updates (PowerShell)
|
||||
|
||||
Create a script `deploy-remote.ps1` in your project root to automate updates.
|
||||
|
||||
**Pre-requisite**: Create `deploy-config.json` (Add to .gitignore!):
|
||||
```json
|
||||
{
|
||||
"host": "<SERVER_IP>",
|
||||
"username": "root",
|
||||
"password": "<SSH_PASSWORD>",
|
||||
"remotePath": "/var/www/<APP_NAME>"
|
||||
}
|
||||
```
|
||||
|
||||
**Script `deploy-remote.ps1`**:
|
||||
```powershell
|
||||
# Reads config and updates remote server
|
||||
$Config = Get-Content "deploy-config.json" | ConvertFrom-Json
|
||||
$User = $Config.username; $HostName = $Config.host; $Pass = $Config.password
|
||||
$RemotePath = $Config.remotePath
|
||||
|
||||
# Commands to run remotely
|
||||
$Cmds = "
|
||||
cd $RemotePath
|
||||
echo '⬇️ Pulling code...'
|
||||
git pull
|
||||
echo '📦 Installing deps...'
|
||||
npm install
|
||||
echo '🚀 Restarting service...'
|
||||
systemctl restart <APP_NAME>
|
||||
systemctl status <APP_NAME> --no-pager
|
||||
"
|
||||
|
||||
echo y | plink -ssh -t -pw $Pass "$User@$HostName" $Cmds
|
||||
```
|
||||
|
||||
**Usage**: Just run `./deploy-remote.ps1` to deploy!
|
||||
# 🚀 Proxmox Deployment Template (TurnKey Node.js)
|
||||
|
||||
**Use this guide to deploy ANY Node.js application to a TurnKey Linux LXC Container.**
|
||||
|
||||
---
|
||||
|
||||
## 📋 Prerequisites
|
||||
|
||||
1. **Project**: A Node.js application (Express, Next.js, etc.) in a Git repository.
|
||||
2. **Server**: A Proxmox TurnKey Node.js Container.
|
||||
3. **Access**: Root SSH password for the container.
|
||||
4. **Domain (Optional)**: If using Cloudflare Tunnel.
|
||||
|
||||
---
|
||||
|
||||
## 🛠️ Step 1: Prepare Your Project
|
||||
|
||||
Ensure your project is ready for production:
|
||||
|
||||
1. **Port Configuration**: Ensure your app listens on a configurable port or a fixed internal port (e.g., `4001`).
|
||||
```javascript
|
||||
// server.js
|
||||
const PORT = process.env.PORT || 4001;
|
||||
app.listen(PORT, ...);
|
||||
```
|
||||
|
||||
2. **Git Ignore**: Ensure `node_modules` and config files with secrets are ignored.
|
||||
```gitignore
|
||||
node_modules/
|
||||
.env
|
||||
config.json
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🖥️ Step 2: One-Time Server Setup
|
||||
|
||||
SSH into your new container:
|
||||
```bash
|
||||
ssh root@<YOUR_SERVER_IP>
|
||||
```
|
||||
|
||||
Run these commands to prepare the environment:
|
||||
|
||||
### 1. Install Essentials
|
||||
```bash
|
||||
apt-get update && apt-get install -y git
|
||||
```
|
||||
|
||||
### 2. Prepare Directory
|
||||
```bash
|
||||
# Standard web directory
|
||||
mkdir -p /var/www/<APP_NAME>
|
||||
cd /var/www/<APP_NAME>
|
||||
|
||||
# Clone your repo (Use Basic Auth with Token if private)
|
||||
# Format: https://<USER>:<TOKEN>@github.com/<ORG>/<REPO>.git
|
||||
git clone <YOUR_REPO_URL> .
|
||||
|
||||
# Install dependencies
|
||||
npm install
|
||||
```
|
||||
|
||||
### 3. Setup Permissions
|
||||
```bash
|
||||
# Give ownership to www-data (Nginx user)
|
||||
chown -R www-data:www-data /var/www/<APP_NAME>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ⚙️ Step 3: Application Configuration
|
||||
|
||||
### 1. Systemd Service
|
||||
Create a service file to keep your app running.
|
||||
|
||||
Create `/etc/systemd/system/<APP_NAME>.service`:
|
||||
```ini
|
||||
[Unit]
|
||||
Description=<APP_NAME> Service
|
||||
After=network.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=root
|
||||
# OR use 'www-data' if app doesn't need root ports
|
||||
# User=www-data
|
||||
WorkingDirectory=/var/www/<APP_NAME>
|
||||
ExecStart=/usr/local/bin/node server.js
|
||||
Restart=always
|
||||
Environment=NODE_ENV=production
|
||||
Environment=PORT=4001
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
Enable and start:
|
||||
```bash
|
||||
systemctl daemon-reload
|
||||
systemctl enable <APP_NAME>
|
||||
systemctl start <APP_NAME>
|
||||
```
|
||||
|
||||
### 2. Nginx Reverse Proxy
|
||||
Configure Nginx to forward port 80 to your app (Port 4001).
|
||||
|
||||
Create `/etc/nginx/sites-available/<APP_NAME>`:
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name _;
|
||||
|
||||
root /var/www/<APP_NAME>;
|
||||
index index.html;
|
||||
|
||||
# Serve static files (Optional)
|
||||
location / {
|
||||
try_files $uri $uri/ =404;
|
||||
}
|
||||
|
||||
# Proxy API/Dynamic requests
|
||||
location /api {
|
||||
proxy_pass http://localhost:4001;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection 'upgrade';
|
||||
proxy_set_header Host $host;
|
||||
proxy_cache_bypass $http_upgrade;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Enable site:
|
||||
```bash
|
||||
# Remove defaults
|
||||
rm -f /etc/nginx/sites-enabled/default
|
||||
rm -f /etc/nginx/sites-enabled/nodejs
|
||||
|
||||
# Link new site
|
||||
ln -s /etc/nginx/sites-available/<APP_NAME> /etc/nginx/sites-enabled/
|
||||
|
||||
# Reload
|
||||
nginx -t && systemctl reload nginx
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## ☁️ Step 4: Cloudflare Tunnel (Secure Access)
|
||||
|
||||
Expose your app securely without opening router ports.
|
||||
|
||||
### 1. Install Cloudflared
|
||||
```bash
|
||||
# Add Key
|
||||
mkdir -p --mode=0755 /usr/share/keyrings
|
||||
curl -fsSL https://pkg.cloudflare.com/cloudflare-public-v2.gpg | tee /usr/share/keyrings/cloudflare-public-v2.gpg >/dev/null
|
||||
|
||||
# Add Repo
|
||||
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-public-v2.gpg] https://pkg.cloudflare.com/cloudflared any main' | tee /etc/apt/sources.list.d/cloudflared.list
|
||||
|
||||
# Install
|
||||
apt-get update && apt-get install -y cloudflared
|
||||
```
|
||||
|
||||
### 2. Create Tunnel
|
||||
```bash
|
||||
cloudflared tunnel login
|
||||
cloudflared tunnel create <TUNNEL_NAME>
|
||||
# Follow on-screen instructions to map domain -> http://localhost:4001
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔄 Step 5: Automated Updates (PowerShell)
|
||||
|
||||
Create a script `deploy-remote.ps1` in your project root to automate updates.
|
||||
|
||||
**Pre-requisite**: Create `deploy-config.json` (Add to .gitignore!):
|
||||
```json
|
||||
{
|
||||
"host": "<SERVER_IP>",
|
||||
"username": "root",
|
||||
"password": "<SSH_PASSWORD>",
|
||||
"remotePath": "/var/www/<APP_NAME>"
|
||||
}
|
||||
```
|
||||
|
||||
**Script `deploy-remote.ps1`**:
|
||||
```powershell
|
||||
# Reads config and updates remote server
|
||||
$Config = Get-Content "deploy-config.json" | ConvertFrom-Json
|
||||
$User = $Config.username; $HostName = $Config.host; $Pass = $Config.password
|
||||
$RemotePath = $Config.remotePath
|
||||
|
||||
# Commands to run remotely
|
||||
$Cmds = "
|
||||
cd $RemotePath
|
||||
echo '⬇️ Pulling code...'
|
||||
git pull
|
||||
echo '📦 Installing deps...'
|
||||
npm install
|
||||
echo '🚀 Restarting service...'
|
||||
systemctl restart <APP_NAME>
|
||||
systemctl status <APP_NAME> --no-pager
|
||||
"
|
||||
|
||||
echo y | plink -ssh -t -pw $Pass "$User@$HostName" $Cmds
|
||||
```
|
||||
|
||||
**Usage**: Just run `./deploy-remote.ps1` to deploy!
|
||||
|
||||
250
README.md
250
README.md
@@ -1,125 +1,125 @@
|
||||
# Beyond Cloud Technology - Website Stress Test
|
||||
|
||||

|
||||
|
||||
## 🌐 Public Access
|
||||
**Live URL:** [https://website-stress-test.beyondcloud.technology/](https://website-stress-test.beyondcloud.technology/)
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Overview
|
||||
The **Website Stress Test** is a professional-grade load testing tool designed to simulate realistic traffic patterns on your web applications. It helps developers and QA engineers identify bottlenecks, test scalability, and ensure production readiness.
|
||||
|
||||
Built with a **modern, high-performance architecture**, it includes a custom NodeJS proxy server to bypass CORS restrictions and allow testing of any target URL.
|
||||
|
||||
## ✨ Key Features
|
||||
|
||||
### 🛠️ Core Functionality
|
||||
* **Custom HTTP Methods**: Support for GET, POST, PUT, DELETE, and PATCH.
|
||||
* **Configurable Load**: Adjust concurrent users (up to 5000) and test duration.
|
||||
* **Traffic Patterns**:
|
||||
* **Steady**: Constant load.
|
||||
* **Burst**: Sudden spikes to test resilience.
|
||||
* **Ramp-up**: Gradual increase to find breaking points.
|
||||
* **Random**: Simulate unpredictable real-world traffic.
|
||||
* **Crawler Mode**: Automatically crawls the target website to test multiple pages and paths, not just the entry point.
|
||||
|
||||
### 📊 Real-Time Analytics
|
||||
* **Interactive Charts**: Live visualization of Requests Per Second (RPS) and Response Times.
|
||||
* **Detailed Metrics**: Track Active Users, Bandwidth, Success Rates, and Error breakdown (4xx, 5xx, Timeouts).
|
||||
* **Percentiles**: Monitor P50, P95, and P99 latency metrics.
|
||||
|
||||
### 🎨 User Experience
|
||||
* **Modern UI**: Sleek, glassmorphism-inspired design with Light/Dark mode support.
|
||||
* **Git Versioning**: Automatic display of the current Git Commit ID and deployment age in the UI.
|
||||
* **Responsive Design**: Fully functional on desktop and tablet devices.
|
||||
|
||||
---
|
||||
|
||||
## 📦 Installation & Setup
|
||||
|
||||
### Prerequisites
|
||||
* Node.js (v18 or higher)
|
||||
* Nginx (for production deployment)
|
||||
* PM2 (for process management)
|
||||
|
||||
### 💻 Local Development
|
||||
1. **Clone the Repository**
|
||||
```bash
|
||||
git clone https://github.com/DeNNiiInc/Website-Stress-Test.git
|
||||
cd Website-Stress-Test
|
||||
```
|
||||
|
||||
2. **Install Dependencies**
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
3. **Start the Proxy Server**
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
The server will start on `http://localhost:3000`.
|
||||
|
||||
4. **Open the Application**
|
||||
Open `index.html` in your browser or serve it using a static file server (e.g., Live Server).
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Deployment Guide (Proxmox / Ubuntu)
|
||||
|
||||
This project includes automated deployment scripts for Proxmox/Ubuntu environments.
|
||||
|
||||
### 1. Configuration
|
||||
Copy `deploy-config.example.json` to `deploy-config.json` and update with your server details:
|
||||
```json
|
||||
{
|
||||
"host": "YOUR_SERVER_IP",
|
||||
"username": "root",
|
||||
"password": "YOUR_PASSWORD",
|
||||
"remotePath": "/var/www/website-stress-test",
|
||||
"repoUrl": "https://github.com/DeNNiiInc/Website-Stress-Test.git",
|
||||
"githubToken": "YOUR_GITHUB_TOKEN",
|
||||
"appName": "website-stress-test"
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Auto-Deployment
|
||||
Run the PowerShell deployment script:
|
||||
```powershell
|
||||
./start-deployment.ps1
|
||||
```
|
||||
This script will:
|
||||
* Connect to your server via SSH.
|
||||
* Install Nginx and Node.js if missing.
|
||||
* Clone/Pull the latest code.
|
||||
* Configure Nginx as a reverse proxy.
|
||||
* Set up a Cron job for auto-updates.
|
||||
|
||||
### 3. Auto-Sync
|
||||
The system automatically checks for Git updates every 5 minutes. If changes are detected, it pulls the code, installs dependencies, and restarts the backend process without downtime.
|
||||
|
||||
**Manual Update Trigger:**
|
||||
```bash
|
||||
/var/www/website-stress-test/auto-sync.sh
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Architecture
|
||||
|
||||
### Backend (`proxy-server.js`)
|
||||
* **Role**: Handles CORS requests and authenticates traffic.
|
||||
* **Port**: 3000 (Internal).
|
||||
* **Endpoints**:
|
||||
* `/proxy`: Forwards stress test requests.
|
||||
* `/git-info`: Returns current commit hash and deployment date.
|
||||
|
||||
### Frontend (`index.html` + `script.js`)
|
||||
* **Technology**: Vanilla JS + Chart.js.
|
||||
* **Communication**: Fetch API to the Proxy Server.
|
||||
|
||||
---
|
||||
|
||||
## 📝 License
|
||||
MIT License - Copyright (c) 2025 Beyond Cloud Technology.
|
||||
# Beyond Cloud Technology - Website Stress Test
|
||||
|
||||

|
||||
|
||||
## 🌐 Public Access
|
||||
**Live URL:** [https://website-stress-test.beyondcloud.technology/](https://website-stress-test.beyondcloud.technology/)
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Overview
|
||||
The **Website Stress Test** is a professional-grade load testing tool designed to simulate realistic traffic patterns on your web applications. It helps developers and QA engineers identify bottlenecks, test scalability, and ensure production readiness.
|
||||
|
||||
Built with a **modern, high-performance architecture**, it includes a custom NodeJS proxy server to bypass CORS restrictions and allow testing of any target URL.
|
||||
|
||||
## ✨ Key Features
|
||||
|
||||
### 🛠️ Core Functionality
|
||||
* **Custom HTTP Methods**: Support for GET, POST, PUT, DELETE, and PATCH.
|
||||
* **Configurable Load**: Adjust concurrent users (up to 5000) and test duration.
|
||||
* **Traffic Patterns**:
|
||||
* **Steady**: Constant load.
|
||||
* **Burst**: Sudden spikes to test resilience.
|
||||
* **Ramp-up**: Gradual increase to find breaking points.
|
||||
* **Random**: Simulate unpredictable real-world traffic.
|
||||
* **Crawler Mode**: Automatically crawls the target website to test multiple pages and paths, not just the entry point.
|
||||
|
||||
### 📊 Real-Time Analytics
|
||||
* **Interactive Charts**: Live visualization of Requests Per Second (RPS) and Response Times.
|
||||
* **Detailed Metrics**: Track Active Users, Bandwidth, Success Rates, and Error breakdown (4xx, 5xx, Timeouts).
|
||||
* **Percentiles**: Monitor P50, P95, and P99 latency metrics.
|
||||
|
||||
### 🎨 User Experience
|
||||
* **Modern UI**: Sleek, glassmorphism-inspired design with Light/Dark mode support.
|
||||
* **Git Versioning**: Automatic display of the current Git Commit ID and deployment age in the UI.
|
||||
* **Responsive Design**: Fully functional on desktop and tablet devices.
|
||||
|
||||
---
|
||||
|
||||
## 📦 Installation & Setup
|
||||
|
||||
### Prerequisites
|
||||
* Node.js (v18 or higher)
|
||||
* Nginx (for production deployment)
|
||||
* PM2 (for process management)
|
||||
|
||||
### 💻 Local Development
|
||||
1. **Clone the Repository**
|
||||
```bash
|
||||
git clone https://github.com/DeNNiiInc/Website-Stress-Test.git
|
||||
cd Website-Stress-Test
|
||||
```
|
||||
|
||||
2. **Install Dependencies**
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
3. **Start the Proxy Server**
|
||||
```bash
|
||||
npm start
|
||||
```
|
||||
The server will start on `http://localhost:3000`.
|
||||
|
||||
4. **Open the Application**
|
||||
Open `index.html` in your browser or serve it using a static file server (e.g., Live Server).
|
||||
|
||||
---
|
||||
|
||||
## 🚀 Deployment Guide (Proxmox / Ubuntu)
|
||||
|
||||
This project includes automated deployment scripts for Proxmox/Ubuntu environments.
|
||||
|
||||
### 1. Configuration
|
||||
Copy `deploy-config.example.json` to `deploy-config.json` and update with your server details:
|
||||
```json
|
||||
{
|
||||
"host": "YOUR_SERVER_IP",
|
||||
"username": "root",
|
||||
"password": "YOUR_PASSWORD",
|
||||
"remotePath": "/var/www/website-stress-test",
|
||||
"repoUrl": "https://github.com/DeNNiiInc/Website-Stress-Test.git",
|
||||
"githubToken": "YOUR_GITHUB_TOKEN",
|
||||
"appName": "website-stress-test"
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Auto-Deployment
|
||||
Run the PowerShell deployment script:
|
||||
```powershell
|
||||
./start-deployment.ps1
|
||||
```
|
||||
This script will:
|
||||
* Connect to your server via SSH.
|
||||
* Install Nginx and Node.js if missing.
|
||||
* Clone/Pull the latest code.
|
||||
* Configure Nginx as a reverse proxy.
|
||||
* Set up a Cron job for auto-updates.
|
||||
|
||||
### 3. Auto-Sync
|
||||
The system automatically checks for Git updates every 5 minutes. If changes are detected, it pulls the code, installs dependencies, and restarts the backend process without downtime.
|
||||
|
||||
**Manual Update Trigger:**
|
||||
```bash
|
||||
/var/www/website-stress-test/auto-sync.sh
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Architecture
|
||||
|
||||
### Backend (`proxy-server.js`)
|
||||
* **Role**: Handles CORS requests and authenticates traffic.
|
||||
* **Port**: 3000 (Internal).
|
||||
* **Endpoints**:
|
||||
* `/proxy`: Forwards stress test requests.
|
||||
* `/git-info`: Returns current commit hash and deployment date.
|
||||
|
||||
### Frontend (`index.html` + `script.js`)
|
||||
* **Technology**: Vanilla JS + Chart.js.
|
||||
* **Communication**: Fetch API to the Proxy Server.
|
||||
|
||||
---
|
||||
|
||||
## 📝 License
|
||||
MIT License - Copyright (c) 2025 Beyond Cloud Technology.
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
{
|
||||
"host": "YOUR_SERVER_IP",
|
||||
"username": "root",
|
||||
"password": "YOUR_SSH_PASSWORD",
|
||||
"remotePath": "/var/www/website-stress-test",
|
||||
"repoUrl": "https://github.com/DeNNiiInc/Website-Stress-Test.git",
|
||||
"githubToken": "YOUR_GITHUB_TOKEN",
|
||||
"appName": "website-stress-test"
|
||||
}
|
||||
{
|
||||
"host": "YOUR_SERVER_IP",
|
||||
"username": "root",
|
||||
"password": "YOUR_SSH_PASSWORD",
|
||||
"remotePath": "/var/www/website-stress-test",
|
||||
"repoUrl": "https://github.com/DeNNiiInc/Website-Stress-Test.git",
|
||||
"githubToken": "YOUR_GITHUB_TOKEN",
|
||||
"appName": "website-stress-test"
|
||||
}
|
||||
|
||||
1054
index.html
1054
index.html
File diff suppressed because it is too large
Load Diff
34
package.json
34
package.json
@@ -1,17 +1,17 @@
|
||||
{
|
||||
"name": "stress-testing-tool",
|
||||
"version": "1.0.0",
|
||||
"description": "Website stress testing tool with CORS proxy",
|
||||
"main": "proxy-server.js",
|
||||
"scripts": {
|
||||
"start": "node proxy-server.js",
|
||||
"proxy": "node proxy-server.js"
|
||||
},
|
||||
"keywords": [
|
||||
"stress-testing",
|
||||
"load-testing",
|
||||
"cors-proxy"
|
||||
],
|
||||
"author": "",
|
||||
"license": "MIT"
|
||||
}
|
||||
{
|
||||
"name": "stress-testing-tool",
|
||||
"version": "1.0.0",
|
||||
"description": "Website stress testing tool with CORS proxy",
|
||||
"main": "proxy-server.js",
|
||||
"scripts": {
|
||||
"start": "node proxy-server.js",
|
||||
"proxy": "node proxy-server.js"
|
||||
},
|
||||
"keywords": [
|
||||
"stress-testing",
|
||||
"load-testing",
|
||||
"cors-proxy"
|
||||
],
|
||||
"author": "",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
||||
624
proxy-server.js
624
proxy-server.js
@@ -1,273 +1,351 @@
|
||||
// ===================================
|
||||
// CORS PROXY SERVER
|
||||
// ===================================
|
||||
// This proxy server allows the stress testing tool to test
|
||||
// production websites without CORS restrictions.
|
||||
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const url = require('url');
|
||||
|
||||
const PORT = 3000;
|
||||
|
||||
// Configuration
|
||||
const CONFIG = {
|
||||
// Maximum request timeout (30 seconds)
|
||||
timeout: 30000,
|
||||
|
||||
// Allowed origins (restrict to your stress testing tool's domain)
|
||||
// Use '*' for development, specific domain for production
|
||||
allowedOrigins: '*',
|
||||
|
||||
// Maximum concurrent connections
|
||||
maxConnections: 5000,
|
||||
|
||||
// User agents for rotation
|
||||
userAgents: [
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0',
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15',
|
||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
]
|
||||
};
|
||||
|
||||
// Get random user agent
|
||||
function getRandomUserAgent() {
|
||||
return CONFIG.userAgents[Math.floor(Math.random() * CONFIG.userAgents.length)];
|
||||
}
|
||||
|
||||
const { exec } = require('child_process');
|
||||
|
||||
// Helper to get git info
|
||||
const getGitInfo = () => {
|
||||
return new Promise((resolve) => {
|
||||
exec('git rev-parse --short HEAD && git log -1 --format=%cd --date=relative', (err, stdout) => {
|
||||
if (err) {
|
||||
console.error('Error fetching git info:', err);
|
||||
resolve({ commit: 'Unknown', date: 'Unknown' });
|
||||
return;
|
||||
}
|
||||
const parts = stdout.trim().split('\n');
|
||||
resolve({
|
||||
commit: parts[0] || 'Unknown',
|
||||
date: parts[1] || 'Unknown'
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
// Create the proxy server
|
||||
const server = http.createServer((req, res) => {
|
||||
// Handle CORS preflight requests
|
||||
if (req.method === 'OPTIONS') {
|
||||
handleCORS(res);
|
||||
res.writeHead(200);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle Git Info request
|
||||
// Nginx proxy_pass might result in double slashes (//git-info)
|
||||
if ((req.url === '/git-info' || req.url === '//git-info') && req.method === 'GET') {
|
||||
handleCORS(res);
|
||||
getGitInfo().then(info => {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(info));
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Only allow POST requests to the proxy
|
||||
if (req.method !== 'POST') {
|
||||
res.writeHead(405, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Method not allowed. Use POST.' }));
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse request body
|
||||
let body = '';
|
||||
req.on('data', chunk => {
|
||||
body += chunk.toString();
|
||||
});
|
||||
|
||||
req.on('end', () => {
|
||||
try {
|
||||
const proxyRequest = JSON.parse(body);
|
||||
handleProxyRequest(proxyRequest, res);
|
||||
} catch (error) {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
error: 'Invalid JSON',
|
||||
message: error.message
|
||||
}));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Handle the actual proxy request
|
||||
function handleProxyRequest(proxyRequest, clientRes) {
|
||||
const { targetUrl, method = 'GET', headers = {}, body = null } = proxyRequest;
|
||||
|
||||
// Validate target URL
|
||||
if (!targetUrl) {
|
||||
clientRes.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
clientRes.end(JSON.stringify({ error: 'targetUrl is required' }));
|
||||
return;
|
||||
}
|
||||
|
||||
let parsedUrl;
|
||||
try {
|
||||
parsedUrl = new URL(targetUrl);
|
||||
} catch (error) {
|
||||
clientRes.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
clientRes.end(JSON.stringify({ error: 'Invalid URL' }));
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine if we need http or https
|
||||
const protocol = parsedUrl.protocol === 'https:' ? https : http;
|
||||
|
||||
// Prepare request options with random user agent
|
||||
const options = {
|
||||
hostname: parsedUrl.hostname,
|
||||
port: parsedUrl.port,
|
||||
path: parsedUrl.pathname + parsedUrl.search,
|
||||
method: method,
|
||||
headers: {
|
||||
...headers,
|
||||
'User-Agent': getRandomUserAgent(),
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
||||
'Accept-Language': 'en-US,en;q=0.5',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'DNT': '1',
|
||||
'Connection': 'keep-alive',
|
||||
'Upgrade-Insecure-Requests': '1'
|
||||
},
|
||||
timeout: CONFIG.timeout
|
||||
};
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
// Make the request to the target server
|
||||
const proxyReq = protocol.request(options, (proxyRes) => {
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
// Collect response data
|
||||
let responseData = '';
|
||||
let responseSize = 0;
|
||||
const maxBodySize = 500000; // 500KB limit for crawler
|
||||
|
||||
proxyRes.on('data', chunk => {
|
||||
responseSize += chunk.length;
|
||||
// Only collect body if under size limit (for crawler)
|
||||
if (responseSize < maxBodySize) {
|
||||
responseData += chunk.toString();
|
||||
}
|
||||
});
|
||||
|
||||
proxyRes.on('end', () => {
|
||||
// Send response back to client with CORS headers
|
||||
handleCORS(clientRes);
|
||||
clientRes.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
|
||||
clientRes.end(JSON.stringify({
|
||||
success: true,
|
||||
statusCode: proxyRes.statusCode,
|
||||
statusMessage: proxyRes.statusMessage,
|
||||
responseTime: responseTime,
|
||||
headers: proxyRes.headers,
|
||||
body: responseData, // Full body for crawler link extraction
|
||||
bodySize: responseSize
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
// Handle request errors
|
||||
proxyReq.on('error', (error) => {
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
handleCORS(clientRes);
|
||||
clientRes.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
|
||||
clientRes.end(JSON.stringify({
|
||||
success: false,
|
||||
error: error.message,
|
||||
responseTime: responseTime,
|
||||
statusCode: 0
|
||||
}));
|
||||
});
|
||||
|
||||
// Handle timeout
|
||||
proxyReq.on('timeout', () => {
|
||||
proxyReq.destroy();
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
handleCORS(clientRes);
|
||||
clientRes.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
|
||||
clientRes.end(JSON.stringify({
|
||||
success: false,
|
||||
error: 'Request timeout',
|
||||
responseTime: responseTime,
|
||||
statusCode: 0
|
||||
}));
|
||||
});
|
||||
|
||||
// Send request body if present
|
||||
if (body && method !== 'GET' && method !== 'HEAD') {
|
||||
proxyReq.write(typeof body === 'string' ? body : JSON.stringify(body));
|
||||
}
|
||||
|
||||
proxyReq.end();
|
||||
}
|
||||
|
||||
// Add CORS headers to response
|
||||
function handleCORS(res) {
|
||||
res.setHeader('Access-Control-Allow-Origin', CONFIG.allowedOrigins);
|
||||
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||||
}
|
||||
|
||||
// Start the server
|
||||
server.listen(PORT, () => {
|
||||
console.log(`
|
||||
╔════════════════════════════════════════════════════════════╗
|
||||
║ CORS Proxy Server for Stress Testing Tool ║
|
||||
╚════════════════════════════════════════════════════════════╝
|
||||
|
||||
✅ Server running on: http://localhost:${PORT}
|
||||
✅ Max connections: ${CONFIG.maxConnections}
|
||||
✅ Request timeout: ${CONFIG.timeout}ms
|
||||
|
||||
📝 Usage:
|
||||
POST to http://localhost:${PORT} with JSON body:
|
||||
{
|
||||
"targetUrl": "https://example.com",
|
||||
"method": "GET",
|
||||
"headers": {},
|
||||
"body": null
|
||||
}
|
||||
|
||||
🔒 Security Note:
|
||||
For production, update CONFIG.allowedOrigins to your
|
||||
stress testing tool's domain (not '*')
|
||||
|
||||
Press Ctrl+C to stop the server
|
||||
`);
|
||||
});
|
||||
|
||||
// Handle server errors
|
||||
server.on('error', (error) => {
|
||||
console.error('❌ Server error:', error.message);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// Graceful shutdown
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n\n🛑 Shutting down proxy server...');
|
||||
server.close(() => {
|
||||
console.log('✅ Server closed');
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
// ===================================
|
||||
// CORS PROXY SERVER
|
||||
// ===================================
|
||||
// This proxy server allows the stress testing tool to test
|
||||
// production websites without CORS restrictions.
|
||||
|
||||
const http = require('http');
|
||||
const https = require('https');
|
||||
const url = require('url');
|
||||
const cluster = require('cluster');
|
||||
const numCPUs = require('os').cpus().length;
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
// Configuration
|
||||
const CONFIG = {
|
||||
// Maximum request timeout (30 seconds)
|
||||
timeout: 30000,
|
||||
|
||||
// Allowed origins (restrict to your stress testing tool's domain)
|
||||
// Use '*' for development, specific domain for production
|
||||
allowedOrigins: '*',
|
||||
|
||||
// Maximum concurrent connections
|
||||
maxConnections: 10000, // Increased for cluster
|
||||
|
||||
// User agents for rotation
|
||||
userAgents: [
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0',
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15',
|
||||
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
||||
]
|
||||
};
|
||||
|
||||
// Global agents for connection pooling
|
||||
const httpAgent = new http.Agent({ keepAlive: true, maxSockets: Infinity });
|
||||
const httpsAgent = new https.Agent({ keepAlive: true, maxSockets: Infinity });
|
||||
|
||||
// Get random user agent
|
||||
function getRandomUserAgent() {
|
||||
return CONFIG.userAgents[Math.floor(Math.random() * CONFIG.userAgents.length)];
|
||||
}
|
||||
|
||||
const { exec } = require('child_process');
|
||||
|
||||
// Helper to get git info
|
||||
const getGitInfo = () => {
|
||||
return new Promise((resolve) => {
|
||||
exec('git rev-parse --short HEAD && git log -1 --format=%cd --date=relative', (err, stdout) => {
|
||||
if (err) {
|
||||
resolve({ commit: 'Unknown', date: 'Unknown' });
|
||||
return;
|
||||
}
|
||||
const parts = stdout.trim().split('\n');
|
||||
resolve({
|
||||
commit: parts[0] || 'Unknown',
|
||||
date: parts[1] || 'Unknown'
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
if (cluster.isMaster) {
|
||||
console.log(`Master ${process.pid} is running`);
|
||||
console.log(`Spawning ${numCPUs} workers...`);
|
||||
|
||||
for (let i = 0; i < numCPUs; i++) {
|
||||
cluster.fork();
|
||||
}
|
||||
|
||||
cluster.on('exit', (worker, code, signal) => {
|
||||
console.log(`Worker ${worker.process.pid} died. Respawning...`);
|
||||
cluster.fork();
|
||||
});
|
||||
|
||||
// Master process only listens for SIGINT to gracefully shut down workers
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n\n🛑 Shutting down proxy server (master)...');
|
||||
for (const id in cluster.workers) {
|
||||
cluster.workers[id].kill();
|
||||
}
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
} else {
|
||||
// Create the proxy server
|
||||
const server = http.createServer((req, res) => {
|
||||
// Handle CORS preflight requests
|
||||
if (req.method === 'OPTIONS') {
|
||||
handleCORS(res);
|
||||
res.writeHead(200);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// Health check
|
||||
if (req.url === '/health' || req.url === '//health') {
|
||||
handleCORS(res);
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ status: 'ok', worker: process.pid }));
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle Git Info request
|
||||
// Nginx proxy_pass might result in double slashes (//git-info)
|
||||
if ((req.url === '/git-info' || req.url === '//git-info') && req.method === 'GET') {
|
||||
handleCORS(res);
|
||||
getGitInfo().then(info => {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(info));
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Serve static files for the UI
|
||||
if (req.method === 'GET') {
|
||||
let requestPath = req.url.split('?')[0];
|
||||
let filePath = '.' + requestPath;
|
||||
if (requestPath === '/') filePath = './index.html';
|
||||
|
||||
// Basic security: don't allow accessing files outside the directory or sensitive files
|
||||
const resolvedPath = path.resolve(filePath);
|
||||
const rootPath = path.resolve('.');
|
||||
|
||||
if (!resolvedPath.startsWith(rootPath) || filePath.includes('..')) {
|
||||
res.writeHead(403);
|
||||
res.end('Forbidden');
|
||||
return;
|
||||
}
|
||||
|
||||
fs.access(filePath, fs.constants.F_OK, (err) => {
|
||||
if (!err) {
|
||||
const extname = path.extname(filePath).toLowerCase();
|
||||
let contentType = 'text/html';
|
||||
const mimeTypes = {
|
||||
'.html': 'text/html',
|
||||
'.js': 'text/javascript',
|
||||
'.css': 'text/css',
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.json': 'application/json'
|
||||
};
|
||||
contentType = mimeTypes[extname] || 'text/plain';
|
||||
|
||||
fs.readFile(filePath, (error, content) => {
|
||||
if (!error) {
|
||||
res.writeHead(200, { 'Content-Type': contentType });
|
||||
res.end(content, 'utf-8');
|
||||
} else {
|
||||
res.writeHead(500);
|
||||
res.end('Server Error');
|
||||
}
|
||||
});
|
||||
} else if (req.url === '/health' || req.url === '//health' || (req.url === '/git-info' || req.url === '//git-info')) {
|
||||
// Handled by other logic (keep going)
|
||||
} else if (req.url === '/') {
|
||||
// Fallback for root if index.html doesn't exist? (Unlikely)
|
||||
} else {
|
||||
// Not a static file, maybe fall through to POST check
|
||||
}
|
||||
});
|
||||
|
||||
// Special handling for health and git-info which are GET but not files
|
||||
if (req.url.includes('/health') || req.url.includes('/git-info')) {
|
||||
// Let it fall through to those handlers
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Only allow POST requests to the proxy
|
||||
if (req.method !== 'POST') {
|
||||
res.writeHead(405, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Method not allowed. Use POST.' }));
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse request body
|
||||
let body = '';
|
||||
req.on('data', chunk => {
|
||||
body += chunk.toString();
|
||||
});
|
||||
|
||||
req.on('end', () => {
|
||||
try {
|
||||
const proxyRequest = JSON.parse(body);
|
||||
handleProxyRequest(proxyRequest, res);
|
||||
} catch (error) {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
error: 'Invalid JSON',
|
||||
message: error.message
|
||||
}));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Handle the actual proxy request
|
||||
function handleProxyRequest(proxyRequest, clientRes) {
|
||||
const { targetUrl, method = 'GET', headers = {}, body = null } = proxyRequest;
|
||||
|
||||
// Validate target URL
|
||||
if (!targetUrl) {
|
||||
clientRes.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
clientRes.end(JSON.stringify({ error: 'targetUrl is required' }));
|
||||
return;
|
||||
}
|
||||
|
||||
let parsedUrl;
|
||||
try {
|
||||
parsedUrl = new URL(targetUrl);
|
||||
} catch (error) {
|
||||
clientRes.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
clientRes.end(JSON.stringify({ error: 'Invalid URL' }));
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine if we need http or https and which agent to use
|
||||
const isHttps = parsedUrl.protocol === 'https:';
|
||||
const protocol = isHttps ? https : http;
|
||||
const agent = isHttps ? httpsAgent : httpAgent;
|
||||
|
||||
// Prepare request options with random user agent
|
||||
const options = {
|
||||
hostname: parsedUrl.hostname,
|
||||
port: parsedUrl.port,
|
||||
path: parsedUrl.pathname + parsedUrl.search,
|
||||
method: method,
|
||||
agent: agent, // Use the global agent for connection pooling
|
||||
headers: {
|
||||
...headers,
|
||||
'User-Agent': getRandomUserAgent(),
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
||||
'Accept-Language': 'en-US,en;q=0.5',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'DNT': '1',
|
||||
'Connection': 'keep-alive',
|
||||
'Upgrade-Insecure-Requests': '1'
|
||||
},
|
||||
timeout: CONFIG.timeout
|
||||
};
|
||||
|
||||
const startTime = Date.now();
|
||||
|
||||
// Make the request to the target server
|
||||
const proxyReq = protocol.request(options, (proxyRes) => {
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
// Collect response data
|
||||
let responseData = '';
|
||||
let responseSize = 0;
|
||||
const maxBodySize = 500000; // 500KB limit for crawler
|
||||
|
||||
proxyRes.on('data', chunk => {
|
||||
responseSize += chunk.length;
|
||||
// Only collect body if under size limit (for crawler)
|
||||
if (responseSize < maxBodySize) {
|
||||
responseData += chunk.toString();
|
||||
}
|
||||
});
|
||||
|
||||
proxyRes.on('end', () => {
|
||||
// Send response back to client with CORS headers
|
||||
handleCORS(clientRes);
|
||||
clientRes.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
|
||||
clientRes.end(JSON.stringify({
|
||||
success: true,
|
||||
statusCode: proxyRes.statusCode,
|
||||
statusMessage: proxyRes.statusMessage,
|
||||
responseTime: responseTime,
|
||||
headers: proxyRes.headers,
|
||||
body: responseData, // Full body for crawler link extraction
|
||||
bodySize: responseSize,
|
||||
proxyWorker: process.pid // Add worker ID for debugging
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
// Handle request errors
|
||||
proxyReq.on('error', (error) => {
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
handleCORS(clientRes);
|
||||
clientRes.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
|
||||
clientRes.end(JSON.stringify({
|
||||
success: false,
|
||||
error: error.message,
|
||||
responseTime: responseTime,
|
||||
statusCode: 0
|
||||
}));
|
||||
});
|
||||
|
||||
// Handle timeout
|
||||
proxyReq.on('timeout', () => {
|
||||
proxyReq.destroy();
|
||||
const responseTime = Date.now() - startTime;
|
||||
|
||||
handleCORS(clientRes);
|
||||
clientRes.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
|
||||
clientRes.end(JSON.stringify({
|
||||
success: false,
|
||||
error: 'Request timeout',
|
||||
responseTime: responseTime,
|
||||
statusCode: 0
|
||||
}));
|
||||
});
|
||||
|
||||
// Send request body if present
|
||||
if (body && method !== 'GET' && method !== 'HEAD') {
|
||||
proxyReq.write(typeof body === 'string' ? body : JSON.stringify(body));
|
||||
}
|
||||
|
||||
proxyReq.end();
|
||||
}
|
||||
|
||||
// Add CORS headers to response
|
||||
function handleCORS(res) {
|
||||
res.setHeader('Access-Control-Allow-Origin', CONFIG.allowedOrigins);
|
||||
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||||
}
|
||||
|
||||
// Start the server
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Worker ${process.pid} running on http://localhost:${PORT}`);
|
||||
});
|
||||
|
||||
// Handle server errors
|
||||
server.on('error', (error) => {
|
||||
console.error(`❌ Worker ${process.pid} server error:`, error.message);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
// Graceful shutdown for workers
|
||||
process.on('SIGINT', () => {
|
||||
console.log(`\n\n🛑 Worker ${process.pid} shutting down...`);
|
||||
server.close(() => {
|
||||
console.log(`✅ Worker ${process.pid} closed`);
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,23 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
# setup-server.sh - Initial Setup Script
|
||||
|
||||
# 1. Install Global Dependencies
|
||||
# 1. System Tuning for High Concurrency
|
||||
echo "Tuning system limits..."
|
||||
# Increase max open files for high connection counts
|
||||
if ! grep -q "soft nofile 65535" /etc/security/limits.conf; then
|
||||
echo "* soft nofile 65535" >> /etc/security/limits.conf
|
||||
echo "* hard nofile 65535" >> /etc/security/limits.conf
|
||||
fi
|
||||
# Apply limits to current session (for the rest of this script)
|
||||
ulimit -n 65535
|
||||
|
||||
# 2. Install Global Dependencies
|
||||
echo "Installing PM2..."
|
||||
npm install -g pm2
|
||||
|
||||
# 2. Clone Repository
|
||||
# Expects: REPO_URL, APP_DIR, GITHUB_TOKEN inside the script or env
|
||||
# We'll use arguments passed to this script: $1=REPO_URL $2=APP_DIR $3=GITHUB_TOKEN
|
||||
|
||||
# 3. Clone Repository
|
||||
# ... (rest of cloning logic)
|
||||
REPO_URL="$1"
|
||||
APP_DIR="$2"
|
||||
GITHUB_TOKEN="$3"
|
||||
|
||||
# Construct URL with token for auth
|
||||
# Extract host and path from REPO_URL (assuming https://github.com/user/repo.git)
|
||||
# We need to insert token: https://TOKEN@github.com/user/repo.git
|
||||
# Simple replacement:
|
||||
AUTH_REPO_URL="${REPO_URL/https:\/\//https:\/\/$GITHUB_TOKEN@}"
|
||||
|
||||
echo "Preparing application directory: $APP_DIR"
|
||||
@@ -33,18 +35,20 @@ else
|
||||
cd "$APP_DIR"
|
||||
fi
|
||||
|
||||
# 3. Install App Dependencies
|
||||
# 4. Install App Dependencies
|
||||
echo "Installing application dependencies..."
|
||||
npm install
|
||||
|
||||
# 4. Start Application with PM2
|
||||
# 5. Start Application with PM2
|
||||
APP_NAME="website-stress-test"
|
||||
echo "Starting application with PM2 ($APP_NAME)..."
|
||||
pm2 start proxy-server.js --name "$APP_NAME" --watch --ignore-watch="node_modules"
|
||||
# Using Node built-in clustering, but PM2 monitors the master
|
||||
pm2 stop "$APP_NAME" || true
|
||||
pm2 start proxy-server.js --name "$APP_NAME" --max-memory-restart 1G
|
||||
pm2 save
|
||||
pm2 startup | tail -n 1 | bash # Setup startup script
|
||||
|
||||
# 5. Setup Cron Job for Auto-Sync
|
||||
# 6. Setup Cron Job for Auto-Sync
|
||||
echo "Setting up Cron Job for auto-sync..."
|
||||
SCRIPT_PATH="$APP_DIR/auto-sync.sh"
|
||||
chmod +x "$SCRIPT_PATH"
|
||||
@@ -52,5 +56,5 @@ chmod +x "$SCRIPT_PATH"
|
||||
# Add to crontab if not exists
|
||||
(crontab -l 2>/dev/null; echo "*/5 * * * * $SCRIPT_PATH >> /var/log/app-sync.log 2>&1") | crontab -
|
||||
|
||||
echo "✅ Setup Complete! Application is running."
|
||||
echo "✅ Setup Complete! Application is running with system optimizations."
|
||||
pm2 status
|
||||
|
||||
@@ -1,73 +1,73 @@
|
||||
# start-deployment.ps1
|
||||
# Automates the deployment by reading config, uploading scripts, and executing setup.
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$ConfigPath = "deploy-config.json"
|
||||
|
||||
if (-not (Test-Path $ConfigPath)) {
|
||||
Write-Error "Configuration file '$ConfigPath' not found. Please copy 'deploy-config.example.json' to '$ConfigPath' and fill in your details."
|
||||
}
|
||||
|
||||
$Config = Get-Content $ConfigPath | ConvertFrom-Json
|
||||
|
||||
# Validate Config
|
||||
$Required = @("host", "username", "password", "remotePath", "repoUrl", "githubToken")
|
||||
foreach ($Key in $Required) {
|
||||
if (-not $Config.$Key) {
|
||||
Write-Error "Missing required config key: $Key"
|
||||
}
|
||||
}
|
||||
|
||||
$User = $Config.username
|
||||
$HostName = $Config.host
|
||||
$Pass = $Config.password
|
||||
# Note: Using password directly in script is tricky with standard ssh/scp without key.
|
||||
# We will check if 'sshpass' or 'plink' is available, or guide user to use keys.
|
||||
# Since the user specifically mentioned providing credentials, they might expect us to use them.
|
||||
# The template used 'plink -pw $Pass'. We will stick to that if available, or warn.
|
||||
|
||||
# Check for plink
|
||||
if (Get-Command "plink.exe" -ErrorAction SilentlyContinue) {
|
||||
Write-Host "Using plink for connection..."
|
||||
$UsePlink = $true
|
||||
}
|
||||
else {
|
||||
Write-Warning "plink.exe not found. Falling back to standard scp/ssh. You may be prompted for password multiple times."
|
||||
$UsePlink = $false
|
||||
}
|
||||
|
||||
$RemoteTmp = "/tmp"
|
||||
$SetupScript = "setup-server.sh"
|
||||
$SyncScript = "auto-sync.sh"
|
||||
|
||||
Write-Host "🚀 Starting Deployment to $HostName..."
|
||||
|
||||
# 1. Upload Scripts
|
||||
Write-Host "Uploading scripts..."
|
||||
if ($UsePlink) {
|
||||
echo y | pscp -P 22 -pw $Pass $SetupScript "$User@$HostName`:$RemoteTmp/$SetupScript"
|
||||
echo y | pscp -P 22 -pw $Pass $SyncScript "$User@$HostName`:$RemoteTmp/$SyncScript"
|
||||
}
|
||||
else {
|
||||
scp $SetupScript "$User@$HostName`:$RemoteTmp/$SetupScript"
|
||||
scp $SyncScript "$User@$HostName`:$RemoteTmp/$SyncScript"
|
||||
}
|
||||
|
||||
# 2. Execute Setup
|
||||
Write-Host "Executing setup on remote server..."
|
||||
$AppDir = $Config.remotePath
|
||||
$Repo = $Config.repoUrl
|
||||
$Token = $Config.githubToken
|
||||
|
||||
# Make scripts executable and run setup
|
||||
$RemoteCmd = "chmod +x $RemoteTmp/$SetupScript $RemoteTmp/$SyncScript; $RemoteTmp/$SetupScript '$Repo' '$AppDir' '$Token'; rm $RemoteTmp/$SetupScript"
|
||||
|
||||
if ($UsePlink) {
|
||||
echo y | plink -ssh -P 22 -t -pw $Pass "$User@$HostName" $RemoteCmd
|
||||
}
|
||||
else {
|
||||
ssh -t "$User@$HostName" $RemoteCmd
|
||||
}
|
||||
|
||||
Write-Host "🎉 Deployment command sent!"
|
||||
# start-deployment.ps1
|
||||
# Automates the deployment by reading config, uploading scripts, and executing setup.
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$ConfigPath = "deploy-config.json"
|
||||
|
||||
if (-not (Test-Path $ConfigPath)) {
|
||||
Write-Error "Configuration file '$ConfigPath' not found. Please copy 'deploy-config.example.json' to '$ConfigPath' and fill in your details."
|
||||
}
|
||||
|
||||
$Config = Get-Content $ConfigPath | ConvertFrom-Json
|
||||
|
||||
# Validate Config
|
||||
$Required = @("host", "username", "password", "remotePath", "repoUrl", "githubToken")
|
||||
foreach ($Key in $Required) {
|
||||
if (-not $Config.$Key) {
|
||||
Write-Error "Missing required config key: $Key"
|
||||
}
|
||||
}
|
||||
|
||||
$User = $Config.username
|
||||
$HostName = $Config.host
|
||||
$Pass = $Config.password
|
||||
# Note: Using password directly in script is tricky with standard ssh/scp without key.
|
||||
# We will check if 'sshpass' or 'plink' is available, or guide user to use keys.
|
||||
# Since the user specifically mentioned providing credentials, they might expect us to use them.
|
||||
# The template used 'plink -pw $Pass'. We will stick to that if available, or warn.
|
||||
|
||||
# Check for plink
|
||||
if (Get-Command "plink.exe" -ErrorAction SilentlyContinue) {
|
||||
Write-Host "Using plink for connection..."
|
||||
$UsePlink = $true
|
||||
}
|
||||
else {
|
||||
Write-Warning "plink.exe not found. Falling back to standard scp/ssh. You may be prompted for password multiple times."
|
||||
$UsePlink = $false
|
||||
}
|
||||
|
||||
$RemoteTmp = "/tmp"
|
||||
$SetupScript = "setup-server.sh"
|
||||
$SyncScript = "auto-sync.sh"
|
||||
|
||||
Write-Host "🚀 Starting Deployment to $HostName..."
|
||||
|
||||
# 1. Upload Scripts
|
||||
Write-Host "Uploading scripts..."
|
||||
if ($UsePlink) {
|
||||
echo y | pscp -P 22 -pw $Pass $SetupScript "$User@$HostName`:$RemoteTmp/$SetupScript"
|
||||
echo y | pscp -P 22 -pw $Pass $SyncScript "$User@$HostName`:$RemoteTmp/$SyncScript"
|
||||
}
|
||||
else {
|
||||
scp $SetupScript "$User@$HostName`:$RemoteTmp/$SetupScript"
|
||||
scp $SyncScript "$User@$HostName`:$RemoteTmp/$SyncScript"
|
||||
}
|
||||
|
||||
# 2. Execute Setup
|
||||
Write-Host "Executing setup on remote server..."
|
||||
$AppDir = $Config.remotePath
|
||||
$Repo = $Config.repoUrl
|
||||
$Token = $Config.githubToken
|
||||
|
||||
# Make scripts executable and run setup
|
||||
$RemoteCmd = "chmod +x $RemoteTmp/$SetupScript $RemoteTmp/$SyncScript; $RemoteTmp/$SetupScript '$Repo' '$AppDir' '$Token'; rm $RemoteTmp/$SetupScript"
|
||||
|
||||
if ($UsePlink) {
|
||||
echo y | plink -ssh -P 22 -t -pw $Pass "$User@$HostName" $RemoteCmd
|
||||
}
|
||||
else {
|
||||
ssh -t "$User@$HostName" $RemoteCmd
|
||||
}
|
||||
|
||||
Write-Host "🎉 Deployment command sent!"
|
||||
|
||||
2202
styles.css
2202
styles.css
File diff suppressed because it is too large
Load Diff
318
worker.js
Normal file
318
worker.js
Normal file
@@ -0,0 +1,318 @@
|
||||
// ===================================
|
||||
// STRESS TESTING TOOL - WEB WORKER
|
||||
// Handles request loops for a group of users
|
||||
// ===================================
|
||||
|
||||
let config = {};
|
||||
let state = {
|
||||
active: false,
|
||||
users: [],
|
||||
startTime: 0,
|
||||
totalRequests: 0,
|
||||
successfulRequests: 0,
|
||||
failedRequests: 0,
|
||||
responseTimes: [],
|
||||
bytesSent: 0,
|
||||
bytesReceived: 0,
|
||||
pageLoadTimes: [],
|
||||
totalAssetRequests: 0,
|
||||
errorsByCategory: {
|
||||
"4xx": 0,
|
||||
"5xx": 0,
|
||||
"timeout": 0,
|
||||
"network": 0
|
||||
}
|
||||
};
|
||||
|
||||
// Listen for messages from the main thread
|
||||
self.onmessage = function (e) {
|
||||
const { type, data } = e.data;
|
||||
|
||||
switch (type) {
|
||||
case 'INIT':
|
||||
config = data.config;
|
||||
break;
|
||||
case 'START':
|
||||
state.active = true;
|
||||
state.startTime = Date.now();
|
||||
startUsers(data.users);
|
||||
break;
|
||||
case 'STOP':
|
||||
state.active = false;
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
async function startUsers(userIndices) {
|
||||
const pattern = config.trafficPattern;
|
||||
const totalDuration = config.duration * 1000;
|
||||
|
||||
for (const index of userIndices) {
|
||||
if (!state.active) break;
|
||||
|
||||
const delay = calculateStartDelay(index, userIndices.length, pattern, totalDuration);
|
||||
|
||||
setTimeout(() => {
|
||||
if (state.active) {
|
||||
runUser(index);
|
||||
}
|
||||
}, delay);
|
||||
}
|
||||
|
||||
// Start reporting results periodically
|
||||
const reportInterval = setInterval(() => {
|
||||
if (!state.active) {
|
||||
clearInterval(reportInterval);
|
||||
return;
|
||||
}
|
||||
reportResults();
|
||||
}, 500);
|
||||
}
|
||||
|
||||
function calculateStartDelay(index, count, pattern, duration) {
|
||||
switch (pattern) {
|
||||
case 'steady':
|
||||
return (index % count) * 100;
|
||||
case 'burst':
|
||||
const burstIndex = Math.floor((index % count) / (count / 5));
|
||||
return burstIndex * (duration / 5);
|
||||
case 'rampup':
|
||||
return (index % count) * (duration / count);
|
||||
case 'random':
|
||||
return Math.random() * (duration / 2);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async function runUser(id) {
|
||||
const endTime = state.startTime + config.duration * 1000;
|
||||
let currentUrl = config.targetUrl;
|
||||
let crawlDepth = 0;
|
||||
|
||||
while (state.active && Date.now() < endTime) {
|
||||
const pageLoadStart = performance.now();
|
||||
const result = await makeRequest(currentUrl);
|
||||
let totalPageTime = result.responseTime;
|
||||
|
||||
// asset simulation
|
||||
if (config.simulateAssets && result.success && result.body) {
|
||||
const assets = extractAssets(result.body, currentUrl);
|
||||
if (assets.length > 0) {
|
||||
const assetResults = await fetchAssetsThrottled(assets);
|
||||
const pageLoadEnd = performance.now();
|
||||
totalPageTime = pageLoadEnd - pageLoadStart;
|
||||
state.pageLoadTimes.push(totalPageTime);
|
||||
state.totalAssetRequests += assets.length;
|
||||
}
|
||||
}
|
||||
|
||||
// Report individual request for history log (sampled)
|
||||
if (Math.random() < 0.1 || config.userCount < 50) {
|
||||
self.postMessage({
|
||||
type: 'LOG',
|
||||
data: {
|
||||
url: currentUrl,
|
||||
status: result.status,
|
||||
responseTime: result.responseTime,
|
||||
success: result.success,
|
||||
timestamp: new Date().toLocaleTimeString()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Logic for crawler (simplified for worker)
|
||||
if (config.crawlerEnabled && result.success && result.body && crawlDepth < config.crawlDepth) {
|
||||
const nextUrl = extractRandomLink(result.body, currentUrl);
|
||||
if (nextUrl) {
|
||||
currentUrl = nextUrl;
|
||||
crawlDepth++;
|
||||
}
|
||||
}
|
||||
|
||||
// Think time with jitter
|
||||
const jitter = 0.5 + Math.random(); // 50% to 150%
|
||||
const sleepTime = config.thinkTime * jitter;
|
||||
await new Promise(resolve => setTimeout(resolve, sleepTime));
|
||||
}
|
||||
}
|
||||
|
||||
async function makeRequest(targetUrl) {
|
||||
const startTime = performance.now();
|
||||
let result = {
|
||||
success: false,
|
||||
status: 0,
|
||||
responseTime: 0,
|
||||
body: null
|
||||
};
|
||||
|
||||
try {
|
||||
const payload = {
|
||||
targetUrl: targetUrl,
|
||||
method: config.httpMethod,
|
||||
headers: config.customHeaders,
|
||||
body: config.requestBody
|
||||
};
|
||||
|
||||
const payloadStr = JSON.stringify(payload);
|
||||
state.bytesSent += payloadStr.length;
|
||||
|
||||
const response = await fetch(config.proxyUrl, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: payloadStr
|
||||
});
|
||||
|
||||
const proxyResponse = await response.json();
|
||||
const endTime = performance.now();
|
||||
|
||||
result.responseTime = proxyResponse.responseTime || (endTime - startTime);
|
||||
result.status = proxyResponse.statusCode;
|
||||
result.success = proxyResponse.success && result.status >= 200 && result.status < 400;
|
||||
result.body = proxyResponse.body;
|
||||
|
||||
if (result.body) {
|
||||
state.bytesReceived += result.body.length;
|
||||
}
|
||||
|
||||
updateStats(result);
|
||||
|
||||
} catch (error) {
|
||||
result.responseTime = performance.now() - startTime;
|
||||
state.failedRequests++;
|
||||
state.errorsByCategory["network"]++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function updateStats(result) {
|
||||
state.totalRequests++;
|
||||
if (result.success) {
|
||||
state.successfulRequests++;
|
||||
} else {
|
||||
state.failedRequests++;
|
||||
const category = categorizeError(result.status);
|
||||
state.errorsByCategory[category]++;
|
||||
}
|
||||
state.responseTimes.push(result.responseTime);
|
||||
|
||||
// Keep response times capped in worker to save memory
|
||||
if (state.responseTimes.length > 500) {
|
||||
state.responseTimes.shift();
|
||||
}
|
||||
}
|
||||
|
||||
function categorizeError(status) {
|
||||
if (status >= 400 && status < 500) return "4xx";
|
||||
if (status >= 500) return "5xx";
|
||||
return "network";
|
||||
}
|
||||
|
||||
function reportResults() {
|
||||
self.postMessage({
|
||||
type: 'STATS',
|
||||
data: {
|
||||
totalRequests: state.totalRequests,
|
||||
successfulRequests: state.successfulRequests,
|
||||
failedRequests: state.failedRequests,
|
||||
bytesSent: state.bytesSent,
|
||||
bytesReceived: state.bytesReceived,
|
||||
errorsByCategory: state.errorsByCategory,
|
||||
responseTimes: state.responseTimes // Sampled
|
||||
}
|
||||
});
|
||||
|
||||
// Clear local counters that are cumulative but reported incrementally if needed
|
||||
// Actually, state object above is cumulative. Main thread will track totals.
|
||||
}
|
||||
|
||||
function extractRandomLink(html, baseUrl) {
|
||||
try {
|
||||
const linkRegex = /href=["'](https?:\/\/[^"']+|(?:\/[^"']+))["']/gi;
|
||||
const links = [];
|
||||
let match;
|
||||
const baseUrlObj = new URL(baseUrl);
|
||||
|
||||
while ((match = linkRegex.exec(html)) !== null) {
|
||||
let href = match[1];
|
||||
try {
|
||||
const absoluteUrl = new URL(href, baseUrl);
|
||||
if (absoluteUrl.hostname === baseUrlObj.hostname) {
|
||||
links.push(absoluteUrl.href);
|
||||
}
|
||||
} catch (e) { }
|
||||
if (links.length > 50) break; // Limit extraction
|
||||
}
|
||||
|
||||
if (links.length > 0) {
|
||||
return links[Math.floor(Math.random() * links.length)];
|
||||
}
|
||||
} catch (e) { }
|
||||
return null;
|
||||
}
|
||||
|
||||
function extractAssets(html, baseUrl) {
|
||||
const assets = [];
|
||||
try {
|
||||
// Regex for scripts, links (css), and images
|
||||
const scriptRegex = /<script\b[^>]*src=["']([^"']+)["'][^>]*>/gi;
|
||||
const linkRegex = /<link\b[^>]*href=["']([^"']+)["'][^>]*>/gi;
|
||||
const imgRegex = /<img\b[^>]*src=["']([^"']+)["'][^>]*>/gi;
|
||||
|
||||
const extract = (regex) => {
|
||||
let match;
|
||||
while ((match = regex.exec(html)) !== null) {
|
||||
try {
|
||||
const url = new URL(match[1], baseUrl).href;
|
||||
assets.push(url);
|
||||
} catch (e) { }
|
||||
if (assets.length > 20) break; // Limit per page for performance
|
||||
}
|
||||
};
|
||||
|
||||
extract(scriptRegex);
|
||||
extract(linkRegex);
|
||||
extract(imgRegex);
|
||||
} catch (e) { }
|
||||
return assets;
|
||||
}
|
||||
|
||||
async function fetchAssetsThrottled(assets) {
|
||||
const limit = 6; // Max concurrent connections like a browser
|
||||
const results = [];
|
||||
|
||||
for (let i = 0; i < assets.length; i += limit) {
|
||||
const batch = assets.slice(i, i + limit);
|
||||
const promises = batch.map(url => fetchAsset(url));
|
||||
results.push(...(await Promise.all(promises)));
|
||||
if (!state.active) break;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
async function fetchAsset(url) {
|
||||
try {
|
||||
const payload = JSON.stringify({
|
||||
targetUrl: url,
|
||||
method: 'GET',
|
||||
headers: config.customHeaders
|
||||
});
|
||||
|
||||
state.bytesSent += payload.length;
|
||||
|
||||
const response = await fetch(config.proxyUrl, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: payload
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
if (data.body) {
|
||||
state.bytesReceived += data.body.length;
|
||||
}
|
||||
return data.success;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user