github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/openvpn/service/options.go (about)

     1  /*
     2   * Copyright (C) 2018 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 service
    19  
    20  import (
    21  	"encoding/json"
    22  
    23  	"github.com/mysteriumnetwork/node/config"
    24  	"github.com/mysteriumnetwork/node/core/service"
    25  	"github.com/rs/zerolog/log"
    26  )
    27  
    28  // Options describes options which are required to start Openvpn service
    29  type Options struct {
    30  	Protocol string `json:"protocol"`
    31  	Port     int    `json:"port"`
    32  	Subnet   string `json:"subnet"`
    33  	Netmask  string `json:"netmask"`
    34  }
    35  
    36  // GetOptions returns effective OpenVPN service options from application configuration.
    37  func GetOptions() Options {
    38  	return Options{
    39  		Protocol: config.GetString(config.FlagOpenvpnProtocol),
    40  		Port:     config.GetInt(config.FlagOpenvpnPort),
    41  		Subnet:   config.GetString(config.FlagOpenvpnSubnet),
    42  		Netmask:  config.GetString(config.FlagOpenvpnNetmask),
    43  	}
    44  }
    45  
    46  // ParseJSONOptions function fills in OpenVPN options from JSON request, falling back to configured options for
    47  // missing values
    48  func ParseJSONOptions(request *json.RawMessage) (service.Options, error) {
    49  	var requestOptions = GetOptions()
    50  	if request == nil {
    51  		return requestOptions, nil
    52  	}
    53  	err := json.Unmarshal(*request, &requestOptions)
    54  	if err != nil {
    55  		log.Warn().Err(err).Msg("Failed to parse options from request, using effective options")
    56  		return &Options{}, err
    57  	}
    58  	return requestOptions, nil
    59  }