Skip to content

Commit

Permalink
Refactored WireGuard service
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Feb 17, 2023
1 parent 64a4832 commit a7ac631
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 75 deletions.
9 changes: 3 additions & 6 deletions services/wireguard/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package types

import (
"fmt"
"io/ioutil"
"net"
"path/filepath"
"os"
"strings"
)

type Config struct {
Name string
Interface Interface
Peers []Peer
}
Expand Down Expand Up @@ -109,7 +107,6 @@ func (c *Config) ToWgQuick() string {
return output.String()
}

func (c *Config) WriteToFile(dir string) error {
path := filepath.Join(dir, fmt.Sprintf("%s.conf", c.Name))
return ioutil.WriteFile(path, []byte(c.ToWgQuick()), 0600)
func (c *Config) WriteToFile(path string) error {
return os.WriteFile(path, []byte(c.ToWgQuick()), 0600)
}
3 changes: 2 additions & 1 deletion services/wireguard/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package types

const (
DefaultInterface = "wg99"
DefaultConfigFileName = "wg99.conf"
DefaultInterface = "wg99"
)
65 changes: 42 additions & 23 deletions services/wireguard/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strconv"
"strings"

"github.com/alessio/shellescape"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/viper"

Expand All @@ -25,24 +24,36 @@ type WireGuard struct {
info []byte
}

func NewWireGuard() *WireGuard {
return &WireGuard{}
func NewWireGuard(cfg *types.Config, info []byte) *WireGuard {
return &WireGuard{
cfg: cfg,
info: info,
}
}

func (w *WireGuard) WithConfig(v *types.Config) *WireGuard { w.cfg = v; return w }
func (w *WireGuard) WithInfo(v []byte) *WireGuard { w.info = v; return w }
func (s *WireGuard) home() string { return viper.GetString(flags.FlagHome) }

func (s *WireGuard) configFilePath() string {
return filepath.Join(s.home(), types.DefaultConfigFileName)
}

func (w *WireGuard) Home() string { return viper.GetString(flags.FlagHome) }
func (w *WireGuard) Info() []byte { return w.info }
func (s *WireGuard) Info() []byte { return s.info }

func (w *WireGuard) IsUp() bool {
iFace, err := w.RealInterface()
func (s *WireGuard) IsUp() bool {
iFace, err := s.realInterface()
if err != nil {
return false
}

output, err := exec.Command(w.ExecFile("wg"), strings.Split(
fmt.Sprintf("show %s", shellescape.Quote(iFace)), " ")...).CombinedOutput()
cmd := exec.Command(
s.execFile("wg"),
strings.Split(
fmt.Sprintf("show %s", iFace),
" ",
)...,
)

output, err := cmd.CombinedOutput()
if err != nil {
return false
}
Expand All @@ -53,30 +64,38 @@ func (w *WireGuard) IsUp() bool {
return true
}

func (w *WireGuard) PreUp() error {
return w.cfg.WriteToFile(w.Home())
func (s *WireGuard) PreUp() error {
cfgFilePath := s.configFilePath()
return s.cfg.WriteToFile(cfgFilePath)
}

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

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

return os.Remove(cfgFilePath)
}

func (w *WireGuard) Transfer() (u int64, d int64, err error) {
iFace, err := w.RealInterface()
func (s *WireGuard) Transfer() (u int64, d int64, err error) {
iFace, err := s.realInterface()
if err != nil {
return 0, 0, err
}

output, err := exec.Command(w.ExecFile("wg"), strings.Split(
fmt.Sprintf("show %s transfer", shellescape.Quote(iFace)), " ")...).Output()
cmd := exec.Command(
s.execFile("wg"),
strings.Split(
fmt.Sprintf("show %s transfer", iFace),
" ",
)...,
)

output, err := cmd.Output()
if err != nil {
return 0, 0, err
}
Expand All @@ -88,12 +107,12 @@ func (w *WireGuard) Transfer() (u int64, d int64, err error) {
continue
}

d, err := strconv.ParseInt(columns[1], 10, 64)
d, err = strconv.ParseInt(columns[1], 10, 64)
if err != nil {
return 0, 0, err
}

u, err := strconv.ParseInt(columns[2], 10, 64)
u, err = strconv.ParseInt(columns[2], 10, 64)
if err != nil {
return 0, 0, err
}
Expand Down
43 changes: 25 additions & 18 deletions services/wireguard/wireguard_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,57 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/alessio/shellescape"
"github.com/sentinel-official/cli-client/services/wireguard/types"
)

func (w *WireGuard) RealInterface() (string, error) {
nameFile, err := os.Open(
fmt.Sprintf("/var/run/wireguard/%s.name", shellescape.Quote(w.cfg.Name)))
func (s *WireGuard) realInterface() (string, error) {
nameFile, err := os.Open(fmt.Sprintf("/var/run/wireguard/%s.name", types.DefaultInterface))
if err != nil {
return "", err
}

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

line, err := scanner.ReadString('\n')
line, err := reader.ReadString('\n')
if err != nil {
return "", err
}

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

func (w *WireGuard) ExecFile(name string) string {
func (s *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)), " ")...)
func (s *WireGuard) Up() error {
cmd := exec.Command(
s.execFile("wg-quick"),
strings.Split(
fmt.Sprintf("up %s", s.configFilePath()),
" ",
)...,
)

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)), " ")...)
func (s *WireGuard) Down() error {
iFace, err := s.realInterface()
if err != nil {
return err
}

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

cmd.Stdout = os.Stdout
Expand Down
34 changes: 20 additions & 14 deletions services/wireguard/wireguard_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,46 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/alessio/shellescape"
"github.com/sentinel-official/cli-client/services/wireguard/types"
)

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

func (w *WireGuard) ExecFile(name string) string {
func (s *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)), " ")...)
func (s *WireGuard) Up() error {
cmd := exec.Command(
s.execFile("wg-quick"),
strings.Split(
fmt.Sprintf("up %s", s.configFilePath()),
" ",
)...,
)

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

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

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

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
26 changes: 13 additions & 13 deletions services/wireguard/wireguard_windows.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package wireguard

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

"github.com/sentinel-official/cli-client/services/wireguard/types"
)

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

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

func (w *WireGuard) Up() error {
func (s *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 = exec.Command(
s.execFile("wireguard"),
"/installtunnelservice", s.configFilePath(),
)
)

Expand All @@ -29,14 +29,14 @@ func (w *WireGuard) Up() error {
return cmd.Run()
}

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

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

Expand Down

0 comments on commit a7ac631

Please sign in to comment.