github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/options_start.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 services
    19  
    20  import (
    21  	"strings"
    22  
    23  	"github.com/mysteriumnetwork/node/config"
    24  	"github.com/mysteriumnetwork/node/core/service"
    25  	"github.com/mysteriumnetwork/node/services/datatransfer"
    26  	"github.com/mysteriumnetwork/node/services/dvpn"
    27  	"github.com/mysteriumnetwork/node/services/noop"
    28  	"github.com/mysteriumnetwork/node/services/openvpn"
    29  	"github.com/mysteriumnetwork/node/services/scraping"
    30  	"github.com/mysteriumnetwork/node/services/wireguard"
    31  	"github.com/urfave/cli/v2"
    32  )
    33  
    34  // GetStartOptions returns options to use for starting a service.
    35  func GetStartOptions(serviceType string) (opts StartOptions, err error) {
    36  	opts.TypeOptions, err = TypeConfiguredOptions(serviceType)
    37  	if err != nil {
    38  		return StartOptions{
    39  			AccessPolicyList: nil,
    40  			TypeOptions:      nil,
    41  		}, err
    42  	}
    43  
    44  	switch serviceType {
    45  	case openvpn.ServiceType:
    46  		opts.AccessPolicyList = getPolicies(config.FlagOpenVPNAccessPolicies, config.FlagAccessPolicyList)
    47  	case wireguard.ServiceType:
    48  		opts.AccessPolicyList = getPolicies(config.FlagWireguardAccessPolicies, config.FlagAccessPolicyList)
    49  	case noop.ServiceType:
    50  		opts.AccessPolicyList = getPolicies(config.FlagNoopAccessPolicies, config.FlagAccessPolicyList)
    51  	case scraping.ServiceType:
    52  		opts.AccessPolicyList = []string{"mysterium"}
    53  	case datatransfer.ServiceType:
    54  		opts.AccessPolicyList = []string{"mysterium"}
    55  	case dvpn.ServiceType:
    56  		opts.AccessPolicyList = []string{"mysterium"}
    57  	}
    58  	return opts, nil
    59  }
    60  
    61  func getPolicies(flag cli.StringFlag, fallback cli.StringFlag) []string {
    62  	policiesStr := config.GetString(flag)
    63  	if policiesStr == "" {
    64  		policiesStr = config.GetString(fallback)
    65  	}
    66  
    67  	var policies []string
    68  	if len(policiesStr) > 0 {
    69  		policies = strings.Split(policiesStr, ",")
    70  	} else {
    71  		policies = []string{}
    72  	}
    73  	return policies
    74  }
    75  
    76  // StartOptions describes options shared among multiple services
    77  type StartOptions struct {
    78  	AccessPolicyList []string
    79  	TypeOptions      service.Options
    80  }