Skip to content

Windows 通过 SOCKS5 代理访问不可达 IP

背景

本机无法直接访问 ip1,但可以访问 ip2,ip2 可以访问 ip1。ip2 上已配置 nginx 代理 ip1 的服务。目标是本机浏览器直接输入 ip1 地址即可访问,无需手动切换地址。

方案

SSH SOCKS5 代理 + SwitchyOmega 浏览器插件 + NSSM 开机自启。

步骤

1. 建立 SSH SOCKS5 代理

powershell
ssh -D 1080 user@ip2 -N
  • -D 1080:本机 1080 端口开启 SOCKS5 代理
  • -N:不执行远程命令,只做端口转发
  • user@ip2:跳板机

验证代理是否工作:

powershell
curl --socks5 127.0.0.1:1080 http://ip1地址

2. 安装配置 SwitchyOmega

安装:Chrome 应用商店搜索 SwitchyOmega

配置

  1. 新建代理服务器情景模式

    • 协议:SOCKS5
    • 服务器:127.0.0.1
    • 端口:1080
  2. 新建自动切换情景模式

    • 添加规则:条件类型选 IP 通配符,IP 填 10.111.161.60,情景模式选上面的 SOCKS5 代理
    • 默认情景模式:直接连接
  3. 点击插件图标切换到自动切换模式

效果:浏览器访问 http://10.111.161.60/ 自动走代理,其他网站直连。

3. NSSM 注册开机自启服务

下载 NSSMhttps://nssm.cc/download

确认 SSH 路径

powershell
where ssh
# 输出类似 C:\Windows\System32\OpenSSH\ssh.exe

注册服务(管理员 PowerShell):

powershell
nssm install ssh-proxy

GUI 中填写:

字段
Path C:\Windows\System32\OpenSSH\ssh.exe
Arguments -D 1080 -N user@ip2的IP
Startup directory C:\Windows\System32

I/O 标签页设置日志:

  • stdout:C:\ssh-proxy.log
  • stderr:C:\ssh-proxy-error.log

配置自启(管理员 PowerShell):

powershell
nssm set ssh-proxy Start SERVICE_AUTO_START
nssm start ssh-proxy

以当前用户身份运行(避免 SYSTEM 账户找不到 SSH 密钥):

powershell
nssm set ssh-proxy ObjectName .\你的用户名 你的密码
nssm restart ssh-proxy

或者不改用户,直接把密钥复制给 SYSTEM:

powershell
Copy-Item -Recurse C:\Users\你的用户名\.ssh C:\Windows\System32\config\systemprofile\.ssh

4. 常用管理命令

powershell
nssm start ssh-proxy      # 启动
nssm stop ssh-proxy       # 停止
nssm restart ssh-proxy    # 重启
nssm status ssh-proxy     # 查看状态
nssm edit ssh-proxy       # 编辑配置(GUI)
nssm remove ssh-proxy     # 删除服务

注意事项

  • NSSM 所有操作需要管理员 PowerShell
  • SSH 密钥认证:服务默认以 SYSTEM 运行,密钥路径与用户不同,需复制密钥或切换运行用户
  • NSSM 自带断线重连:SSH 连接断开后会自动重启进程
  • 本机防火墙需放行 1080 端口

方案对比

方案 访问地址 覆盖范围 额外软件
SSH -L 端口转发 localhost:8080 仅指定端口
SSH -D + SwitchyOmega ip1 原地址 仅浏览器 SwitchyOmega 插件
SSH -D + Proxifier ip1 原地址 全系统 Proxifier(付费)
nginx + hosts 域名 全系统 无(需域名)

总结

SSH SOCKS5 + SwitchyOmega 是 Windows 下浏览器透明访问不可达 IP 的最佳方案:免费、轻量、原地址访问、开机自启。如需全系统级别代理,换用 Proxifier。

基于 VitePress 构建