Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Docker ignore file to reduce build context size
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime files
config.json
*.log

# Git and development files
.git/
.gitignore
.gitattributes
README.md

# IDE and editor files
.vscode/
.idea/
*.swp
*.swo
*~

# OS files
.DS_Store
Thumbs.db

# Test files
test/
coverage/

# Build artifacts
dist/
build/

# Temporary files
tmp/
temp/
*.tmp

# PM2 files (not needed in container)
ecosystem.config.js
.pm2/
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ node_modules

.DS_Store

.claude/
.claude/

# Docker logs and volumes
logs/
*.log
49 changes: 49 additions & 0 deletions DOCKER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Docker Examples

This directory contains various Docker configuration examples for different deployment scenarios.

## Files

- **config.docker-example.json**: Example configuration for Docker container networks
- **docker-compose.yml**: Main production compose file
- **docker-compose.dev.yml**: Development override with verbose logging

## Quick Start

1. Copy `config-example.json` to `config.json` and customize it
2. Run `docker compose up -d`
3. Your Minecraft reverse proxy is now running on port 25565

## Advanced Examples

### Multi-server setup
```json
[
{
"url_name": "survival.yourdomain.com",
"send_url": "minecraft-survival",
"send_port": 25565
},
{
"url_name": "creative.yourdomain.com",
"send_url": "minecraft-creative",
"send_port": 25565
}
]
```

### With Proxy Protocol (for use behind load balancers)
```json
[
{
"url_name": "mc.yourdomain.com",
"send_url": "minecraft-backend",
"send_port": 25565,
"proxy_protocol": {
"receive": true,
"send": true,
"version": 1
}
}
]
```
45 changes: 45 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Multi-stage build for smaller image size
FROM node:20-alpine AS builder

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install all dependencies (including dev dependencies for building)
RUN npm ci --only=production && npm cache clean --force

# Production stage
FROM node:20-alpine AS production

# Create non-root user for security
RUN addgroup -g 1001 -S nodejs && \
adduser -S minecraft -u 1001 -G nodejs

# Set working directory
WORKDIR /app

# Copy dependencies from builder stage
COPY --from=builder /app/node_modules ./node_modules

# Copy application files
COPY --chown=minecraft:nodejs package*.json ./
COPY --chown=minecraft:nodejs *.js ./
COPY --chown=minecraft:nodejs config-example.json ./

# Create config directory and set proper permissions
RUN mkdir -p /app/config && chown -R minecraft:nodejs /app

# Switch to non-root user
USER minecraft

# Expose the Minecraft proxy port
EXPOSE 25565

# Health check to ensure the service is running
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD timeout 3 nc -z localhost 25565 || exit 1

# Use the long-running server by default for production stability
CMD ["node", "reverse-proxy-server-long.js"]
70 changes: 66 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ This is a tool for multiple minecraft server on the same server with **Proxy Pro

## Usage

### Local Installation

Create config.json base on config-example.json

### Basic Configuration (Legacy Format)
#### Basic Configuration (Legacy Format)
```json
[
{ "url_name": "example1.server.com", "send_url": "localhost", "send_port": 25564 },
{ "url_name": "example2.server.com", "send_url": "localhost", "send_port": 25566 }
]
```

### Advanced Configuration with Proxy Protocol
#### Advanced Configuration with Proxy Protocol
```json
[
{
Expand All @@ -39,13 +41,13 @@ Create config.json base on config-example.json
]
```

### Proxy Protocol Configuration Options
#### Proxy Protocol Configuration Options

- `receive`: Accept incoming Proxy Protocol headers (from upstream proxies)
- `send`: Send Proxy Protocol headers to downstream servers
- `version`: Proxy Protocol version (currently only version 1 is supported)

### Use Cases
#### Use Cases

1. **Behind a Load Balancer**: Set `receive: true` to get real client IPs from your load balancer
2. **Chaining Proxies**: Use both `receive: true` and `send: true` to preserve client IPs through multiple proxy layers
Expand All @@ -58,6 +60,66 @@ npm install
npm run long
```

### Docker Usage

#### Quick Start with Docker

1. **Create configuration file**: Copy `config-example.json` to `config.json` and modify according to your setup.

2. **Using Docker directly**:
```bash
# Build the image
docker build -t minecraft-reverse-proxy .

# Run with your config
docker run -d -p 25565:25565 \
-v $(pwd)/config.json:/app/config.json:ro \
--name minecraft-proxy \
minecraft-reverse-proxy
```

3. **Using Docker Compose (Recommended)**:
```bash
# Create config.json first, then:
docker compose up -d

