Browse Source

add tls in client

sainw 2 years ago
parent
commit
bc85875828
3 changed files with 21 additions and 5 deletions
  1. 1 0
      .gitignore
  2. 17 2
      client/client.go
  3. 3 3
      example/main.go

+ 1 - 0
.gitignore

@@ -3,3 +3,4 @@ fho_forward_sample/main
 example/example
 example/__debug_bin
 example/main
+example/main_local.go

+ 17 - 2
client/client.go

@@ -6,12 +6,14 @@ import (
 	"errors"
 	"io"
 	"log"
+	"strings"
 	"time"
 
 	pb "git.mokkon.com/sainw/fho_forward_proto.git"
 	grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/codes"
+	"google.golang.org/grpc/credentials"
 	"google.golang.org/grpc/metadata"
 	"google.golang.org/grpc/status"
 	"google.golang.org/protobuf/types/known/timestamppb"
@@ -27,9 +29,22 @@ func NewClient(address string, id string, key string) (*FhoClient, error) {
 	opts := []grpc_retry.CallOption{
 		grpc_retry.WithBackoff(grpc_retry.BackoffLinear(100 * time.Millisecond)),
 	}
+	addresses := strings.Split(address, ":")
+	host := addresses[0]
+	port := ""
+	if len(addresses) > 1 {
+		port = addresses[1]
+	}
+	options := make([]grpc.DialOption, 0)
+	options = append(options, grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor(opts...)))
+
+	if port == "443" {
+		options = append(options, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, host)))
+	} else {
+		options = append(options, grpc.WithInsecure())
+	}
 
-	conn, err := grpc.Dial(address, grpc.WithInsecure(),
-		grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor(opts...)))
+	conn, err := grpc.Dial(address, options...)
 	if err != nil {
 		return nil, err
 	}

+ 3 - 3
example/main.go

@@ -9,7 +9,7 @@ import (
 )
 
 const (
-	address = "fhogrpc-test.seinmaungengineering.com:7091"
+	address = "fhogrpc-test.seinmaungengineering.com:443"
 )
 
 func main() {
@@ -19,9 +19,9 @@ func main() {
 	}
 	// go listenProducts(client)
 	// go listenPrices(client)
-	// go query(client)
+	go query(client)
 	// update(client)
-	updateFromFHO(client)
+	// updateFromFHO(client)
 
 	select {}
 }