Skip to content

Commit

Permalink
Fix Host ignored in v2ray websocket transport
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Nov 5, 2023
1 parent bb928f0 commit aaa6702
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion transport/v2ray/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewClientTransport(ctx context.Context, dialer N.Dialer, serverAddr M.Socks
case C.V2RayTransportTypeGRPC:
return NewGRPCClient(ctx, dialer, serverAddr, options.GRPCOptions, tlsConfig)
case C.V2RayTransportTypeWebsocket:
return v2raywebsocket.NewClient(ctx, dialer, serverAddr, options.WebsocketOptions, tlsConfig), nil
return v2raywebsocket.NewClient(ctx, dialer, serverAddr, options.WebsocketOptions, tlsConfig)
case C.V2RayTransportTypeQUIC:
if tlsConfig == nil {
return nil, C.ErrTLSRequired
Expand Down
2 changes: 1 addition & 1 deletion transport/v2rayhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
uri.Path = options.Path
err := sHTTP.URLSetPath(&uri, options.Path)
if err != nil {
return nil, E.New("failed to set path: " + err.Error())
return nil, E.Cause(err, "parse path")
}
client.url = &uri
return client, nil
Expand Down
12 changes: 9 additions & 3 deletions transport/v2raywebsocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type Client struct {
earlyDataHeaderName string
}

func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayWebsocketOptions, tlsConfig tls.Config) adapter.V2RayClientTransport {
func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, options option.V2RayWebsocketOptions, tlsConfig tls.Config) (adapter.V2RayClientTransport, error) {
wsDialer := &websocket.Dialer{
ReadBufferSize: 4 * 1024,
WriteBufferSize: 4 * 1024,
Expand Down Expand Up @@ -68,11 +68,17 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
requestURL.Path = options.Path
err := sHTTP.URLSetPath(&requestURL, options.Path)
if err != nil {
return nil
return nil, E.Cause(err, "parse path")
}
headers := make(http.Header)
for key, value := range options.Headers {
headers[key] = value
if key == "Host" {
if len(value) > 1 {
return nil, E.New("multiple Host headers")
}
requestURL.Host = value[0]
}
}
return &Client{
wsDialer,
Expand All @@ -81,7 +87,7 @@ func NewClient(ctx context.Context, dialer N.Dialer, serverAddr M.Socksaddr, opt
headers,
options.MaxEarlyData,
options.EarlyDataHeaderName,
}
}, nil
}

func (c *Client) DialContext(ctx context.Context) (net.Conn, error) {
Expand Down

0 comments on commit aaa6702

Please sign in to comment.