Skip to content

Commit 2ad84f4

Browse files
committed
Merge branch 'Beta' into Meta
2 parents 5987f8e + c7aa164 commit 2ad84f4

File tree

158 files changed

+1962
-5513
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+1962
-5513
lines changed

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ RUN go mod download &&\
1616
FROM alpine:latest
1717
LABEL org.opencontainers.image.source="https://github.com/MetaCubeX/Clash.Meta"
1818

19-
RUN apk add --no-cache ca-certificates tzdata
19+
RUN apk add --no-cache ca-certificates tzdata iptables
2020

2121
VOLUME ["/root/.config/clash/"]
2222

2323
COPY --from=builder /clash-config/ /root/.config/clash/
2424
COPY --from=builder /clash /clash
2525
RUN chmod +x /clash
26-
ENTRYPOINT [ "/clash" ]
26+
ENTRYPOINT [ "/clash" ]

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ VERSION=$(shell git rev-parse --short HEAD)
1212
endif
1313

1414
BUILDTIME=$(shell date -u)
15-
GOBUILD=CGO_ENABLED=0 go build -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" \
15+
GOBUILD=CGO_ENABLED=0 go build -tags with_gvisor -trimpath -ldflags '-X "github.com/Dreamacro/clash/constant.Version=$(VERSION)" \
1616
-X "github.com/Dreamacro/clash/constant.BuildTime=$(BUILDTIME)" \
1717
-w -s -buildid='
1818

adapter/outbound/base.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (b *Base) Addr() string {
9090
}
9191

9292
// Unwrap implements C.ProxyAdapter
93-
func (b *Base) Unwrap(metadata *C.Metadata) C.Proxy {
93+
func (b *Base) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
9494
return nil
9595
}
9696

adapter/outbound/hysteria.go

+47-27
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@ import (
44
"context"
55
"crypto/sha256"
66
"crypto/tls"
7+
"encoding/base64"
78
"encoding/hex"
89
"encoding/pem"
910
"fmt"
10-
tlsC "github.com/Dreamacro/clash/component/tls"
11-
"github.com/Dreamacro/clash/transport/hysteria/core"
12-
"github.com/Dreamacro/clash/transport/hysteria/obfs"
13-
"github.com/Dreamacro/clash/transport/hysteria/pmtud_fix"
14-
"github.com/Dreamacro/clash/transport/hysteria/transport"
15-
"github.com/lucas-clemente/quic-go"
1611
"net"
1712
"os"
1813
"regexp"
1914
"strconv"
2015
"time"
2116

17+
"github.com/lucas-clemente/quic-go"
18+
"github.com/lucas-clemente/quic-go/congestion"
19+
M "github.com/sagernet/sing/common/metadata"
20+
2221
"github.com/Dreamacro/clash/component/dialer"
22+
tlsC "github.com/Dreamacro/clash/component/tls"
2323
C "github.com/Dreamacro/clash/constant"
2424
"github.com/Dreamacro/clash/log"
2525
hyCongestion "github.com/Dreamacro/clash/transport/hysteria/congestion"
26-
"github.com/lucas-clemente/quic-go/congestion"
27-
M "github.com/sagernet/sing/common/metadata"
26+
"github.com/Dreamacro/clash/transport/hysteria/core"
27+
"github.com/Dreamacro/clash/transport/hysteria/obfs"
28+
"github.com/Dreamacro/clash/transport/hysteria/pmtud_fix"
29+
"github.com/Dreamacro/clash/transport/hysteria/transport"
2830
)
2931

