Chore: compatible with Stash hysteria config

This commit is contained in:
gVisor bot 2022-09-21 23:42:33 +08:00
parent 59ec6a6a32
commit a692e3b2c7

View file

@ -4,27 +4,29 @@ import (
"context" "context"
"crypto/sha256" "crypto/sha256"
"crypto/tls" "crypto/tls"
"encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/pem" "encoding/pem"
"fmt" "fmt"
tlsC "github.com/Dreamacro/clash/component/tls"
"github.com/Dreamacro/clash/transport/hysteria/core"
"github.com/Dreamacro/clash/transport/hysteria/obfs"
"github.com/Dreamacro/clash/transport/hysteria/pmtud_fix"
"github.com/Dreamacro/clash/transport/hysteria/transport"
"github.com/lucas-clemente/quic-go"
"net" "net"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"time" "time"
"github.com/lucas-clemente/quic-go"
"github.com/lucas-clemente/quic-go/congestion"
M "github.com/sagernet/sing/common/metadata"
"github.com/Dreamacro/clash/component/dialer" "github.com/Dreamacro/clash/component/dialer"
tlsC "github.com/Dreamacro/clash/component/tls"
C "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log" "github.com/Dreamacro/clash/log"
hyCongestion "github.com/Dreamacro/clash/transport/hysteria/congestion" hyCongestion "github.com/Dreamacro/clash/transport/hysteria/congestion"
"github.com/lucas-clemente/quic-go/congestion" "github.com/Dreamacro/clash/transport/hysteria/core"
M "github.com/sagernet/sing/common/metadata" "github.com/Dreamacro/clash/transport/hysteria/obfs"
"github.com/Dreamacro/clash/transport/hysteria/pmtud_fix"
"github.com/Dreamacro/clash/transport/hysteria/transport"
) )
const ( const (
@ -85,23 +87,27 @@ func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata
type HysteriaOption struct { type HysteriaOption struct {
BasicOption BasicOption
Name string `proxy:"name"` Name string `proxy:"name"`
Server string `proxy:"server"` Server string `proxy:"server"`
Port int `proxy:"port"` Port int `proxy:"port"`
Protocol string `proxy:"protocol,omitempty"` Protocol string `proxy:"protocol,omitempty"`
Up string `proxy:"up"` ObfsProtocol string `proxy:"obfs-protocol,omitempty"` // compatible with Stash
Down string `proxy:"down"` Up string `proxy:"up"`
AuthString string `proxy:"auth_str,omitempty"` UpSpeed int `proxy:"up-speed,omitempty"` // compatible with Stash
Obfs string `proxy:"obfs,omitempty"` Down string `proxy:"down"`
SNI string `proxy:"sni,omitempty"` DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"` Auth string `proxy:"auth,omitempty"`
Fingerprint string `proxy:"fingerprint,omitempty"` AuthString string `proxy:"auth_str,omitempty"`
ALPN string `proxy:"alpn,omitempty"` Obfs string `proxy:"obfs,omitempty"`
CustomCA string `proxy:"ca,omitempty"` SNI string `proxy:"sni,omitempty"`
CustomCAString string `proxy:"ca_str,omitempty"` SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
ReceiveWindowConn int `proxy:"recv_window_conn,omitempty"` Fingerprint string `proxy:"fingerprint,omitempty"`
ReceiveWindow int `proxy:"recv_window,omitempty"` ALPN []string `proxy:"alpn,omitempty"`
DisableMTUDiscovery bool `proxy:"disable_mtu_discovery,omitempty"` CustomCA string `proxy:"ca,omitempty"`
CustomCAString string `proxy:"ca_str,omitempty"`
ReceiveWindowConn int `proxy:"recv_window_conn,omitempty"`
ReceiveWindow int `proxy:"recv_window,omitempty"`
DisableMTUDiscovery bool `proxy:"disable_mtu_discovery,omitempty"`
} }
func (c *HysteriaOption) Speed() (uint64, uint64, error) { func (c *HysteriaOption) Speed() (uint64, uint64, error) {
@ -172,7 +178,7 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
} }
if len(option.ALPN) > 0 { if len(option.ALPN) > 0 {
tlsConfig.NextProtos = []string{option.ALPN} tlsConfig.NextProtos = option.ALPN
} else { } else {
tlsConfig.NextProtos = []string{DefaultALPN} tlsConfig.NextProtos = []string{DefaultALPN}
} }
@ -186,6 +192,9 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
DisablePathMTUDiscovery: option.DisableMTUDiscovery, DisablePathMTUDiscovery: option.DisableMTUDiscovery,
EnableDatagrams: true, EnableDatagrams: true,
} }
if option.ObfsProtocol != "" {
option.Protocol = option.ObfsProtocol
}
if option.Protocol == "" { if option.Protocol == "" {
option.Protocol = DefaultProtocol option.Protocol = DefaultProtocol
} }
@ -202,6 +211,12 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
} }
var auth = []byte(option.AuthString) var auth = []byte(option.AuthString)
if option.Auth != "" {
auth, err = base64.StdEncoding.DecodeString(option.Auth)
if err != nil {
return nil, err
}
}
var obfuscator obfs.Obfuscator var obfuscator obfs.Obfuscator
if len(option.Obfs) > 0 { if len(option.Obfs) > 0 {
obfuscator = obfs.NewXPlusObfuscator([]byte(option.Obfs)) obfuscator = obfs.NewXPlusObfuscator([]byte(option.Obfs))
@ -211,7 +226,12 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if option.UpSpeed != 0 {
up = uint64(option.UpSpeed * mbpsToBps)
}
if option.DownSpeed != 0 {
down = uint64(option.DownSpeed * mbpsToBps)
}
client, err := core.NewClient( client, err := core.NewClient(
addr, option.Protocol, auth, tlsConfig, quicConfig, clientTransport, up, down, func(refBPS uint64) congestion.CongestionControl { addr, option.Protocol, auth, tlsConfig, quicConfig, clientTransport, up, down, func(refBPS uint64) congestion.CongestionControl {
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS)) return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))