logo
Public
0
0
WeChat Login
feat: v3.0 支持通过 forward 命令进行快速端口转发,无需配置文件 支持 TOML 配置文件格式(与 YAML 任选其一) init 命令支持 -f 选项指定配置文件格式(toml / yaml)

pfwd - 端口转发工具

Go Version License Docker Docker

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

功能特性

  • 多端口同时转发
  • SSH 隧道支持(通过跳板机转发)
  • SSH 密钥认证(自动尝试 ~/.ssh/id_rsa,密码优先)
  • HTTP Host 过滤(基于域名路由)
  • 安全监听(默认绑定 127.0.0.1,仅本机可访问)
  • YAML / TOML 配置文件(任选其一)
  • 跨平台支持(Linux/macOS/Windows)
  • Docker 镜像支持

安装

go install(推荐)

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

从 Release 下载

访问 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

docker run -d \
  --name pfwd \
  -v $(pwd)/config.toml:/app/config.toml \
  pfwd:latest start

命令参考

pfwd init

生成默认配置文件,默认为 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配置格式(tomlyamltoml

pfwd start

读取配置文件并启动所有转发规则。

pfwd start [-c config.toml]

pfwd forward

快速端口转发,无需配置文件。

pfwd forward <远程地址:端口> [本地端口] [选项]
选项说明默认值
--local-host本地监听地址127.0.0.1
--ssh-hostSSH 跳板机地址-
--ssh-portSSH 端口22
--ssh-userSSH 用户名-
--ssh-passSSH 密码-
--ssh-keySSH 私钥文件路径~/.ssh/id_rsa(自动尝试)

pfwd version

显示版本信息。

配置文件

支持 TOML 和 YAML 两种格式,根据文件扩展名自动识别。两种格式结构统一,TOML 用 [[forward]],YAML 用 forward: 下的列表项表示每组转发规则。

TOML 格式

[[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

YAML 格式

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 对外开放
hostHTTP Host 过滤(仅 HTTP 协议有效)
ssh_hostSSH 跳板机地址(填写则启用 SSH 隧道)
ssh_portSSH 端口,默认 22
ssh_userSSH 用户名
ssh_passSSH 密码(与 ssh_key_file 二选一或同时使用)
ssh_key_fileSSH 私钥文件路径(未设置时自动尝试 ~/.ssh/id_rsa

示例配置

通过 SSH 跳板机访问内网

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_passssh_key_file 时,自动尝试 ~/.ssh/id_rsa 作为密钥兜底(密码优先)。

本地应用 -> 127.0.0.1:3306 -> SSH隧道 -> 跳板机 -> 内网MySQL:3306

HTTP Host 过滤

在同一端口上根据域名路由到不同后端(仅 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.0
  • SSH 认证:密码优先,密钥兜底;支持密码、密钥、或两者同时配置
  • 推荐做法:绑定 127.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 映射。

许可证

MIT

联系方式

About

TCP端口转发工具

572.00 KiB
0 forks0 stars2 branches3 TagREADMEMIT license
Language
Go75.9%
Shell14.9%
Dockerfile6.9%
Makefile2.3%