Skip to content

qingwufong/port-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌐 Port Manager

Simple HTTP file server with automatic port conflict resolution

📁 A Rust-based HTTP service that serves files from the executable directory, automatically handles port conflicts

Rust

FeaturesQuick StartInstallationUsage


✨ Features

  • 🚀 Auto Port Conflict Resolution - Automatically detects and terminates processes occupying the target port
  • 📁 File Server - Serves static files from the executable directory
  • 🌍 IPv4/IPv6 Support - Checks both IPv4 and IPv6 port bindings
  • 💾 Built with Rust - Type-safe, memory-safe, and fast
  • 🔒 Windows Process Management - Uses Windows API for reliable process termination
  • 📝 Structured Logging - Clear logs for debugging and monitoring
  • Lightweight - Single binary, no runtime dependencies

🎯 Quick Start

# Build the project
cd port-manager
cargo build --release

# Run the HTTP server
./target/release/port-manager.exe

# Access files
curl http://localhost:8080/yourfile.txt

That's it! The server will automatically resolve port conflicts if port 8080 is already in use. 🎉


📦 Installation

Binary Release

Download the compiled binary from the target/release/ directory:

# The release binary is located at:
port-manager/target/release/port-manager.exe

Build from Source

git clone <your-repo-url>
cd port-manager
cargo build --release

💻 Usage

Basic Usage

# Run the server (default port: 8080)
./port-manager.exe

# The server will:
# 1. Check if port 8080 is occupied
# 2. Terminate any process occupying the port
# 3. Start HTTP service on port 8080
# 4. Serve files from the executable directory

Accessing Files

# Root endpoint
curl http://localhost:8080/

# Health check
curl http://localhost:8080/health

# Access a file in the executable directory
curl http://localhost:8080/Cargo.toml

# Access via /files/ prefix (alternative)
curl http://localhost:8080/files/Cargo.toml

API Endpoints

Endpoint Description
/ Returns "Port Manager HTTP Service"
/health Returns JSON with status and start time
/files/* Serves static files from exe directory
/* Fallback route, serves files directly

Environment Variables

Variable Description
RUST_LOG=info Enable info-level logging (default)
RUST_LOG=debug Enable debug-level logging
RUST_LOG=warn Enable only warnings
RUST_LOG=error Enable only errors

🔧 How it Works

Startup Process

  1. Port Detection - Queries Windows TCP table to check if port 8080 is in use
  2. Process Termination - If occupied, identifies and terminates the owning process
  3. Service Start - Initializes the HTTP server
  4. File Serving - Serves files from the executable's directory

Port Conflict Resolution

The server uses Windows GetExtendedTcpTable API to:

  • Query all IPv4 and IPv6 TCP connections
  • Find processes listening on the target port (state = LISTEN)
  • Terminate those processes using TerminateProcess

Why This Works

  • Uses native Windows APIs for accurate port and process information
  • Handles both IPv4 and IPv6 bindings
  • Waits 1 second after termination for port cleanup
  • Gracefully handles Ctrl+C shutdown

🏗️ Building from Source

Prerequisites

  • Rust 1.70 or later
  • Windows OS (uses Windows-specific APIs)
  • Cargo

Build Commands

# Debug build
cargo build

# Release build (optimized)
cargo build --release

# Run tests
cargo test

# Run with logging
RUST_LOG=debug cargo run

The release binary will be at target/release/port-manager.exe.


📁 File Serving

Files are served from the executable directory - the directory containing the port-manager.exe file.

Example Structure

port-manager/
├── port-manager.exe
├── Cargo.toml
├── README.md
└── data/
    └── myfile.txt

With the above structure:

  • http://localhost:8080/Cargo.toml → serves Cargo.toml
  • http://localhost:8080/data/myfile.txt → serves data/myfile.txt
  • http://localhost:8080/files/Cargo.toml → also serves Cargo.toml

🛡️ Safety

This tool includes safety features:

  • Graceful Shutdown - Handles Ctrl+C properly
  • Error Handling - Continues even if process termination fails
  • Type-Safe - Built with Rust for memory safety
  • Port Cleanup Wait - Waits 1 second after termination
  • Open Source - Fully auditable code

📄 License

This project is provided as-is for use in your own projects.


🙏 Acknowledgments


Made with 🦀 and Rust

About

Simple HTTP file server with automatic port conflict resolution

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages