修复升级到 Caddy V2 后 Surge 中出现的 Handshake Failed 错误

这个问题是我在几个月前将 Caddy 升级到 v2 的时候遇到的。在升级后,其他代理软件均可以使用,唯独 Surge。也去其官网和给作者发了邮件,但均未有回应。在 Caddy 的社区也和作者讨论了很长时间,也未能解决。后来在研究后发现并解决了这个问题。需要变更一处地方,Caddy 在配置路由时

proxy.tourcoder.com {
 tls [email protected]
 @staff_websocket {
  path /helloworld
  header Connection *Upgrade*
  header Upgrade websocket
 }
 reverse_proxy @staff_websocket localhost:1234
}

因为 Surge 在发送 header 的时候发送的是 Connection *upgrade*,而 Caddy 对大小写是有差别的,所以增加一条小写 upgrade 的识别,即

proxy.tourcoder.com {
 tls [email protected]
 @staff_websocket {
  path /helloworld
  header Connection *Upgrade*
  header Upgrade websocket
 }
 @staff_websocket_alias {
  path /helloworld
  header Connection *upgrade*
  header Upgrade websocket
 }
 reverse_proxy @staff_websocket localhost:1234
 reverse_proxy @staff_websocket_alias localhost:1234
}

问题解决,在 Caddy 中也可以写成

proxy.tourcoder.com {
 tls [email protected]
 @staff_websocket {
  path /helloworld
  header Connection *Upgrade*
  header Connection *upgrade*
  header Upgrade websocket
 }
 reverse_proxy @staff_websocket localhost:1234
}

补充,刚才在 Caddy 的官方社区得到了一些新的指导,如果这是一个代理的话,可以不使用 matcher,即 header 的内容,上面配置内容变成

proxy.tourcoder.com {
 tls [email protected]
 @staff_websocket {
  path /helloworld
 }
 reverse_proxy @staff_websocket localhost:1234
}

另外附上 v2ray 和 caddy 的配置,具体的安装看 v2ray 安装caddy 的安装

Caddy V2.4.6 的配置参考上面的内容,v2ray 的服务器端简单配置如下

{
  "inbound": {
      "port": 1234,
      "listen": "127.0.0.1", 
      "protocol": "vmess",
      "settings": {
          "clients": [
              {
                  "id": "这里是 uuid",
                  "alterId": 2
              }
          ]
      },
      "streamSettings": {
          "network": "ws",
          "wsSettings": {
              "path": "/路径"
          }
      }
  },
  "outbound": {
      "protocol": "freedom",
      "settings": {}
  }
}

上面的 uuid 可以通过工具生成,比如 v2ray 就提供了 v2ctl,在目录 /usr/local/bin 下,输入命令 v2ctl uuid 即可。


> 可在 Twitter/X 上评论该篇文章