@@ -22,10 +22,10 @@ import (
22
22
"crypto/tls"
23
23
"flag"
24
24
"fmt"
25
- "golang.org/x/net/http2"
26
25
"io"
27
26
"net"
28
27
"net/http"
28
+ "net/url"
29
29
"os"
30
30
"os/signal"
31
31
runpprof "runtime/pprof"
@@ -35,6 +35,7 @@ import (
35
35
36
36
"github.com/spf13/cobra"
37
37
"github.com/spf13/pflag"
38
+ "golang.org/x/net/http2"
38
39
"google.golang.org/grpc"
39
40
"google.golang.org/grpc/credentials"
40
41
"k8s.io/klog/v2"
@@ -369,10 +370,14 @@ func configureHTTP2Transport(t *http.Transport) error {
369
370
}
370
371
371
372
func (c * Client ) makeRequest (o * GrpcProxyClientOptions , client * http.Client ) error {
372
- requestURL := fmt .Sprintf ("%s://%s:%d/%s" , o .requestProto , o .requestHost , o .requestPort , o .requestPath )
373
- request , err := http .NewRequest ("GET" , requestURL , nil )
373
+ requestURL := url.URL {
374
+ Scheme : o .requestProto ,
375
+ Host : joinHostPort (o .requestHost , o .requestPort ),
376
+ Path : o .requestPath ,
377
+ }
378
+ request , err := http .NewRequest ("GET" , requestURL .String (), nil )
374
379
if err != nil {
375
- return fmt .Errorf ("failed to create request %s to send, got %v" , requestURL , err )
380
+ return fmt .Errorf ("failed to create request %v to send, got %v" , requestURL , err )
376
381
}
377
382
response , err := client .Do (request )
378
383
if err != nil {
@@ -439,13 +444,13 @@ func (c *Client) getUDSDialer(o *GrpcProxyClientOptions) (func(ctx context.Conte
439
444
return nil , fmt .Errorf ("failed to create tunnel %s, got %v" , o .proxyUdsName , err )
440
445
}
441
446
442
- requestAddress := fmt . Sprintf ( "%s:%d" , o .requestHost , o .requestPort )
447
+ requestAddress := joinHostPort ( o .requestHost , o .requestPort )
443
448
proxyConn , err = tunnel .DialContext (ctx , "tcp" , requestAddress )
444
449
if err != nil {
445
450
return nil , fmt .Errorf ("failed to dial request %s, got %v" , requestAddress , err )
446
451
}
447
452
case "http-connect" :
448
- requestAddress := fmt . Sprintf ( "%s:%d" , o .requestHost , o .requestPort )
453
+ requestAddress := joinHostPort ( o .requestHost , o .requestPort )
449
454
450
455
proxyConn , err = net .Dial ("unix" , o .proxyUdsName )
451
456
if err != nil {
@@ -509,20 +514,20 @@ func (c *Client) getMTLSDialer(o *GrpcProxyClientOptions) (func(ctx context.Cont
509
514
case "grpc" :
510
515
transportCreds := credentials .NewTLS (tlsConfig )
511
516
dialOption := grpc .WithTransportCredentials (transportCreds )
512
- serverAddress := fmt . Sprintf ( "%s:%d" , o .proxyHost , o .proxyPort )
517
+ serverAddress := joinHostPort ( o .proxyHost , o .proxyPort )
513
518
tunnel , err := client .CreateSingleUseGrpcTunnel (ctx , serverAddress , dialOption )
514
519
if err != nil {
515
520
return nil , fmt .Errorf ("failed to create tunnel %s, got %v" , serverAddress , err )
516
521
}
517
522
518
- requestAddress := fmt . Sprintf ( "%s:%d" , o .requestHost , o .requestPort )
523
+ requestAddress := joinHostPort ( o .requestHost , o .requestPort )
519
524
proxyConn , err = tunnel .DialContext (ctx , "tcp" , requestAddress )
520
525
if err != nil {
521
526
return nil , fmt .Errorf ("failed to dial request %s, got %v" , requestAddress , err )
522
527
}
523
528
case "http-connect" :
524
- proxyAddress := fmt . Sprintf ( "%s:%d" , o .proxyHost , o .proxyPort )
525
- requestAddress := fmt . Sprintf ( "%s:%d" , o .requestHost , o .requestPort )
529
+ proxyAddress := joinHostPort ( o .proxyHost , o .proxyPort )
530
+ requestAddress := joinHostPort ( o .requestHost , o .requestPort )
526
531
527
532
proxyConn , err = tls .Dial ("tcp" , proxyAddress , tlsConfig )
528
533
if err != nil {
@@ -555,3 +560,7 @@ func (c *Client) getMTLSDialer(o *GrpcProxyClientOptions) (func(ctx context.Cont
555
560
return proxyConn , nil
556
561
}, nil
557
562
}
563
+
564
+ func joinHostPort (host string , port int ) string {
565
+ return net .JoinHostPort (host , strconv .Itoa (port ))
566
+ }
0 commit comments