github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/nat/service_ipforward.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 nat
    19  
    20  import (
    21  	"strings"
    22  
    23  	"github.com/rs/zerolog/log"
    24  )
    25  
    26  type serviceIPForward struct {
    27  	CommandEnable  []string
    28  	CommandDisable []string
    29  	CommandRead    []string
    30  	CommandFactory CommandFactory
    31  	forward        bool
    32  }
    33  
    34  // CommandFactory is responsible for creating new instances of command
    35  type CommandFactory func(name string, arg ...string) Command
    36  
    37  // Command allows us to run commands
    38  type Command interface {
    39  	CombinedOutput() ([]byte, error)
    40  	Output() ([]byte, error)
    41  }
    42  
    43  func (service *serviceIPForward) Enable() error {
    44  	if service.Enabled() {
    45  		service.forward = true
    46  		log.Info().Msg("IP forwarding already enabled")
    47  		return nil
    48  	}
    49  
    50  	if output, err := service.CommandFactory(service.CommandEnable[0], service.CommandEnable[1:]...).CombinedOutput(); err != nil {
    51  		log.Warn().Err(err).Msgf("Failed to enable IP forwarding: %v Cmd output: %v", service.CommandEnable[1:], string(output))
    52  		return err
    53  	}
    54  
    55  	log.Info().Msg("IP forwarding enabled")
    56  	return nil
    57  }
    58  
    59  func (service *serviceIPForward) Disable() {
    60  	if service.forward {
    61  		return
    62  	}
    63  
    64  	if output, err := service.CommandFactory(service.CommandDisable[0], service.CommandDisable[1:]...).CombinedOutput(); err != nil {
    65  		log.Warn().Err(err).Msgf("Failed to disable IP forwarding: %v Cmd output: %v", service.CommandDisable[1:], string(output))
    66  	}
    67  
    68  	log.Info().Msg("IP forwarding disabled")
    69  }
    70  
    71  func (service *serviceIPForward) Enabled() bool {
    72  	output, err := service.CommandFactory(service.CommandRead[0], service.CommandRead[1:]...).Output()
    73  	if err != nil {
    74  		log.Warn().Err(err).Msgf("Failed to check IP forwarding status: %v Cmd output: %v", service.CommandRead[1:], string(output))
    75  	}
    76  
    77  	return strings.TrimSpace(string(output)) == "1"
    78  }