github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/edge/pkg/edgehub/clients/quicclient/quicclient.go (about)

     1  package quicclient
     2  
     3  import (
     4  	"crypto/tls"
     5  	"crypto/x509"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"time"
    10  
    11  	"k8s.io/klog"
    12  
    13  	"github.com/kubeedge/beehive/pkg/core/model"
    14  	"github.com/kubeedge/viaduct/pkg/api"
    15  	qclient "github.com/kubeedge/viaduct/pkg/client"
    16  	"github.com/kubeedge/viaduct/pkg/conn"
    17  )
    18  
    19  // QuicClient a quic client
    20  type QuicClient struct {
    21  	config *QuicConfig
    22  	client conn.Connection
    23  }
    24  
    25  // QuicConfig config for quic
    26  type QuicConfig struct {
    27  	Addr             string
    28  	CaFilePath       string
    29  	CertFilePath     string
    30  	KeyFilePath      string
    31  	HandshakeTimeout time.Duration
    32  	ReadDeadline     time.Duration
    33  	WriteDeadline    time.Duration
    34  	NodeID           string
    35  	ProjectID        string
    36  }
    37  
    38  // NewQuicClient initializes a new quic client instance
    39  func NewQuicClient(conf *QuicConfig) *QuicClient {
    40  	return &QuicClient{config: conf}
    41  }
    42  
    43  // Init initializes quic client
    44  func (qcc *QuicClient) Init() error {
    45  	klog.Infof("Quic start to connect Access")
    46  	cert, err := tls.LoadX509KeyPair(qcc.config.CertFilePath, qcc.config.KeyFilePath)
    47  	if err != nil {
    48  		klog.Errorf("Failed to load x509 key pair: %v", err)
    49  		return fmt.Errorf("failed to load x509 key pair, error: %v", err)
    50  	}
    51  	caCrt, err := ioutil.ReadFile(qcc.config.CaFilePath)
    52  	if err != nil {
    53  		klog.Errorf("Failed to load ca file: %s", err.Error())
    54  		return fmt.Errorf("failed to load ca file: %s", err.Error())
    55  	}
    56  	pool := x509.NewCertPool()
    57  	pool.AppendCertsFromPEM(caCrt)
    58  
    59  	tlsConfig := &tls.Config{
    60  		RootCAs:            pool,
    61  		Certificates:       []tls.Certificate{cert},
    62  		InsecureSkipVerify: true,
    63  	}
    64  
    65  	option := qclient.Options{
    66  		HandshakeTimeout: qcc.config.HandshakeTimeout,
    67  		TLSConfig:        tlsConfig,
    68  		Type:             api.ProtocolTypeQuic,
    69  		Addr:             qcc.config.Addr,
    70  	}
    71  	exOpts := api.QuicClientOption{Header: make(http.Header)}
    72  	exOpts.Header.Set("node_id", qcc.config.NodeID)
    73  	exOpts.Header.Set("project_id", qcc.config.ProjectID)
    74  	client := qclient.NewQuicClient(option, exOpts)
    75  	connection, err := client.Connect()
    76  	if err != nil {
    77  		klog.Errorf("Init quic connection failed %s", err.Error())
    78  		return err
    79  	}
    80  	qcc.client = connection
    81  	klog.Infof("Quic connect to cloud access successful")
    82  
    83  	return nil
    84  }
    85  
    86  //Uninit closes the quic connection
    87  func (qcc *QuicClient) Uninit() {
    88  	qcc.client.Close()
    89  }
    90  
    91  //Send sends the message as JSON object through the connection
    92  func (qcc *QuicClient) Send(message model.Message) error {
    93  	return qcc.client.WriteMessageAsync(&message)
    94  }
    95  
    96  //Receive reads the binary message through the connection
    97  func (qcc *QuicClient) Receive() (model.Message, error) {
    98  	message := model.Message{}
    99  	qcc.client.ReadMessage(&message)
   100  	return message, nil
   101  }
   102  
   103  //Notify logs info
   104  func (qcc *QuicClient) Notify(authInfo map[string]string) {
   105  	klog.Infof("Don not care")
   106  }