github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/services/wireguard/service/options.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 service
    19  
    20  import (
    21  	"encoding/json"
    22  	"net"
    23  
    24  	"github.com/rs/zerolog/log"
    25  
    26  	"github.com/mysteriumnetwork/node/config"
    27  	"github.com/mysteriumnetwork/node/core/service"
    28  )
    29  
    30  // Options describes options which are required to start Wireguard service.
    31  type Options struct {
    32  	Subnet net.IPNet
    33  }
    34  
    35  // DefaultOptions is a wireguard service configuration that will be used if no options provided.
    36  var DefaultOptions = Options{
    37  	Subnet: net.IPNet{
    38  		IP:   net.ParseIP("10.182.0.0").To4(),
    39  		Mask: net.IPv4Mask(255, 255, 0, 0),
    40  	},
    41  }
    42  
    43  // GetOptions returns effective Wireguard service options from application configuration.
    44  func GetOptions() Options {
    45  	_, ipnet, err := net.ParseCIDR(config.GetString(config.FlagWireguardListenSubnet))
    46  	if err != nil {
    47  		log.Warn().Err(err).Msg("Failed to parse subnet option, using default value")
    48  		ipnet = &DefaultOptions.Subnet
    49  	}
    50  
    51  	return Options{
    52  		Subnet: *ipnet,
    53  	}
    54  }
    55  
    56  // ParseJSONOptions function fills in Wireguard options from JSON request
    57  func ParseJSONOptions(request *json.RawMessage) (service.Options, error) {
    58  	requestOptions := GetOptions()
    59  	if request == nil {
    60  		return requestOptions, nil
    61  	}
    62  
    63  	opts := DefaultOptions
    64  	err := json.Unmarshal(*request, &opts)
    65  	return opts, err
    66  }
    67  
    68  // MarshalJSON implements json.Marshaler interface to provide human readable configuration.
    69  func (o Options) MarshalJSON() ([]byte, error) {
    70  	return json.Marshal(&struct {
    71  		Subnet string `json:"subnet"`
    72  	}{
    73  		Subnet: o.Subnet.String(),
    74  	})
    75  }
    76  
    77  // UnmarshalJSON implements json.Unmarshaler interface to receive human readable configuration.
    78  func (o *Options) UnmarshalJSON(data []byte) error {
    79  	var options struct {
    80  		Subnet string `json:"subnet"`
    81  	}
    82  
    83  	if err := json.Unmarshal(data, &options); err != nil {
    84  		return err
    85  	}
    86  
    87  	if len(options.Subnet) > 0 {
    88  		_, ipnet, err := net.ParseCIDR(options.Subnet)
    89  		if err != nil {
    90  			return err
    91  		}
    92  		o.Subnet = *ipnet
    93  	}
    94  
    95  	return nil
    96  }