github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/router/network/network_remote.go (about)

     1  /*
     2   * Copyright (C) 2021 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 network
    19  
    20  import (
    21  	"fmt"
    22  	"net"
    23  
    24  	"github.com/mysteriumnetwork/node/supervisor/client"
    25  )
    26  
    27  // RoutingTableRemote implements a set of commands for supervisor deamon for creating,
    28  // deleting and observe routing tables rules for a different needs.
    29  type RoutingTableRemote struct{}
    30  
    31  // DiscoverGateway returns system default gateway.
    32  func (t *RoutingTableRemote) DiscoverGateway() (net.IP, error) {
    33  	gw, err := client.Command("discover-gateway")
    34  	if err != nil {
    35  		return nil, fmt.Errorf("failed to discover gateway via supervisor: %w", err)
    36  	}
    37  
    38  	return net.ParseIP(gw), nil
    39  }
    40  
    41  // ExcludeRule adds a single IP address to be excluded from the main tunnelled traffic.
    42  // Traffic sent to the IP address will be directed to the system default gaitway
    43  // instead of tunnel.
    44  func (t *RoutingTableRemote) ExcludeRule(ip, gw net.IP) error {
    45  	_, err := client.Command("exclude-route", "-ip", ip.String(), "-gw", gw.String())
    46  	if err != nil {
    47  		return fmt.Errorf("failed to exclude route via supervisor: %w", err)
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  // DeleteRule removes excluded routing table rule to return it back to routing
    54  // thought the tunnel.
    55  func (t *RoutingTableRemote) DeleteRule(ip, gw net.IP) error {
    56  	_, err := client.Command("delete-route", "-ip", ip.String(), "-gw", gw.String())
    57  	if err != nil {
    58  		return fmt.Errorf("failed to delete route via supervisor: %w", err)
    59  	}
    60  
    61  	return nil
    62  }