mirror of
https://github.com/zrepl/zrepl.git
synced 2024-11-26 02:14:44 +01:00
c69ebd3806
cmd subdir does not build on purpose, it's only left in tree to grab old code and move it to github.com/zrepl/zrepl/daemon
44 lines
989 B
Go
44 lines
989 B
Go
package connecter
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"github.com/pkg/errors"
|
|
"github.com/zrepl/zrepl/config"
|
|
"github.com/zrepl/zrepl/tlsconf"
|
|
"net"
|
|
)
|
|
|
|
type TLSConnecter struct {
|
|
Address string
|
|
dialer net.Dialer
|
|
tlsConfig *tls.Config
|
|
}
|
|
|
|
func TLSConnecterFromConfig(in *config.TLSConnect) (*TLSConnecter, error) {
|
|
dialer := net.Dialer{
|
|
Timeout: in.DialTimeout,
|
|
}
|
|
|
|
ca, err := tlsconf.ParseCAFile(in.Ca)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "cannot parse ca file")
|
|
}
|
|
|
|
cert, err := tls.LoadX509KeyPair(in.Cert, in.Key)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "cannot parse cert/key pair")
|
|
}
|
|
|
|
tlsConfig, err := tlsconf.ClientAuthClient(in.ServerCN, ca, cert)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "cannot build tls config")
|
|
}
|
|
|
|
return &TLSConnecter{in.Address, dialer, tlsConfig}, nil
|
|
}
|
|
|
|
func (c *TLSConnecter) Connect(dialCtx context.Context) (conn net.Conn, err error) {
|
|
return tls.DialWithDialer(&c.dialer, "tcp", c.Address, c.tlsConfig)
|
|
}
|