Skip to content

Commit 6d713a5

Browse files
authored
Update ech-workers.go
1 parent ed5fb14 commit 6d713a5

1 file changed

Lines changed: 43 additions & 10 deletions

File tree

ech-workers.go

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"net"
1717
"net/http"
1818
"net/url"
19+
"reflect"
1920
"strings"
2021
"sync"
2122
"time"
@@ -119,15 +120,47 @@ func buildTLSConfigWithECH(serverName string, echList []byte) (*tls.Config, erro
119120
if err != nil {
120121
return nil, fmt.Errorf("加载系统根证书失败: %w", err)
121122
}
122-
return &tls.Config{
123-
MinVersion: tls.VersionTLS13,
124-
ServerName: serverName,
125-
EncryptedClientHelloConfigList: echList,
126-
EncryptedClientHelloRejectionVerify: func(cs tls.ConnectionState) error {
127-
return errors.New("服务器拒绝 ECH")
128-
},
129-
RootCAs: roots,
130-
}, nil
123+
124+
if echList == nil || len(echList) == 0 {
125+
return nil, errors.New("ECH 配置为空,这是必需功能")
126+
}
127+
128+
config := &tls.Config{
129+
MinVersion: tls.VersionTLS13,
130+
ServerName: serverName,
131+
RootCAs: roots,
132+
}
133+
134+
// 使用反射设置 ECH 字段(ECH 是核心功能,必须设置成功)
135+
if err := setECHConfig(config, echList); err != nil {
136+
return nil, fmt.Errorf("设置 ECH 配置失败(需要 Go 1.23+ 或支持 ECH 的版本): %w", err)
137+
}
138+
139+
return config, nil
140+
}
141+
142+
// setECHConfig 使用反射设置 ECH 配置(ECH 是核心功能,必须成功)
143+
func setECHConfig(config *tls.Config, echList []byte) error {
144+
configValue := reflect.ValueOf(config).Elem()
145+
146+
// 设置 EncryptedClientHelloConfigList(必需)
147+
field1 := configValue.FieldByName("EncryptedClientHelloConfigList")
148+
if !field1.IsValid() || !field1.CanSet() {
149+
return fmt.Errorf("EncryptedClientHelloConfigList 字段不可用,需要 Go 1.23+ 版本")
150+
}
151+
field1.Set(reflect.ValueOf(echList))
152+
153+
// 设置 EncryptedClientHelloRejectionVerify(必需)
154+
field2 := configValue.FieldByName("EncryptedClientHelloRejectionVerify")
155+
if !field2.IsValid() || !field2.CanSet() {
156+
return fmt.Errorf("EncryptedClientHelloRejectionVerify 字段不可用,需要 Go 1.23+ 版本")
157+
}
158+
rejectionFunc := func(cs tls.ConnectionState) error {
159+
return errors.New("服务器拒绝 ECH")
160+
}
161+
field2.Set(reflect.ValueOf(rejectionFunc))
162+
163+
return nil
131164
}
132165

133166
// queryHTTPSRecord 通过 DoH 查询 HTTPS 记录
@@ -145,7 +178,7 @@ func queryDoH(domain, dohURL string) (string, error) {
145178
if err != nil {
146179
return "", fmt.Errorf("无效的 DoH URL: %v", err)
147180
}
148-
181+
149182
dnsQuery := buildDNSQuery(domain, typeHTTPS)
150183
dnsBase64 := base64.RawURLEncoding.EncodeToString(dnsQuery)
151184

0 commit comments

Comments
 (0)