From 9511ccfe476787305990e66122b590473e6f10b6 Mon Sep 17 00:00:00 2001 From: MetaCubeX Date: Wed, 8 Jun 2022 01:47:50 +0800 Subject: [PATCH] chore: refine code --- adapter/outbound/hysteria.go | 28 +++++++++++++++++----------- adapter/outbound/vless.go | 4 ++++ adapter/outbound/vmess.go | 13 +++++++------ common/convert/util.go | 9 ++++++--- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/adapter/outbound/hysteria.go b/adapter/outbound/hysteria.go index f782f55a..49a77cf0 100644 --- a/adapter/outbound/hysteria.go +++ b/adapter/outbound/hysteria.go @@ -34,7 +34,8 @@ const ( DefaultConnectionReceiveWindow = 67108864 // 64 MB/s DefaultMaxIncomingStreams = 1024 - DefaultALPN = "hysteria" + DefaultALPN = "hysteria" + DefaultProtocol = "udp" ) var rateStringRegexp = regexp.MustCompile(`^(\d+)\s*([KMGT]?)([Bb])ps$`) @@ -84,10 +85,9 @@ type HysteriaOption struct { ALPN string `proxy:"alpn,omitempty"` CustomCA string `proxy:"ca,omitempty"` CustomCAString string `proxy:"ca_str,omitempty"` - ReceiveWindowConn uint64 `proxy:"recv_window_conn,omitempty"` - ReceiveWindow uint64 `proxy:"recv_window,omitempty"` + ReceiveWindowConn int `proxy:"recv_window_conn,omitempty"` + ReceiveWindow int `proxy:"recv_window,omitempty"` DisableMTUDiscovery bool `proxy:"disable_mtu_discovery,omitempty"` - UDP bool `proxy:"udp,omitempty"` } func (c *HysteriaOption) Speed() (uint64, uint64, error) { @@ -119,9 +119,12 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) { } addr := net.JoinHostPort(option.Server, strconv.Itoa(option.Port)) - + serverName := option.Server + if option.SNI != "" { + serverName = option.Server + } tlsConfig := &tls.Config{ - ServerName: option.SNI, + ServerName: serverName, InsecureSkipVerify: option.SkipCertVerify, MinVersion: tls.VersionTLS13, } @@ -148,14 +151,17 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) { tlsConfig.RootCAs = cp } quicConfig := &quic.Config{ - InitialStreamReceiveWindow: option.ReceiveWindowConn, - MaxStreamReceiveWindow: option.ReceiveWindowConn, - InitialConnectionReceiveWindow: option.ReceiveWindow, - MaxConnectionReceiveWindow: option.ReceiveWindow, + InitialStreamReceiveWindow: uint64(option.ReceiveWindowConn), + MaxStreamReceiveWindow: uint64(option.ReceiveWindowConn), + InitialConnectionReceiveWindow: uint64(option.ReceiveWindow), + MaxConnectionReceiveWindow: uint64(option.ReceiveWindow), KeepAlive: true, DisablePathMTUDiscovery: option.DisableMTUDiscovery, EnableDatagrams: true, } + if option.Protocol == "" { + option.Protocol = DefaultProtocol + } if option.ReceiveWindowConn == 0 { quicConfig.InitialStreamReceiveWindow = DefaultStreamReceiveWindow quicConfig.MaxStreamReceiveWindow = DefaultStreamReceiveWindow @@ -195,7 +201,7 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) { name: option.Name, addr: addr, tp: C.Hysteria, - udp: option.UDP, + udp: true, iface: option.Interface, rmark: option.RoutingMark, }, diff --git a/adapter/outbound/vless.go b/adapter/outbound/vless.go index aa06c26b..2d5c51bb 100644 --- a/adapter/outbound/vless.go +++ b/adapter/outbound/vless.go @@ -6,6 +6,7 @@ import ( "encoding/binary" "errors" "fmt" + "github.com/Dreamacro/clash/common/convert" "io" "net" "net/http" @@ -90,6 +91,9 @@ func (v *Vless) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) { wsOpts.TLSConfig.ServerName = v.option.ServerName } else if host := wsOpts.Headers.Get("Host"); host != "" { wsOpts.TLSConfig.ServerName = host + } else { + wsOpts.Headers.Set("Host", convert.RandHost()) + convert.SetUserAgent(wsOpts.Headers) } c, err = vmess.StreamWebsocketConn(c, wsOpts) case "http": diff --git a/adapter/outbound/vmess.go b/adapter/outbound/vmess.go index b17bb5d0..939855e4 100644 --- a/adapter/outbound/vmess.go +++ b/adapter/outbound/vmess.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "errors" "fmt" + "github.com/Dreamacro/clash/common/convert" "net" "net/http" "strconv" @@ -78,12 +79,6 @@ func (v *Vmess) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) { var err error switch v.option.Network { case "ws": - if v.option.WSOpts.Path == "" { - v.option.WSOpts.Path = v.option.WSPath - } - if len(v.option.WSOpts.Headers) == 0 { - v.option.WSOpts.Headers = v.option.WSHeaders - } host, port, _ := net.SplitHostPort(v.addr) wsOpts := &vmess.WebsocketConfig{ @@ -114,6 +109,9 @@ func (v *Vmess) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) { } else if host := wsOpts.Headers.Get("Host"); host != "" { wsOpts.TLSConfig.ServerName = host } + } else { + wsOpts.Headers.Set("Host", convert.RandHost()) + convert.SetUserAgent(wsOpts.Headers) } c, err = vmess.StreamWebsocketConn(c, wsOpts) case "http": @@ -133,6 +131,9 @@ func (v *Vmess) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) { if err != nil { return nil, err } + } else { + http.Header(v.option.HTTPOpts.Headers).Set("Host", convert.RandHost()) + convert.SetUserAgent(v.option.HTTPOpts.Headers) } host, _, _ := net.SplitHostPort(v.addr) diff --git a/common/convert/util.go b/common/convert/util.go index acaba013..b3bd6e5b 100644 --- a/common/convert/util.go +++ b/common/convert/util.go @@ -291,7 +291,7 @@ var ( ) func RandHost() string { - id, _ := uuid.NewV6() + id, _ := uuid.NewV4() base := strings.ToLower(base64.RawURLEncoding.EncodeToString(id.Bytes())) base = strings.ReplaceAll(base, "-", "") base = strings.ReplaceAll(base, "_", "") @@ -307,7 +307,10 @@ func RandUserAgent() string { return userAgents[rand.Intn(uaLen)] } -func SetUserAgent(req *http.Request) { +func SetUserAgent(header http.Header) { + if header.Get("User-Agent") != "" { + return + } userAgent := RandUserAgent() - req.Header.Set("User-Agent", userAgent) + header.Set("User-Agent", userAgent) }