Skip to content

Commit

Permalink
Fix windows route exec path (#1946)
Browse files Browse the repository at this point in the history
* Enable release workflow on PR and upload binaries

 add GetSystem32Command to validate if a command is in the path

it will fall back to the full system32, assuming the OS driver is C

---------

Co-authored-by: Maycon Santos <mlsmaycon@gmail.com>
  • Loading branch information
bcmmbaga and mlsmaycon authored May 9, 2024
1 parent ceee421 commit 263abe4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
33 changes: 22 additions & 11 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@ on:
branches:
- main
pull_request:
paths:
- 'go.mod'
- 'go.sum'
- '.goreleaser.yml'
- '.goreleaser_ui.yaml'
- '.goreleaser_ui_darwin.yaml'
- '.github/workflows/release.yml'
- 'release_files/**'
- '**/Dockerfile'
- '**/Dockerfile.*'
- 'client/ui/**'


env:
SIGN_PIPE_VER: "v0.0.11"
Expand Down Expand Up @@ -106,6 +96,27 @@ jobs:
name: release
path: dist/
retention-days: 3
-
name: upload linux packages
uses: actions/upload-artifact@v3
with:
name: linux-packages
path: dist/netbird_linux**
retention-days: 3
-
name: upload windows packages
uses: actions/upload-artifact@v3
with:
name: windows-packages
path: dist/netbird_windows**
retention-days: 3
-
name: upload macos packages
uses: actions/upload-artifact@v3
with:
name: macos-packages
path: dist/netbird_darwin**
retention-days: 3

release_ui:
runs-on: ubuntu-latest
Expand Down
26 changes: 22 additions & 4 deletions client/firewall/uspfilter/allow_netbird_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@ func manageFirewallRule(ruleName string, action action, extraArgs ...string) err
if action == addRule {
args = append(args, extraArgs...)
}

cmd := exec.Command("netsh", args...)
netshCmd := GetSystem32Command("netsh")
cmd := exec.Command(netshCmd, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return cmd.Run()
}

func isWindowsFirewallReachable() bool {
args := []string{"advfirewall", "show", "allprofiles", "state"}
cmd := exec.Command("netsh", args...)

netshCmd := GetSystem32Command("netsh")

cmd := exec.Command(netshCmd, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}

_, err := cmd.Output()
Expand All @@ -87,8 +90,23 @@ func isWindowsFirewallReachable() bool {
func isFirewallRuleActive(ruleName string) bool {
args := []string{"advfirewall", "firewall", "show", "rule", "name=" + ruleName}

cmd := exec.Command("netsh", args...)
netshCmd := GetSystem32Command("netsh")

cmd := exec.Command(netshCmd, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
_, err := cmd.Output()
return err == nil
}

// GetSystem32Command checks if a command can be found in the system path and returns it. In case it can't find it
// in the path it will return the full path of a command assuming C:\windows\system32 as the base path.
func GetSystem32Command(command string) string {
_, err := exec.LookPath(command)
if err == nil {
return command
}

log.Tracef("Command %s not found in PATH, using C:\\windows\\system32\\%s.exe path", command, command)

return "C:\\windows\\system32\\" + command + ".exe"
}
9 changes: 7 additions & 2 deletions client/internal/routemanager/systemops_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/yusufpapurcu/wmi"

"github.com/netbirdio/netbird/client/firewall/uspfilter"
"github.com/netbirdio/netbird/client/internal/peer"
"github.com/netbirdio/netbird/iface"
)
Expand Down Expand Up @@ -173,7 +174,9 @@ func addRouteCmd(prefix netip.Prefix, nexthop netip.Addr, intf *net.Interface) e
args = append(args, "if", strconv.Itoa(intf.Index))
}

out, err := exec.Command("route", args...).CombinedOutput()
routeCmd := uspfilter.GetSystem32Command("route")

out, err := exec.Command(routeCmd, args...).CombinedOutput()
log.Tracef("route %s: %s", strings.Join(args, " "), out)
if err != nil {
return fmt.Errorf("route add: %w", err)
Expand Down Expand Up @@ -202,7 +205,9 @@ func removeFromRouteTable(prefix netip.Prefix, nexthop netip.Addr, _ *net.Interf
args = append(args, nexthop.Unmap().String())
}

out, err := exec.Command("route", args...).CombinedOutput()
routeCmd := uspfilter.GetSystem32Command("route")

out, err := exec.Command(routeCmd, args...).CombinedOutput()
log.Tracef("route %s: %s", strings.Join(args, " "), out)

if err != nil {
Expand Down

0 comments on commit 263abe4

Please sign in to comment.