added build scripts

This commit is contained in:
2025-08-31 21:40:17 +08:00
parent 7b24472ebd
commit 1ae26c3a90
4 changed files with 673 additions and 0 deletions

283
BUILD.md Normal file
View File

@@ -0,0 +1,283 @@
# PSK-Proxy-Tunnel Build Guide
This guide explains how to build single executable binaries for the PSK-Proxy-Tunnel project using the `pkg` tool.
## Prerequisites
- **Node.js**: Version 18.0.0 or higher
- **npm**: Usually comes with Node.js
- **Git**: To clone the repository
## Quick Start
### 1. Install Dependencies
```bash
npm install
```
### 2. Build for Current Platform
```bash
# macOS/Linux: auto-detect current platform via script
./build.sh
# Windows: auto-detect Windows via script
build.bat
# Or run a specific platform via npm
npm run build:macos # macOS
npm run build:linux # Linux
npm run build:windows # Windows
```
### 3. Build for All Platforms
```bash
npm run build
```
## Build Scripts
### Using the Build Scripts
#### Unix/Linux/macOS
```bash
# Make the script executable
chmod +x build.sh
# Build for current platform
./build.sh
# Build for specific platform
./build.sh --platform macos
./build.sh --platform linux
./build.sh --platform windows
# Build for all platforms
./build.sh --all
# Clean build artifacts
./build.sh --clean
# Show help
./build.sh --help
```
#### Windows
```cmd
# Build for Windows (default)
build.bat
# Build for all platforms
build.bat --all
# Clean build artifacts
build.bat --clean
# Show help
build.bat --help
```
### Using npm Scripts Directly
```bash
# Build for specific platform
npm run build:macos
npm run build:linux
npm run build:windows
# Build for all platforms
npm run build
# Clean build artifacts
npm run clean
```
## Build Output
After building, you'll find the executables in the `dist/` directory:
```
dist/
├── psk-proxy-server-macos # macOS server executable
├── psk-proxy-client-macos # macOS client executable
├── psk-proxy-server-linux # Linux server executable
├── psk-proxy-client-linux # Linux client executable
├── psk-proxy-server-windows.exe # Windows server executable
└── psk-proxy-client-windows.exe # Windows client executable
```
## Platform-Specific Builds
### macOS
```bash
npm run build:macos
# Creates: psk-proxy-server-macos, psk-proxy-client-macos
```
### Linux
```bash
npm run build:linux
# Creates: psk-proxy-server-linux, psk-proxy-client-linux
```
### Windows
```bash
npm run build:windows
# Creates: psk-proxy-server-windows.exe, psk-proxy-client-windows.exe
```
## Cross-Platform Building
You can build for other platforms from any operating system:
```bash
# Build for all platforms from macOS
npm run build
# Build for all platforms from Linux
npm run build
# Build for all platforms from Windows
npm run build
```
## Using the Built Executables
### Server
```bash
# macOS/Linux
./dist/psk-proxy-server-macos --tunnel-port 8443 --tcp-port 8080 --host 0.0.0.0 --psk-file /path/to/psk.key
# Windows
.\dist\psk-proxy-server-windows.exe --tunnel-port 8443 --tcp-port 8080 --host 0.0.0.0 --psk-file C:\path\to\psk.key
```
### Client
```bash
# macOS/Linux
./dist/psk-proxy-client-macos --server-host server.example.com --server-port 8443 --origin-host 127.0.0.1 --origin-port 8080 --psk-file /path/to/psk.key --identity client1
# Windows
.\dist\psk-proxy-client-windows.exe --server-host server.example.com --server-port 8443 --origin-host 127.0.0.1 --origin-port 8080 --psk-file C:\path\to\psk.key --identity client1
```
## Build Configuration
The build configuration is defined in `package.json` under the `pkg` section:
```json
{
"pkg": {
"assets": [],
"scripts": [],
"targets": [
"node18-macos-x64",
"node18-linux-x64",
"node18-win-x64"
]
}
}
```
### Available Targets
- `node18-macos-x64`: macOS 64-bit
- `node18-linux-x64`: Linux 64-bit
- `node18-win-x64`: Windows 64-bit
You can modify these targets to support different Node.js versions or architectures.
## Troubleshooting
### Common Issues
1. **"pkg command not found"**
```bash
npm install -g pkg
# or use the local version
npx pkg proxy-server.js
```
2. **Build fails with permission errors**
```bash
# Make sure the build script is executable
chmod +x build.sh
# Or run with sudo if needed
sudo npm run build
```
3. **Executable doesn't run on target platform**
- Ensure you're building for the correct target platform
- Check that the target platform supports the Node.js version you're using
- Verify the executable has proper permissions
4. **Missing dependencies in built executable**
- The `pkg` tool automatically bundles Node.js built-in modules
- External dependencies are included automatically
- If you have custom assets, add them to the `pkg.assets` array
### Performance Considerations
- **File Size**: Single executables are larger than the source code because they include Node.js runtime
- **Startup Time**: Slightly slower startup compared to running with `node` directly
- **Memory Usage**: Similar to running with Node.js directly
### Security Notes
- Built executables contain your source code (though it's compiled)
- The `pkg` tool doesn't obfuscate or encrypt your code
- Consider this when distributing executables with sensitive information
## Advanced Configuration
### Custom pkg Options
You can customize the build process by modifying the `pkg` section in `package.json`:
```json
{
"pkg": {
"assets": [
"config/*.json",
"templates/*.html"
],
"scripts": [
"scripts/**/*.js"
],
"targets": [
"node18-macos-x64",
"node18-linux-x64",
"node18-win-x64"
],
"outputPath": "custom-dist"
}
}
```
### Environment Variables
You can set environment variables to customize the build:
```bash
# Set custom output directory
PKG_OUTPUT_PATH=./my-dist npm run build
# Set custom Node.js version
PKG_NODE_VERSION=16 npm run build
```
## Contributing
When adding new build targets or modifying the build process:
1. Update the `package.json` scripts
2. Update the build scripts (`build.sh` and `build.bat`)
3. Test builds on all target platforms
4. Update this documentation
## License
This build system is part of the PSK-Proxy-Tunnel project and follows the same license terms.

158
build.bat Normal file
View File

@@ -0,0 +1,158 @@
@echo off
REM PSK-Proxy-Tunnel Build Script for Windows
REM This script builds single executable binaries for Windows
setlocal enabledelayedexpansion
REM Colors for output (Windows 10+ supports ANSI colors)
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"
REM Function to print colored output
:print_status
echo %BLUE%[INFO]%NC% %~1
goto :eof
:print_success
echo %GREEN%[SUCCESS]%NC% %~1
goto :eof
:print_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof
:print_error
echo %RED%[ERROR]%NC% %~1
goto :eof
REM Function to check if command exists
:command_exists
where %1 >nul 2>&1
if %errorlevel% equ 0 (
exit /b 0
) else (
exit /b 1
)
REM Function to show help
:show_help
echo PSK-Proxy-Tunnel Build Script for Windows
echo.
echo Usage: %0 [OPTIONS]
echo.
echo Options:
echo --windows Build for Windows (default)
echo --all Build for all platforms
echo --clean Clean build artifacts
echo --help Show this help message
echo.
echo Examples:
echo %0 --windows Build for Windows only
echo %0 --all Build for all platforms
echo %0 --clean Clean build artifacts
echo.
goto :eof
REM Function to clean build artifacts
:clean_build
call :print_status "Cleaning build artifacts..."
call npm run clean
if %errorlevel% equ 0 (
call :print_success "Build artifacts cleaned"
) else (
call :print_error "Failed to clean build artifacts"
)
goto :eof
REM Function to build for Windows
:build_windows
call :print_status "Building for Windows..."
call npm run prebuild
call npm run build:windows
if %errorlevel% equ 0 (
call :print_success "Build completed for Windows"
) else (
call :print_error "Build failed for Windows"
)
goto :eof
REM Function to build for all platforms
:build_all
call :print_status "Building for all platforms..."
call npm run build
if %errorlevel% equ 0 (
call :print_success "Build completed for all platforms"
) else (
call :print_error "Build failed for all platforms"
)
goto :eof
REM Main script logic
:main
REM Check if we're in the right directory
if not exist "package.json" (
call :print_error "package.json not found. Please run this script from the project root directory."
exit /b 1
)
REM Check if Node.js is installed
call :command_exists node
if %errorlevel% neq 0 (
call :print_error "Node.js is not installed. Please install Node.js first."
exit /b 1
)
REM Check if npm is installed
call :command_exists npm
if %errorlevel% neq 0 (
call :print_error "npm is not installed. Please install npm first."
exit /b 1
)
REM Install dependencies if node_modules doesn't exist
if not exist "node_modules" (
call :print_status "Installing dependencies..."
call npm install
if %errorlevel% equ 0 (
call :print_success "Dependencies installed"
) else (
call :print_error "Failed to install dependencies"
exit /b 1
)
)
REM Parse command line arguments
if "%1"=="" (
REM No arguments provided, build for Windows
call :build_windows
goto :eof
)
:parse_args
if "%1"=="--windows" (
call :build_windows
shift
goto :parse_args
) else if "%1"=="--all" (
call :build_all
shift
goto :parse_args
) else if "%1"=="--clean" (
call :clean_build
shift
goto :parse_args
) else if "%1"=="--help" (
call :show_help
exit /b 0
) else if "%1"=="" (
goto :eof
) else (
call :print_error "Unknown option: %1"
call :show_help
exit /b 1
)
goto :parse_args

181
build.sh Executable file
View File

@@ -0,0 +1,181 @@
#!/bin/bash
# PSK-Proxy-Tunnel Build Script
# This script builds single executable binaries for different platforms
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to detect current platform
detect_platform() {
case "$(uname -s)" in
Darwin*) echo "macos" ;;
Linux*) echo "linux" ;;
CYGWIN*|MINGW*|MSYS*) echo "windows" ;;
*) echo "unknown" ;;
esac
}
# Function to build for specific platform
build_for_platform() {
local platform=$1
print_status "Building for $platform..."
# Ensure dist initialized and clean for single-platform builds
npm run prebuild
case $platform in
"macos")
npm run build:macos
;;
"linux")
npm run build:linux
;;
"windows")
npm run build:windows
;;
*)
print_error "Unknown platform: $platform"
exit 1
;;
esac
print_success "Build completed for $platform"
}
# Function to build for all platforms
build_all() {
print_status "Building for all platforms..."
npm run build
print_success "Build completed for all platforms"
}
# Function to show help
show_help() {
echo "PSK-Proxy-Tunnel Build Script"
echo ""
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -p, --platform PLATFORM Build for specific platform (macos|linux|windows)"
echo " -a, --all Build for all platforms"
echo " -c, --clean Clean build artifacts"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " $0 --platform macos Build for macOS only"
echo " $0 --all Build for all platforms"
echo " $0 --clean Clean build artifacts"
echo ""
echo "Current platform: $(detect_platform)"
}
# Function to clean build artifacts
clean_build() {
print_status "Cleaning build artifacts..."
npm run clean
print_success "Build artifacts cleaned"
}
# Main script logic
main() {
# Check if we're in the right directory
if [ ! -f "package.json" ]; then
print_error "package.json not found. Please run this script from the project root directory."
exit 1
fi
# Check if Node.js is installed
if ! command_exists node; then
print_error "Node.js is not installed. Please install Node.js first."
exit 1
fi
# Check if npm is installed
if ! command_exists npm; then
print_error "npm is not installed. Please install npm first."
exit 1
fi
# Install dependencies if node_modules doesn't exist
if [ ! -d "node_modules" ]; then
print_status "Installing dependencies..."
npm install
print_success "Dependencies installed"
fi
# Parse command line arguments
if [ $# -eq 0 ]; then
# No arguments provided, build for current platform
local current_platform=$(detect_platform)
if [ "$current_platform" = "unknown" ]; then
print_warning "Could not detect platform, building for all platforms"
build_all
else
print_status "Building for detected platform: $current_platform"
build_for_platform "$current_platform"
fi
return
fi
while [[ $# -gt 0 ]]; do
case $1 in
-p|--platform)
if [ -z "$2" ]; then
print_error "Platform not specified"
exit 1
fi
build_for_platform "$2"
shift 2
;;
-a|--all)
build_all
shift
;;
-c|--clean)
clean_build
shift
;;
-h|--help)
show_help
exit 0
;;
*)
print_error "Unknown option: $1"
show_help
exit 1
;;
esac
done
}
# Run main function with all arguments
main "$@"

