Fix(vmess): set current server name in tls

This commit is contained in:
gVisor bot 2019-07-31 11:13:49 +08:00
parent 4c7da630f5
commit 8ded4918b2
3 changed files with 21 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package obfs
import ( import (
"crypto/tls" "crypto/tls"
"net" "net"
"net/http"
"github.com/Dreamacro/clash/component/vmess" "github.com/Dreamacro/clash/component/vmess"
) )
@ -17,11 +18,16 @@ type WebsocketOption struct {
// NewWebsocketObfs return a HTTPObfs // NewWebsocketObfs return a HTTPObfs
func NewWebsocketObfs(conn net.Conn, option *WebsocketOption) (net.Conn, error) { 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{ config := &vmess.WebsocketConfig{
Host: option.Host, Host: option.Host,
Path: option.Path, Path: option.Path,
TLS: option.TLSConfig != nil, TLS: option.TLSConfig != nil,
Headers: option.Headers, Headers: header,
TLSConfig: option.TLSConfig, TLSConfig: option.TLSConfig,
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"net" "net"
"net/http"
"runtime" "runtime"
"sync" "sync"
@ -132,6 +133,11 @@ func NewClient(config Config) (*Client, error) {
return nil, fmt.Errorf("Unknown network type: %s", config.NetWork) 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) host := net.JoinHostPort(config.HostName, config.Port)
var tlsConfig *tls.Config var tlsConfig *tls.Config
@ -144,6 +150,9 @@ func NewClient(config Config) (*Client, error) {
if tlsConfig.ClientSessionCache == nil { if tlsConfig.ClientSessionCache == nil {
tlsConfig.ClientSessionCache = getClientSessionCache() tlsConfig.ClientSessionCache = getClientSessionCache()
} }
if host := header.Get("Host"); host != "" {
tlsConfig.ServerName = host
}
} }
var wsConfig *WebsocketConfig var wsConfig *WebsocketConfig
@ -151,7 +160,7 @@ func NewClient(config Config) (*Client, error) {
wsConfig = &WebsocketConfig{ wsConfig = &WebsocketConfig{
Host: host, Host: host,
Path: config.WebSocketPath, Path: config.WebSocketPath,
Headers: config.WebSocketHeaders, Headers: header,
TLS: config.TLS, TLS: config.TLS,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
} }

View File

@ -22,7 +22,7 @@ type websocketConn struct {
type WebsocketConfig struct { type WebsocketConfig struct {
Host string Host string
Path string Path string
Headers map[string]string Headers http.Header
TLS bool TLS bool
TLSConfig *tls.Config TLSConfig *tls.Config
} }
@ -131,14 +131,14 @@ func NewWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {
headers := http.Header{} headers := http.Header{}
if c.Headers != nil { if c.Headers != nil {
for k, v := range c.Headers { for k := range c.Headers {
headers.Set(k, v) headers.Add(k, c.Headers.Get(k))
} }
} }
wsConn, resp, err := dialer.Dial(uri.String(), headers) wsConn, resp, err := dialer.Dial(uri.String(), headers)
if err != nil { if err != nil {
var reason string reason := err.Error()
if resp != nil { if resp != nil {
reason = resp.Status reason = resp.Status
} }