# View logs
docker compose logs -f

# Stop
docker compose down
```

#### Docker Configuration

**Environment Variables:**
- `VERBOSE_LEVEL`: Logging level (1=verbose, 2=info, 3=quiet) - default: 3
- `NODE_ENV`: Environment (production/development) - default: production

**Example Docker run with environment variables:**
```bash
docker run -d -p 25565:25565 \
-v $(pwd)/config.json:/app/config.json:ro \
-e VERBOSE_LEVEL=2 \
-e NODE_ENV=production \
--name minecraft-proxy \
minecraft-reverse-proxy
```

**Development mode:**
```bash
# Use development compose file for verbose logging
docker compose -f docker-compose.yml -f docker-compose.dev.yml up
```

#### Docker Benefits

- **Security**: Runs as non-root user
- **Resource limits**: Memory and CPU constraints
- **Health checks**: Automatic container health monitoring
- **Easy deployment**: Single command deployment
- **Isolation**: Contained environment with minimal dependencies

This will use pm2 to create a process that for minecraft proxy.

## Report
Expand Down
22 changes: 22 additions & 0 deletions config.docker-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"url_name": "mc1.example.com",
"send_url": "minecraft-server-1",
"send_port": 25565
},
{
"url_name": "mc2.example.com",
"send_url": "minecraft-server-2",
"send_port": 25565
},
{
"url_name": "proxy.example.com",
"send_url": "upstream-server",
"send_port": 25565,
"proxy_protocol": {
"receive": true,
"send": true,
"version": 1
}
}
]
26 changes: 26 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Development override for docker-compose
# Use with: docker compose -f docker-compose.yml -f docker-compose.dev.yml up

services:
minecraft-proxy:
# Enable verbose logging for development
environment:
- NODE_ENV=development
- VERBOSE_LEVEL=1 # Most verbose logging

# Mount source code for development (optional)
volumes:
- ./config.json:/app/config.json:ro
- ./logs:/app/logs
# Uncomment below for live code reloading (requires restart)
# - .:/app

# Remove resource limits for development
deploy: {}

# More frequent health checks in development
healthcheck:
interval: 15s
timeout: 3s
retries: 2
start_period: 5s
50 changes: 50 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
services:
minecraft-proxy:
build: .
container_name: minecraft-reverse-proxy
restart: unless-stopped

# Port mapping - Minecraft default port
ports:
- "25565:25565"

# Mount configuration
volumes:
- ./config.json:/app/config.json:ro
# Optional: Mount logs directory
- ./logs:/app/logs

# Environment variables for configuration
environment:
- NODE_ENV=production
- VERBOSE_LEVEL=2 # Info level logging

# Resource limits (adjust based on your needs)
deploy:
resources:
limits:
memory: 512M
cpus: '1.0'
reservations:
memory: 256M
cpus: '0.5'

# Health check
healthcheck:
test: ["CMD", "timeout", "3", "nc", "-z", "localhost", "25565"]
interval: 30s
timeout: 3s
retries: 3
start_period: 10s

# Logging configuration
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "3"

# Optional: Example with custom network
networks:
default:
name: minecraft-proxy-network
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
"start:verbose": "node reverse-proxy-server.js -verbose 1",
"start:info": "node reverse-proxy-server.js -verbose 2",
"start:quiet": "node reverse-proxy-server.js -verbose 3",
"long": "pm2 start reverse-proxy-server.js --name 'minecraft-proxy' --max-memory-restart 4G"
"long": "pm2 start reverse-proxy-server.js --name 'minecraft-proxy' --max-memory-restart 4G",
"docker:build": "docker build -t minecraft-reverse-proxy .",
"docker:run": "docker run -d -p 25565:25565 -v $(pwd)/config.json:/app/config.json:ro --name minecraft-proxy minecraft-reverse-proxy",
"docker:stop": "docker stop minecraft-proxy && docker rm minecraft-proxy",
"docker:logs": "docker logs -f minecraft-proxy",
"compose:up": "docker compose up -d",
"compose:down": "docker compose down",
"compose:logs": "docker compose logs -f",
"compose:dev": "docker compose -f docker-compose.yml -f docker-compose.dev.yml up -d"
},
"author": "NightTangerine",
"license": "ISC",
Expand Down
2 changes: 1 addition & 1 deletion reverse-proxy-server-long.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { execSync } = require('child_process');
let server;
let config;
let targets = new Map();
let verboseLevel = 3;
let verboseLevel = parseInt(process.env.VERBOSE_LEVEL) || 3;

// 錯誤處理和日誌記錄
function log(level, ...messages) {
Expand Down