diff --git a/component/v2ray-plugin/websocket.go b/component/v2ray-plugin/websocket.go index 1e53cb9b..96909aff 100644 --- a/component/v2ray-plugin/websocket.go +++ b/component/v2ray-plugin/websocket.go @@ -3,6 +3,7 @@ package obfs import ( "crypto/tls" "net" + "net/http" "github.com/Dreamacro/clash/component/vmess" ) @@ -17,11 +18,16 @@ type WebsocketOption struct { // NewWebsocketObfs return a HTTPObfs func NewWebsocketObfs(conn net.Conn, option *WebsocketOption) (net.Conn, error) { + header := http.Header{} + for k, v := range option.Headers { + header.Add(k, v) + } + config := &vmess.WebsocketConfig{ Host: option.Host, Path: option.Path, TLS: option.TLSConfig != nil, - Headers: option.Headers, + Headers: header, TLSConfig: option.TLSConfig, } diff --git a/component/vmess/vmess.go b/component/vmess/vmess.go index 6e2767ce..49890354 100644 --- a/component/vmess/vmess.go +++ b/component/vmess/vmess.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" "net" + "net/http" "runtime" "sync" @@ -132,6 +133,11 @@ func NewClient(config Config) (*Client, error) { return nil, fmt.Errorf("Unknown network type: %s", config.NetWork) } + header := http.Header{} + for k, v := range config.WebSocketHeaders { + header.Add(k, v) + } + host := net.JoinHostPort(config.HostName, config.Port) var tlsConfig *tls.Config @@ -144,6 +150,9 @@ func NewClient(config Config) (*Client, error) { if tlsConfig.ClientSessionCache == nil { tlsConfig.ClientSessionCache = getClientSessionCache() } + if host := header.Get("Host"); host != "" { + tlsConfig.ServerName = host + } } var wsConfig *WebsocketConfig @@ -151,7 +160,7 @@ func NewClient(config Config) (*Client, error) { wsConfig = &WebsocketConfig{ Host: host, Path: config.WebSocketPath, - Headers: config.WebSocketHeaders, + Headers: header, TLS: config.TLS, TLSConfig: tlsConfig, } diff --git a/component/vmess/websocket.go b/component/vmess/websocket.go index bd06c39d..ae737d94 100644 --- a/component/vmess/websocket.go +++ b/component/vmess/websocket.go @@ -22,7 +22,7 @@ type websocketConn struct { type WebsocketConfig struct { Host string Path string - Headers map[string]string + Headers http.Header TLS bool TLSConfig *tls.Config } @@ -131,14 +131,14 @@ func NewWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) { headers := http.Header{} if c.Headers != nil { - for k, v := range c.Headers { - headers.Set(k, v) + for k := range c.Headers { + headers.Add(k, c.Headers.Get(k)) } } wsConn, resp, err := dialer.Dial(uri.String(), headers) if err != nil { - var reason string + reason := err.Error() if resp != nil { reason = resp.Status }