connection.go 1.5 KB

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