3032
const (
@@ -85,23 +87,27 @@ func (h *Hysteria) ListenPacketContext(ctx context.Context, metadata *C.Metadata
8587

8688
type HysteriaOption struct {
8789
BasicOption
88-
Name string `proxy:"name"`
89-
Server string `proxy:"server"`
90-
Port int `proxy:"port"`
91-
Protocol string `proxy:"protocol,omitempty"`
92-
Up string `proxy:"up"`
93-
Down string `proxy:"down"`
94-
AuthString string `proxy:"auth_str,omitempty"`
95-
Obfs string `proxy:"obfs,omitempty"`
96-
SNI string `proxy:"sni,omitempty"`
97-
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
98-
Fingerprint string `proxy:"fingerprint,omitempty"`
99-
ALPN string `proxy:"alpn,omitempty"`
100-
CustomCA string `proxy:"ca,omitempty"`
101-
CustomCAString string `proxy:"ca_str,omitempty"`
102-
ReceiveWindowConn int `proxy:"recv_window_conn,omitempty"`
103-
ReceiveWindow int `proxy:"recv_window,omitempty"`
104-
DisableMTUDiscovery bool `proxy:"disable_mtu_discovery,omitempty"`
90+
Name string `proxy:"name"`
91+
Server string `proxy:"server"`
92+
Port int `proxy:"port"`
93+
Protocol string `proxy:"protocol,omitempty"`
94+
ObfsProtocol string `proxy:"obfs-protocol,omitempty"` // compatible with Stash
95+
Up string `proxy:"up"`
96+
UpSpeed int `proxy:"up-speed,omitempty"` // compatible with Stash
97+
Down string `proxy:"down"`
98+
DownSpeed int `proxy:"down-speed,omitempty"` // compatible with Stash
99+
Auth string `proxy:"auth,omitempty"`
100+
AuthString string `proxy:"auth_str,omitempty"`
101+
Obfs string `proxy:"obfs,omitempty"`
102+
SNI string `proxy:"sni,omitempty"`
103+
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
104+
Fingerprint string `proxy:"fingerprint,omitempty"`
105+
ALPN []string `proxy:"alpn,omitempty"`
106+
CustomCA string `proxy:"ca,omitempty"`
107+
CustomCAString string `proxy:"ca_str,omitempty"`
108+
ReceiveWindowConn int `proxy:"recv_window_conn,omitempty"`
109+
ReceiveWindow int `proxy:"recv_window,omitempty"`
110+
DisableMTUDiscovery bool `proxy:"disable_mtu_discovery,omitempty"`
105111
}
106112

107113
func (c *HysteriaOption) Speed() (uint64, uint64, error) {
@@ -172,7 +178,7 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
172178
}
173179

174180
if len(option.ALPN) > 0 {
175-
tlsConfig.NextProtos = []string{option.ALPN}
181+
tlsConfig.NextProtos = option.ALPN
176182
} else {
177183
tlsConfig.NextProtos = []string{DefaultALPN}
178184
}
@@ -186,6 +192,9 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
186192
DisablePathMTUDiscovery: option.DisableMTUDiscovery,
187193
EnableDatagrams: true,
188194
}
195+
if option.ObfsProtocol != "" {
196+
option.Protocol = option.ObfsProtocol
197+
}
189198
if option.Protocol == "" {
190199
option.Protocol = DefaultProtocol
191200
}
@@ -202,6 +211,12 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
202211
}
203212

204213
var auth = []byte(option.AuthString)
214+
if option.Auth != "" {
215+
auth, err = base64.StdEncoding.DecodeString(option.Auth)
216+
if err != nil {
217+
return nil, err
218+
}
219+
}
205220
var obfuscator obfs.Obfuscator
206221
if len(option.Obfs) > 0 {
207222
obfuscator = obfs.NewXPlusObfuscator([]byte(option.Obfs))
@@ -211,7 +226,12 @@ func NewHysteria(option HysteriaOption) (*Hysteria, error) {
211226
if err != nil {
212227
return nil, err
213228
}
214-
229+
if option.UpSpeed != 0 {
230+
up = uint64(option.UpSpeed * mbpsToBps)
231+
}
232+
if option.DownSpeed != 0 {
233+
down = uint64(option.DownSpeed * mbpsToBps)
234+
}
215235
client, err := core.NewClient(
216236
addr, option.Protocol, auth, tlsConfig, quicConfig, clientTransport, up, down, func(refBPS uint64) congestion.CongestionControl {
217237
return hyCongestion.NewBrutalSender(congestion.ByteCount(refBPS))

adapter/outbound/shadowsocks.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net"
88
"strconv"
99

10-
"github.com/Dreamacro/clash/common/pool"
1110
"github.com/Dreamacro/clash/common/structure"
1211
"github.com/Dreamacro/clash/component/dialer"
1312
C "github.com/Dreamacro/clash/constant"
@@ -16,16 +15,11 @@ import (
1615
v2rayObfs "github.com/Dreamacro/clash/transport/v2ray-plugin"
1716
"github.com/sagernet/sing-shadowsocks"
1817
"github.com/sagernet/sing-shadowsocks/shadowimpl"
19-
"github.com/sagernet/sing/common/buf"
2018
"github.com/sagernet/sing/common/bufio"
2119
M "github.com/sagernet/sing/common/metadata"
2220
"github.com/sagernet/sing/common/uot"
2321
)
2422

25-
func init() {
26-
buf.DefaultAllocator = pool.DefaultAllocator
27-
}
28-
2923
type ShadowSocks struct {
3024
*Base
3125
method shadowsocks.Method
@@ -82,8 +76,7 @@ func (ss *ShadowSocks) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, e
8276
}
8377
}
8478
if metadata.NetWork == C.UDP && ss.option.UDPOverTCP {
85-
metadata.Host = uot.UOTMagicAddress
86-
metadata.DstPort = "443"
79+
return ss.method.DialConn(c, M.ParseSocksaddr(uot.UOTMagicAddress+":443"))
8780
}
8881
return ss.method.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
8982
}

adapter/outbound/vmess.go

+30-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ type VmessOption struct {
5252
GrpcOpts GrpcOptions `proxy:"grpc-opts,omitempty"`
5353
WSOpts WSOptions `proxy:"ws-opts,omitempty"`
5454
PacketAddr bool `proxy:"packet-addr,omitempty"`
55+
XUDP bool `proxy:"xudp,omitempty"`
56+
PacketEncoding string `proxy:"packet-encoding,omitempty"`
57+
GlobalPadding bool `proxy:"global-padding,omitempty"`
5558
AuthenticatedLength bool `proxy:"authenticated-length,omitempty"`
5659
}
5760

@@ -197,7 +200,11 @@ func (v *Vmess) StreamConn(c net.Conn, metadata *C.Metadata) (net.Conn, error) {
197200
return nil, err
198201
}
199202
if metadata.NetWork == C.UDP {
200-
return v.client.DialPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
203+
if v.option.XUDP {
204+
return v.client.DialXUDPPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
205+
} else {
206+
return v.client.DialPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
207+
}
201208
} else {
202209
return v.client.DialConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
203210
}
@@ -244,6 +251,8 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
244251
}
245252

246253
if v.option.PacketAddr {
254+
_metadata := *metadata // make a copy
255+
metadata = &_metadata
247256
metadata.Host = packetaddr.SeqPacketMagicAddress
248257
metadata.DstPort = "443"
249258
}
@@ -257,7 +266,11 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
257266
}
258267
defer safeConnClose(c, err)
259268