51
package.json Normal file
View File

@@ -0,0 +1,51 @@
{
"name": "psk-proxy-tunnel",
"version": "1.0.0",
"description": "TLS-PSK multiplexed TCP tunnel server and local HTTP proxy client for secure NAT traversal and TCP forwarding",
"main": "proxy-client.js",
"type": "commonjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "node -e \"try{require('fs').rmSync('dist',{recursive:true,force:true})}catch(e){}\"",
"prebuild": "npm run clean && node -e \"require('fs').mkdirSync('dist',{recursive:true})\"",
"build": "npm run build:macos && npm run build:linux && npm run build:windows",
"build:macos": "npm run build:server:macos && npm run build:client:macos",
"build:server:macos": "pkg proxy-server.js --targets node18-macos-x64 --output dist/psk-proxy-server-macos",
"build:client:macos": "pkg proxy-client.js --targets node18-macos-x64 --output dist/psk-proxy-client-macos",
"build:linux": "npm run build:server:linux && npm run build:client:linux",
"build:server:linux": "pkg proxy-server.js --targets node18-linux-x64 --output dist/psk-proxy-server-linux",
"build:client:linux": "pkg proxy-client.js --targets node18-linux-x64 --output dist/psk-proxy-client-linux",
"build:windows": "npm run build:server:windows && npm run build:client:windows",
"build:server:windows": "pkg proxy-server.js --targets node18-win-x64 --output dist/psk-proxy-server-windows.exe",
"build:client:windows": "pkg proxy-client.js --targets node18-win-x64 --output dist/psk-proxy-client-windows.exe",
"start:server": "node proxy-server.js",
"start:client": "node proxy-client.js"
},
"repository": {
"type": "git",
"url": "https://gitea.jaydenha.uk/Jayden/PSK-Proxy-Tunnel.git"
},
"author": "Jayden",
"dependencies": {
"commander": "^11.0.0"
},
"devDependencies": {
"pkg": "^5.8.1"
},
"engines": {
"node": ">=18.0.0"
},
"bin": {
"psk-proxy-server": "./proxy-server.js",
"psk-proxy-client": "./proxy-client.js"
},
"pkg": {
"assets": [],
"scripts": [],
"targets": [
"node18-macos-x64",
"node18-linux-x64",
"node18-win-x64"
]
}
}