2019-02-11 15:25:10 +08:00
|
|
|
package obfs
|
|
|
|
|
|
|
|
import (
|
2023-05-18 13:15:08 +08:00
|
|
|
"context"
|
2021-10-16 20:19:59 +08:00
|
|
|
"crypto/tls"
|
2019-02-11 15:25:10 +08:00
|
|
|
"net"
|
2019-07-31 11:13:49 +08:00
|
|
|
"net/http"
|
2019-02-11 15:25:10 +08:00
|
|
|
|
2023-09-22 14:45:34 +08:00
|
|
|
"github.com/Dreamacro/clash/component/ca"
|
2021-05-13 22:18:49 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/vmess"
|
2019-02-11 15:25:10 +08:00
|
|
|
)
|
|
|
|
|
2019-09-21 23:49:00 +08:00
|
|
|
// Option is options of websocket obfs
|
|
|
|
type Option struct {
|
2023-11-02 11:11:19 +08:00
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
Path string
|
|
|
|
Headers map[string]string
|
|
|
|
TLS bool
|
|
|
|
SkipCertVerify bool
|
|
|
|
Fingerprint string
|
|
|
|
Mux bool
|
|
|
|
V2rayHttpUpgrade bool
|
2019-02-11 15:25:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-21 23:49:00 +08:00
|
|
|
// NewV2rayObfs return a HTTPObfs
|
2023-05-18 13:15:08 +08:00
|
|
|
func NewV2rayObfs(ctx context.Context, conn net.Conn, option *Option) (net.Conn, error) {
|
2019-07-31 11:13:49 +08:00
|
|
|
header := http.Header{}
|
|
|
|
for k, v := range option.Headers {
|
|
|
|
header.Add(k, v)
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:25:10 +08:00
|
|
|
config := &vmess.WebsocketConfig{
|
2023-11-02 11:11:19 +08:00
|
|
|
Host: option.Host,
|
|
|
|
Port: option.Port,
|
|
|
|
Path: option.Path,
|
|
|
|
V2rayHttpUpgrade: option.V2rayHttpUpgrade,
|
|
|
|
Headers: header,
|
2021-10-16 20:19:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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"},
|
|
|
|
}
|
2023-09-22 14:45:34 +08:00
|
|
|
var err error
|
|
|
|
config.TLSConfig, err = ca.GetSpecifiedFingerprintTLSConfig(tlsConfig, option.Fingerprint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-07-11 13:42:28 +08:00
|
|
|
}
|
|
|
|
|
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
|
2023-05-18 13:15:08 +08:00
|
|
|
conn, err = vmess.StreamWebsocketConn(ctx, conn, config)
|
2019-02-11 15:25:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-21 23:49:00 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|