Skip to content

Commit

Permalink
Merge branch master into development
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Jan 19, 2023
2 parents 97e6b51 + 1cc3f0b commit 8a0f823
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 33 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ build:
-o ./build/sentinelcli-${VERSION}-darwin-amd64 main.go
GOOS=linux GOARCH=amd64 go build -mod=readonly -tags="${BUILD_TAGS}" -ldflags="${LD_FLAGS}" \
-o ./build/sentinelcli-${VERSION}-linux-amd64 main.go
GOOS=windows GOARCH=amd64 go build -mod=readonly -tags="${BUILD_TAGS}" -ldflags="${LD_FLAGS}" \
-o ./build/sentinelcli-${VERSION}-windows-amd64.exe main.go

.PHONY: install
install:
Expand Down
36 changes: 6 additions & 30 deletions services/wireguard/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (w *WireGuard) IsUp() bool {
return false
}

output, err := exec.Command("wg", strings.Split(
output, err := exec.Command(w.ExecFile("wg"), strings.Split(
fmt.Sprintf("show %s", shellescape.Quote(iFace)), " ")...).CombinedOutput()
if err != nil {
return false
Expand All @@ -56,40 +56,16 @@ func (w *WireGuard) PreUp() error {
return w.cfg.WriteToFile(w.home)
}

func (w *WireGuard) Up() error {
var (
path = filepath.Join(w.home, fmt.Sprintf("%s.conf", w.cfg.Name))
cmd = exec.Command("wg-quick", strings.Split(
fmt.Sprintf("up %s", shellescape.Quote(path)), " ")...)
)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func (w *WireGuard) PostUp() error { return nil }
func (w *WireGuard) PreDown() error { return nil }

func (w *WireGuard) Down() error {
var (
path = filepath.Join(w.home, fmt.Sprintf("%s.conf", w.cfg.Name))
cmd = exec.Command("wg-quick", strings.Split(
fmt.Sprintf("down %s", shellescape.Quote(path)), " ")...)
)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func (w *WireGuard) PostDown() error {
path := filepath.Join(w.home, fmt.Sprintf("%s.conf", w.cfg.Name))
if _, err := os.Stat(path); err == nil {
return os.Remove(path)
cfgFilePath := filepath.Join(w.Home(), fmt.Sprintf("%s.conf", w.cfg.Name))
if _, err := os.Stat(cfgFilePath); err != nil {
return nil
}

return nil
return os.Remove(cfgFilePath)
}

func (w *WireGuard) Transfer() (u int64, d int64, err error) {
Expand All @@ -98,7 +74,7 @@ func (w *WireGuard) Transfer() (u int64, d int64, err error) {
return 0, 0, err
}

output, err := exec.Command("wg", strings.Split(
output, err := exec.Command(w.ExecFile("wg"), strings.Split(
fmt.Sprintf("show %s transfer", shellescape.Quote(iFace)), " ")...).Output()
if err != nil {
return 0, 0, err
Expand Down
34 changes: 31 additions & 3 deletions services/wireguard/wireguard_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/alessio/shellescape"
Expand All @@ -16,9 +18,7 @@ func (w *WireGuard) RealInterface() (string, error) {
return "", err
}

var (
scanner = bufio.NewReader(nameFile)
)
scanner := bufio.NewReader(nameFile)

line, err := scanner.ReadString('\n')
if err != nil {
Expand All @@ -27,3 +27,31 @@ func (w *WireGuard) RealInterface() (string, error) {

return strings.Trim(line, "\n"), nil
}

func (w *WireGuard) ExecFile(name string) string {
return name
}

func (w *WireGuard) Up() error {
var (
cfgFilePath = filepath.Join(w.Home(), fmt.Sprintf("%s.conf", w.cfg.Name))
cmd = exec.Command(w.ExecFile("wg-quick"), strings.Split(
fmt.Sprintf("up %s", shellescape.Quote(cfgFilePath)), " ")...)
)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func (w *WireGuard) Down() error {
var (
cfgFilePath = filepath.Join(w.Home(), fmt.Sprintf("%s.conf", w.cfg.Name))
cmd = exec.Command(w.ExecFile("wg-quick"), strings.Split(
fmt.Sprintf("down %s", shellescape.Quote(cfgFilePath)), " ")...)
)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
40 changes: 40 additions & 0 deletions services/wireguard/wireguard_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
package wireguard

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/alessio/shellescape"
)

func (w *WireGuard) RealInterface() (string, error) {
return w.cfg.Name, nil
}

func (w *WireGuard) ExecFile(name string) string {
return name
}

func (w *WireGuard) Up() error {
var (
cfgFilePath = filepath.Join(w.Home(), fmt.Sprintf("%s.conf", w.cfg.Name))
cmd = exec.Command(w.ExecFile("wg-quick"), strings.Split(
fmt.Sprintf("up %s", shellescape.Quote(cfgFilePath)), " ")...)
)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func (w *WireGuard) Down() error {
iFace, err := w.RealInterface()
if err != nil {
return err
}

cmd := exec.Command(w.ExecFile("wg-quick"), strings.Split(
fmt.Sprintf("down %s", shellescape.Quote(iFace)), " ")...)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
46 changes: 46 additions & 0 deletions services/wireguard/wireguard_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package wireguard

import (
"fmt"
"os"
"os/exec"
"path/filepath"
)

func (w *WireGuard) RealInterface() (string, error) {
return w.cfg.Name, nil
}

func (w *WireGuard) ExecFile(name string) string {
return ".\\" + filepath.Join("WireGuard", name)
}

func (w *WireGuard) Up() error {
var (
cfgFilePath = filepath.Join(w.Home(), fmt.Sprintf("%s.conf", w.cfg.Name))
cmd = exec.Command(
w.ExecFile("wireguard.exe"),
"/installtunnelservice", cfgFilePath,
)
)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func (w *WireGuard) Down() error {
iFace, err := w.RealInterface()
if err != nil {
return err
}

cmd := exec.Command(
w.ExecFile("wireguard.exe"),
"/uninstalltunnelservice", iFace,
)

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

0 comments on commit 8a0f823

Please sign in to comment.