fasthttp icon indicating copy to clipboard operation
fasthttp copied to clipboard

Can the client support a custom TLS package?

Open zhangyongding opened this issue 2 years ago • 4 comments

Because crypto/tls does not support some required cipher suites, so it needs to be customized

zhangyongding avatar Jul 25 '22 02:07 zhangyongding

I haven't tried it but I think you can use any tls package by using Client.Dial to supply your own Dial function using your own tls package. As long as that returns anything implementing net.Conn it doesn't matter which tls library is used.

erikdubbelboer avatar Jul 29 '22 14:07 erikdubbelboer

There is no way to use custom tls config

zhangyongding avatar Aug 01 '22 02:08 zhangyongding

I guess there is no way for it to know that it shouldn't wrap the conn in a new tls.Conn here: https://github.com/valyala/fasthttp/blob/a5f448fc970972ab47113971d898a22fb28fef52/client.go#L1983-L1989

I would be open to a pull that somehow makes this work. Either by seeing some property on conn that tells it it doesn't have to wrap it, or some other way.

erikdubbelboer avatar Aug 01 '22 09:08 erikdubbelboer

Thank you. Looking forward to your solution

zhangyongding avatar Aug 01 '22 09:08 zhangyongding

PipelineClient and HostClient cannot use custom TLS Dial. When a custom Dial is used, the TLS handshake is not checked. expample code:

func dialAddr(addr string, dial DialFunc, dialDualStack, isTLS bool, tlsConfig *tls.Config, timeout time.Duration) (net.Conn, error) {
	deadline := time.Now().Add(timeout)
	var customDial bool
	if dial == nil {
		if dialDualStack {
			dial = DialDualStack
		} else {
			dial = Dial
		}
		addr = addMissingPort(addr, isTLS)
	}else{
		customDial = true
	}
	conn, err := dial(addr)
	if err != nil {
		return nil, err
	}
	if conn == nil {
		panic("BUG: DialFunc returned (nil, nil)")
	}
	if !customDial{
		_, isTLSAlready := conn.(*tls.Conn)
		if isTLS && !isTLSAlready {
			if timeout == 0 {
				return tls.Client(conn, tlsConfig), nil
			}
			return tlsClientHandshake(conn, tlsConfig, deadline)
		}
	}
	return conn, nil
}

zhangyongding avatar Sep 03 '22 07:09 zhangyongding

Should be fixed in https://github.com/valyala/fasthttp/commit/2f1e949d91d0ba1817afd80d7c981fafe7154774 will tag a release next week.

erikdubbelboer avatar Sep 03 '22 09:09 erikdubbelboer