仅本页所有页面
由 GitBook 提供支持
1 / 45

Surge 使用手册

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

HTTP 处理

Loading...

Loading...

Loading...

脚本

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

其他

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

Loading...

简介

Surge 官方手册的汉化

来自官方的汉化文档会直接搬过来并做说明,自己翻译的不做说明。

除了官方手册,之后还会放一些其他关于 Surge 的教程用来补充官方手册中没有提及或者说的不清楚的地方。可以联系我投放你写的教程,但请保证提供的页面中没有商业推广内容。因为本项目是个自发的非盈利开源项目,也不希望别人拿翻译后的内容进行商业行为。

转载和演绎都是可以的,前者请备注当前页面的地址,后者视改动幅度,可以不标注地址。

域名规则

根据域名判断的规则

域名规则

有三种根据域名判断的规则

DOMAIN

DOMAIN,www.apple.com,Proxy

规则会匹配与请求完全相同的地址

DOMAIN-SUFFIX

DOMAIN-SUFFIX,apple.com,Proxy

规则会匹配与请求与主域名相同的地址

DOMAIN-KEYWORD

DOMAIN-KEYWORD,google,Proxy

规则会匹配包含相应关键字的域名

DOMAIN-SET

为支持快速在千万级域名列表文件中搜索而设计。文件中的每一行都是一个域名,如果某一行以.开头,则匹配所有子域名和域名本身。这可用于广告过滤。

也支持在文件中使用 IPv4 和 IPv6 地址,但不支持 CIDR 的形式。

本项目开源在 ,如果对文章内容有疑问,请发 Issue(不解答技术问题)。如果内容有错误和不恰当,也可以 Fork 后修改。

比如,'' 匹配 '', '' 和 '' 本身,但不会匹配 ''

除了 "apple" 会匹配 "","app" 同样也会匹配到

GitHub
google.com
www.google.com
mail.google.com
google.com
content-google.com
www.apple.com

配置文件

Surge 的绝大多数功能都是由配置文件构成的。iOS 和 macOS 可以通过 iCloud 共享配置文件。你可以通过文本编辑器、iOS app 或是 Dashboard 编辑配置文件。

注释

Surge 的配置文件支持注释,以 #、; 和 // 开头(不含引号)。支持内部注释,以 // 开头。对于规则和主机名的注释,在图形界面编辑也可以保存;但对于其他内容的注释,如果在图形界面修改了相关功能导致对应段落的配置文件变化,可能会丢失。

总览

Surge 是一个网络开发和代理工具。他是为开发者打造的工具,所以使用时需要具备一定的专业知识。

以下四种是 Surge 的核心工作流。

  • 接管:接管设备发起的网络连接。Surge 支持代理服务器和虚拟网卡两种接管模式。

  • 处理:可以修改已经接管的网络请求和响应。包括 URL 重定向、本地文件映射、使用 JavaScript 自定义修改等多种方法。

  • 转发:可以将已接管的网络请求转发到其他代理服务器。这可以是全局转发,也可以通过灵活的规则系统来确定出站策略。

  • 拦截:可以拦截并保存网络请求和响应的数据,也可以通过 MITM 解密 HTTPS 流量。

特点和功能

  • 高性能、稳定、高效率:Surge 可以在耗费极少资源并且保持高稳定性的前提下轻松处理网络数据。

  • 规则灵活:你可以编写基于域名、IP CIDR、GeoIP 等的规则。代理支持以下协议:HTTP、HTTPS、SOCKS5、SOCKS5-TLS、Shadowsocks。

  • HTTPS 解密: 通过中间人攻击解密 HTTPS 流量。可以帮助你生成可被系统信任的用于调试 CA 证书以达到解密功能。

  • 本地 DNS 映射:Surge 支持本地自定义的 DNS 映射。所包含的多个功能,包括通配符、别名和自定义 DNS 服务器,可以满足各种需求。

  • 策略组:你可以把多个代理放进一个策略组里,以便在编写规则时,能够一次性使用这一系列的代理。有多种策略组可选,包括 URL Test (会对策略组内每个代理进行连接测试,自动选择最快的)、SSID(根据当前的 SSID 选择连接的代理)以及手动选择代理的策略组。

  • HTTP 重写:可以使用自定义规则将 HTTP/HTTPS 请求重写为另一个 URL,或者阻止这些请求。

  • 远程请求查看器:通过 USB 或者网络,可以远程查看另一个运行 Surge 设备上的请求查看器。

  • 完整的 IPv6 支持:所有的功能都可以在 IPv6 的环境下正常工作。

Surge Mac 版独有的特性和功能

  • 增强模式:Surge 会设置一个虚拟网络去接管所有的网络请求,尤其是那些对 Web 代理支持不好的软件。

  • 计费网络模式:当你使用移动热点等计费网络时,通过打开计费网络模式,可以控制应用或进程是否可以使用网络。

  • 网关模式:Surge Mac 可以当做三层网关去处理同网络下其他设备的网络请求。(简单来说就可以当做一个路由器?)

Surge iOS 版独有的特性和功能

  • 所有的功能都可以在蜂窝数据网络下正常工作。

  • 即便有些 App 可能不遵循系统代理设置,仍然可以捕获所有的 HTTP、HTTPS、TCP 流量,并根据高度可定制化的规则转发给 HTTP、HTTPS、SOCKS5、Shadowsocks 代理。

  • 可以覆盖系统的 DNS 设置,并通过并发查询提高解析性能。

  • 通过 Wi-Fi 或 USB 连接时,将 Surge Dashboard 连接到 Surge iOS,监测和分析iOS设备上的网络请求。也可以在通过 USB 连接时检查蜂窝网络请求。

理解 Surge

我们已经发布了官方指南帮你理解 Surge。

代理规则

Surge 可以根据自定义规则,决定将请求转发给代理服务器,还是直接发送给原始服务器。

优先度

规则按照配置文件里的顺序,按照从头到尾的顺序进行匹配。换句话说,第一条规则的优先级最高。

编辑规则

每一条规则由三部分组成:规则类型、匹配的流量(FINAL 规则没有这一项)以及所采取的策略。即:TYPR,VALUE,POLICY。比如:DOMAIN-SUFFIX,apple.com,DIRECT IP-CIDR,192.168.0.0/16,ProxyA。

Surge 支持 6 中不同种类的规则:DOMAIN、DOMAIN-SUFFIX、DOMAIN-KEYWORD、GEOIP、IP-CIDR 和 FINAL。对于所采取的策略,可以是一个代理服务器,也可以是一个策略组,或者是内置的DIRECT和REJECT。不管使用什么样的规则、写了多少条规则,都必须要以一个FINAL规则结束,作为对所写规则以外情况的判断。

例子:

[Rule]
DOMAIN-SUFFIX,company.com,ProxyA
DOMAIN-KEYWORD,google,DIRECT
GEOIP,US,DIRECT
IP-CIDR,192.168.0.0/16,DIRECT
FINAL,ProxyB

英文版本:

简体中文版本:

DOMAIN、DOMAIN-SUFFIX 和 DOMAIN-KEYWORD 是。 而 IP-CIDR 和 GEOIP 则是。

https://manual.nssurge.com/book/understanding-surge/en/
https://manual.nssurge.com/book/understanding-surge/cn/
按域名判断的规则
按 IP 地址判断的规则

HTTP 规则

有两种 HTTP 规则类型。HTTP 规则对 HTTP 以及 HTTPS 请求有效,但对 TCP 连接无效。

USER-AGENT

USER-AGENT,Instagram*,DIRECT

这个规则匹配相应的 USER-AGENT,可以使用像 * 或者 ? 这样的通配符。

URL-REGEX

URL-REGEX,^http://google\.com,DIRECT

这个规则匹配符合正则表达式的 URL。

IP 地址规则

有两个根据 IP 匹配的规则。如果请求的主机名是一个域名,那么根据 IP 的规则将触发 DNS 查询。如果 DNS 查询失败,Surge 会停止规则测试并报错。

IP-CIDR

IP-CIDR,192.168.0.0/16,DIRECT
IP-CIDR,10.0.0.0/8,DIRECT
IP-CIDR,172.16.0.0/12,DIRECT
IP-CIDR,127.0.0.1/8,DIRECT

规则会匹配规则范围内请求的 IP 地址

IP-CIDR6

IP-CIDR6,2001:db8:abce:8000::/50,DIRECT

规则会匹配规则范围内请求的 IPv6 地址

GEOIP

GEOIP,US,DIRECT

规则会匹配相应国家和地区的 IP 地址

根据 IP 判断的规则的可选项

选项: no-resolve

GEOIP,US,DIRECT,no-resolve
IP-CIDR,172.16.0.0/12,DIRECT,no-resolve

当 GEOIP 或者 IP-CIDR 规则被触发,Surge 会发送一个解析请求去检查主机名是否为一个域名。你可以加上 “no-resolve” 以跳过这个过程。

请注意: 如果本地 DNS 服务器无法解析某些域名,请确保在这个规则前没有基于 IP 的规则会匹配到这个域名。否则,由于 DNS 解析错误,规则测试将失败。你可以用“no-resolve”来解决这个问题。

逻辑规则

需要复杂条件时,可以用逻辑运算符去组合多个规则。并且逻辑规则是可以相互嵌套的。U

AND 规则

当所含规则全部匹配时,会被触发

AND,((#Rule1), (#Rule2), (#Rule3)...),Policy

例子:

AND,((SRC-IP,192.168.1.110), (DOMAIN, example.com)),DIRECT

OR 规则

当所含的任意一个规则匹配时,会被触发

OR,((#Rule1), (#Rule2), (#Rule3)...),Policy

例子:

OR,((SRC-IP,192.168.1.110), (SRC-IP,192.168.1.111)),DIRECT

NOT 规则

当不符合所含规则时,会被触发

NOT,((#Rule1)),Policy

例子:

AND,((NOT,((SRC-IP,192.168.1.110))),(DOMAIN, example.com)),DIRECT

杂项规则

DEST-PORT

规则会匹配相应端口的出站请求。

DEST-PORT,80,DIRECT

SRC-IP

规则会匹配相应来源 IP 的入站请求。

SRC-IP,192.168.20.100,DIRECT

IN-PORT

规则会匹配相应端口的入站请求。 在 Surge 监听多个端口时会有用。

IN-PORT,6152,DIRECT

PROTOCOL

规则会匹配与协议参数相符合的请求。可选值:HTTP,HTTPS,TCP,UDP,DOH。

PROTOCOL,HTTP,DIRECT

SUBNET

如果 SSID / BSSID / 路由器IP地址 匹配,则规则匹配。支持通配符模式。

SUBNET,MyHome,DIRECT

SCRIPT

使用一段 JavaScript 脚本判断是否匹配。

SCRIPT,ScriptName,DIRECT

CELLULAR-RADIO(仅 iOS)

如果与当前网络使用的无线通信技术匹配,则规则匹配。可选值:GPRS, Edge, WCDMA, HSDPA, HSUPA, CDMA1x, CDMAEVDORev0, CDMAEVDORevA, CDMAEVDORevB, eHRPD, HRPD, LTE, NRNSA, NR

CELLULAR-RADIO,LTE,DIRECT

CELLULAR-CARRIER(仅 iOS)

如果与当前网络所使用的运营商匹配,则规则匹配。可选值为 MCC-MNC 风格代码。

CELLULAR-CARRIER,289-67,DIRECT

更多代码,请参考。

这里

策略

根据自定义规则,Surge 可以将一个请求转发给另一个代理服务器,或者直接发送到原始目标服务器上,即”直连“。Surge 会根据规则的”策略“部分决定如何处理请求。

有三种策略:代理、策略组、内置策略

代理策略

一个代理类型的策略,会将当前规则匹配到的请求发送到指定的代理服务器。Surge 支持以下几种协议的代理:HTTP、HTTPS、SOCKS5、SOCKS5-TLS。

配置文件中,[Prosy] 这一部分定义代理类型的策略(即代理服务器列表)。你可以填写多个代理服务器,这样就可以让不同的规则使用不同的服务器代理策略。

例子:

[Proxy]
ProxyHTTP = http, 1.2.3.4, 443, username, password
ProxyHTTPS = https, 1.2.3.4, 443, username, password
ProxySOCKS5 = socks5, 1.2.3.4, 443, username, password
ProxySOCKS5TLS = socks5-tls, 1.2.3.4, 443, username, password, skip-common-name-verify=true

代理类型

Surge 支持标准的的几个代理协议。

  • HTTP Proxy:ProxyHTTP = http, 1.2.3.4, 443, username, password

  • HTTPS Proxy (HTTP Proxy via TLS):ProxyHTTPS = https, 1.2.3.4, 443, username, password

  • SOCKS5:ProxySOCKS5 = socks5, 1.2.3.4, 443, username, password

  • SOCKS5 via TLS:ProxySOCKS5TLS = socks5-tls, 1.2.3.4, 443, username, password

Surge 也支持几个非标准的代理协议。

  • Snell:ProxySnell = snell, 1.2.3.4, 8000, psk=password

  • Shadowsocks:ProxySS = ss, 1.2.3.4, 8000, encrypt-method=chacha20-ietf-poly1305, password=abcd1234

  • VMess:ProxyVMess = vmess, 1.2.3.4, 8000, username=0233d11c-15a4-47d3-ade3-48ffca0ce119

  • Trojan:ProxyTrojan = trojan, 192.168.20.6, 443, password=password1

参数

所有类型的代理策略都包含的

  • interface:可选(默认值: N/A)。

    让该代理强制使用某个网络接口(只在 macOS 中有效)。请确保指定的网络接口到目的地址有正确的路由表。

    ProxyHTTP = http, 1.2.3.4, 443, username, password, interface = en2
  • allow-other-interface:可选(默认值:false)。

    ProxyHTTP = http, 1.2.3.4, 443, username, password, interface = en2, allow-other-interface=true

    除此之外,你还可以在 interface 基础上增加 allow-other-interface 参数。这样当指定的网络接口不可用时,会使用默认的网络接口,不然则会导致错误。

  • tfo

  • mptcp

  • no-error-alert

对于通过 TLS 代理(HTTP、SOCKS5-TLS、VMess、Trojan)的代理

  • skip-cert-verify:可选项,"true" 或 "false" (默认值为 "false")。

    如果该选项设置为 true,Surge 则不会验证服务器的证书是否有效。

  • sni (默认值:hostname)

    你可以在 TLS 握手时自定义 Server Name Indication(SNI)。通过添加 sni=off 可以关闭 SNI。 默认情况下,Surge 像大多数浏览器一样,会发送域名作为 SNI。

HTTP/HTTPS 协议的代理

  • always-use-connect

对于支持混淆的代理(Shadowsocks, Snell)

  • obfs

  • obfs-host

  • obfs-uri

对于 Snell

  • psk

  • version

对于 Shadowsocks

  • udp-relay

对于 VMess 协议

  • ws

  • ws-path

  • ws-headers

  • encrypt-method

TLS 代理的客户端证书

Surge 支持基于 TLS 代理的客户端证书验证。

[Proxy] Proxy = https, example.com, 443, client-cert=cert1
[Keystore] cert1 = base64=, password=123456

组件

Surge 由几个组件构成

Surge Proxy Server

这是 Surge 最核心的部分,由于使用 Objective-C 编写,并对 macOS 和 iOS 分别进行了优化,所以可以高效且稳定地作为 HTTP/SOCKS5 代理服务器处理所有请求。

Surge Virtual Network Interface (Surge VIF)

有些应用不遵从系统代理设置(比如邮件),因为他们需要使用原生 TCP socket。Surge VIF 可以处理这些流量。

在 iOS 种,这项功能是默认被开启的;而在 macOS 中,可以通过打开 增强模式 使用该功能。

以下是 Surge iOS 版的结构:

Surge Dashboard (macOS 版独有功能)

Surge Dashboard(在新版被译为“请求查看器”)是一个用于查看请求和 DNS 缓存的图形化界面。当开启远程访问时(设置 - 通用 - 远程 Dashboard),可以通过其他设备查看该设备的 Dashboard 信息。

其中 Snell 是由我们开发的一个轻量化的加密代理协议。你可以在这里查看服务端的代码 。

https://github.com/surge-networks/snell

Final 规则

FINAL 规则必须写在所有其他类型的规则之后。当某条请求不匹配上面的任何一条规则,都会与 FINAL 规则匹配。

例子:

[Rule]
DOMAIN-SUFFIX,company.com,ProxyA
DOMAIN-KEYWORD,google,DIRECT
GEOIP,US,DIRECT
IP-CIDR,192.168.0.0/16,DIRECT
FINAL,ProxyB

选项

选项: dns-failed

当 DNS 查询失败后,也会匹配 FINAL 规则,前提是 FINAL 规则的策略不是 DIRECT。

内置策略

有两个内置的策略:DIRECT 和 REJECT。DIRECT 会把流量直接发送到目标服务端,REJECT 则会拒绝请求。

DIRECT 和 REJECT 这两个策略可以直接用。或者你也可以像下面这样,用一个名字代替这两个内置策略。

别名

[Proxy]
On = direct
Off = reject

之后你就可以在规则里使用 On 和 Off 作为策略组的策略名。

网络接口选项

直连策略支持指定网络接口,即可以使用”interface“这个参数。

[Proxy]
Crop-VPN = direct, interface = utun0
WiFi = direct, interface = en2, allow-other-interface=true

请确保指定的网络接口到目的地址有正确的路由表。

除此之外,你还可以在 interface 基础上增加 allow-other-interface 参数。这样当指定的网络接口不可用时,会使用默认的网络接口,不然则会导致错误。

DNS

为了支持高级特性,Surge 使用自己的自定义 DNS 服务器。所以可能跟你在系统里设置的效果不太一样。

上游 DNS 服务器

默认情况下,Surge 会使用系统设置里的 DNS 服务器。你可以通过 dns-server 这个选项来覆盖系统设置。

细节信息

Surge 通过并发查询提高性能,会使用返回结果最快的服务器,这类似于 dnsmasq 命令的 --all-servers 选项。Surge iOS 版和 macOS 版的 Dashboard 会显示返回结果最快的那个服务器地址。如果 Surge 在 2 秒内没有收到任何响应,它将再次查询所有预设的 DNS 服务器。重试 4 次后如果还没有结果,Surge 将放弃查询并报告 DNS 错误。 某些域名可能具有性能较差的权限域名服务器,导致上游 DNS 服务器由于服务器端超时或其他连接问题返回空结果。如果所有上游 DNS 服务器确实都返回空的 DNS 查询结果,或者某些服务器返回空的结果,但其他服务器在 2 秒内没有响应,那 Surge 也将返回 DNS 查询结果为空的错误。 当 IPv6 可用并启用时,Surge DNS 客户端将向上游 DNS 服务器同时发送 A 和 AAAA 查询。将使用返回的第一个 A 或 AAAA 查询结果。

Surge 通过并发查询提高性能,会使用返回结果最快的服务器,这类似于 dnsmasq 命令的 --all-servers 选项。Surge iOS 版和 macOS 版的 Dashboard 会显示返回结果最快的那个服务器地址。如果 Surge 在 2 秒内没有收到任何响应,它将再次查询所有预设的 DNS 服务器。重试 4 次后如果还没有结果,Surge 将放弃查询并报告 DNS 错误。 某些域名可能具有性能较差的权限域名服务器,导致上游 DNS 服务器由于服务器端超时或其他连接问题返回空结果。如果所有上游 DNS 服务器确实都返回空的 DNS 查询结果,或者某些服务器返回空的结果,但其他服务器在 2 秒内没有响应,那 Surge 也将返回 DNS 查询结果为空的错误。 当 IPv6 可用并启用时,Surge DNS 客户端将向上游 DNS 服务器同时发送 A 和 AAAA 查询。将使用返回的第一个 A 或 AAAA 查询结果。

本地 DNS 映射

Surge 支持本地自定义 DNS 映射。这跟你编辑系统的 /etc/hosts 文件是一样的,但功能更强大。

[Host]
abc.com = 1.2.3.4
*.dev = 6.7.8.9
foo.com = bar.com
bar.com = server:8.8.8.8
baz.com = server:https://cloudflare-dns.com/dns-query

通配符

你可以像下面这样使用像 * 这样的通配符匹配整个域名。但这个 * 能匹配一整个不被 . 分隔的字符串,所以使用时请注意。例如,*google.com 可以匹配 google.com、foo.google.com 和 bargoogle.com。而 *.google.com 则不能匹配 google.com。

[Host]
*.dev = 6.7.8.9

同义名

这类似于 CNAME 类型的解析。

[Host]
foo.com = bar.com

关联 DNS Server

你可以给一个或多个域名指定想要的 DNS 服务器。

[Host]
bar.com = server:8.8.8.8

由于 Surge 有自己的 DNS 客户端实现方式,所以一些主机名可能无法解析。您可以使用 server:system 让系统解析。

[Host]
Macbook = server:system

默认情况下 .local 的域名都是由系统自己解析的。

组合使用

上面说的几个使用方法可以组合使用。

[Host]
*.dev = foo.com
*.bar.com = server:system

进程规则

你可以写能够匹配软件进程的规则,该规则只在 Surge macOS 版生效,iOS 版会自动忽略这个类型的规则。

PROCESS-NAME

PROCESS-NAME,Telegram,Proxy

规则会匹配这个进程名的程序,支持 * 和 ? 两种通配符。

你也可以把该进程名的所在目录写清楚。至于如何找到这个名称,对于 macOS 软件包,一般在 .app/Contents/MacOS 位置下

DNS 服务器

你可以使用这些选项去覆盖系统的 DNS 设置。

[General]
dns-server = 8.8.8.8, 8.8.4.4

可以用 system 来代表系统本身的 DNS 设置(其中,与后面重复的 DNS 服务器将被忽略)。

[General]
dns-server = system, 8.8.8.8, 8.8.4.4

HTTPS 解密

证书生成器会帮你生成一份受系统信任的用于开发的 CA 证书。这个在 macOS 和 iOS 版都能操作。生成的证书只会被保存在本地和系统的钥匙串。新生成的证书对应的 key 通过 OpenSSL 随机生成。

你也可以用既存的 CA 证书,需要以 PKCS#12(.p12)格式导出并随附密码,然后就像下面这样导入。需要注意的是,系统不接受 ca-passphrase 为空的证书,所以这一项必须要填。密码可以用 base64 命令加密成一个 base64 类型的字符串。

[MITM]
enable = true
ca-p12 = MIIJtQ.........
ca-passphrase = password
hostname = *google.com

打开 MitM 解密后,Surge 默认不会解密所有流量,只会解密被声明的流量。

  • 可以使用像 *、? 这样的通配符。

  • 使用 - 排除域名。

  • 默认情况下,只会解密 443 端口的请求。

    • 添加 :port 来解密其他端口的 HTTPS 流量。

    • 添加 suffix :0 来解密所有端口的 HTTPS 流量。

Example:

  • -*.apple.com: 排除所有通过 443 端口发送给 *.apple.com 的请求。

  • www.google.com: 解密通过 443 端口的 www.google.com 的 HTTPS 流量。

  • www.google.com:8080: 解密通过 8080 端口的 www.google.com 的 HTTPS 流量。

  • www.google.com:0: 解密所有端口的 www.google.com 的 HTTPS 流量。

  • *:0: 解密所有的 HTTPS 流量。

如果有多条,可以像下面这样写:

hostname = -*.apple.com, -*.icloud.com, *

某些程序有着严格的安全策略,不会认可自己生成的证书。此时,使用解密的话可能会导致一些问题。

选项

tcp-connection

默认情况下,MitM 只会解密通过 Surge HTTP 代理的流量。如果使用 tcp-connection 选项,Surge 则会通过使用 VIF(增强模式)对原始 TCP 流量进行解密。但这样你仍然需要在 hostname 列表中声明解密的域名。

skip-server-cert-verify

MitM 时不验证证书。

Surge 通过中间人攻击(MitM)解密 HTTPS 流量。你可以参考维基百科的这篇文章获取关于 MitM 的更多信息:。

Wikipedia

重写 URL

Surge 可以通过两种方式来重写请求的 URL,或者根据 URL 拒绝特定的请求。

例子:

[URL Rewrite]
^http://www\.google\.cn http://www.google.com header
^http://yachen\.com https://yach.me 302
^http://ad\.com/ad\.png - reject

一条重写规则由 3 部分组成:正则表达式、替换的内容和类型。

重写请求头的域名

Surge 能重写请求头的域名,指向一个新的地址发送请求。发送请求的客户端不会发现这个操作。

例子:

[URL Rewrite]
^http://www\.google\.cn http://www.google.com header

不能使用 HTTPS Scheme 重定向。而且也不能重定向 HTTPS 请求。

302 重定向

Surge 会直接返回一个 302 重定向的响应。如果启用了 MitM,并将域名添加到了解密列表,那么这个方法可以使用 HTTPS。

[URL Rewrite]
^http://yachen\.com https://yach.me 302

拒绝请求

拒绝符合正则表达式的请求。这种就不用写替换的内容了。如果启用了 MitM,并将域名添加到了解密列表,那么这个方法可以使用 HTTPS。

[URL Rewrite]
^http://ad\.com/ad\.png - reject

规则集

从 Surge macOS 3.0 版本、Surge iOS 3.4 版本之后,你可以通过 URL 或者文件导入一个规则集,这个规则集里可以包含很多条规则。 而 Surge 本身则提供两个内置的规则集,包含了已经写好的预置规则。

内置规则集

SYSTEM

RULE-SET,SYSTEM,DIRECT

这个规则集包含了绝大多数来自 macOS 和 iOS 系统本身所发送的请求。但 App Store、iTunes 和其他虽然是苹果推出,但却是内容提供为主的 App 所发送的请求不包含在内。

USER-AGENT,*com.apple.mobileme.fmip1
USER-AGENT,*WeatherFoundation*
USER-AGENT,%E5%9C%B0%E5%9B%BE*
USER-AGENT,%E8%AE%BE%E7%BD%AE*
USER-AGENT,com.apple.geod*
USER-AGENT,com.apple.Maps
USER-AGENT,FindMyFriends*
USER-AGENT,FindMyiPhone*
USER-AGENT,FMDClient*
USER-AGENT,FMFD*
USER-AGENT,fmflocatord*
USER-AGENT,geod*
USER-AGENT,locationd*
USER-AGENT,Maps*
DOMAIN,api.smoot.apple.com
DOMAIN,captive.apple.com
DOMAIN,configuration.apple.com
DOMAIN,guzzoni.apple.com
DOMAIN,smp-device-content.apple.com
DOMAIN,xp.apple.com
DOMAIN-SUFFIX,ess.apple.com
DOMAIN-SUFFIX,push-apple.com.akadns.net
DOMAIN-SUFFIX,push.apple.com
DOMAIN,aod.itunes.apple.com
DOMAIN,mesu.apple.com
DOMAIN,api.smoot.apple.cn
DOMAIN,gs-loc.apple.com
DOMAIN,mvod.itunes.apple.com
DOMAIN,streamingaudio.itunes.apple.com
DOMAIN-SUFFIX,lcdn-locator.apple.com
DOMAIN-SUFFIX,lcdn-registration.apple.com
DOMAIN-SUFFIX,ls.apple.com
PROCESS-NAME,trustd

这些规则在今后的版本中可能会发生变化,请留意软件中相关说明。

LAN

RULE-SET,LAN,DIRECT

包括了 'local' 后缀和私有 IP 地址。注意:这个规则集会触发 DNS 查询。

DOMAIN-SUFFIX,local
IP-CIDR,192.168.0.0/16
IP-CIDR,10.0.0.0/8
IP-CIDR,172.16.0.0/12
IP-CIDR,127.0.0.0/8
IP-CIDR,100.64.0.0/10
IP-CIDR6,fe80::/10

External Ruleset

可以通过 URL 或者本地文件的形式导入外置规则集。规则集的内容是一行一条规则,但不写对应的策略。

例子:

DOMAIN,exampleA.com
DOMAIN,exampleB.com

策略组

一个策略组可以包含多个策略,然后这个策略组本身可以被当成一个策略来使用,并且也可以被嵌套。

有以下几种类型的策略组:select、url-test 和 ssid。配置文件中,在 [Proxy Group] 下定义策略组。

手动选择的策略组

可以在用户界面选择使用策略组里的某个策略。

SelectGroup = select, ProxyHTTP, ProxyHTTPS, DIRECT, REJECT

在 iOS 版中,你可以在系统的 Today 页面中的小组件控制第一个 select 类型的策略组。而在 macOS 版中,可以通过点击状态栏中的 Surge 图标来手动切换 select 类型的策略组。

自动测试的策略组

这个策略组会测试到包含的所有策略的延迟,自动选择最低的策略。

AutoTestGroup = url-test, ProxySOCKS5, ProxySOCKS5TLS, url = http://www.google.com/generate_204

参数

url:必须

Surge 会发送一个 HTTP 头请求到目的地址。这个测试只关心是否收到了应答数据,即便返回的是一个 HTTP 错误 的数据。

interval:可选,单位是秒(默认值:600s)

超过这个时间,就不会再进行测试了,除非重新或者正在使用这个策略组。

tolerance:可选,单位是毫秒(默认值:100ms)

每次重新测试时,只有当延迟最低的策略比上一次测试的延迟最低的策略快 100ms 时,才会使用新的策略。

timeout:可选,单位是秒(默认值:5s)

当测速策略组中的某个策略超过该时间没有应答,则会放弃该策略。

Fallback 策略组

与上面提到的自动测试的策略组原理相同,只不过 Fallback 按照从前到后的顺序选择可用策略,而不是选择延迟最低的策略。

FallbackGroup = fallback, ProxySOCKS5, ProxySOCKS5TLS, url = http://www.google.com/generate_204

参数

url:必须

Surge 会发送一个 HTTP 头请求到目的地址。这个测试只关心是否收到了应答数据,即便返回的是一个 HTTP 错误 的数据。

interval:可选,单位是毫秒(默认值:100ms)

超过这个时间,就不会再进行测试了,除非重新或者正在使用这个策略组。

timeout:可选,单位是秒(默认值:5s)

当测速策略组中的某个策略超过该时间没有应答,则会放弃该策略,使用后面一个策略。

SSID 策略组

根据当前的 Wi-Fi 名称选择相应的策略。

SSIDGroup = ssid, default = ProxyHTTP, cellular = ProxyHTTP, SSIDName = ProxySOCKS5

参数

default:必须

如果当前 Wi-Fi 名称不在该策略组里,则使用 default 指定的策略。

cellular:可选

指定在蜂窝数据下使用的策略。如果不指定,则使用 default 所指定的策略。T

External 策略组

这个功能在 macOS 3.0 版以及 iOS 3.4 版和之后提供。策略组中包含的策略,可以通过链接或者文件的方式远程引用。

egroup = select, policy-path=proxies.txt

这个文件包含了一系列的策略,格式如下:

Proxy-A = https, example1.com, 443
Proxy-B = https, example2.com, 443

update-interval:可选,单位为秒

如果是远程引用,可以设定更新的时间间隔。

policy-regex-filter:可选

使用正则表达式在引用的节点中筛选出需要的。

重写请求头

这个功能目前仅在 Beta 版中提供。

在请求被转发到服务器前,Surge 可以修改请求头。

例子:

[Header Rewrite]
^http://example.com header-add DNT 1
^http://example.com header-del Cookie
^http://example.com header-replace User-Agent Unknown

每条规则包含 4 个部分:URL 正则表达式、行为类型、请求头里的哪个部分以及变量。T

header-add

像请求头中附加一条属性。

例子:

[Header Rewrite]
^http://example.com header-add DNT 1

Before:
GET /index.html HTTP/1.1
Host: example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30
Accept-Language: en-us
Accept-Encoding: gzip, deflate

After:
GET /index.html HTTP/1.1
Host: example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30
Accept-Language: en-us
Accept-Encoding: gzip, deflate
DNT: 1

header-del

删除请求头中的某条属性。

例子:

[Header Rewrite]
^http://example.com header-del DNT

Before:
GET /index.html HTTP/1.1
Host: example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30
Accept-Language: en-us
Accept-Encoding: gzip, deflate
DNT: 1

After:
GET /index.html HTTP/1.1
Host: example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30
Accept-Language: en-us
Accept-Encoding: gzip, deflate

header-replace

替换请求头中某条属性的值。

例子:

[Header Rewrite]
^http://example.com header-replace DNT 1

Before:
GET /index.html HTTP/1.1
Host: example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30
Accept-Language: en-us
Accept-Encoding: gzip, deflate
DNT: 0

After:
GET /index.html HTTP/1.1
Host: example.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.1 Safari/603.1.30
Accept-Language: en-us
Accept-Encoding: gzip, deflate
DNT: 1

如果你想添加或替换请求头中的任意一个参数,你可以将 header-add 和 header-del 组合起来用。

[Header Rewrite]
^http://example.com header-del DNT
^http://example.com header-add DNT 1

HTTP 响应

用于修改 HTTP 返回体,该类型下第二参数为匹配 URL 的正则表达式,被匹配到的请求会被执行脚本。

传入参数为 $request 和 $response,字段为

  • $request.url<String>:请求的 URL

  • $request.method<String>:请求的 HTTP 方法

  • $response.status<Number>: 响应的 HTTP 状态码

  • $response.headers<Object>: 响应的 HTTP Headers

  • $response.body<String>: 响应的 HTTP Body,以 UTF-8 解码后的字符串,仅当 requires-body=true 时有效

应执行 $done 返回一个对象,可选包含三个字段:

  • body<String>:使用该 body 覆盖原来的响应 body,仅当 requires-body=true 时有效

  • headers<Object>:使用该 headers 词典完全覆盖原来的 headers,注意部分 HTTP 特殊字段不可被修改,如 Content-Length

  • status<Number>:覆盖原来的 HTTP 状态码

使用 $done(); 表示终止该请求;使用 $done({}); 表示不对该请求进行修改。

一个简单样例

let headers = $response.headers;
headers['X-Modified-By'] = 'Surge';

$done({headers});

注意样例使用了 JS ES6 语法。

基础

脚本需要 Surge iOS 4 或 Surge Mac 3.3.0 以上版本

脚本

使用 JavaScript 随心所欲的拓展 Surge 功能。

脚本字段

[Script]
script1 = type=http-response,pattern=^http://www.example.com/test script-path=test.js,max-size=16384,debug=true
script2 = type=cron,cronexp="* * * * *",script-path=fired.js
script3 = type=http-request,pattern=^http://httpbin.org script-path=http-request.js,max-size=16384,debug=true,requires-body=true
script4 = type=dns,script-path=dns.js,debug=true
script5 = type=event,event-name=network-changed,control-api=1,script-path=event.js

每一行都有两个组成部分:脚本名称、参数。常见的参数:

  • type:脚本的类型,可选值:http-request, http-response, cron, event, dns, rule.

  • script-path:脚本的路径,可以是相对路径、绝对路径或者 URL。

  • script-update-interval:当脚本路径为 URL 时的自动更新频率,单位为秒。

  • debug:开启 debug 模式,如果是一个本地脚本,那么每次执行脚本时,都会从本地存储重新加载脚本,方便调试。

  • control-api:允许脚本内使用 $surge 变量,控制 Surge。

  • timeout:脚本的最长运行时间,默认为 10s。

http-request/http-response 类型脚本的可用参数:

  • pattern:匹配URL的正则。

  • requires-body:表示该脚本需要对 body 进行处理,默认为 false。如果只需要修改 URL 或者 Headers 请不要开启该选项,将大幅节约资源。

  • max-size:表示该脚本最大允许处理的 body 大小,若超过则放弃处理,默认值为 131072 (128KB)。

由于进行脚本修改会需要 Surge 先将 response body 完全下载后再进行处理,如果遇到了较大的数据将导致内存占用量暴增,Surge iOS 受系统内存限制在该情况下极易被直接终止。所以请务必仔细配置 URL 匹配规则,仅对需要的 URL 进行处理。

当返回的数据长度超过 max-size 设定值后,Surge 将放弃对该请求执行脚本并回退到 passthrough 模式。

基本定义

所有脚本允许异步操作,使用 $done(value<Object>) 方法表示完成并返回相应结果。即使是不要求返回结果的脚本类型也应当在完成任务后调用 $done() 退出,否则脚本会因为超时而产生警告。

性能

JS Script 的执行效率极高,不必担心因使用脚本而带来性能问题(Body 修改类除外,会影响整体逻辑),在我们的测试环境下,一个简单脚本的完整执行仅耗时 0.2ms。

公共API

  • console.log(message<String>) 输出到 Surge 日志

  • setTimeout(function[, delay]) 与浏览器的 setTimeout 方法一致

  • $httpClient.post(URL<String> or options<Object>, callback<Function>) 发起一个 HTTP POST 请求。第一个参数可以是一个 URL 或参数表,参数表为。

    {
      url:"http://www.example.com/",
        headers:{
          Content-Type:"application/json"
        },
      body:"{}"
    }

    当使用参数表时,url 参数必选,其余选填,header 字段存在会覆盖默认的所有 Header。body 可以是 string 或 object。当为 object 时,将自动进行 JSON 编码,并设置 'Content-Type' 为 'application/json'。

    callback 定义为callback(error<String>, response<Object>, data<String>) error 为 Null 表示请求成功,response 包含 status 和 headers 两个字段。

    其余类似的方法有:$httpClient.get,$httpClient.put,$httpClient.delete,$httpClient.head,$httpClient.options,$httpClient.patch。

  • $notification.post(title<String>, subtitle<String>, body<String>) 向通知中心发送通知,Surge iOS 上需开启通知总开关

  • $utils.geoip(ip<String>) 进行 GeoIP 查询,返回结果为 ISO 3166 的国家编码

  • $surge.setSelectGroupPolicy(groupName<String>, policyName<String>) 修改 select 策略组的当前选项,返回 bool 值表示是否成功

  • $surge.selectGroupDetails() 获得当前 select 策略组的信息,包含组名称,子策略,和当前选择的策略

  • $surge.setOutboundMode(mode<String>) 修改 Surge 的出站模式,返回 bool 值表示是否成功,取值可为 "direct", "global-proxy", "rule"

  • $surge.setHTTPCaptureEnabled(enabled<Boolean>) 控制 Surge 截获 HTTP 功能的开启

  • $surge.setCellularModeEnabled(enabled<Boolean>) 控制 Surge 计费网络模式的开启

  • $surge.setRewriteEnabled(enabled<Boolean>) 控制 Surge 重写功能的开启

  • $surge.setEnhancedModeEnabled(enabled<Boolean>) 控制 Surge 增强模式的开启 (仅 Surge Mac 可用)

  • $network 当前网络状态的总览,包含 IP 和 SSID 等信息

  • $script.name<String> 当前执行的脚本的文件名

  • $script.startTime<Date> 当前执行的脚本的开始时间

  • $persistentStore.write(data<String>, [key<String>]) 持久化保存数据,返回 bool 值表示是否成功,仅支持传入 string

  • $persistentStore.read([key<String>]) 读取保存的持久化数据,返回 string 或 Null 不传入 key 时,同一个 script-path 的脚本共享一个存储池。可传入一个固定的 key 以在多个脚本间共享数据。

HTTP 请求

用于修改 HTTP 请求体,该类型下第二参数为匹配 URL 的正则表达式,被匹配到的请求会被执行脚本。

传入参数为 $request,字段为:

  • $request.url<String>:请求的 URL

  • $request.body<String>:请求的 Body,以 UTF-8 解码后的字符串,仅当 requires-body = true 时有效

  • $request.method<String>:请求的 HTTP 方法

  • $request.headers<Object>:请求的 Headers

应执行 $done() 返回一个对象,可选包含字段:

  • url<String>:使用该 URL 覆盖原请求的 URL,注意使用脚本修改 URL 时不会像 URL Rewrite 那样自动调整 Header 的 Host 字段,如果需要调整需要手动返回新的 headers 字段

  • headers<Object>:使用该 headers 词典完全覆盖原来的 headers,注意部分 HTTP 特殊字段不可被修改,如 Content-Length

  • body<String>:使用该 body 覆盖原来的请求 body,仅当 requires-body = true 时有效。

  • response<Object>:如果返回了该字段,则表示 Surge 会直接返回 HTTP 响应,而不进行真实的网络操作。可包含三个字段:

    • status<Number>: 响应的 HTTP 状态码。(可选,默认为:200)

    • headers<Object>: 响应的 HTTP Headers。(可选)

    • body<String>: 响应的 HTTP Body,将以 UTF-8 编码后返回。(可选)

使用 $done(); 表示终止该请求,使用 $done({}); 表示不对该请求进行修改。

当前版本的一些限制:

  • 当请求使用 chunked encoding 上传 body 时,不可以进行 body 修改。

  • 当 Expect: 100-continue 存在时,不可以进行 body 修改。

一个简单样例

let headers = $request.headers;
headers['X-Modified-By'] = 'Surge';

$done({headers});

注意样例使用了 JS ES6 语法。

事件

在发生特定事件时执行脚本,该类型下第二参数为事件名称,目前只有 network-changed 一个事件。

脚本任务执行完毕后请调用 $done() 退出

一个简单样例

// script = type=event,event-name=network-changed,control-api=1,script-path=event.js
$notification.post('DNS Update', $network.dns.join(', '));
$done();

脚本规则

使用脚本进行规则判定,该类型下第二参数为规则名。

script = type=rule,script-path=rule.js

然后在 [Rule] 中加入规则:

SCRIPT,ssid-rule,DIRECT

脚本返回一个词典,字段 matched<Boolean> 表示是否匹配该规则。

传入参数有:

  • $request.hostname<String>

  • $request.destPort<Number>

  • $request.processPath<String>

  • $request.userAgent<String>

  • $request.url<String>

  • $request.sourceIP<String>

  • $request.listenPort<Number>

  • $request.dnsResult<Object>

默认情况下,SCRIPT 规则不会触发 DNS 解析,如果需要进行 DNS 解析,可使用 requires-resolve 修饰规则

SCRIPT,ssid-rule,DIRECT,requires-resolve

DNS 结果将出现在 $request.dnsResult 字段。

一个简单样例:

var hostnameMatched = ($request.hostname === 'home.com');
var ssidMatched = ($network.wifi.ssid === 'My Home');

$done({matched: (hostnameMatched && ssidMatched)});

杂项

General 部分

ipv6 默认值:false

启用完整的IPv6支持

loglevel 默认值:notify

可选值为 verbose info notify 和 warning。不建议在日常使用中开启 verbose,因为这会消耗过多的资源使系统变慢。

skip-proxy

在 iOS 平台,这个选项中的域名和IP段将由 Surge TUN 处理,而不是 Surge Proxy。 在 macOS 平台,这个选项仅在“设置为系统代理”开启时生效,用于解决某些应用的兼容问题。 指定某个域名。例如,apple.com 指定网站的所有域名,在域名前添加星号。例如,*apple.com 指定某个域的特定部分。例如,store.appple.com 通过 IP 地址指定某个主机或网络,输入 IP 地址或地址范围。例如,192.168.2.* 192.168.2.0/24

注意: 如果设置了IP地址或地址范围,仅在使用该地址连接主机时绕过代理,连接到解析后的地址时无法生效。

use-default-policy-if-wifi-not-primary (仅macOS)

如果 Wi-Fi 不是主接口,则使用SSID组的默认策略。

proxy-settings-interface (仅macOS)

助手代理接口设置。

always-real-ip

当 Surge VIF 处理 DNS 问题时,这个选项要求 Surge 返回一个真实的 IP 地址,而不是一个假的 IP 地址(Fake-IP)。

DNS 请求包将被转发到上游的 DNS 服务器。

hijack-dns

默认情况下,Surge 只对发送至 Surge DNS地址(198.18.0.2)的 DNS 查询返回 Fake-IP 地址。发送到标准 DNS 的查询将被转发。

有些设备或软件始终使用硬编码的 DNS 服务器。例如,谷歌的一款音箱总是使用 8.8.8.8)。你可以使用这个选项来劫持查询以获得一个 Fake-IP 地址。

你可以使用 hijack-dns = *:53 来劫持所有 DNS 请求。

force-http-engine-hosts

让 Surge 将 TCP 连接视为 HTTP 请求。Surge 的 HTTP 引擎将处理这些请求,并且所有的高级功能都可以使用,如捕获、重写和脚本。

  • 通配符支持 * 和 ?。

  • 使用前缀 - 排除域名。

  • 默认情况下,只有去往 80 端口的请求会被解码。

    • 使用后缀 :port 启用指定端口

    • 使用后缀 :0 启用所有端口。

例如:

  • -*.apple.com: 排除所有去往 *.apple.com 的 80 端口。

  • www.google.com: 强制 HTTP 引擎处理通往 www.google.com 的 80 端口的请求。

  • www.google.com:8080: 强制 HTTP 引擎处理通往 www.google.com 的 8080 端口的请求。

  • www.google.com:0: 强制 HTTP 引擎处理通往 www.google.com 所有端口的请求。

  • *:0: 强制 HTTP 引擎处理通往所有主机的所有端口请求。

tun-excluded-routes

Surge VIF 只能处理 TCP 和 UDP 协议。使用此选项可以绕过特定的 IP 范围,允许所有流量通过。

注意:此选项只适用于 Surge VIF。由 Surge 代理服务器处理的请求不受影响。结合skip-proxy 和 tun-excluded-routes 来确保特定的 HTTP 流量绕过 Surge。 这个选项可能会导致系统错误 ENOMEM(无法分配内存)。这似乎是 iOS 系统的一个 Bug。如果可能的话,请不要使用这个选项。

tun-included-routes

在默认情况下,Surge VIF 声明自己为默认路由。但是,由于 Wi-Fi 接口的路由较小,有些流量可能不会通过 Surge VIF 接口。使用此选项可以添加一个较小的路由。

compatibility-mode (仅iOS)

allow-wifi-access (仅iOS)

wifi-access-http-port (仅iOS)

wifi-access-socks5-port (仅iOS)

show-error-page-for-reject

exclude-simple-hostnames

socks5-listen (仅macOS)

http-listen (仅macOS)

proxy-test-url

test-timeout

internet-test-url

read-etc-hosts

network-framework

tls-provider

debug-cpu-usage

debug-memory-usage

[General]
ipv6 = false
loglevel = notify
skip-proxy = 127.0.0.1, 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12, 100.64.0.0/10, localhost, *.local
tun-excluded-routes = 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12
tun-included-routes = 192.168.1.12/32
ipv6 = true
loglevel = notify
skip-proxy = 127.0.0.1, 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12, 100.64.0.0/10, localhost, *.local
use-default-policy-if-wifi-not-primary = false
proxy-settings-interface = Wi-Fi
always-real-ip = *.apple.com
hijack-dns = 8.8.8.8:53
force-http-engine-hosts = example.com:80
tun-excluded-routes = 192.168.0.0/16, 10.0.0.0/8, 172.16.0.0/12
tun-included-routes = 192.168.1.12/32

计划任务

在指定时间执行 cron 脚本。cronexp 应该是一个 cron 表达式,它是一个由五个或六个子表达式(字段)组成的字符串,描述了计划的各个细节。

译者按:Surge 兼容五位和六位的表示方法。

一些 cron 表达式的样例如下:

  • at 2am daily: 0 2 * * *

  • at 5 AM and 5 PM daily: 0 5,17 * * *

  • on every minutes: * * * * *

  • on every Sunday at 5 PM: 0 17 * * sun

  • every 10 minutes: */10 * * * *

只有一个传入参数: $cronexp。

脚本任务执行完毕后请调用 $done() 退出。

一个简单样例:

// script = type=cron,cronexp="* * * * *",script-path=cron.js
$surge.setSelectGroupPolicy('Group', 'Proxy');
$done();

托管配置

如果配置文件以下面内容作为文件的开始,则 Surge 会自动的从 URL 更新配置文件。

#!MANAGED-CONFIG http://test.com/surge.conf interval=60 strict=true

配置文件仅在 Surge 主程序运行时更新。

注意:新版的托管配置一定包含#!MANAGED-CONFIG,否则会视为普通配置文件。

参数

  • interval: 可选,单位秒(默认值:86400s)

    更新配置文件的间隔时间。

  • strict: 可选值 true 或 false (默认值:false)

    当设置为 true 时,则会在间隔时间到达后,强制更新。如果更新失败用户则会使用过时的配置文件。

    注意: 即使 strict 设置为 true,用户仍可以通过小组件和 VPN 开关启动 Surge。

增强模式

仅 Surge Mac 2.1.0 以上版本支持此功能

有些应用可能不会遵循系统代理设置,使用增强模式可以处理让 Surge 处理所有应用。

  • Surge 会安装一个虚拟网卡并设置为默认路由。所有的 DNS 响应会返回 198.18.0.0/15 地址段的一个虚拟 IP 地址。

  • Surge 虚拟网卡只能处理 TCP,UDP 和 ICMP 流量,仅在必要时开启此功能。

  • ICMP 流量不能被代理,Surge 虚拟网卡将直接返回响应结果。

网关模式

什么是网关模式

使用 Surge 当做网关接管其他设备的所有网络请求。

如何启用

  1. 启动增强模式。

  2. 修改其他设备上的网络设置

    • 设置网关为当前设备的 IP 地址

    • 设置 DNS 为 192.0.2.2

    • 保持 IP 地址和子网掩码不变

SSID 挂起

仅 Surge iOS 支持此功能

您可以通过一下配置,指定 Surge iOS 在 Wi-Fi SSID 下暂时挂起。

[SSID Setting]
"Crop-VPN" suspend=true

DNS

使用脚本去执行 DNS 解析操作,该类型下第二参数为脚本名。

script = type=dns,script-path=dns.js,debug=true

之后需要在 [Host] 中对相应域名进行配置。

[Host]
baidu.com = script:dnspod
*.baidu.com = script:dnspod

传入参数为 $domain,当前查询的域名。

返回结果可为下列中的任意一个:

  • 当返回 address<String> 时,直接使用该 IP 地址为结果,必须是一个有效的 IPv4 或者 IPv6 地址的字符串表示。

  • 当返回 addresses<Array> 时,使用多个IP地址作为结果。

  • 当返回 server<String> 时,表示应交给特定的 DNS 服务器进行查询。

  • 当返回 servesr<Array> 时,表示指定多个上游 DNS 服务器进行查询。

当返回 address<String> 或者 addresses<Array> 时,可以额外返回 TTL 字段,将本次的结果记入 DNS 缓存避免重复查询,单位为秒。

以下样例使用了 DNSPod 的公开 HTTP DNS API,通过脚本扩展的方式让 Surge 可以随意支持各种协议的 DNS 查询

$httpClient.get('http://119.29.29.29/d?dn=' + $domain, function(error, response, data){
  if (error) {
    $done({}); // Fallback to standard DND query
  } else {
$done({addresses: data.split(';'), ttl: 600});
  }
});

Snell 服务端

从 3.1.0 开始,你可以将 Surge macOS 版本身作为一个 Snell 代理服务器。在配置文件中加入如下字段:

[Snell Server]
interface = 0.0.0.0
port = 6160
psk = RANDOM_KEY_HERE
obfs = off

Snell 是由我们团队开发的一个新的加密代理协议。你可以在这里找到服务端的代码:

https://github.com/surge-networks/snell

URL Scheme

URL Scheme(仅 Surge iOS)

Surge iOS 支持 4 种操作和 1 种参数。

操作:

  • surge:///start

    启动当前选中的配置文件。

  • surge:///stop

    停止当前会话。

  • surge:///toggle

    启动或停止当前选中的配置文件。

  • surge:///install-config?url=x

    从某个 URL 安装配置文件。该 URL 需先通过百分号编码处理。

参数:

  • autoclose=true

    操作完成时自动关闭 Surge。(不能与 install-config 一同使用)

    样例:surge:///toggle?autoclose=true

x-callback-url

Surge 从 3.4 版本开始支持 x-callback-url 规范。URL 的 scheme 为 surge,可用的操作有 start、stop 和 toggle。

DNS over HTTPS

如果配置了 DNS-over-HTTPS,传统的 DNS 仅用于测试连接性和解析 DOH 地址里面的域名。

为所有域名使用 DoH

[General]
doh-server = https://9.9.9.9/dns-query

你可以指定多个 DNS-over-HTTPS 服务器(不推荐)。

为特定域名使用 DoH

[Host]
example.com = server:https://cloudflare-dns.com/dns-query

如果直接使用 IP 地址

像下面这样设置即可:

[General]
dns-server = https://9.9.9.9/dns-query

你可以指定多个 DoH 服务器(并不建议),甚至和普通 DNS 服务器地址放在一起。但由于 DoH 返回结果一般比普通 DNS 慢,所以反而会在并发查询中被丢弃。

DNS over HTTPS 格式

对于 DoH 格式,有两种形式:JSON 和 DNS wireformat (RFC1035)。

你需要确定你的 DoH 服务支持哪一种形式。

  • Surge iOS 4.1 和以下的版本 / Surge Mac 3.4.1 和以下的版本: 只支持 JSON 形式的文件。

  • Surge iOS 4.2 和以上的版本 / Surge Mac 3.5.0 和以上的版本: Surge 默认使用 DNS wireformat,你也可以继续使用 JSON 文件。

      [General]
      doh-format=json

通过代理使用 DoH

如果你想通过代理使用 DoH,你可以打开 doh-follow-outbound-mode。

[General]
doh-follow-outbound-mode=true

所有的 DoH 会走出站模式的设置,然后再编辑规则,让 DoH 的域名走代理。

或者,使用 PROTOCOL,DOH 规则去匹配所有的 DoH 连接。

更新日志

由于内容过多,而且很多就是老信息,所以随缘更新,可能只会翻译后来更新的内容

模块

Module(模块)是一系列设置的集合,可用于覆盖当前配置的部分设定,有非常多的使用场景:

  • 微调不可编辑的配置的设定,如托管配置和企业配置。

  • 快捷的在不同工作环境中切换,比如临时开启对所有域名的 MitM 并调整过滤器。

  • 使用他人编写的模块以完成某些特定任务,比如,你的同事可以编写一个模块将应用的 API 请求重定向至测试服务器。

  • 如果你在多个设备间使用了同一份配置,有可能需要根据设备的使用场景进行微调。模块的开启状态是保存于当前设备的,可以用于在不同设备间的差异性修改。

基本概念

模块相当于给当前配置打补丁,其优先级要高于配置本身的设置。

有三种模块:

  • 内置模块:Surge 会预置一些模块,随着 Surge 自身更新。

  • 本地模块:放置在配置文件目录中的 .sgmodule 文件。

  • 安装的模块:从某个 URL 安装的模块。

你可以同时开启多个模块,模块的开启状态保存于当前设备,不会进行同步。切换配置也不影响模块的开启状态。

编写模块

模块的内容和标准配置基本一致,目前支持调整以下段:

  • General, Replica, MITM

    • 直接覆盖原始值:key = value

    • 在原始值的末尾进行追加:key = %APPEND% value

    • 在原始值的开始进行插入:key = %INSERT% value

    你只能在 MITM 段中操作 hostname 字段。

  • Rule, Script, URL Rewrite, Header Rewrite, Host

    新加入的定义将会追加在原始内容的顶部。

    模块中的规则只可以使用 DIRECT、REJECT、REJECT-TINYGIF 三个内置策略。

  • 元数据

    你可以在模块文件里添加元数据:

    #!name=Name Here
    #!desc=Description Here

    (可选)你还可以限制模块的使用平台(可取值为 ios 和 mac):

    #!system=mac

模块样例:

#!name=MitM All Hostnames
#!desc=Perform MitM on all hostnames with port 443, except those to Apple and other common sites which can‘t be inspected. You still need to configure a CA certificate and enable the main switch of MitM.

[MITM]
hostname = -*.apple.com, -*.icloud.com, -*.mzstatic.com, -*.crashlytics.com, -*.facebook.com, -*.instagram.com, *
#!name=Game Console SNAT
#!desc=Let Surge handle SNAT conversation properly for PlayStation, Xbox, and Nintendo Switch. Only useful if Surge Mac acts the router for these devices.
#!system=mac

[General]
always-real-ip = %APPEND% *.srv.nintendo.net, *.stun.playstation.net, xbox.*.microsoft.com, *.xboxlive.com

💡本页文档来自,并对照手册对部分内容进行了修正

官方社区

HTTP API

  • 仅Surge iOS 4.4.0 和 Surge Mac 4.0.0 以上版本支持通过 HTTP API 控制 Surge。

配置

[General]
http-api = examplekey@0.0.0.0:6171

身份验证

API key 必须填写在所有请求的 'X-Key' 的 Header 中。

GET /v1/events
X-Key: examplekey
Accept: */*

基本约束

当前版本仅支持未经 TLS 加密的 HTTP 请求。Surge 仅适用 GET 和 POST 方法。

  • 对于 GET 请求,应适用 URL 发送参数。

  • 对于 POST 请求,应使用 JSON Body 发送参数。

Surge 总会返回 JSON Body 格式的响应。

路径

切换开关功能

  • GET /v1/features/mitm

  • POST /v1/features/mitm

  • GET /v1/features/capture

  • POST /v1/features/capture

  • GET /v1/features/rewrite

  • POST /v1/features/rewrite

  • GET /v1/features/scripting

  • POST /v1/features/scripting

  • GET /v1/features/system_proxy (仅 Surge Mac 可用)

  • POST /v1/features/system_proxy (仅 Surge Mac 可用)

  • GET /v1/features/enhanced_mode (仅 Surge Mac 可用)

  • POST /v1/features/enhanced_mode (仅 Surge Mac 可用)

使用 GET 方法获取开关的状态。

GET 方法响应示例:

{"enabled":true}

适用 POST 请求调整开关状态。

POST 方法请求示例:

{"enabled":true}

出站模式

  • GET /v1/outbound

  • POST /v1/outbound

适用 GET 请求 请求获取状态,适用 POST 请求修改。

GET 方法响应示例:

{"mode":"rule"}

适用 POST 请求调整开关状态。

POST 方法请求示例:

{"mode":"rule"}

模式的值可能为:direct, proxy, rule

  • GET /v1/outbound/global

  • POST /v1/outbound/global

获取或更改全局出站模式的默认策略。

GET 方法响应示例:

{"policy":"ProxyA"}

适用 POST 请求调整开关状态。

POST 方法请求示例:

{"policy":"ProxyB"}

代理策略组

  • GET /v1/policies

获取所有策略组。

  • GET /v1/policies/detail?policy_name=ProxyNameHere

获取代理的详细信息。

  • POST /v1/policies/test

对策略组进行测速。

请求示例:

{"policy_names": ["ProxyA", "ProxyB"], "url": "http://bing.com"}
  • GET /v1/policy_groups

列出所有的策略组和他们的选项。

  • GET /v1/policy_groups/test_results

获取 url-test/fallback/load-balance 组的测速结果。

  • GET /v1/policy_groups/select?group_name=GroupNameHere

获取 select 组的选项。

请求示例:

{"policy": "ProxyA"}
  • POST /v1/policy_groups/select

改变 select 策略组的选项。

请求示例:

{"group_name": "GroupA", "policy": "ProxyA"}
  • POST /v1/policy_groups/test

立即测试一个策略组。

请求示例:

{"group_name": "GroupA"}

响应示例:

{
    "results": [
        {
            "data": {
                "ProxyA": {
                    "tcp": 48,
                    "rtt": 45,
                    "receive": 273,
                    "available": 213
                },
                "ProxyB": {
                    "tcp": 48,
                    "tfo": false,
                    "receive": 164,
                    "rtt": 51,
                    "available": 48
                },
                "ProxyC": {}
            },
            "time": 1595510609.6405091
        }
    ],
    "time": 1595510609.6405091,
    "winner": "ProxyA"
}

请求

  • GET /v1/requests/recent

获取最近请求。

  • GET /v1/requests/active

获取所有活动中的请求。

  • POST /v1/requests/kill

杀死一个请求。

请求示例:

{"id": 100}

Profiles

  • GET /v1/profiles/current?sensitive=0

获取当前配置文件的内容。 如果"sensitive"为假,则所有密码字段都将被屏蔽。

  • POST /v1/profiles/reload

立即重新载入配置文件。

  • POST /v1/profiles/switch (仅 Surge Mac 可用)

请求示例:

{"name": "Profile2"}

切换到另一个配置。

  • GET /v1/profiles (仅 Surge Mac 4.0.6 及以上版本可用)

获取可用的配置文件名。

  • POST /v1/profiles/check (仅 Surge Mac 4.0.6 及以上版本可用)

请求示例:

{"name": "Profile2"}

检查配置。如果配置文件无效返回错误,否则 "error" 字段为空。

DNS

  • POST /v1/dns/flush

刷新 DNS 缓存。

  • GET /v1/dns

获取当前 DNS 缓存内容。

  • POST /v1/test/dns_delay

测试 DNS 延时。

模块

  • GET /v1/modules

获取可用和激活的模块。

响应示例:

{
    "enabled": [
        "router.com"
    ],
    "available": [
        "Game Console SNAT",
        "Google Home Devices",
        "router.com",
        "MitM All Hostnames"
    ]
}
  • POST /v1/modules

启动或关闭模块

请求示例:

{
    "router.com": false,
    "Google Home Devices": true
}

脚本

  • GET /v1/scripting

获取脚本列表。

POST /v1/scripting/evaluate

适用 Mock 环境执行脚本。

请求示例:

{
    "script_text": "The content of JS script",
    "mock_type": "cron",
    "timeout": 5
}
  • POST /v1/scripting/cron/evaluate

立即执行一个定时脚本。

请求示例:

{
    "script_name": "script1",
}

设备管理(仅 Mac 4.0.6 以上版本可用)

  • GET /v1/devices

获取当前激活和保存的设备列表。

  • GET /v1/devices/icon?id={iconID}

获取设备的图标。你可以从 "device.dhcpDevice.icon" 中获取图标 ID。

  • POST /v1/devices

改变设备的属性。physicalAddress 字段是必须的,可以从name、address、shouldHandledBySurge 中调整一个或多个属性。

请求示例:

{
    "physicalAddress":"F0:9F:C2:00:00:00",
    "name": "Computer",
    "address": "192.168.1.200",
    "shouldHandledBySurge": true
}

杂项

  • POST /v1/stop

停止 Surge。若在 Surge iOS 上开启了 Always On 则会重启。

  • GET /v1/events

获取事件中心的内容。

  • GET /v1/rules

获取规则列表。

  • GET /v1/traffic

获取流量信息。

  • POST /v1/log/level

修改当前会话的日志等级。

请求示例:

{"level": "verbose"}

Surge Mac

Version 4.1.0

脚本

你现在可以通过 UI 界面配置脚本了。

配置文件

HTTP API

  1. 添加了关于配置文件的 HTTP API,包括 GET /profiles 和 POST /profiles/check。

  2. 添加了关于设备管理的 HTTP API,包括 GET /devices、POST /devices 和 GET /devices/icon。

  3. HTTP API、代理服务、外部控制器(external controller)现在都支持监听 IPv6 地址了(UI 界面尚无,需要手动在配置文件里配置)。

  4. 你可以用 http-api-tls=true 来为 HTTP API 启用 TLS(HTTPS-API)。

自动化改进

外部资源在 Surge 启动时就会更新,并且当应用一个配置文件时会自动下载其中引用的外部资源。

其他改进

  1. 新的规则类型:SUBNET,可以用通配符匹配 SSID、BSSID 和 路由IP地址。

  2. 当处理大量请求的时候,Dashboard 性能有显著提升。

下载地址

Version 4.0.5

Policy Group

In this release, we completely refactored the policy group functionality, bringing the following changes:

  1. The url-test/fallback/load-balance policy group can no longer be configured with a specific testing URL but with a global testing URL or a policy-configured testing URL. The policy's test results can be used directly in all policy group decisions, eliminating the need to retest each policy group individually.

  2. All types of policy groups support mixed nesting. The only requirement is that no circular references can be used.

  3. When a group policy is used as a sub-policy of the url-test/fallback/load-balance group.

    • The latency of the select/url-test/fallback/ssid group is the latency of the selected policy.

    • The latency of the load-balance group is the average of the latencies of all available policies.

  4. The timeout parameter of a policy group marks policies with latency exceeding this parameter as unavailable when making decisions for the group. But the maximum time taken to test the policy group is controlled by the global test-timeout parameter. (Default is 5s)

  5. When testing a group due to decision making, all sub-policies that the group may use are tested, including sub-policies of the sub-policy group.

  6. You may use no-alert=true parameter to suppress notifications for particular groups.

Cloud Notification

You can receive the notifications on iOS devices. Enable this option first and then configure it on Surge iOS. The two device must use a same iCloud account.

Minor Changes

  • Bug fixes.

Version 4.0.4

  • Bug fixes.

Version 4.0.3

  • You may override the testing URL of a policy for network diagnostics and activity cards.

  • The GeoIP database can be updated automatically in the background.

  • Bug fixes.

Version 4.0.2

  • You may now customize the GeoIP database updating URL.

  • tun-excluded-routes and tun-included-routes are now available for Surge Mac.

  • Bug fixes.

Version 4.0.1

  • You may configure the proxy chain with the UI now.

  • Fixed some visual inconsistency under reducing transparency mode.

  • Bug fixes.

Version 4.0.0

  • The first version of 4.0.0.

Surge Mac V3

Version 3.5.8

  • Bug fixes

Version 3.5.7

  • Bug fixes

Version 3.5.5

Minor Changes

  • You may switch among the main views with shortcut keys.

  • Bug fixes.

Version 3.5.4

Changes in Policy Group

  • New parameter: policy-regex-filter. If the parameter is configured, only matched policy line will be used.

Minor Changes

  • Provides more details for the TLS handshake error.

  • Increases the file description limitation alert threshold.

Version 3.5.3

New Parameter: use-local-host-item-for-proxy

[General]

use-local-host-item-for-proxy = true

If use-local-host-item-for-proxy is true, Surge sends the proxy request with the IP address defined in the [Host] section, instead of the original domain.

Changes in Load Balance Group

  • load-balance group now supports connectivity testing before being used. Add 'url' parameter to enable it.

  • Parameters 'timeout', 'interval' and 'evaluate-before-use' are also available.

Minor Changes

  • Surge will send an ICMP port unreachable message if UDP forwarding fails.

  • Eliminate unnecessary local DNS lookup while forwarding UDP traffic to a proxy server.

  • Fixed a bug that connecting to Surge iOS via USB is not working in Surge Dashboard.

Version 3.5.2

SSID Suspend

  • Surge Mac supports SSID suspend now. The system proxy and enhanced mode will be temporarily suspended under specified SSIDs.

  • The name of WiFi can be an SSID, a BSSID, or a gateway IP address.

  • No UI configuration in the current version.

REJECT-DROP

  • REJECT-DROP policy is now effective to proxy connections. The connections matched with a REJECT-DROP policy will be closed in 60-120s later without any data returned.

Global Proxy

  • You may now select and view sub-policy for policy groups while using the global proxy mode.

Version 3.5.1

New rule type: DOMAIN-SET

  • DOMAIN-SET is just like RULE-SET. But it is designed a large number of rules and highly efficient.

  • Unlike RULE-SET, you can only write hostnames (domain or IP address) in it. One hostname per line.

  • You may use "." prefix to include all sub-domains.

Changes in SRC-IP

  • SRC-IP rule now supports IP-CIDR for both IPv4 and IPv6.

Changes in DNS over HTTPS

  • From this version, if DNS-over-HTTPS is configured, the traditional DNS will only be used to test the connectivity and resolve the domain in the DOH URL.

  • The DNS over HTTPS now has a separate parameter: doh-server. The DOH servers in 'dns-server' will be moved to the new parameter after saving.

  • The legacy DNS is always required now.

  • DOH can be matched with rule 'PROTOCOL,DOH' now.

  • Added a new parameter 'doh-follow-outbound-mode'. In the previous version, the DOH client follows the system proxy settings. From this version, all DOH requests will use DIRECT policy by default. If 'doh-follow-outbound-mode' is set, the DOH requests will follow the outbound mode settings regardless of the system proxy settings.

  • We are refactoring the HTTP client for DOH and scripting. Please feedback if you encounter any issue.

Changes in Scripting

  • Added a simple view to test the script. You may find it in the Window menu.

Minor Changes

  • Fixed a crash in Dashboard while using search.

  • Bug fixes.

Known Issues

  • You may not configure DOH with UI in this version temporarily.

Version 3.5.0

  • You may enable modules in the menu now.

  • You may view the detail of a module by double clicking.

  • Supports pattern filter for Dashboard requests.

  • Added a new rule type: PROTOCOL. The possible values are HTTP, HTTPS, SOCKS, SNELL, TCP, UDP.

  • You may now use UI to add and edit load-balance group.

    • DNS over HTTP (DoH) now uses DNS wireformat by default. You may configure doh-format=json in [General] to continue using JSON format.

    • TCP connection setup optimizations.

    • Bug fixes.

Version 3.4.0

  • Supports a new proxy protocol: Trojan.

  • Remote Dashboard now upgraded to Remote Controller. You may use Surge iOS to select policy group, toggle HTTP capture/MitM, and switch outbound mode remotely.

  • The comment lines in the text config won't lost after editing with UI.

  • You may open the new connection window of Dashboard by holding the Option key while clicking the Dashboard item in the main menu.

  • Fixed a bug that Surge may not be able to process DNS answer packets which is longer than 512 bytes.

Version 3.3.3

  • Fixed a bug which causes TFO failed.

  • You may use a profile which stores in a subdirectory of the profile directory.

  • Added Traditional Chinese localizations.

  • Fixed a bug that the menu might be unresponsive.

  • Fixed crashs on macOS 10.11.

Version 3.3.2

  • Supports MITM on non-standard port for TCP mode.

  • Proxy editing view now supports VMess protocol and all misc options.

  • A new option 'persistent' has been added to the load-balance group. (aka PCC, per connection classifier) When 'persistent=true' is set, a same hostname will always get the same policy.

  • Bug fixes.

Version 3.3.1

  • Supports VMess proxy protocol.

    • vmess-proxy= vmess, example.com, 443, username = 12345678-abcd-1234-1234-47ffca0ce229, ws=true, tls=true, ws-path=/v2, ws-headers=X-Header-1:value|X-Header-2:value

    • All proxy options for TLS proxy are available.

    • Web-socket and TLS options would degrade performance. Only enable when necessary.

    • Surge only supports chacha20-poly1305 encryption algorithm. Please make sure the server supports it. We have no plan to implement other ciphers.

Version 3.3.0

  • Added support for TLS 1.3. Append 'tls13=true' to the proxy line to enable it. (Requires macOS 10.14 or above)

  • Supports to use 'X-Surge-Policy' to force policy for HTTP/HTTPS requests.

  • SSID group now supports to use the IP address of default router as an identifier.

  • New policy group type: load-balance, which will use a random sub-policy for every request.

  • IN-PORT and DEST-PORT rule now supports port range expression: DEST-PORT,8000-8999,DIRECT

  • Provides compatibility for Surge iOS 4.

  • Surge Mac software package is now notarized by Apple.

  • A new standalone view to manage all external resources.

Version 3.2.1

  • Fixed a bug that Handoff doesn't work between Surge iOS and Dashboard.

  • Fixed a bug that 'Update All Remote Resources' may not work.

Version 3.2.0

Scripting

  • You can now use a script to modify the response headers and status code.

Dashboard

  • USB module has been refactored to improve stability. Also, you may choose the device from multiple USB devices now.

MitM

  • HTTP and MitM engine has been refactored. Please report if you encounter any issues.

  • You can now use URL-REGEX rule for MitM connections.

  • You may use prefix '-' to exclude domains for MitM. Example:

      [MITM]
      hostname = -*.apple.com, -*.icloud.com, *
  • MitM hostname list now supports port number. By default only the connections to port 443 will be decrypted. Use suffix :port to enable MitM for other ports. Use suffix :0 to enable MitM for all ports on the hostname.

  • URL rewrite type 'header' is now available for MitM connections. You may also use it to rewrite a plain HTTP request to an HTTPS request.

Misc

  • You can now enable/disable a rule.

  • Added a small indicator in the menu icon for Metered Network Mode.</lo>

  • Added main switches for rewrite and scripting.

  • Supports TCP SACKs for Surge VIF.

  • New general option: force-http-engine-hosts. You can force Surge to treat a raw TCP connection as an HTTP connection, to enable high-level functions such as URL-REGEX rules, rewrite and scripting. This option uses the same format as [MITM] hostname option.

  • New option for url-test/fallback group: evaluate-before-use. By default, the requests before a connection evaluation will use the first policy in the list and trigger the evaluate. Enable the option to delay the requests until the evaluation completed.

Version 3.1.1

  • Bug fixes.

Version 3.1.0

  • Added more feature to the main menu.

  • Dashboard now supports to export all requests to an archive file for opening later or sharing.

  • A new option to automatically reload if the profile was modified externally/remotely.

  • Fixed a compatibility issue with some FTP clients.

  • Added a new option to disable automatically notification dismissing.

  • The update notification is now shown as a banner instead of an alert window.

  • Bug fixes.

Version 3.0.6

  • Optimizations for no network error handling.

  • Reduces CPU usage on idle.

  • Fixed a bug while enabling MitM with a new certificate.

  • Fixed crashes on macOS 10.11.

Version 3.0.5

  • CPU usage optimizations (50% reduced for high throughout).

  • Enabled Hardened Runtime to get enhanced security protections in macOS Mojave.

  • Add more notes for rule evaluating stage.

  • WeChat.app may flood ping when network is unstable, which causes a high CPU usage of Surge. We added a mechanism to limit ICMP throughput in this version.

Version 3.0.4

  • Bug fixes

Version 3.0.3

  • Supports new iCloud container for Surge iOS migration.

  • The MitM feature is now compatible with Android system. Please regenerate an new CA certificate before using with Android.

  • Fixed some UI issues in Dashboard.

  • Fixed a bug that MitM may refuse to enable after modifying settings.

  • Fixed a bug that br decompress may fail.

  • Fixed a bug that the menu item may use a wrong color if the accent color of system isn't blue.

  • Fixed the JSON viewer color issue in the Dark Mode.

  • Minor bug fixes.

Version 3.0.2

  • Allows import the profile from a URL.

  • Fixed an issue that the HTTP capture button may show wrong state in Dashboard.

  • Fixed an issue that Dashboard doesn't show User-Agent as the process name while connecting to iOS device.

  • Fixed an issue that the bandwidth of processes may be inaccurate.

  • Fixed an issue that the DEST-PORT rule may not be parsed.

  • Fixed an issue that ruleset can't be used with logical type rule.

Version 3.0.1

  • Fixed an issue that TFO option will not be saved.

  • Fixed an issue that UDP relay option shows wrong state.

  • Fixed some i18n issues.

  • Fixed crashs on macOS 10.11.

  • Save proxy declarations with legacy style (custom) if the proxy is written in legacy style in the text file.

  • Other minor bug fixes.

Version 3.0.0

Surge Mac V2

Version 2.6.7

  • Fixed a compatibility issue with 304 response.

  • Fixed a Dashboard crash.

Version 2.6.6

  • Fixed a compatibility issue with 304 response.

  • Fixed an issue that Dashboard may not use the correct encoding to decode text body.

Version 2.6.5

  • Bug fixes.

Version 2.6.4

  • Bug fixes.

Version 2.6.3

  • Surge will automatically track system proxy settings now. When Surge is no longer the default proxy, the status icon will turn grey and a notification will raise.

  • Fixed a compatibility issue with Docker.

Version 2.6.2

  • Fixed an issue that the UDP mode with AEAD ciphers doesn't work.

  • Bug fixes.

Version 2.6.1

  • Performance improvements.

  • Fixed an issue that UDP traffics are not included in the real-time speed.

  • Supports hardware acceleration for AES-GCM encryption.

  • Supports NAT64 in a pure IPv6 network. (Previous versions already supported DNS64)

Version 2.6.0

  • Supports using Surge Mac as a gateway.

  • A new setup guide view.

  • A new config panel for traffic capture options.

  • Fixed an issue which Dashboard may disconnect unexpectedly under huge pressure.

  • The status bar icon will be red while traffic capture is enabled.

  • Improved TUN interface performance.

  • Enabling TCP Fast Open in macOS 10.14.

Version 2.5.3

  • You may use Dashboard to view UDP conversations.

  • Dashboard now can save multiple remote machine profiles.

  • Improved the JSON viewer in Dashboard.

  • Added an UI switch for the dns-failed option in FINAL rule.

  • Bug fixes.

Version 2.5.2

  • You may toggle the hidden state of columns in Dashboard now.

  • Supports to export selected rows to csv file.

  • Added a connection duration column in Dashboard.

  • Supports obfs-uri parameter.

  • Improved the benchmark view.

  • Fixed a serious bug in the SOCKS5 proxy implementation.

  • Bug fixes.

Version 2.5.1

  • The MitM enabling switch has been moved to the main menu and isolated from profile.

  • Bug fixes.

Version 2.5.0

  • Added Outbound Mode options: Direct Outbound, Global Proxy and By Rule.

  • Added options for all policy to specify outgoing interface: 'interface' and 'allow-other-interface'.

  • Added all_proxy environment variable for 'Copy Shell Export Command'

  • Refined MitM.

  • Concurrently setup connection to host with Round-robin DNS to boost performance.

  • Bug fixes.

Version 2.4.6

  • Supports xchacha20-ietf-poly1305.

  • Bug fixes.

  • HTTP request header and response header can be extracted from TCP connection now. (SOCKS5 and TUN)

  • Enhanced mode can handle all connections now, even for connections initialized with IP address directly.

  • Surge TUN now supports forwarding ICMP packets.

From this version, the minimum system version requirement was raised to macOS 10.11. If you are still using macOS 10.10, please use version 2.4.5.

Version 2.4.5

  • Bug fixes.

  • Improved performance for high concurrency.

  • TCP fast open has been disabled temporarily since there is a serious problem in macOS/iOS kernel.

  • Dashboard will display decoded URL query now.

Version 2.4.4

  • Supports obfs=tls for shadowsocks protocol.

  • Refined the proxy edit panel.

  • Added Simplified Chinese language.

Version 2.4.3

  • Supports obfs=tls for shadowsocks protocol.

  • Refined the proxy edit panel.

  • Added Simplified Chinese language.

Version 2.4.2

  • Fixed an issue that enhanced mode may not be closed properly when switching to a profile without dns-server.

  • Fixed an issue that managed profile updating and license info are unavailable while enhanced mode enabled.

  • When the necessary port is used by another process, the error alert will show which process is using the port.

  • Fixed an issue that map local items can't be edited with UI.

  • Fixed an issue that system proxy settings may not be reset properly.

  • Auto URL test group will execute a retest immediately after the selected policy has failed.

Version 2.4.1

  • Bug fixes.

Version 2.4.0

  • Supports enterprise license and profile management.

  • Fixed a bug that some fields are unavailable in the configuration panel in some cases.

  • Fixed a bug that the FINAL rule can't be edited.

  • Fixed a bug that you may not be able to use custom storage path for profiles.

  • The interface related options are no longer controlled by profile. Sorry for the repetitive changes.

  • You may use $1, $2 to use the matched string in the value while using header rewrite.

  • Added an option for HTTP/HTTPS proxy: always-use-connect. When it is true, Surge will use CONNECT method for plain HTTP requests.

Version 2.3.2

  • Added a option to control whether show proxy error notification.

  • Fixed a problem that Dashboard show data doesn't exist error.

Version 2.3.1

  • Added a wizard to install CA’s root certificate for iOS simulator.

  • Connectivity quality is now an option. (Not show by default)

  • Line comments in [Rule] section in profile file is now presented in UI.

  • You may add proxy rule with Dashboard by right-clicking the request or process.

  • Dashboard will always open a new window for local machine, instead of asking. You may use "File" menu to connect to a remote machine.

Version 2.3.0

  • Completely redesign the configure interface. You may configure every function with UI now.

  • Proxy benchmark is now moved to main application from Dashboard.

  • You may switch profile with command line now: surge-cli switch-profile profilename.

Version 2.2.4

  • Notifications presented by Surge will be removed from Notification Center automatically.

  • The interval of attempts to refresh managed config changes to one hour from one minute. (After config expired)

  • Supports new encryption methods for shadowsocks-libev 3.0.

  • Optimized Dashboard performance.

  • Supports TCP Fast Open for shadowsocks proxy. You need add "tfo=true" flag in [Proxy] section to enable the feature. You may use benchmark to confirm TFO is working.

  • You can sort benchmark results now.

  • You may choose to reload config after managed config updated.

Version 2.2.2

  • Fixed a bug when using SOCKS5 without authorization.

Version 2.2.1

  • You may use Dashboard to benchmark proxies now.

  • Fixed "Too many open files" error by raising limit to 2048.

  • Fixed a bug in SOCKS5 with authorization.

  • Fixed a bug that managed config may refresh continuously.

Version 2.2.0

  • Map local function is now available.

  • Adds notifications when proxy encounters errors.

  • Network changed notification will show service name instead of BSD name now.

  • Fixed a bug that Dashboard may show the incorrect state of body dump.

  • Changes for HTTPS and SOCK5-TLS proxy:

    • Option 'skip-common-name-verify' is deprecated.

    • Add a new option 'skip-cert-verify' to skip certificate verify completely.

    • Add a new option 'sni' to customize SNI field while handshaking. You may use 'sni=off' to disable SNI.

  • New rule type: PROCESS-NAME, USER-AGENT and URL-REGEX.

  • You can use simple wildcard matching (? and *) for PROCESS-NAME rule, local DNS mapping and MitM hosts.

  • Dashboard supports display POST form data in a table view.

  • You may let Surge reload config by sending SIGHUP. You can use command 'killall -HUP Surge' or 'surge-cli reload'.

  • Managed configuration is supported now.

  • Add a new option 'skip-server-cert-verify' for MitM.

Version 2.1.4

  • Fixed a bug that helper may crash on macOS 10.10.

  • Add a option to remove Surge helper for troubleshooting.

  • Bug fixes.

Version 2.1.3

  • Fixed a bug that helper may crash on macOS 10.10.

  • Add a option to remove Surge helper for troubleshooting.

  • Bug fixes.

Version 2.1.2

  • New option: Collapse policy group items in menu

  • Fixed a bug that enhanced mode DNS settings may not be reverted.

  • Hold option key to click 'Copy Shell Export Command' to get a command with primary interface IP instead of 127.0.0.1.

  • Bug fixes.

Version 2.1.0

  • New feature: Enhanced Mode

    Some applications may not obey the system proxy settings. Using enhanced mode can make all applications handled by Surge.

  • New rule type: IP-CIDR6

    Example: IP-CIDR6,2005::/16,DIRECT,no-resolve

  • The /etc/hosts file will be reloaded automatically if it has changes.

Version 2.0.13

  • Dashboard supports to use ⌘ + 1,2,3,4 to switch panel.

  • Dashboard Supports handoff with Surge iOS.

  • Fixed a bug that Dashboard may show incorrect process name.

Version 2.0.12

  • Bug fixes.

  • Supported SNI while performing MitM.

  • The original certificate will be resigned and used while performing MitM, instead of generating a new certificate.

Version 2.0.11

  • Rule test cache will be flushed after network switching now.

  • Added a option 'Grey icon if set as system proxy is disabled'.

  • Bug fixes and performance improvements.

Version 2.0.10

  • Surge talks to HTTP proxies with a plain HTTP method for non-HTTPS requests now, instead of CONNECT.

  • Improved compatibility with some HTTP server.

  • Improved compatibility with some DNS server.

Version 2.0.9

  • Dashborad: The height of the detail panel will not change now while switching pages.

  • A notification will show when proxy client access from other machine.

  • Used SF Mono as monospaced font for header and body data display.

  • Supported TCP half-open mechanism.

Version 2.0.8

  • Add a new option 'exclude-simple-hostnames' in the gereral section.

  • Dashborad: Selected row will not be lost while the filter or sort column changed.

  • Dashborad: Fixes some issues in the active panel.

Version 2.0.5

  • Bug fixes.

Version 2.0.3

  • New feature: Show connectivity quality in menu.

    Surge will send a DNS question to all DNS servers concurrently to test physical network connectivity while opening the menu.

  • Fixes a problem that Surge may freeze while opening the menu.

  • Fixes a problem that if a policy group contains duplicate policies, Surge may crash.

Version 2.0.2

  • Dashboard will no longer display process icon in remote mode.

  • Fixes a bug: "Set as System Proxy" option does not work properly if only SOCKS service is enabled.

  • Fixes a bug: Dashboard can't add a rule with no-resolve option on and comment not empty.

  • Minor bug fixes.

Version 2.0.1

  • Bug fixes

你现在可以把配置文件中的部分段落用单独的一个文件写。详情参考:

All URL resources now support URLs with a username and password (e.g. ), including managed profile, external resources, and importing profile form URL.

New feature: Module, which can override the current profile with a set of settings. Highly flexible for diverse purposes. See the post in the community for more information: .

Snell protocol now upgrade to version 2, supporting to reuse TCP connections to improve performance.

Supports to use OpenSSL as TLS provider. See the post in the community for more information: .

The scripting has been rewritten totally. The old scripts are not compatible with this version. See

Supports DNS over HTTPS. More information in community:

New major feature: scripting. You may use JavaScript to modify the response as you wish. See the manual for more information:

Supports a new proxy protocol: Snell. ()

Surge Mac can work as a Snell proxy server now. See for more information.

Added a new option 'hijack-dns' to hijack DNS queries to other DNS servers with fake IP addresses. See manual for more information: .

New Features: External Proxy Provider. See for more information.

Surge now allows expired DNS answers for performance reasons. See 'Optimistic DNS' section in for more information.

Supports UDP relay for shadowsocks protocol. A brief introduction in Chinese: .

Supports client-side SSL/TLS certificate validation for HTTPS and SOCKS5-TLS proxy. A config example is here: (DO NOT support editing with UI in this version.)

Added a patch mechanism for adjusting settings for managed config. See manual for more information:

New feature: Header rewrite. See manual for more information: .

https://surge.mitsea.com/overview/configuration。
https://dl.nssurge.com/mac/v4/Surge-4.1.0-1298-f07b1b8713b2397518f4b252b5786452.zip
https://dl.nssurge.com/mac/v4/Surge-4.0.5-1262-db70f680cd0f15236c8415ec7b804c3a.zip
https://dl.nssurge.com/mac/v4/Surge-4.0.4-1227-9acb8b9e3f39e9048fc82e427184a4af.zip
https://dl.nssurge.com/mac/v4/Surge-4.0.3-1224-4ef8ae10c8a74c395bb4b6c3f6af6af6.zip
https://dl.nssurge.com/mac/v4/Surge-4.0.2-1219-dbd08724b90aa8b444cd6d0679a245b5.zip
https://dl.nssurge.com/mac/v4/Surge-4.0.1-1207-ee7bea1b244950c82a6f90e060fa2d89.zip
https://dl.nssurge.com/mac/v4/Surge-4.0.0-1191-d8140b0084223fd3fc4335e4414c0884.zip
https://dl.nssurge.com/mac/v3/Surge-3.5.8-1130.zip
https://dl.nssurge.com/mac/v3/Surge-3.5.7-1129.zip
https://user:pass@example.com
https://dl.nssurge.com/mac/v3/Surge-3.5.5-1123.zip
https://dl.nssurge.com/mac/v3/Surge-3.5.4-1119.zip
https://dl.nssurge.com/mac/v3/Surge-3.5.3-1094.zip
https://dl.nssurge.com/mac/v3/Surge-3.5.2-1082.zip
https://dl.nssurge.com/mac/v3/Surge-3.5.1-1069.zip
https://community.nssurge.com/d/225-module
https://dl.nssurge.com/mac/v3/Surge-3.5.0-1039.zip
https://github.com/surge-networks/snell/releases
https://community.nssurge.com/d/196-surge-ios-mac-tls-provider
https://dl.nssurge.com/mac/v3/Surge-3.4.0-989.zip
https://dl.nssurge.com/mac/v3/Surge-3.3.3-939.zip
https://dl.nssurge.com/mac/v3/Surge-3.3.2-925.zip
https://dl.nssurge.com/mac/v3/Surge-3.3.1-906.zip
https://community.nssurge.com/d/33-scripting/3
https://community.nssurge.com/d/48-dns-over-http
https://dl.nssurge.com/mac/v3/Surge-3.3.0-893.zip
https://dl.nssurge.com/mac/v3/Surge-3.2.1-863.zip
https://manual.nssurge.com/http-processing/scripting.html
https://dl.nssurge.com/mac/v3/Surge-3.2.0-860.zip
https://dl.nssurge.com/mac/v3/Surge-3.1.1-811.zip
https://github.com/surge-networks/snell
https://manual.nssurge.com/others/snell-server.html
https://dl.nssurge.com/mac/v3/Surge-3.1.0-807.zip
https://dl.nssurge.com/mac/v3/Surge-3.0.6-781.zip
https://dl.nssurge.com/mac/v3/Surge-3.0.5-773.zip
https://manual.nssurge.com/others/misc-options.html
https://dl.nssurge.com/mac/v3/Surge-3.0.4-759.zip
https://dl.nssurge.com/mac/v3/Surge-3.0.3-754.zip
https://dl.nssurge.com/mac/v3/Surge-3.0.2-736.zip
https://dl.nssurge.com/mac/v3/Surge-3.0.1-711.zip
https://dl.nssurge.com/mac/v3/Surge-3.0.0-702.zip
https://dl.nssurge.com/mac/Surge-2.6.7-656.zip
https://dl.nssurge.com/mac/Surge-2.6.6-654.zip
https://nssurge.com/mac/Surge-2.6.5-652.zip
https://nssurge.com/mac/Surge-2.6.4-647.zip
https://medium.com/@Blankwonder/surge-mac-new-features-external-proxy-provider-375e0e9ea660
https://nssurge.com/mac/Surge-2.6.3-637.zip
https://nssurge.com/mac/Surge-2.6.2-618.zip
https://developer.apple.com/videos/play/wwdc2018/714/
https://nssurge.com/mac/Surge-2.6.1-612.zip
https://nssurge.com/mac/Surge-2.6.0-596.zip
https://trello.com/c/ugOMxD3u
https://nssurge.com/mac/Surge-2.5.3-563.zip
https://nssurge.com/mac/Surge-2.5.2-544.zip
http://dl.nssurge.com/mac/Surge-2.5.1-528.zip
https://gist.github.com/Blankwonder/cd9fa1987e41cf1a1f1df50583ba1d9c
http://dl.nssurge.com/mac/Surge-2.5.0-520.zip
http://dl.nssurge.com/mac/Surge-2.4.6-490.zip
http://dl.nssurge.com/mac/Surge-2.4.5-468.zip
http://dl.nssurge.com/mac/Surge-2.4.4-459.zip
http://dl.nssurge.com/mac/Surge-2.4.3-457.zip
http://dl.nssurge.com/mac/Surge-2.4.2-445.zip
http://dl.nssurge.com/mac/Surge-2.4.1-439.zip
http://dl.nssurge.com/mac/Surge-2.4.0-429.zip
http://dl.nssurge.com/mac/Surge-2.3.2-421.zip
https://manual.nssurge.com/others/managed-configuration.html
http://dl.nssurge.com/mac/Surge-2.3.1-420.zip
https://manual.nssurge.com/header-rewrite.html
http://dl.nssurge.com/mac/Surge-2.3.0-416.zip
http://dl.nssurge.com/mac/Surge-2.2.4-394.zip
http://dl.nssurge.com/mac/Surge-2.2.2-375.zip
http://dl.nssurge.com/mac/Surge-2.2.1-374.zip
http://dl.nssurge.com/mac/Surge-2.2.0-368.zip
http://dl.nssurge.com/mac/Surge-2.1.4-362.zip
http://dl.nssurge.com/mac/Surge-2.1.3-337.zip
http://dl.nssurge.com/mac/Surge-2.1.2-327.zip
http://dl.nssurge.com/mac/Surge-2.1.0-318.zip
http://dl.nssurge.com/mac/Surge-2.0.13-304.zip
http://dl.nssurge.com/mac/Surge-2.0.12-295.zip
http://dl.nssurge.com/mac/Surge-2.0.11-289.zip
http://dl.nssurge.com/mac/Surge-2.0.10-280.zip
http://dl.nssurge.com/mac/Surge-2.0.9-273.zip
http://dl.nssurge.com/mac/Surge-2.0.8-260.zip
http://dl.nssurge.com/mac/Surge-2.0.5-255.zip
http://dl.nssurge.com/mac/Surge-2.0.3-250.zip