Skip to content

Commit

Permalink
Added Name field in WireGuard config
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Feb 17, 2023
1 parent a7ac631 commit d94d89e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
35 changes: 18 additions & 17 deletions services/wireguard/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@ import (
)

type Config struct {
Interface Interface
Peers []Peer
Name string `json:"name"`
Interface Interface `json:"-"`
Peers []Peer `json:"-"`
}

type Interface struct {
PrivateKey Key
Addresses []IPNet
ListenPort uint16
MTU uint16
DNS []net.IP
DNSSearch []string
PreUp string
PostUp string
PreDown string
PostDown string
PrivateKey Key `json:"-"`
Addresses []IPNet `json:"-"`
ListenPort uint16 `json:"-"`
MTU uint16 `json:"-"`
DNS []net.IP `json:"-"`
DNSSearch []string `json:"-"`
PreUp string `json:"-"`
PostUp string `json:"-"`
PreDown string `json:"-"`
PostDown string `json:"-"`
}

type Peer struct {
PublicKey Key
PresharedKey Key
AllowedIPs []IPNet
Endpoint Endpoint
PersistentKeepalive uint16
PublicKey Key `json:"-"`
PresharedKey Key `json:"-"`
AllowedIPs []IPNet `json:"-"`
Endpoint Endpoint `json:"-"`
PersistentKeepalive uint16 `json:"-"`
}

func (c *Config) ToWgQuick() string {
Expand Down
3 changes: 1 addition & 2 deletions services/wireguard/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package types

const (
DefaultConfigFileName = "wg99.conf"
DefaultInterface = "wg99"
DefaultInterface = "wg99"
)
20 changes: 13 additions & 7 deletions services/wireguard/wireguard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wireguard

import (
"encoding/json"
"fmt"
"os"
"os/exec"
Expand All @@ -20,24 +21,29 @@ var (
)

type WireGuard struct {
cfg *types.Config
info []byte
cfg *types.Config
}

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

func (s *WireGuard) home() string { return viper.GetString(flags.FlagHome) }

func (s *WireGuard) configFilePath() string {
return filepath.Join(s.home(), types.DefaultConfigFileName)
return filepath.Join(s.home(), fmt.Sprintf("%s.conf", s.cfg.Name))
}

func (s *WireGuard) Info() []byte { return s.info }
func (s *WireGuard) Info() []byte {
buf, err := json.Marshal(s.cfg)
if err != nil {
panic(err)
}

return buf
}

func (s *WireGuard) IsUp() bool {
iFace, err := s.realInterface()
Expand Down
4 changes: 1 addition & 3 deletions services/wireguard/wireguard_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"os"
"os/exec"
"strings"

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

func (s *WireGuard) realInterface() (string, error) {
nameFile, err := os.Open(fmt.Sprintf("/var/run/wireguard/%s.name", types.DefaultInterface))
nameFile, err := os.Open(fmt.Sprintf("/var/run/wireguard/%s.name", s.cfg.Name))
if err != nil {
return "", err
}
Expand Down
4 changes: 1 addition & 3 deletions services/wireguard/wireguard_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import (
"os"
"os/exec"
"strings"

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

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

func (s *WireGuard) execFile(name string) string {
Expand Down
4 changes: 1 addition & 3 deletions services/wireguard/wireguard_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"os"
"os/exec"
"path/filepath"

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

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

func (s *WireGuard) execFile(name string) string {
Expand Down

0 comments on commit d94d89e

Please sign in to comment.