github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/broker/network/network_gateways.go (about)

     1  // Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package network
     6  
     7  import (
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/choria-io/go-choria/srvcache"
    12  	gnatsd "github.com/nats-io/nats-server/v2/server"
    13  )
    14  
    15  func (s *Server) setupGateways() (err error) {
    16  	if s.config.Choria.NetworkGatewayPort == 0 || len(s.config.Choria.NetworkGatewayRemotes) == 0 {
    17  		return nil
    18  	}
    19  
    20  	if s.config.Choria.NetworkGatewayName == "" {
    21  		return fmt.Errorf("network Gateways require a name")
    22  	}
    23  
    24  	if len(s.config.Choria.NetworkGatewayRemotes) == 0 {
    25  		return fmt.Errorf("network Gateways require at least one remote")
    26  	}
    27  
    28  	s.log.Infof("Starting Broker Gateway %s listening on %s:%d", s.config.Choria.NetworkGatewayName, s.config.Choria.NetworkListenAddress, s.config.Choria.NetworkGatewayPort)
    29  
    30  	s.opts.Gateway.Host = s.config.Choria.NetworkListenAddress
    31  	s.opts.Gateway.Port = s.config.Choria.NetworkGatewayPort
    32  	s.opts.Gateway.Name = s.config.Choria.NetworkGatewayName
    33  	s.opts.Gateway.RejectUnknown = true
    34  	s.opts.Gateway.TLSTimeout = s.opts.TLSTimeout
    35  
    36  	for _, r := range s.config.Choria.NetworkGatewayRemotes {
    37  		s.log.Infof("Adding gateway %s", r)
    38  
    39  		remote := &gnatsd.RemoteGatewayOpts{Name: r}
    40  
    41  		urlStr := s.extractKeyedConfigString("gateway_remote", r, "urls", "")
    42  		if urlStr == "" {
    43  			s.log.Errorf("Gateway %s has no remote url, ignoring", r)
    44  			continue
    45  		}
    46  
    47  		urlsStr := []string{}
    48  		for _, u := range strings.Split(urlStr, ",") {
    49  			urlsStr = append(urlsStr, strings.TrimSpace(u))
    50  		}
    51  
    52  		urlSrvs, err := srvcache.StringHostsToServers(urlsStr, "nats")
    53  		if err != nil {
    54  			s.log.Errorf("Could not parse URL for gateway remote %s urls '%s': %s", r, urlStr, err)
    55  			continue
    56  		}
    57  
    58  		if urlSrvs.Count() == 0 {
    59  			s.log.Errorf("Could not parse URL for gateway remote %s url '%s': needs at least 1 url", r, urlStr)
    60  			continue
    61  		}
    62  
    63  		urlU, err := urlSrvs.URLs()
    64  		if err != nil {
    65  			s.log.Errorf("Could not parse URL for gateway remote %s url '%s': %s", r, urlStr, err)
    66  			continue
    67  		}
    68  
    69  		remote.URLs = urlU
    70  
    71  		remote.TLSTimeout = s.opts.Gateway.TLSTimeout
    72  
    73  		if s.IsTLS() {
    74  			remote.TLSConfig = s.opts.Gateway.TLSConfig
    75  		}
    76  
    77  		tlsc, disable, err := s.extractTLSCFromKeyedConfig("gateway_remote", r)
    78  		if err != nil {
    79  			s.log.Errorf("Could not configure custom TLS for remote Gateway %s: %s", r, err)
    80  			continue
    81  		}
    82  
    83  		if disable {
    84  			s.log.Warnf("Disabling TLS for remote Gateway %s", r)
    85  			remote.TLSConfig = nil
    86  		} else if tlsc != nil {
    87  			s.log.Infof("Using custom TLS config for remote Gateway %s", r)
    88  			remote.TLSConfig = tlsc
    89  		}
    90  
    91  		s.opts.Gateway.Gateways = append(s.opts.Gateway.Gateways, remote)
    92  
    93  		s.log.Infof("Added remote Gateway %s with servers %s", r, strings.Join(urlSrvs.Strings(), ", "))
    94  	}
    95  
    96  	return nil
    97  }