github.com/vmware/transport-go@v1.3.4/bridge/broker_connector_config.go (about)

     1  // Copyright 2019-2020 VMware, Inc.
     2  // SPDX-License-Identifier: BSD-2-Clause
     3  
     4  package bridge
     5  
     6  import (
     7  	"crypto/tls"
     8  	"net/http"
     9  	"time"
    10  )
    11  
    12  type WebSocketConfig struct {
    13  	WSPath    string      // if UseWS is true, set this to your websocket path (e.g. '/fabric')
    14  	UseTLS    bool        // use TLS encryption with WebSocket connection
    15  	TLSConfig *tls.Config // TLS config for WebSocket connection
    16  	CertFile  string      // X509 certificate for TLS
    17  	KeyFile   string      // matching key file for the X509 certificate
    18  }
    19  
    20  // BrokerConnectorConfig is a configuration used when connecting to a message broker
    21  type BrokerConnectorConfig struct {
    22  	Username        string
    23  	Password        string
    24  	ServerAddr      string
    25  	UseWS           bool             // use WebSocket instead of TCP
    26  	WebSocketConfig *WebSocketConfig // WebSocket configuration for when UseWS is true
    27  	HostHeader      string
    28  	HeartBeatOut    time.Duration     // outbound heartbeat interval (from client to server)
    29  	HeartBeatIn     time.Duration     // inbound heartbeat interval (from server to client)
    30  	STOMPHeader     map[string]string // additional STOMP headers for handshake
    31  	HttpHeader      http.Header       // additional HTTP headers for WebSocket Upgrade
    32  }
    33  
    34  // LoadX509KeyPairFromFiles loads from paths to x509 cert and its matching key files and initializes
    35  // the Certificates field of the TLS config instance with their contents, only if both Certificates is
    36  // an empty slice and GetCertificate is nil
    37  func (b *WebSocketConfig) LoadX509KeyPairFromFiles(certFile, keyFile string) error {
    38  	var err error
    39  	config := b.TLSConfig
    40  	configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil
    41  	if !configHasCert || certFile != "" || keyFile != "" {
    42  		config.Certificates = make([]tls.Certificate, 1)
    43  		config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
    44  		if err != nil {
    45  			return err
    46  		}
    47  	}
    48  	return err
    49  }