Browse Source

use net functions for hostport

Karmanyaah Malhotra 4 years ago
parent
commit
e6729d2384
2 changed files with 24 additions and 17 deletions
  1. 21 15
      netclient/config/config.go
  2. 3 2
      netclient/functions/register.go

+ 21 - 15
netclient/config/config.go

@@ -8,6 +8,7 @@ import (
 	"errors"
 	"errors"
 	"strings"
 	"strings"
 	"fmt"
 	"fmt"
+	"net"
 	"log"
 	"log"
 	"gopkg.in/yaml.v3"
 	"gopkg.in/yaml.v3"
 	nodepb "github.com/gravitl/netmaker/grpc"
 	nodepb "github.com/gravitl/netmaker/grpc"
@@ -435,22 +436,27 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, error){
 
 
 func GetCLIConfigRegister(c *cli.Context) (GlobalConfig, error){
 func GetCLIConfigRegister(c *cli.Context) (GlobalConfig, error){
 	var cfg GlobalConfig
 	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]
 		cfg.Client.ServerKey = tokenvals[4]
 
 
                 if c.String("grpcserver") != "" {
                 if c.String("grpcserver") != "" {

+ 3 - 2
netclient/functions/register.go

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