connection.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package functions
  2. import (
  3. "fmt"
  4. "github.com/gravitl/netmaker/netclient/config"
  5. "github.com/gravitl/netmaker/netclient/ncutils"
  6. "github.com/gravitl/netmaker/netclient/wireguard"
  7. )
  8. // Connect - will attempt to connect a node on given network
  9. func Connect(network string) error {
  10. cfg, err := config.ReadConfig(network)
  11. if err != nil {
  12. return err
  13. }
  14. if cfg.Node.Connected == "yes" {
  15. return fmt.Errorf("node already connected")
  16. }
  17. cfg.Node.Connected = "yes"
  18. filePath := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
  19. if err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, filePath); err != nil {
  20. return err
  21. }
  22. return config.ModNodeConfig(&cfg.Node)
  23. }
  24. // Disconnect - attempts to disconnect a node on given network
  25. func Disconnect(network string) error {
  26. cfg, err := config.ReadConfig(network)
  27. if err != nil {
  28. return err
  29. }
  30. if cfg.Node.Connected == "no" {
  31. return fmt.Errorf("node already disconnected")
  32. }
  33. cfg.Node.Connected = "no"
  34. filePath := ncutils.GetNetclientPathSpecific() + cfg.Node.Interface + ".conf"
  35. if err = wireguard.ApplyConf(&cfg.Node, cfg.Node.Interface, filePath); err != nil {
  36. return err
  37. }
  38. return config.ModNodeConfig(&cfg.Node)
  39. }