github.com/jingruilea/kubeedge@v1.2.0-beta.0.0.20200410162146-4bb8902b3879/cloud/pkg/cloudhub/servers/server.go (about)

     1  package servers
     2  
     3  import (
     4  	"crypto/tls"
     5  	"crypto/x509"
     6  	"fmt"
     7  
     8  	"k8s.io/klog"
     9  
    10  	"github.com/kubeedge/kubeedge/cloud/pkg/cloudhub/channelq"
    11  	hubconfig "github.com/kubeedge/kubeedge/cloud/pkg/cloudhub/config"
    12  	"github.com/kubeedge/kubeedge/cloud/pkg/cloudhub/handler"
    13  	"github.com/kubeedge/viaduct/pkg/api"
    14  	"github.com/kubeedge/viaduct/pkg/server"
    15  )
    16  
    17  // StartCloudHub starts the cloud hub service
    18  func StartCloudHub(messageq *channelq.ChannelMessageQueue) {
    19  	handler.InitHandler(messageq)
    20  	// start websocket server
    21  	if hubconfig.Config.WebSocket.Enable {
    22  		go startWebsocketServer()
    23  	}
    24  	// start quic server
    25  	if hubconfig.Config.Quic.Enable {
    26  		go startQuicServer()
    27  	}
    28  }
    29  
    30  func createTLSConfig(ca, cert, key []byte) tls.Config {
    31  	// init certificate
    32  	pool := x509.NewCertPool()
    33  	ok := pool.AppendCertsFromPEM(ca)
    34  	if !ok {
    35  		panic(fmt.Errorf("fail to load ca content"))
    36  	}
    37  	certificate, err := tls.X509KeyPair(cert, key)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  	return tls.Config{
    42  		ClientCAs:    pool,
    43  		ClientAuth:   tls.RequireAndVerifyClientCert,
    44  		Certificates: []tls.Certificate{certificate},
    45  		MinVersion:   tls.VersionTLS12,
    46  		CipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256},
    47  	}
    48  }
    49  
    50  func startWebsocketServer() {
    51  	tlsConfig := createTLSConfig(hubconfig.Config.Ca, hubconfig.Config.Cert, hubconfig.Config.Key)
    52  	svc := server.Server{
    53  		Type:       api.ProtocolTypeWS,
    54  		TLSConfig:  &tlsConfig,
    55  		AutoRoute:  true,
    56  		ConnNotify: handler.CloudhubHandler.OnRegister,
    57  		Addr:       fmt.Sprintf("%s:%d", hubconfig.Config.WebSocket.Address, hubconfig.Config.WebSocket.Port),
    58  		ExOpts:     api.WSServerOption{Path: "/"},
    59  	}
    60  	klog.Infof("Startting cloudhub %s server", api.ProtocolTypeWS)
    61  	svc.ListenAndServeTLS("", "")
    62  }
    63  
    64  func startQuicServer() {
    65  	tlsConfig := createTLSConfig(hubconfig.Config.Ca, hubconfig.Config.Cert, hubconfig.Config.Key)
    66  	svc := server.Server{
    67  		Type:       api.ProtocolTypeQuic,
    68  		TLSConfig:  &tlsConfig,
    69  		AutoRoute:  true,
    70  		ConnNotify: handler.CloudhubHandler.OnRegister,
    71  		Addr:       fmt.Sprintf("%s:%d", hubconfig.Config.Quic.Address, hubconfig.Config.Quic.Port),
    72  		ExOpts:     api.QuicServerOption{MaxIncomingStreams: int(hubconfig.Config.Quic.MaxIncomingStreams)},
    73  	}
    74  
    75  	klog.Infof("Startting cloudhub %s server", api.ProtocolTypeQuic)
    76  	svc.ListenAndServeTLS("", "")
    77  }