github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/options_type.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  	"encoding/json"
    22  
    23  	"github.com/pkg/errors"
    24  
    25  	"github.com/mysteriumnetwork/node/core/service"
    26  	"github.com/mysteriumnetwork/node/services/datatransfer"
    27  	"github.com/mysteriumnetwork/node/services/dvpn"
    28  	"github.com/mysteriumnetwork/node/services/noop"
    29  	"github.com/mysteriumnetwork/node/services/openvpn"
    30  	openvpn_service "github.com/mysteriumnetwork/node/services/openvpn/service"
    31  	"github.com/mysteriumnetwork/node/services/scraping"
    32  	"github.com/mysteriumnetwork/node/services/wireguard"
    33  	wireguard_service "github.com/mysteriumnetwork/node/services/wireguard/service"
    34  )
    35  
    36  // JSONParsersByType parsers of service specific options from JSON request.
    37  var JSONParsersByType = map[string]ServiceOptionsParser{
    38  	noop.ServiceType:         noop.ParseJSONOptions,
    39  	openvpn.ServiceType:      openvpn_service.ParseJSONOptions,
    40  	wireguard.ServiceType:    wireguard_service.ParseJSONOptions,
    41  	scraping.ServiceType:     wireguard_service.ParseJSONOptions,
    42  	datatransfer.ServiceType: wireguard_service.ParseJSONOptions,
    43  	dvpn.ServiceType:         wireguard_service.ParseJSONOptions,
    44  }
    45  
    46  // ServiceOptionsParser parses request to service specific options
    47  type ServiceOptionsParser func(*json.RawMessage) (service.Options, error)
    48  
    49  // Types returns all possible service types.
    50  func Types() []string {
    51  	return []string{
    52  		openvpn.ServiceType,
    53  		wireguard.ServiceType,
    54  		noop.ServiceType,
    55  		scraping.ServiceType,
    56  		datatransfer.ServiceType,
    57  		dvpn.ServiceType,
    58  	}
    59  }
    60  
    61  // TypeConfiguredOptions returns specific service options.
    62  func TypeConfiguredOptions(serviceType string) (service.Options, error) {
    63  	switch serviceType {
    64  	case openvpn.ServiceType:
    65  		return openvpn_service.GetOptions(), nil
    66  	case wireguard.ServiceType:
    67  		return wireguard_service.GetOptions(), nil
    68  	case noop.ServiceType:
    69  		return noop.GetOptions(), nil
    70  	case scraping.ServiceType:
    71  		return wireguard_service.GetOptions(), nil
    72  	case datatransfer.ServiceType:
    73  		return wireguard_service.GetOptions(), nil
    74  	case dvpn.ServiceType:
    75  		return wireguard_service.GetOptions(), nil
    76  	default:
    77  		return nil, errors.Errorf("unknown service type: %q", serviceType)
    78  	}
    79  }
    80  
    81  // TypeJSONParser get parser to parse service specific options from JSON request.
    82  func TypeJSONParser(serviceType string) (ServiceOptionsParser, error) {
    83  	parser, exist := JSONParsersByType[serviceType]
    84  	if !exist {
    85  		return nil, errors.Errorf("unknown service type: %q", serviceType)
    86  	}
    87  	return parser, nil
    88  }
    89  
    90  // IsTypeValid returns true if a given string is valid service type.
    91  func IsTypeValid(s string) bool {
    92  	for _, v := range Types() {
    93  		if v == s {
    94  			return true
    95  		}
    96  	}
    97  	return false
    98  }