一个简单、高效的 Go 端口转发工具,支持同时转发多个端口。
~/.ssh/id_rsa,密码优先)127.0.0.1,仅本机可访问)go install cnb.cool/4moe/pfwd@latest
访问 CNB Releases 下载对应平台的二进制文件。
tar -xzf pfwd-linux-amd64.tar.gz
chmod +x pfwd
sudo mv pfwd /usr/local/bin/
# 生成默认配置(TOML 格式)
pfwd init
# 生成 YAML 格式配置
pfwd init -f yaml
# 启动转发
pfwd start
# 指定配置文件
pfwd -c /path/to/config.toml start
pfwd forward <远程地址:端口> [本地端口] [选项]
示例:
# 转发到远程 MySQL,本地监听 13306
pfwd forward mysql.example.com:3306 13306
# 通过 SSH 跳板机访问内网数据库
pfwd forward 10.0.0.5:3306 13306 \
--ssh-host jump-server.com \
--ssh-user root \
--ssh-key ~/.ssh/id_rsa
# 仅本机访问(默认行为)
pfwd forward redis.example.com:6379 16379
# 对外开放(需显式指定)
pfwd forward redis.example.com:6379 16379 --local-host 0.0.0.0
# 使用 SSH 密码认证
pfwd forward 10.0.0.5:3306 13306 \
--ssh-host jump-server.com \
--ssh-user root \
--ssh-pass yourpassword
docker run -d \
--name pfwd \
-v $(pwd)/config.toml:/app/config.toml \
pfwd:latest start
生成默认配置文件,默认为 TOML 格式。
pfwd init # 生成 config.toml
pfwd init -f yaml # 生成 config.yaml
pfwd init -c my.conf # 生成 my.conf.toml
pfwd init -c my.conf -f yaml # 生成 my.conf.yaml
| 选项 | 说明 | 默认值 |
|---|---|---|
-f, --format | 配置格式(toml、yaml) | toml |
读取配置文件并启动所有转发规则。
pfwd start [-c config.toml]
快速端口转发,无需配置文件。
pfwd forward <远程地址:端口> [本地端口] [选项]
| 选项 | 说明 | 默认值 |
|---|---|---|
--local-host | 本地监听地址 | 127.0.0.1 |
--ssh-host | SSH 跳板机地址 | - |
--ssh-port | SSH 端口 | 22 |
--ssh-user | SSH 用户名 | - |
--ssh-pass | SSH 密码 | - |
--ssh-key | SSH 私钥文件路径 | ~/.ssh/id_rsa(自动尝试) |
显示版本信息。
支持 TOML 和 YAML 两种格式,根据文件扩展名自动识别。两种格式结构统一,TOML 用 [[forward]],YAML 用 forward: 下的列表项表示每组转发规则。
[[forward]]
remote_host = "10.0.0.5"
remote_port = 3306
local_port = 13306
local_host = "127.0.0.1"
[[forward]]
remote_host = "redis.internal.com"
remote_port = 6379
forward:
- remote_host: 10.0.0.5
remote_port: 3306
local_port: 13306
local_host: 127.0.0.1
- remote_host: redis.internal.com
remote_port: 6379
| 字段 | 必需 | 说明 |
|---|---|---|
remote_host | 是 | 远程服务器地址(IP 或域名) |
remote_port | 是 | 远程服务器端口(1-65535) |
local_port | 否 | 本地监听端口,默认等于 remote_port |
local_host | 否 | 本地监听地址,默认 127.0.0.1,设为 0.0.0.0 对外开放 |
host | 否 | HTTP Host 过滤(仅 HTTP 协议有效) |
ssh_host | 否 | SSH 跳板机地址(填写则启用 SSH 隧道) |
ssh_port | 否 | SSH 端口,默认 22 |
ssh_user | 否 | SSH 用户名 |
ssh_pass | 否 | SSH 密码(与 ssh_key_file 二选一或同时使用) |
ssh_key_file | 否 | SSH 私钥文件路径(未设置时自动尝试 ~/.ssh/id_rsa) |
TOML:
[[forward]]
remote_host = "10.0.0.5" # 内网地址(相对于跳板机)
remote_port = 3306
ssh_host = "jump.example.com" # 跳板机地址
ssh_user = "root"
ssh_key_file = "~/.ssh/id_rsa" # 使用密钥认证
YAML:
forward:
- remote_host: 10.0.0.5
remote_port: 3306
ssh_host: jump.example.com
ssh_user: root
ssh_key_file: ~/.ssh/id_rsa
未配置 ssh_pass 和 ssh_key_file 时,自动尝试 ~/.ssh/id_rsa 作为密钥兜底(密码优先)。
本地应用 -> 127.0.0.1:3306 -> SSH隧道 -> 跳板机 -> 内网MySQL:3306
在同一端口上根据域名路由到不同后端(仅 HTTP 协议有效):
[[forward]]
local_port = 80
remote_host = "192.168.1.100"
remote_port = 8080
host = "api.example.com"
[[forward]]
local_port = 80
remote_host = "192.168.1.101"
remote_port = 80
host = "*.example.com" # 支持通配符
[[forward]]
remote_host = "10.0.0.5" # 私有数据库内网地址
remote_port = 3306
local_host = "127.0.0.1" # 仅本机可访问
ssh_host = "bastion.com" # 通过堡垒机
ssh_user = "deploy"
# ssh_key_file 未设置,自动使用 ~/.ssh/id_rsa
# 在本地电脑通过 SSH 隧道安全连接
ssh -L 3306:127.0.0.1:3306 user@server-running-pfwd
# 然后本地连接 localhost:3306 即可
127.0.0.1:仅本机可访问,不会暴露到公网local_host: 0.0.0.0127.0.0.1 + SSH 隧道,确保数据全程加密传输端口被占用:bind: address already in use
lsof -i :3306 # 查看占用进程
# 修改 local_port 或停止占用进程
SSH 密钥权限问题:
chmod 600 ~/.ssh/id_rsa
Docker 端口映射:容器内默认监听 127.0.0.1,如需从宿主机访问,配置文件中设 local_host: 0.0.0.0,再通过 Docker -p 映射。