register.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package functions
  2. import (
  3. "log"
  4. "io/ioutil"
  5. "bytes"
  6. "github.com/gravitl/netmaker/netclient/config"
  7. "github.com/gravitl/netmaker/netclient/wireguard"
  8. "github.com/gravitl/netmaker/models"
  9. "encoding/json"
  10. "net/http"
  11. "errors"
  12. "github.com/davecgh/go-spew/spew"
  13. )
  14. func Register(cfg config.GlobalConfig) error {
  15. postclient := &models.IntClient{
  16. AccessKey: cfg.Client.AccessKey,
  17. PublicKey: cfg.Client.PublicKey,
  18. PrivateKey: cfg.Client.PublicKey,
  19. Address: cfg.Client.Address,
  20. Address6: cfg.Client.Address6,
  21. Network: "comms",
  22. }
  23. jsonstring, err := json.Marshal(postclient)
  24. if err != nil {
  25. return err
  26. }
  27. jsonbytes := []byte(jsonstring)
  28. body := bytes.NewBuffer(jsonbytes)
  29. log.Println(jsonstring)
  30. log.Println("http://"+cfg.Client.ServerEndpoint+"/api/client/register","application/json")
  31. res, err := http.Post("http://"+cfg.Client.ServerEndpoint+"/api/intclient/register","application/json",body)
  32. if err != nil {
  33. return err
  34. }
  35. if res.StatusCode != http.StatusOK {
  36. return errors.New("request to server failed: " + res.Status)
  37. }
  38. bodyBytes, err := ioutil.ReadAll(res.Body)
  39. if err != nil {
  40. return err
  41. }
  42. var wgclient models.IntClient
  43. json.Unmarshal(bodyBytes, &wgclient)
  44. spew.Dump(wgclient)
  45. err = config.ModGlobalConfig(wgclient)
  46. if err != nil {
  47. return err
  48. }
  49. err = wireguard.InitGRPCWireguard(wgclient)
  50. if err != nil {
  51. return err
  52. }
  53. return err
  54. }