Add WebSocket path for CDN mode

add new option to README

Change field name and default value of CDNWsUrlPath
This commit is contained in:
name 2023-03-07 00:37:05 +08:00
parent e696b18187
commit 11e42dd542
3 changed files with 14 additions and 5 deletions

View File

@ -155,8 +155,13 @@ Example:
`CDNOriginHost` is the domain name of the _origin_ server (i.e. the server running Cloak) under `CDN` mode. This only `CDNOriginHost` is the domain name of the _origin_ server (i.e. the server running Cloak) under `CDN` mode. This only
has effect when `Transport` is set to `CDN`. If unset, it will default to the remote hostname supplied via the has effect when `Transport` is set to `CDN`. If unset, it will default to the remote hostname supplied via the
commandline argument (in standalone mode), or by Shadowsocks (in plugin mode). After a TLS session is established with commandline argument (in standalone mode), or by Shadowsocks (in plugin mode). After a TLS session is established with
the CDN server, this domain name will be used in the HTTP request to ask the CDN server to establish a WebSocket the CDN server, this domain name will be used in the `Host` header of the HTTP request to ask the CDN server to
connection with this host. establish a WebSocket connection with this host.
`CDNWsUrlPath` is the url path used to build websocket request sent under `CDN` mode, and also only has effect
when `Transport` is set to `CDN`. If unset, it will default to "/". This option is used to build the first line of the
HTTP request after a TLS session is extablished. It's mainly for a Cloak server behind a reverse proxy, while only
requests under specific url path are forwarded.
`NumConn` is the amount of underlying TCP connections you want to use. The default of 4 should be appropriate for most `NumConn` is the amount of underlying TCP connections you want to use. The default of 4 should be appropriate for most
people. Setting it too high will hinder the performance. Setting it to 0 will disable connection multiplexing and each people. Setting it too high will hinder the performance. Setting it to 0 will disable connection multiplexing and each

View File

@ -37,6 +37,7 @@ type RawConfig struct {
BrowserSig string // nullable BrowserSig string // nullable
Transport string // nullable Transport string // nullable
CDNOriginHost string // nullable CDNOriginHost string // nullable
CDNWsUrlPath string // nullable
StreamTimeout int // nullable StreamTimeout int // nullable
KeepAlive int // nullable KeepAlive int // nullable
} }
@ -225,10 +226,13 @@ func (raw *RawConfig) ProcessRawConfig(worldState common.WorldState) (local Loca
} else { } else {
cdnDomainPort = net.JoinHostPort(raw.CDNOriginHost, raw.RemotePort) cdnDomainPort = net.JoinHostPort(raw.CDNOriginHost, raw.RemotePort)
} }
if raw.CDNWsUrlPath == "" {
raw.CDNWsUrlPath = "/"
}
remote.TransportMaker = func() Transport { remote.TransportMaker = func() Transport {
return &WSOverTLS{ return &WSOverTLS{
cdnDomainPort: cdnDomainPort, wsUrl: "ws://" + cdnDomainPort + raw.CDNWsUrlPath,
} }
} }
case "direct": case "direct":

View File

@ -15,7 +15,7 @@ import (
type WSOverTLS struct { type WSOverTLS struct {
*common.WebSocketConn *common.WebSocketConn
cdnDomainPort string wsUrl string
} }
func (ws *WSOverTLS) Handshake(rawConn net.Conn, authInfo AuthInfo) (sessionKey [32]byte, err error) { func (ws *WSOverTLS) Handshake(rawConn net.Conn, authInfo AuthInfo) (sessionKey [32]byte, err error) {
@ -41,7 +41,7 @@ func (ws *WSOverTLS) Handshake(rawConn net.Conn, authInfo AuthInfo) (sessionKey
return return
} }
u, err := url.Parse("ws://" + ws.cdnDomainPort) u, err := url.Parse(ws.wsUrl)
if err != nil { if err != nil {
return sessionKey, fmt.Errorf("failed to parse ws url: %v", err) return sessionKey, fmt.Errorf("failed to parse ws url: %v", err)
} }