github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/utils/netutil/network_darwin.go (about) 1 /* 2 * Copyright (C) 2020 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 netutil 19 20 import ( 21 "fmt" 22 "net" 23 "os/exec" 24 25 "github.com/mysteriumnetwork/node/utils/cmdutil" 26 ) 27 28 func assignIP(iface string, subnet net.IPNet) error { 29 if err := cmdutil.SudoExec("ifconfig", iface, subnet.String(), peerIP(subnet).String()); err != nil { 30 return err 31 } 32 33 if err := cmdutil.SudoExec("ifconfig", iface, "inet6", "100::2"); err != nil { 34 return err 35 } 36 37 return nil 38 } 39 40 func excludeRoute(ip, gw net.IP) error { 41 return cmdutil.SudoExec("route", "add", "-host", ip.String(), gw.String()) 42 } 43 44 func deleteRoute(ip, gw string) error { 45 return cmdutil.SudoExec("route", "delete", ip, gw) 46 } 47 48 func addDefaultRoute(iface string) error { 49 if err := cmdutil.SudoExec("route", "add", "-net", "0.0.0.0/1", "-interface", iface); err != nil { 50 return err 51 } 52 53 if err := cmdutil.SudoExec("route", "add", "-net", "128.0.0.0/1", "-interface", iface); err != nil { 54 return err 55 } 56 57 if err := cmdutil.SudoExec("route", "add", "-inet6", "::/1", fmt.Sprintf("100::1%%%s", iface)); err != nil { 58 return err 59 } 60 61 if err := cmdutil.SudoExec("route", "add", "-inet6", "8000::/1", fmt.Sprintf("100::1%%%s", iface)); err != nil { 62 return err 63 } 64 65 return nil 66 } 67 68 func peerIP(subnet net.IPNet) net.IP { 69 lastOctetID := len(subnet.IP) - 1 70 if subnet.IP[lastOctetID] == byte(1) { 71 subnet.IP[lastOctetID] = byte(2) 72 } else { 73 subnet.IP[lastOctetID] = byte(1) 74 } 75 return subnet.IP 76 } 77 78 func logNetworkStats() { 79 for _, args := range [][]string{{"ifconfig", "-a"}, {"netstat", "-rn"}, {"pfctl", "-s", "all"}} { 80 out, err := exec.Command("sudo", args...).CombinedOutput() 81 logOutputToTrace(out, err, args...) 82 } 83 }