clash/transport/v2ray-plugin/websocket.go

63 lines
1.2 KiB
Go
Raw Normal View History

2019-02-11 07:25:10 +00:00
package obfs
import (
2021-10-16 12:19:59 +00:00
"crypto/tls"
2019-02-11 07:25:10 +00:00
"net"
"net/http"
2019-02-11 07:25:10 +00:00
2021-05-13 14:18:49 +00:00
"github.com/Dreamacro/clash/transport/vmess"
2019-02-11 07:25:10 +00:00
)
// Option is options of websocket obfs
type Option struct {
Host string
Port string
Path string
Headers map[string]string
TLS bool
SkipCertVerify bool
Mux bool
2019-02-11 07:25:10 +00:00
}
// NewV2rayObfs return a HTTPObfs
func NewV2rayObfs(conn net.Conn, option *Option) (net.Conn, error) {
header := http.Header{}
for k, v := range option.Headers {
header.Add(k, v)
}
2019-02-11 07:25:10 +00:00
config := &vmess.WebsocketConfig{
2021-10-16 12:19:59 +00:00
Host: option.Host,
Port: option.Port,
Path: option.Path,
Headers: header,
}
if option.TLS {
config.TLS = true
config.TLSConfig = &tls.Config{
ServerName: option.Host,
InsecureSkipVerify: option.SkipCertVerify,
NextProtos: []string{"http/1.1"},
}
if host := config.Headers.Get("Host"); host != "" {
config.TLSConfig.ServerName = host
}
2019-02-11 07:25:10 +00:00
}
var err error
conn, err = vmess.StreamWebsocketConn(conn, config)
2019-02-11 07:25:10 +00:00
if err != nil {
return nil, err
}
if option.Mux {
conn = NewMux(conn, MuxOption{
ID: [2]byte{0, 0},
Host: "127.0.0.1",
Port: 0,
})
}
2019-02-11 07:25:10 +00:00
return conn, nil
}