ClashforWindows如何配置可以让电脑的所有流量都走代理?

  • A+
所属分类:软件

浏览器之类的应用都是使用系统代理访问的,有些时候一些非系统代理应用,例如玩游戏或者使用某个特定软件需要特定IP时,目前比较常见的V2ray或者Shadowsocks等代理工具,只能实现网页访问的代理功能。如何实现网卡层访问?接下来详细介绍Clash for Windows的TUN模式。

首先我们先了解一下,什么是Clash for Windows的TUN模式。

TUN模式

在Windows中对于不遵循系统代理的软件,TUN模式可以接管其流量并交由 CFW 处理

配置TUN模式

当前Clash for Windows最新版本是V0.20.18,推荐使用TUN模式

首先下载最新版本的Clash for Windows,具体可以参考:Clash for Windows

接下来安装TUN模式。

注意

近期大部分浏览器默认已经开启“安全 DNS”功能,此功能会影响 TUN 模式劫持 DNS 请求导致反推域名失败,请在浏览器设置中关闭此功能以保证 TUN 模式正常运行

 

TUN模式

首先安装好汉化后的Clash for Windows最新版本后,打开软件!

在常规(General)页面中找到服务模式(Service Mode),点击后面的管理(Manage)。点击之后会弹出小窗口。

点击安装后,软件会自动关闭然后会重新启动,安装成功后,服务模式后面的灰色地球的图标会点亮变成绿色。图标变成绿色后,TUN模式安装成功!

win电脑Service Mode 无法安装

先确定系统安装了.NET framework runtime

然后尝试手动安装:

  1. 打开 C:/Program Files 目录,新建 Clash for Windows Service 目录
  2. 进入 安装目录/resources/static/files/win/[x64|arm64|ia32]/service 目录,拷贝里面两个 exe 文件到 1 中新建目录中
  3. 进入 安装目录/resources/static/files/win/common 目录,拷贝 service.yml 文件到 1 中新建目录中
  4. 在 1 中新建目录中,使用管理员权限打开 CMD 执行一下命令:
    service.exe install
    service.exe start
    

如果 install 时出现如下提示:

Service with id 'Clash Core Service' already exists
To install the service, delete the existing one or change service Id in the configuration file
2022-09-26 09:37:32,316 FATAL - Unhandled exception
System.Exception: Installation failure: Service with id 'Clash Core Service' already exists
   at WinSW.Program.<Run>g__Install|2_0(<>c__DisplayClass2_0& )
   at WinSW.Program.Run(String[] argsArray, IWinSWConfiguration descriptor)
   at WinSW.Program.Main(String[] args)
System.Exception: Installation failure: Service with id 'Clash Core Service' already exists
   at WinSW.Program.<Run>g__Install|2_0(<>c__DisplayClass2_0& )
   at WinSW.Program.Run(String[] argsArray, IWinSWConfiguration descriptor)
   at WinSW.Program.Main(String[] args)

可以先执行:

service.exe stop
service.exe uninstall

如果安装后,Service Mode manage 里显示 active 但图标显示红色,可以进入 Home Directory 管理员启动 CMD,执行 icacls.exe service /remove:d Everyone 后手动删除 service 文件夹,重启 APP 即可

注意

如果使用system作为 TUN stack,需要同时在系统防火墙中将 clash core 放行,方法如下:

0.19.27及以上版本中,点击 Clash Core 版本号前的图标,并在 UAC 弹窗(若有)中允许运行,CFW 将自动配置对应的防火墙规则。

成功配置防火墙规则后该图标作为指示灯亮起。

ClashforWindows如何配置可以让电脑的所有流量都走代理?

在 Scoop (opens new window)版上使用此功能需要0.20.3及以上版本,并且每次更新 CFW 后都需要更新防火墙规则。如果要通过 Scoop 安装脚本实现自动更新规则,可以参考:manifest (opens new window)script(opens new window)

由于查询防火墙权限受限等原因,指示灯可能无法正常工作,请以系统防火墙列表及 Clash 网卡运行状态为准。

这里提供一个可用于自查的 PowerShell 脚本(可能需要管理员权限):

#Requires -Version 3
#Requires -Modules NetSecurity

$List = Get-NetFirewallRule -Enabled True -Action Allow -Description 'Work with Clash for Windows.' | Where-Object { 'Clash Core' -eq $_.DisplayName }
$Report = foreach ($Rule in $List)
{
    $Program = (Get-NetFirewallApplicationFilter -AssociatedNetFirewallRule $Rule).Program

    [pscustomobject] @{
        Enabled     = $Rule.Enabled
        Action      = $Rule.Action
        Protocol    = (Get-NetFirewallPortFilter -AssociatedNetFirewallRule $Rule).Protocol
        Program     = $Program
        IsPathValid = Test-Path -PathType Leaf -LiteralPath $Program
    }
}
$Report
Pause

以 x86-64 版本为例,如果输出类似以下内容,那么规则添加成功(请自行验证 Program 路径的有效性):

Enabled     : True
Action      : Allow
Protocol    : TCP
Program     : C:\Program Files\Clash for Windows\resources\static\files\win\x64\clash-win64.exe
IsPathValid : True

Enabled     : True
Action      : Allow
Protocol    : UDP
Program     : C:\Program Files\Clash for Windows\resources\static\files\win\x64\clash-win64.exe
IsPathValid : True

 

  1. 在使用的配置文件中加入如下内容:
dns:
  enable: true
  enhanced-mode: fake-ip
  nameserver:
    - 8.8.8.8 # 真实请求DNS,可多设置几个
    - 114.114.114.114
# interface-name: WLAN # 出口网卡名称,或者使用下方的自动检测
tun:
  enable: true
  stack: gvisor # 使用 system 需要 Clash Premium 2021.05.08 及更高版本
  dns-hijack:
    - 198.18.0.2:53 # 请勿更改
  auto-route: true
  auto-detect-interface: true # 自动检测出口网卡

Clash Premium 2021.05.08开始,使用auto-*代替macOS-auto-*,往后数个版本将暂时兼容旧字段名。此版本同时添加了system stack支持。参考(opens new window)

#注意事项

enhanced-mode设置为fake-ip时,会出现系统检测到网卡无法联网,微软系 APP 无法登陆使用等问题,可以通过添加fake-ip-filter解决:

dns:
  enable: true
  enhanced-mode: fake-ip
  nameserver:
    - 114.114.114.114
  fake-ip-filter:
    - "dns.msftncsi.com"
    - "www.msftncsi.com"
    - "www.msftconnecttest.com"

TUN 模式更推荐使用 fake-ip 模式

 

常见问题

 

General 加载失败,显示“failed to clash core, logs are not available.”

将安装目录添加到杀毒软件白名单。

如 Windows Defender 开启,则需要将安装目录添加到排除项 (opens new window)

如依然无法启动,请尝试删除配置文件目录(一般是C:\Users\<用户名>\.config\clash

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: