Skip to content

Commit

Permalink
Add all cmds called by path relative to PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Apr 30, 2020
1 parent 8c2b690 commit c095e85
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
9 changes: 4 additions & 5 deletions internal/vpn/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import (

// SetupTUN sets the allocated TUN interface up, setting its IP, gateway, netmask and MTU.
func SetupTUN(ifcName, ip, netmask, gateway string, mtu int) error {
//return run("/sbin/ifconfig", ifcName, ip, gateway, "mtu", strconv.Itoa(mtu), "netmask", netmask, "up")
return run("ifconfig", ifcName, ip, gateway, "mtu", strconv.Itoa(mtu), "netmask", netmask, "up")
}

// NetworkInterfaceGateway gets gateway of the network interface with name `ifcName`.
func NetworkInterfaceGateway(ifcName string) (net.IP, error) {
cmd := fmt.Sprintf(gatewayForIfcCMDFmt, ifcName)
outBytes, err := exec.Command("/bin/sh", "-c", cmd).Output() //nolint:gosec
outBytes, err := exec.Command("sh", "-c", cmd).Output() //nolint:gosec
if err != nil {
return nil, fmt.Errorf("error running command %s: %w", cmd, err)
}
Expand Down Expand Up @@ -149,7 +148,7 @@ func GetIPv6ForwardingValue() (string, error) {
// SetIPv4ForwardingValue sets `val` value of IPv4 forwarding.
func SetIPv4ForwardingValue(val string) error {
cmd := fmt.Sprintf(setIPv4ForwardingCMDFmt, val)
if err := exec.Command("/bin/sh", "-c", cmd).Run(); err != nil { //nolint:gosec
if err := exec.Command("sh", "-c", cmd).Run(); err != nil { //nolint:gosec
return fmt.Errorf("error running command %s: %w", cmd, err)
}

Expand All @@ -159,7 +158,7 @@ func SetIPv4ForwardingValue(val string) error {
// SetIPv6ForwardingValue sets `val` value of IPv6 forwarding.
func SetIPv6ForwardingValue(val string) error {
cmd := fmt.Sprintf(setIPv6ForwardingCMDFmt, val)
if err := exec.Command("/bin/sh", "-c", cmd).Run(); err != nil { //nolint:gosec
if err := exec.Command("sh", "-c", cmd).Run(); err != nil { //nolint:gosec
return fmt.Errorf("error running command %s: %w", cmd, err)
}

Expand All @@ -177,7 +176,7 @@ func EnableIPv6Forwarding() error {
}

func getIPForwardingValue(cmd string) (string, error) {
outBytes, err := exec.Command("/bin/sh", "-c", cmd).Output() //nolint:gosec
outBytes, err := exec.Command("sh", "-c", cmd).Output() //nolint:gosec
if err != nil {
return "", fmt.Errorf("error running command %s: %w", cmd, err)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/vpn/os_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
gatewayForIfcCMDFmt = "/usr/sbin/netstat -rn | /usr/bin/grep default | /usr/bin/grep %s | /usr/bin/awk '{print $2}'"
gatewayForIfcCMDFmt = "netstat -rn | grep default | grep %s | awk '{print $2}'"
setIPv4ForwardingCMDFmt = "sysctl -w net.inet.ip.forwarding=%s"
setIPv6ForwardingCMDFmt = "sysctl -w net.inet6.ip6.forwarding=%s"
getIPv4ForwardingCMD = "sysctl net.inet.ip.forwarding"
Expand All @@ -29,19 +29,19 @@ func DisableIPMasquerading(_ string) error {
// AddRoute adds route to `ip` with `netmask` through the `gateway` to the OS routing table.
func AddRoute(ip, gateway, netmask string) error {
if netmask == "" {
return run("/sbin/route", "add", "-net", ip, gateway)
return run("route", "add", "-net", ip, gateway)
}

return run("/sbin/route", "add", "-net", ip, gateway, netmask)
return run("route", "add", "-net", ip, gateway, netmask)
}

// DeleteRoute removes route to `ip` with `netmask` through the `gateway` from the OS routing table.
func DeleteRoute(ip, gateway, netmask string) error {
if netmask == "" {
return run("/sbin/route", "delete", "-net", ip, gateway)
return run("route", "delete", "-net", ip, gateway)
}

return run("/sbin/route", "delete", "-net", ip, gateway, netmask)
return run("route", "delete", "-net", ip, gateway, netmask)
}

func parseIPForwardingOutput(output []byte) (string, error) {
Expand Down
10 changes: 5 additions & 5 deletions internal/vpn/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
gatewayForIfcCMDFmt = "/sbin/route -n | /bin/grep %s | /usr/bin/awk '$1 == \"0.0.0.0\" {print $2}'"
gatewayForIfcCMDFmt = "route -n | grep %s | awk '$1 == \"0.0.0.0\" {print $2}'"
setIPv4ForwardingCMDFmt = "sysctl -w net.ipv4.ip_forward=%s"
setIPv6ForwardingCMDFmt = "sysctl -w net.ipv6.conf.all.forwarding=%s"
getIPv4ForwardingCMD = "sysctl net.ipv4.ip_forward"
Expand All @@ -21,7 +21,7 @@ const (
// EnableIPMasquerading enables IP masquerading for the interface with name `ifcName`.
func EnableIPMasquerading(ifcName string) error {
cmd := fmt.Sprintf(enableIPMasqueradingCMDFmt, ifcName)
if err := exec.Command("/bin/sh", "-c", cmd).Run(); err != nil {
if err := exec.Command("sh", "-c", cmd).Run(); err != nil {
return fmt.Errorf("error running command %s: %w", cmd, err)
}

Expand All @@ -31,7 +31,7 @@ func EnableIPMasquerading(ifcName string) error {
// DisableIPMasquerading disables IP masquerading for the interface with name `ifcName`.
func DisableIPMasquerading(ifcName string) error {
cmd := fmt.Sprintf(disableIPMasqueradingCMDFmt, ifcName)
if err := exec.Command("/bin/sh", "-c", cmd).Run(); err != nil {
if err := exec.Command("sh", "-c", cmd).Run(); err != nil {
return fmt.Errorf("error running command %s: %w", cmd, err)
}

Expand All @@ -44,7 +44,7 @@ func AddRoute(ip, gateway, netmask string) error {
netmask = "255.255.255.255"
}

return run("/sbin/route", "add", "-net", ip, "netmask", netmask, "gw", gateway)
return run("route", "add", "-net", ip, "netmask", netmask, "gw", gateway)
}

// DeleteRoute removes route to `ip` with `netmask` through the `gateway` from the OS routing table.
Expand All @@ -53,7 +53,7 @@ func DeleteRoute(ip, gateway, netmask string) error {
netmask = "255.255.255.255"
}

return run("/sbin/route", "delete", "-net", ip, "netmask", netmask, "gw", gateway)
return run("route", "delete", "-net", ip, "netmask", netmask, "gw", gateway)
}

func parseIPForwardingOutput(output []byte) (string, error) {
Expand Down

0 comments on commit c095e85

Please sign in to comment.