2019-02-11 15:25:10 +08:00
|
|
|
package obfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
2019-07-31 11:13:49 +08:00
|
|
|
"net/http"
|
2019-02-11 15:25:10 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/vmess"
|
|
|
|
)
|
|
|
|
|
2019-09-21 23:49:00 +08:00
|
|
|
// Option is options of websocket obfs
|
|
|
|
type Option struct {
|
2019-02-11 15:25:10 +08:00
|
|
|
Host string
|
|
|
|
Path string
|
2019-03-15 09:43:46 +08:00
|
|
|
Headers map[string]string
|
2019-02-11 15:25:10 +08:00
|
|
|
TLSConfig *tls.Config
|
2019-09-21 23:49:00 +08:00
|
|
|
Mux bool
|
2019-02-11 15:25:10 +08:00
|
|
|
}
|
|
|
|
|
2019-09-21 23:49:00 +08:00
|
|
|
// NewV2rayObfs return a HTTPObfs
|
|
|
|
func NewV2rayObfs(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{
|
|
|
|
Host: option.Host,
|
|
|
|
Path: option.Path,
|
|
|
|
TLS: option.TLSConfig != nil,
|
2019-07-31 11:13:49 +08:00
|
|
|
Headers: header,
|
2019-02-11 15:25:10 +08:00
|
|
|
TLSConfig: option.TLSConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
conn, err = vmess.NewWebsocketConn(conn, config)
|
|
|
|
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
|
|
|
|
}
|