use net functions for hostport

This commit is contained in:
Karmanyaah Malhotra 2021-06-10 02:31:55 -04:00
parent b4fbbeb869
commit e6729d2384
2 changed files with 24 additions and 17 deletions

View file

@ -8,6 +8,7 @@ import (
"errors"
"strings"
"fmt"
"net"
"log"
"gopkg.in/yaml.v3"
nodepb "github.com/gravitl/netmaker/grpc"
@ -435,22 +436,27 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, error){
func GetCLIConfigRegister(c *cli.Context) (GlobalConfig, error){
var cfg GlobalConfig
if c.String("token") != "" {
tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
if err != nil {
log.Println("error decoding token")
return cfg, err
}
token := string(tokenbytes)
tokenvals := strings.Split(token, "|")
grpcvals := strings.Split(tokenvals[1],":")
apivals := strings.Split(tokenvals[2], ":")
cfg.Client.ServerWGPort = tokenvals[0]
cfg.Client.ServerPrivateAddress = grpcvals[0]
cfg.Client.ServerGRPCPort = grpcvals[1]
cfg.Client.ServerPublicEndpoint = apivals[0]
cfg.Client.ServerAPIPort = apivals[1]
if c.String("token") != "" {
tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
if err != nil {
log.Println("error decoding token")
return cfg, err
}
token := string(tokenbytes)
tokenvals := strings.Split(token, "|")
cfg.Client.ServerPrivateAddress, cfg.Client.ServerGRPCPort, err = net.SplitHostPort(tokenvals[1])
if err != nil {
log.Println("error decoding token grpcserver")
return cfg, err
}
cfg.Client.ServerPublicEndpoint, cfg.Client.ServerAPIPort, err = net.SplitHostPort(tokenvals[2])
if err != nil {
log.Println("error decoding token apiserver")
return cfg, err
}
cfg.Client.ServerWGPort = tokenvals[0]
cfg.Client.ServerKey = tokenvals[4]
if c.String("grpcserver") != "" {

View file

@ -3,6 +3,7 @@ package functions
import (
"time"
"os"
"net"
"log"
"io/ioutil"
"bytes"
@ -41,7 +42,7 @@ func Register(cfg config.GlobalConfig) error {
}
jsonbytes := []byte(jsonstring)
body := bytes.NewBuffer(jsonbytes)
publicaddress := cfg.Client.ServerPublicEndpoint + ":" + cfg.Client.ServerAPIPort
publicaddress := net.JoinHostPort(cfg.Client.ServerPublicEndpoint, cfg.Client.ServerAPIPort)
res, err := http.Post("http://"+publicaddress+"/api/intclient/register","application/json",body)
if err != nil {
@ -76,7 +77,7 @@ func Register(cfg config.GlobalConfig) error {
func Unregister(cfg config.GlobalConfig) error {
client := &http.Client{ Timeout: 7 * time.Second,}
publicaddress := cfg.Client.ServerPublicEndpoint + ":" + cfg.Client.ServerAPIPort
publicaddress := net.JoinHostPort(cfg.Client.ServerPublicEndpoint, cfg.Client.ServerAPIPort)
log.Println("sending delete request to: " + "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID)
req, err := http.NewRequest("DELETE", "http://"+publicaddress+"/api/intclient/"+cfg.Client.ClientID, nil)
if err != nil {