diff options
Diffstat (limited to 'cmd')
| -rw-r--r-- | cmd/testclient/main.go | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/cmd/testclient/main.go b/cmd/testclient/main.go index 224d9d9..85f9917 100644 --- a/cmd/testclient/main.go +++ b/cmd/testclient/main.go | |||
| @@ -7,9 +7,11 @@ import ( | |||
| 7 | "io" | 7 | "io" |
| 8 | "log" | 8 | "log" |
| 9 | "os" | 9 | "os" |
| 10 | "strings" | ||
| 10 | "time" | 11 | "time" |
| 11 | 12 | ||
| 12 | "google.golang.org/grpc" | 13 | "google.golang.org/grpc" |
| 14 | "google.golang.org/grpc/credentials" | ||
| 13 | "google.golang.org/grpc/credentials/insecure" | 15 | "google.golang.org/grpc/credentials/insecure" |
| 14 | 16 | ||
| 15 | pb "northwest.io/nostr-grpc/api/nostr/v1" | 17 | pb "northwest.io/nostr-grpc/api/nostr/v1" |
| @@ -20,7 +22,20 @@ func main() { | |||
| 20 | addr := flag.String("addr", "localhost:50051", "relay address") | 22 | addr := flag.String("addr", "localhost:50051", "relay address") |
| 21 | flag.Parse() | 23 | flag.Parse() |
| 22 | 24 | ||
| 23 | conn, err := grpc.NewClient(*addr, grpc.WithTransportCredentials(insecure.NewCredentials())) | 25 | // Auto-detect TLS: use TLS for port 443 or non-localhost addresses |
| 26 | var opts []grpc.DialOption | ||
| 27 | useTLS := strings.HasSuffix(*addr, ":443") || | ||
| 28 | (!strings.HasPrefix(*addr, "localhost") && !strings.HasPrefix(*addr, "127.0.0.1") && !strings.HasPrefix(*addr, ":")) | ||
| 29 | |||
| 30 | if useTLS { | ||
| 31 | opts = append(opts, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, ""))) | ||
| 32 | log.Printf("Connecting with TLS to %s", *addr) | ||
| 33 | } else { | ||
| 34 | opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
| 35 | log.Printf("Connecting without TLS to %s", *addr) | ||
| 36 | } | ||
| 37 | |||
| 38 | conn, err := grpc.NewClient(*addr, opts...) | ||
| 24 | if err != nil { | 39 | if err != nil { |
| 25 | log.Fatalf("failed to connect: %v", err) | 40 | log.Fatalf("failed to connect: %v", err) |
| 26 | } | 41 | } |
