MCPcopy Create free account
hub / github.com/DNAProject/DNA / TLSDial

Function TLSDial

p2pserver/net/netserver/net_utils.go:71–105  ·  view source on GitHub ↗

TLSDial return net.Conn with TLS

(nodeAddr string)

Source from the content-addressed store, hash-verified

69
70//TLSDial return net.Conn with TLS
71func TLSDial(nodeAddr string) (net.Conn, error) {
72 CertPath := config.DefConfig.P2PNode.CertPath
73 KeyPath := config.DefConfig.P2PNode.KeyPath
74 CAPath := config.DefConfig.P2PNode.CAPath
75
76 clientCertPool := x509.NewCertPool()
77
78 cacert, err := ioutil.ReadFile(CAPath)
79 if err != nil {
80 log.Error("[p2p]load CA file fail", err)
81 return nil, err
82 }
83 cert, err := tls.LoadX509KeyPair(CertPath, KeyPath)
84 if err != nil {
85 return nil, err
86 }
87
88 ret := clientCertPool.AppendCertsFromPEM(cacert)
89 if !ret {
90 return nil, errors.New("[p2p]failed to parse root certificate")
91 }
92
93 conf := &tls.Config{
94 RootCAs: clientCertPool,
95 Certificates: []tls.Certificate{cert},
96 }
97
98 var dialer net.Dialer
99 dialer.Timeout = time.Second * common.DIAL_TIMEOUT
100 conn, err := tls.DialWithDialer(&dialer, "tcp", nodeAddr, conf)
101 if err != nil {
102 return nil, err
103 }
104 return conn, nil
105}
106
107//initNonTlsListen return net.Listener with nonTls mode
108func initNonTlsListen(port uint16) (net.Listener, error) {

Callers 1

ConnectMethod · 0.85

Calls 1

ErrorFunction · 0.92

Tested by

no test coverage detected