Skip to content

Commit

Permalink
Renamed cfg to config
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Mar 21, 2023
1 parent f2266f4 commit 41c82c9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
10 changes: 5 additions & 5 deletions services/v2ray/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func configShow() *cobra.Command {
v := viper.New()
v.SetConfigFile(path)

cfg, err := v2raytypes.ReadInConfig(v)
config, err := v2raytypes.ReadInConfig(v)
if err != nil {
return err
}

fmt.Println(cfg.String())
fmt.Println(config.String())
return nil
},
}
Expand All @@ -103,18 +103,18 @@ func configSet() *cobra.Command {
v := viper.New()
v.SetConfigFile(path)

cfg, err := v2raytypes.ReadInConfig(v)
config, err := v2raytypes.ReadInConfig(v)
if err != nil {
return err
}

v.Set(args[0], args[1])

if err = v.Unmarshal(cfg); err != nil {
if err = v.Unmarshal(config); err != nil {
return err
}

return cfg.SaveToPath(path)
return config.SaveToPath(path)
},
}

Expand Down
10 changes: 5 additions & 5 deletions services/wireguard/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func configShow() *cobra.Command {
v := viper.New()
v.SetConfigFile(path)

cfg, err := wgtypes.ReadInConfig(v)
config, err := wgtypes.ReadInConfig(v)
if err != nil {
return err
}

fmt.Println(cfg.String())
fmt.Println(config.String())
return nil
},
}
Expand All @@ -103,18 +103,18 @@ func configSet() *cobra.Command {
v := viper.New()
v.SetConfigFile(path)

cfg, err := wgtypes.ReadInConfig(v)
config, err := wgtypes.ReadInConfig(v)
if err != nil {
return err
}

v.Set(args[0], args[1])

if err = v.Unmarshal(cfg); err != nil {
if err = v.Unmarshal(config); err != nil {
return err
}

return cfg.SaveToPath(path)
return config.SaveToPath(path)
},
}

Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions services/wireguard/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ func (c *Config) String() string {
}

func ReadInConfig(v *viper.Viper) (*Config, error) {
cfg := NewConfig().WithDefaultValues()
config := NewConfig().WithDefaultValues()
if err := v.ReadInConfig(); err != nil {
return nil, err
}
if err := v.Unmarshal(cfg); err != nil {
if err := v.Unmarshal(config); err != nil {
return nil, err
}

return cfg, nil
return config, nil
}
38 changes: 19 additions & 19 deletions services/wireguard/wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ var (
)

type WireGuard struct {
info []byte
cfg *wgtypes.Config
peers *wgtypes.Peers
pool *wgtypes.IPPool
info []byte
config *wgtypes.Config
peers *wgtypes.Peers
pool *wgtypes.IPPool
}

func NewWireGuard(pool *wgtypes.IPPool) types.Service {
return &WireGuard{
pool: pool,
cfg: wgtypes.NewConfig(),
info: make([]byte, InfoLen),
peers: wgtypes.NewPeers(),
pool: pool,
config: wgtypes.NewConfig(),
info: make([]byte, InfoLen),
peers: wgtypes.NewPeers(),
}
}

Expand All @@ -50,11 +50,11 @@ func (s *WireGuard) Init(home string) (err error) {
v := viper.New()
v.SetConfigFile(filepath.Join(home, wgtypes.ConfigFileName))

s.cfg, err = wgtypes.ReadInConfig(v)
s.config, err = wgtypes.ReadInConfig(v)
if err != nil {
return err
}
if err = s.cfg.Validate(); err != nil {
if err = s.config.Validate(); err != nil {
return err
}

Expand All @@ -64,21 +64,21 @@ func (s *WireGuard) Init(home string) (err error) {
}

var buffer bytes.Buffer
if err = t.Execute(&buffer, s.cfg); err != nil {
if err = t.Execute(&buffer, s.config); err != nil {
return err
}

path := fmt.Sprintf("/etc/wireguard/%s.conf", s.cfg.Interface)
path := fmt.Sprintf("/etc/wireguard/%s.conf", s.config.Interface)
if err = os.WriteFile(path, buffer.Bytes(), 0600); err != nil {
return err
}

key, err := wgtypes.KeyFromString(s.cfg.PrivateKey)
key, err := wgtypes.KeyFromString(s.config.PrivateKey)
if err != nil {
return err
}

binary.BigEndian.PutUint16(s.info[:2], s.cfg.ListenPort)
binary.BigEndian.PutUint16(s.info[:2], s.config.ListenPort)
copy(s.info[2:], key.Public().Bytes())

return nil
Expand All @@ -90,7 +90,7 @@ func (s *WireGuard) Info() []byte {

func (s *WireGuard) Start() error {
cmd := exec.Command("wg-quick", strings.Split(
fmt.Sprintf("up %s", s.cfg.Interface), " ")...)
fmt.Sprintf("up %s", s.config.Interface), " ")...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand All @@ -99,7 +99,7 @@ func (s *WireGuard) Start() error {

func (s *WireGuard) Stop() error {
cmd := exec.Command("wg-quick", strings.Split(
fmt.Sprintf("down %s", s.cfg.Interface), " ")...)
fmt.Sprintf("down %s", s.config.Interface), " ")...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand All @@ -122,7 +122,7 @@ func (s *WireGuard) AddPeer(data []byte) (result []byte, err error) {

cmd := exec.Command("wg", strings.Split(
fmt.Sprintf(`set %s peer %s allowed-ips %s/32,%s/128`,
s.cfg.Interface, identity, v4.IP(), v6.IP()), " ")...)
s.config.Interface, identity, v4.IP(), v6.IP()), " ")...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand Down Expand Up @@ -157,7 +157,7 @@ func (s *WireGuard) RemovePeer(data []byte) error {

cmd := exec.Command("wg", strings.Split(
fmt.Sprintf(`set %s peer %s remove`,
s.cfg.Interface, identity), " ")...)
s.config.Interface, identity), " ")...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

Expand All @@ -175,7 +175,7 @@ func (s *WireGuard) RemovePeer(data []byte) error {

func (s *WireGuard) Peers() (items []types.Peer, err error) {
output, err := exec.Command("wg", strings.Split(
fmt.Sprintf("show %s transfer", s.cfg.Interface), " ")...).Output()
fmt.Sprintf("show %s transfer", s.config.Interface), " ")...).Output()
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,13 @@ func (c *Config) String() string {
}

func ReadInConfig(v *viper.Viper) (*Config, error) {
cfg := NewConfig().WithDefaultValues()
config := NewConfig().WithDefaultValues()
if err := v.ReadInConfig(); err != nil {
return nil, err
}
if err := v.Unmarshal(cfg); err != nil {
if err := v.Unmarshal(config); err != nil {
return nil, err
}

return cfg, nil
return config, nil
}

0 comments on commit 41c82c9

Please sign in to comment.