logo
Public
0
0
WeChat Login

pfwd - 端口转发工具

Go Version License Docker Docker

一个简单、高效的 Go 端口转发工具,支持同时转发多个端口。

功能特性

  • ✅ 多端口同时转发
  • ✅ YAML 配置文件管理
  • ✅ 跨平台支持(Linux/macOS/Windows)
  • ✅ Docker 镜像支持
  • ✅ 优雅的启动和停止
  • ✅ 实时连接状态显示

快速开始

方式一:使用 go install 安装(推荐)

安装 pfwd

go install cnb.cool/4moe/pfwd@latest

安装完成后,pfwd 命令将自动添加到 $GOPATH/bin 目录,可以全局使用:

# 检查是否安装成功 pfwd version # 查看 pfwd 可执行文件位置 which pfwd

初始化配置文件

pfwd init

启动转发

# 使用默认配置文件 pfwd start # 使用自定义配置文件 pfwd --config /path/to/config.yaml start

方式二:从 CNB Release 下载

1. 下载二进制文件

访问 CNB Releases 页面,选择适合你系统的版本:

平台文件名
Linux AMD64pfwd-linux-amd64.tar.gz
Linux ARM64pfwd-linux-arm64.tar.gz
Windows AMD64pfwd-windows-amd64.zip
macOS AMD64pfwd-darwin-amd64.tar.gz
macOS ARM64pfwd-darwin-arm64.tar.gz

下载对应的压缩包。

2. 解压并安装

Linux/macOS:

# 解压文件 tar -xzf pfwd-linux-amd64.tar.gz # 或 tar -xzf pfwd-darwin-amd64.tar.gz # 添加执行权限 chmod +x pfwd # 移动到系统路径(可选) sudo mv pfwd /usr/local/bin/ # 验证安装 pfwd version

Windows:

# 解压文件 Unzip pfwd-windows-amd64.zip # 添加到 PATH(可选) # 或者直接在当前目录使用 .\pfwd.exe version

3. 初始化配置文件

pfwd init

4. 启动转发

# 使用默认配置文件 pfwd start # 使用自定义配置文件 pfwd --config /path/to/config.yaml start

方式三:使用编译好的二进制文件

1. 获取 pfwd

下载已编译好的二进制文件或从源码编译

2. 初始化配置文件

./pfwd init

这将生成默认的 config.yaml 文件:

# 端口转发配置(支持多个转发规则) forwards: - local_port: 1080 remote_host: mysql-server.com remote_port: 3306 name: "MySQL数据库" - local_port: 8080 remote_host: redis-server.com remote_port: 6379 name: "Redis缓存" - local_port: 9090 remote_host: api-server.com remote_port: 443 name: "API服务"

3. 修改配置文件

编辑 config.yaml,配置你的转发规则:

forwards: - local_port: 10080 # 本地监听端口 remote_host: example.com # 远程服务器地址 remote_port: 80 # 远程服务器端口 - local_port: 10081 remote_host: example.com remote_port: 81

4. 启动转发

# 使用默认配置文件(config.yaml) ./pfwd start # 使用自定义配置文件 ./pfwd --config /path/to/config.yaml start

5. 查看状态

启动后会显示:

=== Go 端口转发器 === :10080 → example.com:80 :10081 → example.com:81 ========================= 准备启动 2 个转发器... ========================= [1/2] 启动转发器... ✅ 开始监听 :10080,转发到 example.com:80 [2/2] 启动转发器... ✅ 开始监听 :10081,转发到 example.com:81 ✅ 所有转发器已启动 按 Ctrl+C 停止

6. 停止转发

Ctrl+C 优雅停止所有转发器。

方式四:使用 Docker

1. 拉取镜像

docker pull your-registry.com/pfwd:latest

2. 创建配置文件

创建 config.yaml

forwards: - local_port: 3306 remote_host: mysql.example.com remote_port: 3306 - local_port: 6379 remote_host: redis.example.com remote_port: 6379

3. 运行容器

docker run -d \ --name pfwd \ -p 3306:3306 \ -p 6379:6379 \ -v $(pwd)/config.yaml:/app/config.yaml \ pfwd:latest start

4. Docker Compose 方式(推荐)

创建 docker-compose.yml

version: '3.8' services: pfwd: image: pfwd:latest container_name: pfwd restart: unless-stopped ports: - "3306:3306" - "6379:6379" - "9090:9090" volumes: - ./config.yaml:/app/config.yaml

启动:

docker-compose up -d

查看日志:

docker-compose logs -f pfwd

停止:

docker-compose down

命令说明

init - 初始化配置文件

./pfwd init

生成默认配置文件 config.yaml

start - 启动端口转发

./pfwd start [--config /path/to/config.yaml]

参数说明:

  • --config, -c: 指定配置文件路径(默认:./config.yaml

version - 查看版本信息

./pfwd version

输出:

版本: 0.1.0 构建时间: 2026-02-13_03:20:29 Git提交: 13a62f4

配置文件说明

配置结构

forwards: - local_port: 10080 # 必需:本地监听端口 remote_host: example.com # 必需:远程服务器地址 remote_port: 80 # 必需:远程服务器端口 name: "描述名称" # 可选:转发规则描述

配置规则

  1. local_port: 1-65535 的整数
  2. remote_host: IP 地址或域名
  3. remote_port: 1-65535 的整数
  4. name: 字符串描述(可选)

示例配置

MySQL 数据库转发

forwards: - local_port: 3306 remote_host: mysql.internal.com remote_port: 3306

Redis 缓存转发

forwards: - local_port: 6379 remote_host: redis.internal.com remote_port: 6379

多服务转发

forwards: - local_port: 3306 remote_host: mysql.internal.com remote_port: 3306 - local_port: 6379 remote_host: redis.internal.com remote_port: 6379 - local_port: 9090 remote_host: api.internal.com remote_port: 8080

常见问题

1. 端口被占用

错误:bind: address already in use

解决:

  • 检查端口占用:lsof -i :3306 (macOS/Linux) 或 netstat -ano | findstr :3306 (Windows)
  • 修改 config.yaml 中的 local_port
  • 停止占用端口的服务

2. 无法连接到远程服务器

错误:connection refusedtimeout

解决:

  • 检查 remote_hostremote_port 是否正确
  • 确认远程服务器可访问:telnet example.com 3306
  • 检查防火墙规则

3. Docker 端口映射问题

确保 Docker 容器的端口映射与配置文件中的 local_port 一致:

# config.yaml forwards: - local_port: 3306 # 容器内监听的端口 remote_host: mysql.example.com remote_port: 3306
docker run -d \ -p 3306:3306 \ # 宿主机:容器内端口 pfwd:latest start

4. 权限问题

如果遇到权限错误:

# Linux/macOS chmod +x pfwd

5. IPv6 支持

当前配置文件支持 IPv6 地址:

forwards: - local_port: 8080 remote_host: "[2001:db8::1]" # IPv6 地址需要方括号 remote_port: 80

许可证

本项目采用 MIT 许可证 - 详见 LICENSE 文件

贡献

欢迎提交 Issue 和 Pull Request!

联系方式

About

TCP端口转发工具

324.00 KiB
0 forks0 stars1 branches1 TagREADMEMIT license
Language
Go49.3%
Shell31.3%
Dockerfile14.5%
Makefile4.9%