Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否添加一个直接设置ClientHelloSpec的方法? #390

Open
1992w opened this issue Sep 30, 2024 · 0 comments
Open

能否添加一个直接设置ClientHelloSpec的方法? #390

1992w opened this issue Sep 30, 2024 · 0 comments

Comments

@1992w
Copy link

1992w commented Sep 30, 2024

我有自定义ClientHelloSpec的需求,用SetTLSHandshake能解决,但能否新增一个设置ClientHelloSpec的方法,类似于下面这样:

utls.HelloCustom 支持自定义ClientHelloSpec

func (c *Client) SetTLSClientHelloSpec(spec *utls.ClientHelloSpec) *Client {
	fn := func(ctx context.Context, addr string, plainConn net.Conn) (conn net.Conn, tlsState *tls.ConnectionState, err error) {
		colonPos := strings.LastIndex(addr, ":")
		if colonPos == -1 {
			colonPos = len(addr)
		}
		hostname := addr[:colonPos]
		tlsConfig := c.GetTLSClientConfig()
		utlsConfig := &utls.Config{
			ServerName:                  hostname,
			Rand:                        tlsConfig.Rand,
			Time:                        tlsConfig.Time,
			RootCAs:                     tlsConfig.RootCAs,
			NextProtos:                  tlsConfig.NextProtos,
			ClientCAs:                   tlsConfig.ClientCAs,
			InsecureSkipVerify:          tlsConfig.InsecureSkipVerify,
			CipherSuites:                tlsConfig.CipherSuites,
			SessionTicketsDisabled:      tlsConfig.SessionTicketsDisabled,
			MinVersion:                  tlsConfig.MinVersion,
			MaxVersion:                  tlsConfig.MaxVersion,
			DynamicRecordSizingDisabled: tlsConfig.DynamicRecordSizingDisabled,
			KeyLogWriter:                tlsConfig.KeyLogWriter,
		}
		uconn := &uTLSConn{utls.UClient(plainConn, utlsConfig, utls.HelloCustom)}
		err = uconn.ApplyPreset(spec)
		if err != nil {
			return
		}
		err = uconn.BuildHandshakeState()
		if err != nil {
			return
		}
		err = uconn.HandshakeContext(ctx)
		if err != nil {
			return
		}
		cs := uconn.Conn.ConnectionState()
		conn = uconn
		tlsState = &tls.ConnectionState{
			Version:                     cs.Version,
			HandshakeComplete:           cs.HandshakeComplete,
			DidResume:                   cs.DidResume,
			CipherSuite:                 cs.CipherSuite,
			NegotiatedProtocol:          cs.NegotiatedProtocol,
			NegotiatedProtocolIsMutual:  cs.NegotiatedProtocolIsMutual,
			ServerName:                  cs.ServerName,
			PeerCertificates:            cs.PeerCertificates,
			VerifiedChains:              cs.VerifiedChains,
			SignedCertificateTimestamps: cs.SignedCertificateTimestamps,
			OCSPResponse:                cs.OCSPResponse,
			TLSUnique:                   cs.TLSUnique,
		}
		return
	}
	c.Transport.SetTLSHandshake(fn)
	return c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant