github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/ui/discovery/discovery.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package discovery
    19  
    20  import (
    21  	"github.com/rs/zerolog/log"
    22  
    23  	"github.com/mysteriumnetwork/node/config"
    24  	"github.com/mysteriumnetwork/node/requests"
    25  )
    26  
    27  // LANDiscovery provides local network discovery service for Mysterium Node UI.
    28  type LANDiscovery interface {
    29  	Start() error
    30  	Stop() error
    31  }
    32  
    33  type multiDiscovery struct {
    34  	ssdp    LANDiscovery
    35  	bonjour LANDiscovery
    36  }
    37  
    38  // NewLANDiscoveryService creates SSDP and Bonjour services for LAN discovery.
    39  func NewLANDiscoveryService(uiPort int, httpClient *requests.HTTPClient) LANDiscovery {
    40  	if !config.GetBool(config.FlagLocalServiceDiscovery) {
    41  		return &noopLANDiscovery{}
    42  	}
    43  	return &multiDiscovery{
    44  		ssdp:    newSSDPServer(uiPort, httpClient),
    45  		bonjour: newBonjourServer(uiPort),
    46  	}
    47  }
    48  
    49  func (md *multiDiscovery) Start() error {
    50  	if err := md.bonjour.Start(); err != nil {
    51  		return err
    52  	}
    53  
    54  	return md.ssdp.Start()
    55  }
    56  
    57  func (md *multiDiscovery) Stop() error {
    58  	// bonjour Stop does not return any error, nothing to check
    59  	_ = md.bonjour.Stop()
    60  	return md.ssdp.Stop()
    61  }
    62  
    63  type noopLANDiscovery struct{}
    64  
    65  func (nd *noopLANDiscovery) Start() error {
    66  	log.Info().Msgf("LAN discovery disabled. Starting noop local service discovery.")
    67  	return nil
    68  }
    69  
    70  func (nd *noopLANDiscovery) Stop() error {
    71  	log.Info().Msgf("LAN discovery disabled. Stopping noop local service discovery.")
    72  	return nil
    73  }