netmaker/netclient/functions/register.go

100 lines
2.9 KiB
Go
Raw Normal View History

2021-05-26 00:48:04 +08:00
package functions
import (
2021-05-30 01:22:18 +08:00
"time"
2021-05-30 03:12:15 +08:00
"os"
"log"
2021-05-26 04:09:49 +08:00
"io/ioutil"
"bytes"
2021-05-26 00:48:04 +08:00
"github.com/gravitl/netmaker/netclient/config"
2021-05-30 01:22:18 +08:00
"github.com/gravitl/netmaker/netclient/local"
2021-05-26 00:48:04 +08:00
"github.com/gravitl/netmaker/netclient/wireguard"
2021-05-26 04:09:49 +08:00
"github.com/gravitl/netmaker/models"
"encoding/json"
"net/http"
"errors"
"github.com/davecgh/go-spew/spew"
2021-05-26 00:48:04 +08:00
)
2021-05-26 04:09:49 +08:00
func Register(cfg config.GlobalConfig) error {
2021-05-26 00:48:04 +08:00
2021-05-30 03:12:15 +08:00
_, err := os.Stat("/etc/netclient")
if os.IsNotExist(err) {
os.Mkdir("/etc/netclient", 744)
} else if err != nil {
log.Println("couldnt find or create /etc/netclient")
return err
}
2021-05-28 02:54:24 +08:00
postclient := &models.IntClient{
2021-05-26 04:09:49 +08:00
AccessKey: cfg.Client.AccessKey,
PublicKey: cfg.Client.PublicKey,
PrivateKey: cfg.Client.PublicKey,
Address: cfg.Client.Address,
Address6: cfg.Client.Address6,
Network: "comms",
2021-05-26 00:48:04 +08:00
}
2021-06-02 00:23:05 +08:00
2021-05-26 04:09:49 +08:00
jsonstring, err := json.Marshal(postclient)
2021-05-26 00:48:04 +08:00
if err != nil {
return err
}
2021-05-26 04:09:49 +08:00
jsonbytes := []byte(jsonstring)
body := bytes.NewBuffer(jsonbytes)
2021-06-02 06:40:04 +08:00
publicaddress := cfg.Client.ServerPublicEndpoint + ":" + cfg.Client.ServerAPIPort
log.Println("registering to http://"+publicaddress+"/api/client/register")
res, err := http.Post("http://"+publicaddress+"/api/intclient/register","application/json",body)
2021-05-26 00:48:04 +08:00
if err != nil {
return err
}
2021-05-26 04:09:49 +08:00
if res.StatusCode != http.StatusOK {
return errors.New("request to server failed: " + res.Status)
2021-05-26 00:48:04 +08:00
}
2021-05-26 04:09:49 +08:00
bodyBytes, err := ioutil.ReadAll(res.Body)
2021-06-02 10:31:41 +08:00
bodyString := string(bodyBytes)
spew.Dump(bodyString)
2021-05-26 04:09:49 +08:00
if err != nil {
return err
2021-05-26 00:48:04 +08:00
}
2021-05-28 02:54:24 +08:00
var wgclient models.IntClient
2021-05-26 04:09:49 +08:00
json.Unmarshal(bodyBytes, &wgclient)
spew.Dump(wgclient)
err = config.ModGlobalConfig(wgclient)
2021-05-26 00:48:04 +08:00
if err != nil {
return err
}
2021-06-02 00:23:05 +08:00
spew.Dump(wgclient)
2021-05-26 04:09:49 +08:00
err = wireguard.InitGRPCWireguard(wgclient)
2021-05-26 00:48:04 +08:00
if err != nil {
return err
}
return err
}
2021-05-30 01:22:18 +08:00
func Unregister(cfg config.GlobalConfig) error {
client := &http.Client{ Timeout: 7 * time.Second,}
2021-06-02 06:40:04 +08:00
publicaddress := cfg.Client.ServerPublicEndpoint + ":" + cfg.Client.ServerAPIPort
2021-06-02 10:31:41 +08:00
log.Println("sending delete request to: " + "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID)
2021-06-02 06:40:04 +08:00
req, err := http.NewRequest("DELETE", "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID, nil)
2021-06-02 00:23:05 +08:00
if err != nil {
log.Println(err)
} else {
res, err := client.Do(req)
if res == nil {
2021-06-02 06:40:04 +08:00
err = errors.New("server not reachable at " + "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID)
2021-06-02 00:23:05 +08:00
log.Println(err)
} else if res.StatusCode != http.StatusOK {
err = errors.New("request to server failed: " + res.Status)
log.Println(err)
defer res.Body.Close()
2021-05-30 01:22:18 +08:00
}
}
2021-06-02 00:23:05 +08:00
err = local.WipeGRPCClient()
if err == nil {
log.Println("successfully removed grpc client interface")
}
2021-05-30 01:22:18 +08:00
return err
}