clash/transport/v2ray-plugin/websocket.go

63 lines
1.2 KiB
Go
Raw Normal View History

2019-02-11 15:25:10 +08:00
package obfs
import (
2021-10-16 20:19:59 +08:00
"crypto/tls"
2019-02-11 15:25:10 +08:00
"net"
"net/http"
2019-02-11 15:25:10 +08:00
2021-05-13 22:18:49 +08:00
"github.com/Dreamacro/clash/transport/vmess"
2019-02-11 15:25:10 +08: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 15:25:10 +08: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 15:25:10 +08:00
config := &vmess.WebsocketConfig{
2021-10-16 20:19:59 +08: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 15:25:10 +08:00
}
var err error
conn, err = vmess.StreamWebsocketConn(conn, config)
2019-02-11 15:25:10 +08: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 15:25:10 +08:00
return conn, nil
}