Feature: v2ray-plugin support disable mux

This commit is contained in:
Dreamacro 2019-09-21 23:49:00 +08:00
parent e0c8aed5c7
commit 1a8a6d0b5d
3 changed files with 26 additions and 19 deletions

View file

@ -179,6 +179,7 @@ Proxy:
# skip-cert-verify: true # skip-cert-verify: true
# host: bing.com # host: bing.com
# path: "/" # path: "/"
# mux: true
# headers: # headers:
# custom: value # custom: value

View file

@ -25,7 +25,7 @@ type ShadowSocks struct {
// obfs // obfs
obfsMode string obfsMode string
obfsOption *simpleObfsOption obfsOption *simpleObfsOption
wsOption *v2rayObfs.WebsocketOption v2rayOption *v2rayObfs.Option
} }
type ShadowSocksOption struct { type ShadowSocksOption struct {
@ -55,6 +55,7 @@ type v2rayObfsOption struct {
TLS bool `obfs:"tls,omitempty"` TLS bool `obfs:"tls,omitempty"`
Headers map[string]string `obfs:"headers,omitempty"` Headers map[string]string `obfs:"headers,omitempty"`
SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"` SkipCertVerify bool `obfs:"skip-cert-verify,omitempty"`
Mux bool `obfs:"mux,omitempty"`
} }
func (ss *ShadowSocks) Dial(metadata *C.Metadata) (C.Conn, error) { func (ss *ShadowSocks) Dial(metadata *C.Metadata) (C.Conn, error) {
@ -71,7 +72,7 @@ func (ss *ShadowSocks) Dial(metadata *C.Metadata) (C.Conn, error) {
c = obfs.NewHTTPObfs(c, ss.obfsOption.Host, port) c = obfs.NewHTTPObfs(c, ss.obfsOption.Host, port)
case "websocket": case "websocket":
var err error var err error
c, err = v2rayObfs.NewWebsocketObfs(c, ss.wsOption) c, err = v2rayObfs.NewV2rayObfs(c, ss.v2rayOption)
if err != nil { if err != nil {
return nil, fmt.Errorf("%s connect error: %s", ss.server, err.Error()) return nil, fmt.Errorf("%s connect error: %s", ss.server, err.Error())
} }
@ -116,7 +117,7 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error()) return nil, fmt.Errorf("ss %s initialize error: %s", server, err.Error())
} }
var wsOption *v2rayObfs.WebsocketOption var v2rayOption *v2rayObfs.Option
var obfsOption *simpleObfsOption var obfsOption *simpleObfsOption
obfsMode := "" obfsMode := ""
@ -144,7 +145,7 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
obfsMode = opts.Mode obfsMode = opts.Mode
obfsOption = &opts obfsOption = &opts
} else if option.Plugin == "v2ray-plugin" { } else if option.Plugin == "v2ray-plugin" {
opts := v2rayObfsOption{Host: "bing.com"} opts := v2rayObfsOption{Host: "bing.com", Mux: true}
if err := decoder.Decode(option.PluginOpts, &opts); err != nil { if err := decoder.Decode(option.PluginOpts, &opts); err != nil {
return nil, fmt.Errorf("ss %s initialize v2ray-plugin error: %s", server, err.Error()) return nil, fmt.Errorf("ss %s initialize v2ray-plugin error: %s", server, err.Error())
} }
@ -162,11 +163,12 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
ClientSessionCache: getClientSessionCache(), ClientSessionCache: getClientSessionCache(),
} }
} }
wsOption = &v2rayObfs.WebsocketOption{ v2rayOption = &v2rayObfs.Option{
Host: opts.Host, Host: opts.Host,
Path: opts.Path, Path: opts.Path,
Headers: opts.Headers, Headers: opts.Headers,
TLSConfig: tlsConfig, TLSConfig: tlsConfig,
Mux: opts.Mux,
} }
} }
@ -180,7 +182,7 @@ func NewShadowSocks(option ShadowSocksOption) (*ShadowSocks, error) {
cipher: ciph, cipher: ciph,
obfsMode: obfsMode, obfsMode: obfsMode,
wsOption: wsOption, v2rayOption: v2rayOption,
obfsOption: obfsOption, obfsOption: obfsOption,
}, nil }, nil
} }

View file

@ -8,16 +8,17 @@ import (
"github.com/Dreamacro/clash/component/vmess" "github.com/Dreamacro/clash/component/vmess"
) )
// WebsocketOption is options of websocket obfs // Option is options of websocket obfs
type WebsocketOption struct { type Option struct {
Host string Host string
Path string Path string
Headers map[string]string Headers map[string]string
TLSConfig *tls.Config TLSConfig *tls.Config
Mux bool
} }
// NewWebsocketObfs return a HTTPObfs // NewV2rayObfs return a HTTPObfs
func NewWebsocketObfs(conn net.Conn, option *WebsocketOption) (net.Conn, error) { func NewV2rayObfs(conn net.Conn, option *Option) (net.Conn, error) {
header := http.Header{} header := http.Header{}
for k, v := range option.Headers { for k, v := range option.Headers {
header.Add(k, v) header.Add(k, v)
@ -36,10 +37,13 @@ func NewWebsocketObfs(conn net.Conn, option *WebsocketOption) (net.Conn, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if option.Mux {
conn = NewMux(conn, MuxOption{ conn = NewMux(conn, MuxOption{
ID: [2]byte{0, 0}, ID: [2]byte{0, 0},
Host: "127.0.0.1", Host: "127.0.0.1",
Port: 0, Port: 0,
}) })
}
return conn, nil return conn, nil
} }