Clash.Meta/transport/v2ray-plugin/websocket.go

74 lines
1.5 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
tlsC "github.com/Dreamacro/clash/component/tls"
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
2022-07-11 13:42:28 +08:00
Fingerprint string
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
2022-07-11 13:42:28 +08:00
tlsConfig := &tls.Config{
2021-10-16 20:19:59 +08:00
ServerName: option.Host,
InsecureSkipVerify: option.SkipCertVerify,
NextProtos: []string{"http/1.1"},
}
2022-07-11 13:42:28 +08:00
if len(option.Fingerprint) == 0 {
config.TLSConfig = tlsC.GetGlobalTLSConfig(tlsConfig)
2022-07-11 13:42:28 +08:00
} else {
var err error
if config.TLSConfig, err = tlsC.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint); err != nil {
return nil, err
}
}
2021-10-16 20:19:59 +08:00
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
}