chore: refine code

This commit is contained in:
MetaCubeX 2022-06-08 01:47:50 +08:00
parent ed17a1bf23
commit 9511ccfe47
4 changed files with 34 additions and 20 deletions

View file

@ -35,6 +35,7 @@ const (
DefaultMaxIncomingStreams = 1024
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,
},

View file

@ -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":

View file

@ -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)

View file

@ -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)
}