260-
c, err = v.client.DialPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
269+
if v.option.XUDP {
270+
c, err = v.client.DialXUDPPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
271+
} else {
272+
c, err = v.client.DialPacketConn(c, M.ParseSocksaddr(metadata.RemoteAddress()))
273+
}
261274
} else {
262275
c, err = dialer.DialContext(ctx, "tcp", v.addr, v.Base.DialOptions(opts...)...)
263276
if err != nil {
@@ -274,7 +287,7 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
274287
}
275288

276289
if v.option.PacketAddr {
277-
return newPacketConn(&threadSafePacketConn{PacketConn: packetaddr.NewBindClient(c)}, v), nil
290+
return newPacketConn(&threadSafePacketConn{PacketConn: packetaddr.NewBindConn(c)}, v), nil
278291
} else if pc, ok := c.(net.PacketConn); ok {
279292
return newPacketConn(&threadSafePacketConn{PacketConn: pc}, v), nil
280293
}
@@ -284,7 +297,7 @@ func (v *Vmess) ListenPacketContext(ctx context.Context, metadata *C.Metadata, o
284297
// ListenPacketOnStreamConn implements C.ProxyAdapter
285298
func (v *Vmess) ListenPacketOnStreamConn(c net.Conn, metadata *C.Metadata) (_ C.PacketConn, err error) {
286299
if v.option.PacketAddr {
287-
return newPacketConn(&threadSafePacketConn{PacketConn: packetaddr.NewBindClient(c)}, v), nil
300+
return newPacketConn(&threadSafePacketConn{PacketConn: packetaddr.NewBindConn(c)}, v), nil
288301
} else if pc, ok := c.(net.PacketConn); ok {
289302
return newPacketConn(&threadSafePacketConn{PacketConn: pc}, v), nil
290303
}
@@ -299,6 +312,9 @@ func (v *Vmess) SupportUOT() bool {
299312
func NewVmess(option VmessOption) (*Vmess, error) {
300313
security := strings.ToLower(option.Cipher)
301314
var options []vmess.ClientOption
315+
if option.GlobalPadding {
316+
options = append(options, vmess.ClientWithGlobalPadding())
317+
}
302318
if option.AuthenticatedLength {
303319
options = append(options, vmess.ClientWithAuthenticatedLength())
304320
}
@@ -307,6 +323,16 @@ func NewVmess(option VmessOption) (*Vmess, error) {
307323
return nil, err
308324
}
309325

326+
switch option.PacketEncoding {
327+
case "packetaddr", "packet":
328+
option.PacketAddr = true
329+
case "xudp":
330+
option.XUDP = true
331+
}
332+
if option.XUDP {
333+
option.PacketAddr = false
334+
}
335+
310336
switch option.Network {
311337
case "h2", "grpc":
312338
if !option.TLS {

adapter/outboundgroup/fallback.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (f *Fallback) DialContext(ctx context.Context, metadata *C.Metadata, opts .
3131
c.AppendToChains(f)
3232
f.onDialSuccess()
3333
} else {
34-
f.onDialFailed()
34+
f.onDialFailed(proxy.Type(), err)
3535
}
3636

3737
return c, err
@@ -72,8 +72,8 @@ func (f *Fallback) MarshalJSON() ([]byte, error) {
7272
}
7373

7474
// Unwrap implements C.ProxyAdapter
75-
func (f *Fallback) Unwrap(metadata *C.Metadata) C.Proxy {
76-
proxy := f.findAliveProxy(true)
75+
func (f *Fallback) Unwrap(metadata *C.Metadata, touch bool) C.Proxy {
76+
proxy := f.findAliveProxy(touch)
7777
return proxy
7878
}
7979

0 commit comments

Comments
 (0)