github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/utils/netutil/network_windows.go (about) 1 //go:build windows 2 3 /* 4 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package netutil 21 22 import ( 23 "fmt" 24 "net" 25 "os/exec" 26 "strconv" 27 28 "github.com/pkg/errors" 29 "github.com/rs/zerolog/log" 30 ) 31 32 func assignIP(iface string, subnet net.IPNet) error { 33 out, err := exec.Command("powershell", "-Command", "netsh interface ip set address name=\""+iface+"\" source=static "+subnet.String()).CombinedOutput() 34 return errors.Wrap(err, string(out)) 35 } 36 37 func excludeRoute(ip, gw net.IP) error { 38 out, err := exec.Command("powershell", "-Command", "route add "+ip.String()+"/32 "+gw.String()).CombinedOutput() 39 return errors.Wrap(err, string(out)) 40 } 41 42 func deleteRoute(ip, gw string) error { 43 out, err := exec.Command("powershell", "-Command", "route delete "+ip+"/32").CombinedOutput() 44 if err != nil { 45 return fmt.Errorf("failed to delete route: %w, %s", err, string(out)) 46 } 47 48 return nil 49 } 50 51 func addDefaultRoute(name string) error { 52 id, gw, err := interfaceInfo(name) 53 if err != nil { 54 return errors.Wrap(err, "failed to get info of interface: "+name) 55 } 56 57 if out, err := exec.Command("powershell", "-Command", "route add 0.0.0.0/1 "+gw+" if "+id).CombinedOutput(); err != nil { 58 return errors.Wrap(err, string(out)) 59 } 60 61 if out, err := exec.Command("powershell", "-Command", "route add 128.0.0.0/1 "+gw+" if "+id).CombinedOutput(); err != nil { 62 return errors.Wrap(err, string(out)) 63 } 64 65 if out, err := exec.Command("powershell", "-Command", "route add ::/1 100::1 if "+id).CombinedOutput(); err != nil { 66 return errors.Wrap(err, string(out)) 67 } 68 69 if out, err := exec.Command("powershell", "-Command", "route add 8000::/1 100::1 if "+id).CombinedOutput(); err != nil { 70 return errors.Wrap(err, string(out)) 71 } 72 73 return nil 74 } 75 76 func interfaceInfo(name string) (id, gw string, err error) { 77 iface, err := net.InterfaceByName(name) 78 if err != nil { 79 return "", "", errors.Wrap(err, "failed to get interfaces "+name) 80 } 81 82 addrs, err := iface.Addrs() 83 if err != nil { 84 return "", "", errors.Wrap(err, "failed to get interfaces addresses") 85 } 86 87 var ipv4 net.IP 88 for _, addr := range addrs { 89 ip, _, err := net.ParseCIDR(addr.String()) 90 if err != nil { 91 log.Error().Err(err).Msg("Failed to parse an interface IP address") 92 } 93 94 if ip.To4() == nil { 95 continue 96 } 97 98 if ipv4.Equal(net.IPv4zero) { 99 return "", "", errors.New("failed to get interface info: exactly 1 IPv4 expected") 100 } 101 102 ipv4 = ip.To4() 103 ipv4[net.IPv4len-1] = byte(1) 104 } 105 106 return strconv.Itoa(iface.Index), ipv4.String(), nil 107 } 108 109 func logNetworkStats() { 110 for _, args := range []string{"ipconfig /all", "netstat -r"} { 111 out, err := exec.Command("powershell", "-Command", args).CombinedOutput() 112 logOutputToTrace(out, err, args) 113 } 114 }