6.6 KiB
6.6 KiB
完全正确!既然主机B可以直接访问主机C(同局域网),那么只需要在主机B上配置端口转发即可,无需在主机A和主机C上安装任何软件。 github
简化方案:仅配置主机B
场景重新分析
主机B作为中转枢纽,提供三个端口转发服务:
- 内网端口 → 主机A:39000(供主机C使用)
- 内网端口 → 主机A:1999(供主机C使用)
- 公网端口 → 主机C:1998(供主机A使用)
主机B完整配置(Windows Server)
1. 下载Gost
# 从 https://github.com/go-gost/gost/releases 下载Windows版本
# 解压到 C:\gost\
2. 创建配置文件
在 C:\gost\ 目录创建 gost.yaml:
services:
# 服务1: 主机C访问主机A的39000端口(TCP)
# 主机C连接 192.168.10.1:39000
- name: forward-c-to-a-39000
addr: 192.168.10.1:39000
handler:
type: tcp
listener:
type: tcp
forwarder:
nodes:
- name: host-a-39000
addr: 144.7.97.70:39000
# 服务2: 主机C访问主机A的1999端口(HTTP)
# 主机C连接 192.168.10.1:1999 或 http://192.168.10.1:1999
- name: forward-c-to-a-1999
addr: 192.168.10.1:1999
handler:
type: tcp
listener:
type: tcp
forwarder:
nodes:
- name: host-a-1999
addr: 144.7.97.70:1999
# 服务3: 主机A访问主机C的1998端口(HTTP)
# 主机A连接 144.7.8.50:11998 或 http://144.7.8.50:11998
- name: forward-a-to-c-1998
addr: 144.7.8.50:11998
handler:
type: tcp
listener:
type: tcp
forwarder:
nodes:
- name: host-c-1998
addr: 192.168.10.2:1998
3. 测试配置
# 在PowerShell或CMD中测试运行
cd C:\gost
.\gost.exe -C gost.yaml
# 观察输出,确认没有错误
# 看到类似以下输出表示成功:
# 2026/02/11 11:26:00 forward-c-to-a-39000: listening on 192.168.10.1:39000
# 2026/02/11 11:26:00 forward-c-to-a-1999: listening on 192.168.10.1:1999
# 2026/02/11 11:26:00 forward-a-to-c-1998: listening on 144.7.8.50:11998
如果测试正常,按 Ctrl+C 停止,继续下一步。
4. 使用NSSM注册为Windows服务
# 下载NSSM: http://www.nssm.cc/download
# 解压到 C:\nssm\
# 以管理员身份打开CMD或PowerShell
cd C:\nssm\win64
# 安装服务
nssm.exe install GostService
# 在弹出的NSSM窗口中配置:
# Application标签页:
# Path: C:\gost\gost.exe
# Startup directory: C:\gost
# Arguments: -C gost.yaml
#
# Details标签页:
# Display name: Gost Port Forwarding Service
# Description: Gost端口转发服务
#
# 点击 Install service
5. 启动服务
# 启动服务
nssm start GostService
# 查看服务状态
nssm status GostService
# 如果需要,可以查看服务是否在运行
sc query GostService
6. 服务管理命令
# 启动服务
nssm start GostService
# 停止服务
nssm stop GostService
# 重启服务(修改配置后使用)
nssm restart GostService
# 查看服务状态
nssm status GostService
# 编辑服务配置
nssm edit GostService
# 查看服务日志(在服务属性中可配置日志输出路径)
nssm edit GostService
# 在I/O标签页可以设置:
# Output (stdout): C:\gost\logs\gost.log
# Error (stderr): C:\gost\logs\gost-error.log
# 删除服务(如果需要重新配置)
nssm stop GostService
nssm remove GostService confirm
防火墙配置
在主机B的Windows防火墙中添加入站规则:
# 以管理员身份运行PowerShell
# 允许39000端口(内网访问)
New-NetFirewallRule -DisplayName "Gost-39000" -Direction Inbound -LocalPort 39000 -Protocol TCP -Action Allow
# 允许1999端口(内网访问)
New-NetFirewallRule -DisplayName "Gost-1999" -Direction Inbound -LocalPort 1999 -Protocol TCP -Action Allow
# 允许11998端口(公网访问)
New-NetFirewallRule -DisplayName "Gost-11998" -Direction Inbound -LocalPort 11998 -Protocol TCP -Action Allow
或者通过图形界面:
- 打开
Windows Defender 防火墙→高级设置 - 点击
入站规则→新建规则 - 选择
端口→TCP→ 输入端口号39000,1999,11998 - 选择
允许连接→ 完成
使用方式
需求1:主机C访问主机A的39000端口(TCP)
在主机C上:
# 连接到主机B的内网IP
telnet 192.168.10.1 39000
# 或使用你的应用程序连接
# 目标地址: 192.168.10.1:39000
需求2:主机C访问主机A的1999端口(HTTP)
在主机C上:
# 浏览器访问
http://192.168.10.1:1999
# 或使用curl
curl http://192.168.10.1:1999
# 或使用你的应用程序
# 目标地址: 192.168.10.1:1999
需求3:主机A访问主机C的1998端口(HTTP)
在主机A上:
# 访问主机B的公网IP
curl http://144.7.8.50:11998
# 或使用浏览器
http://144.7.8.50:11998
# 实际流量路径:主机A → 144.7.8.50:11998 → 192.168.10.2:1998
验证配置
在主机B上验证服务是否运行
# 查看Gost进程
tasklist | findstr gost
# 查看监听端口
netstat -ano | findstr "39000"
netstat -ano | findstr "1999"
netstat -ano | findstr "11998"
# 应该能看到类似输出:
# TCP 192.168.10.1:39000 0.0.0.0:0 LISTENING [PID]
# TCP 192.168.10.1:1999 0.0.0.0:0 LISTENING [PID]
# TCP 144.7.8.50:11998 0.0.0.0:0 LISTENING [PID]
测试连通性
在主机B上进行本地测试:
# 测试到主机A的连接(需要确保主机A的服务在运行)
Test-NetConnection -ComputerName 144.7.97.70 -Port 39000
Test-NetConnection -ComputerName 144.7.97.70 -Port 1999
# 测试到主机C的连接(需要确保主机C的1998端口服务在运行)
Test-NetConnection -ComputerName 192.168.10.2 -Port 1998
配置优化建议
如果需要添加日志功能,可以修改配置文件:
log:
level: info
format: json
output: C:\gost\logs\gost.log
services:
# ... 其他配置保持不变
故障排查
如果遇到问题:
- 检查Gost服务状态:
nssm status GostService
- 查看Gost日志(如果配置了日志输出):
type C:\gost\logs\gost.log
type C:\gost\logs\gost-error.log
- 手动运行查看错误:
cd C:\gost
.\gost.exe -C gost.yaml -L debug
- 检查端口占用冲突:
netstat -ano | findstr "39000"
netstat -ano | findstr "1999"
netstat -ano | findstr "11998"
这个简化方案只需要在主机B上操作,维护更简单,性能也更好! nephen