Skip to content

Commit b151e7d

Browse files
committed
chore: support fingerprint for anytls
1 parent 808fdcf commit b151e7d

File tree

2 files changed

+21
-38
lines changed

2 files changed

+21
-38
lines changed

adapter/outbound/anytls.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package outbound
22

33
import (
44
"context"
5-
"crypto/tls"
65
"errors"
76
"net"
87
"runtime"
@@ -16,6 +15,7 @@ import (
1615
tlsC "github.com/metacubex/mihomo/component/tls"
1716
C "github.com/metacubex/mihomo/constant"
1817
"github.com/metacubex/mihomo/transport/anytls"
18+
"github.com/metacubex/mihomo/transport/vmess"
1919

2020
M "github.com/sagernet/sing/common/metadata"
2121
"github.com/sagernet/sing/common/uot"
@@ -38,6 +38,7 @@ type AnyTLSOption struct {
3838
SNI string `proxy:"sni,omitempty"`
3939
ClientFingerprint string `proxy:"client-fingerprint,omitempty"`
4040
SkipCertVerify bool `proxy:"skip-cert-verify,omitempty"`
41+
Fingerprint string `proxy:"fingerprint,omitempty"`
4142
UDP bool `proxy:"udp,omitempty"`
4243
IdleSessionCheckInterval int `proxy:"idle-session-check-interval,omitempty"`
4344
IdleSessionTimeout int `proxy:"idle-session-timeout,omitempty"`
@@ -97,21 +98,21 @@ func NewAnyTLS(option AnyTLSOption) (*AnyTLS, error) {
9798
Dialer: singDialer,
9899
IdleSessionCheckInterval: time.Duration(option.IdleSessionCheckInterval) * time.Second,
99100
IdleSessionTimeout: time.Duration(option.IdleSessionTimeout) * time.Second,
100-
ClientFingerprint: option.ClientFingerprint,
101101
}
102-
tlsConfig := &tls.Config{
103-
ServerName: option.SNI,
104-
InsecureSkipVerify: option.SkipCertVerify,
105-
NextProtos: option.ALPN,
102+
tlsConfig := &vmess.TLSConfig{
103+
Host: option.SNI,
104+
SkipCertVerify: option.SkipCertVerify,
105+
NextProtos: option.ALPN,
106+
FingerPrint: option.Fingerprint,
107+
ClientFingerprint: option.ClientFingerprint,
106108
}
107-
if tlsConfig.ServerName == "" {
108-
tlsConfig.ServerName = "127.0.0.1"
109+
if tlsConfig.Host == "" {
110+
tlsConfig.Host = option.Server
109111
}
110-
tOption.TLSConfig = tlsConfig
111-
112-
if tlsC.HaveGlobalFingerprint() && len(tOption.ClientFingerprint) == 0 {
113-
tOption.ClientFingerprint = tlsC.GetGlobalFingerprint()
112+
if tlsC.HaveGlobalFingerprint() && len(option.ClientFingerprint) == 0 {
113+
tlsConfig.ClientFingerprint = tlsC.GetGlobalFingerprint()
114114
}
115+
tOption.TLSConfig = tlsConfig
115116

116117
outbound := &AnyTLS{
117118
Base: &Base{

transport/anytls/client.go

+8-26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package anytls
33
import (
44
"context"
55
"crypto/sha256"
6-
"crypto/tls"
76
"encoding/binary"
87
"net"
98
"time"
@@ -25,13 +24,12 @@ type ClientConfig struct {
2524
IdleSessionTimeout time.Duration
2625
Server M.Socksaddr
2726
Dialer N.Dialer
28-
TLSConfig *tls.Config
29-
ClientFingerprint string
27+
TLSConfig *vmess.TLSConfig
3028
}
3129

3230
type Client struct {
3331
passwordSha256 []byte
34-
tlsConfig *tls.Config
32+
tlsConfig *vmess.TLSConfig
3533
clientFingerprint string
3634
dialer N.Dialer
3735
server M.Socksaddr
@@ -42,11 +40,10 @@ type Client struct {
4240
func NewClient(ctx context.Context, config ClientConfig) *Client {
4341
pw := sha256.Sum256([]byte(config.Password))
4442
c := &Client{
45-
passwordSha256: pw[:],
46-
tlsConfig: config.TLSConfig,
47-
clientFingerprint: config.ClientFingerprint,
48-
dialer: config.Dialer,
49-
server: config.Server,
43+
passwordSha256: pw[:],
44+
tlsConfig: config.TLSConfig,
45+
dialer: config.Dialer,
46+
server: config.Server,
5047
}
5148
// Initialize the padding state of this client
5249
padding.UpdatePaddingScheme(padding.DefaultPaddingScheme, &c.padding)
@@ -85,24 +82,9 @@ func (c *Client) CreateOutboundTLSConnection(ctx context.Context) (net.Conn, err
8582
}
8683

8784
getTlsConn := func() (net.Conn, error) {
88-
if len(c.clientFingerprint) != 0 {
89-
utlsConn, valid := vmess.GetUTLSConn(conn, c.clientFingerprint, c.tlsConfig)
90-
if valid {
91-
ctx, cancel := context.WithTimeout(ctx, C.DefaultTLSTimeout)
92-
defer cancel()
93-
94-
err := utlsConn.HandshakeContext(ctx)
95-
return utlsConn, err
96-
}
97-
}
98-
99-
tlsConn := tls.Client(conn, c.tlsConfig)
100-
101-
ctx, cancel := context.WithTimeout(context.Background(), C.DefaultTLSTimeout)
85+
ctx, cancel := context.WithTimeout(ctx, C.DefaultTLSTimeout)
10286
defer cancel()
103-
104-
err = tlsConn.HandshakeContext(ctx)
105-
return tlsConn, err
87+
return vmess.StreamTLSConn(ctx, conn, c.tlsConfig)
10688
}
10789
tlsConn, err := getTlsConn()
10890
if err != nil {

0 commit comments

Comments
 (0)