github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/integration/nwo/operational_client.go (about)

     1  /*
     2  Copyright hechain All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package nwo
     8  
     9  import (
    10  	"crypto/tls"
    11  	"crypto/x509"
    12  	"io/ioutil"
    13  	"net/http"
    14  	"path/filepath"
    15  	"time"
    16  
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  func OrdererOperationalClients(n *Network, o *Orderer) (authClient, unauthClient *http.Client) {
    21  	return operationalClients(n, n.OrdererLocalTLSDir(o))
    22  }
    23  
    24  func PeerOperationalClients(n *Network, p *Peer) (authClient, unauthClient *http.Client) {
    25  	return operationalClients(n, n.PeerLocalTLSDir(p))
    26  }
    27  
    28  func operationalClients(n *Network, tlsDir string) (authClient, unauthClient *http.Client) {
    29  	fingerprint := "http::" + tlsDir
    30  	if d := n.throttleDuration(fingerprint); d > 0 {
    31  		time.Sleep(d)
    32  	}
    33  
    34  	clientCert, err := tls.LoadX509KeyPair(
    35  		filepath.Join(tlsDir, "server.crt"),
    36  		filepath.Join(tlsDir, "server.key"),
    37  	)
    38  	Expect(err).NotTo(HaveOccurred())
    39  
    40  	clientCertPool := x509.NewCertPool()
    41  	caCert, err := ioutil.ReadFile(filepath.Join(tlsDir, "ca.crt"))
    42  	Expect(err).NotTo(HaveOccurred())
    43  	clientCertPool.AppendCertsFromPEM(caCert)
    44  
    45  	authenticatedClient := &http.Client{
    46  		Transport: &http.Transport{
    47  			MaxIdleConnsPerHost: -1,
    48  			TLSClientConfig: &tls.Config{
    49  				Certificates: []tls.Certificate{clientCert},
    50  				RootCAs:      clientCertPool,
    51  			},
    52  		},
    53  	}
    54  	unauthenticatedClient := &http.Client{
    55  		Transport: &http.Transport{
    56  			MaxIdleConnsPerHost: -1,
    57  			TLSClientConfig:     &tls.Config{RootCAs: clientCertPool},
    58  		},
    59  	}
    60  
    61  	return authenticatedClient, unauthenticatedClient
    62  }